• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #5049

11 May 2026 06:30AM UTC coverage: 73.313% (+0.09%) from 73.222%
#5049

push

travis-ci

web-flow
feat: refactor taosdump code to improve backup speed and compression ratio (#35292)

6625 of 8435 new or added lines in 28 files covered. (78.54%)

2491 existing lines in 142 files now uncovered.

281233 of 383605 relevant lines covered (73.31%)

132489999.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.17
/source/client/src/clientSmlJson.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include <ctype.h>
17
#include <stdio.h>
18
#include <stdlib.h>
19
#include <string.h>
20
#include "clientSml.h"
21

22
#define OTD_JSON_SUB_FIELDS_NUM 2
23

24
static inline int32_t smlParseMetricFromJSON(SSmlHandle *info, cJSON *metric, SSmlLineInfo *elements) {
465,206✔
25
  elements->measureLen = strlen(metric->valuestring);
465,206✔
26
  if (IS_INVALID_TABLE_LEN(elements->measureLen)) {
465,858✔
27
    uError("SML:0x%" PRIx64 " Metric length is 0 or large than 192", info->id);
1,956✔
28
    return TSDB_CODE_TSC_INVALID_TABLE_ID_LENGTH;
1,304✔
29
  }
30

31
  elements->measure = metric->valuestring;
463,902✔
32
  return TSDB_CODE_SUCCESS;
463,902✔
33
}
34

35
const char    *jsonName[OTD_JSON_FIELDS_NUM] = {"metric", "timestamp", "value", "tags"};
36
static int32_t smlGetJsonElements(cJSON *root, cJSON ***marks) {
465,193✔
37
  for (int i = 0; i < OTD_JSON_FIELDS_NUM; ++i) {
2,327,997✔
38
    cJSON *child = root->child;
1,862,804✔
39
    while (child != NULL) {
4,656,605✔
40
      if (strcasecmp(child->string, jsonName[i]) == 0) {
4,657,947✔
41
        *marks[i] = child;
1,863,494✔
42
        break;
1,862,190✔
43
      }
44
      child = child->next;
2,792,497✔
45
    }
46
    if (*marks[i] == NULL) {
1,860,848✔
47
      uError("SML %s error, not find mark:%d:%s", __FUNCTION__, i, jsonName[i]);
×
48
      return TSDB_CODE_TSC_INVALID_JSON;
×
49
    }
50
  }
51
  return TSDB_CODE_SUCCESS;
465,193✔
52
}
53

54
static int32_t smlConvertJSONBool(SSmlKv *pVal, char *typeStr, cJSON *value) {
452,284✔
55
  if (strcasecmp(typeStr, "bool") != 0) {
452,284✔
56
    uError("SML:invalid type(%s) for JSON Bool", typeStr);
×
57
    return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
58
  }
59
  pVal->type = TSDB_DATA_TYPE_BOOL;
452,284✔
60
  pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
452,284✔
61
  pVal->i = value->valueint;
452,284✔
62

63
  return TSDB_CODE_SUCCESS;
452,284✔
64
}
65

66
static int32_t smlConvertJSONNumber(SSmlKv *pVal, char *typeStr, cJSON *value) {
675,549✔
67
  // tinyint
68
  if (strcasecmp(typeStr, "i8") == 0 || strcasecmp(typeStr, "tinyint") == 0) {
675,549✔
69
    if (!IS_VALID_TINYINT(value->valuedouble)) {
117,808✔
70
      uError("SML:JSON value(%f) cannot fit in type(tinyint)", value->valuedouble);
2,608✔
71
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,608✔
72
    }
73
    pVal->type = TSDB_DATA_TYPE_TINYINT;
115,200✔
74
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
115,200✔
75
    pVal->i = value->valuedouble;
115,200✔
76
    return TSDB_CODE_SUCCESS;
115,200✔
77
  }
78
  // smallint
79
  if (strcasecmp(typeStr, "i16") == 0 || strcasecmp(typeStr, "smallint") == 0) {
557,741✔
80
    if (!IS_VALID_SMALLINT(value->valuedouble)) {
113,908✔
81
      uError("SML:JSON value(%f) cannot fit in type(smallint)", value->valuedouble);
2,620✔
82
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,608✔
83
    }
84
    pVal->type = TSDB_DATA_TYPE_SMALLINT;
111,288✔
85
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
111,288✔
86
    pVal->i = value->valuedouble;
111,288✔
87
    return TSDB_CODE_SUCCESS;
111,288✔
88
  }
89
  // int
90
  if (strcasecmp(typeStr, "i32") == 0 || strcasecmp(typeStr, "int") == 0) {
443,833✔
91
    if (!IS_VALID_INT(value->valuedouble)) {
112,599✔
92
      uError("SML:JSON value(%f) cannot fit in type(int)", value->valuedouble);
2,608✔
93
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,608✔
94
    }
95
    pVal->type = TSDB_DATA_TYPE_INT;
109,991✔
96
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
109,991✔
97
    pVal->i = value->valuedouble;
109,991✔
98
    return TSDB_CODE_SUCCESS;
109,991✔
99
  }
100
  // bigint
101
  if (strcasecmp(typeStr, "i64") == 0 || strcasecmp(typeStr, "bigint") == 0) {
331,234✔
102
    pVal->type = TSDB_DATA_TYPE_BIGINT;
112,580✔
103
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
112,580✔
104
    if (value->valuedouble >= (double)INT64_MAX) {
112,580✔
105
      pVal->i = INT64_MAX;
103,668✔
106
    } else if (value->valuedouble <= (double)INT64_MIN) {
8,912✔
107
      pVal->i = INT64_MIN;
1,304✔
108
    } else {
109
      pVal->i = value->valuedouble;
7,608✔
110
    }
111
    return TSDB_CODE_SUCCESS;
112,580✔
112
  }
113
  // float
114
  if (strcasecmp(typeStr, "f32") == 0 || strcasecmp(typeStr, "float") == 0) {
218,654✔
115
    if (!IS_VALID_FLOAT(value->valuedouble)) {
111,302✔
116
      uError("SML:JSON value(%f) cannot fit in type(float)", value->valuedouble);
2,608✔
117
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,608✔
118
    }
119
    pVal->type = TSDB_DATA_TYPE_FLOAT;
108,694✔
120
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
108,694✔
121
    pVal->f = value->valuedouble;
108,694✔
122
    return TSDB_CODE_SUCCESS;
108,694✔
123
  }
124
  // double
125
  if (strcasecmp(typeStr, "f64") == 0 || strcasecmp(typeStr, "double") == 0) {
107,352✔
126
    pVal->type = TSDB_DATA_TYPE_DOUBLE;
107,352✔
127
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
107,352✔
128
    pVal->d = value->valuedouble;
107,352✔
129
    return TSDB_CODE_SUCCESS;
107,352✔
130
  }
131

132
  // if reach here means type is unsupported
133
  uError("SML:invalid type(%s) for JSON Number", typeStr);
×
134
  return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
135
}
136

137
static int32_t smlConvertJSONString(SSmlKv *pVal, char *typeStr, cJSON *value) {
475,534✔
138
  if (strcasecmp(typeStr, "binary") == 0) {
475,534✔
139
    pVal->type = TSDB_DATA_TYPE_BINARY;
345,858✔
140
  } else if (strcasecmp(typeStr, "varbinary") == 0) {
129,676✔
141
    pVal->type = TSDB_DATA_TYPE_VARBINARY;
×
142
  } else if (strcasecmp(typeStr, "nchar") == 0) {
129,676✔
143
    pVal->type = TSDB_DATA_TYPE_NCHAR;
110,768✔
144
  } else {
145
    uError("SML:invalid type(%s) for JSON String", typeStr);
18,908✔
146
    return TSDB_CODE_TSC_INVALID_JSON_TYPE;
18,908✔
147
  }
148
  pVal->length = strlen(value->valuestring);
456,626✔
149

150
  if ((pVal->type == TSDB_DATA_TYPE_BINARY || pVal->type == TSDB_DATA_TYPE_VARBINARY) && pVal->length > TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE) {
456,626✔
151
    return TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN;
×
152
  }
153
  if (pVal->type == TSDB_DATA_TYPE_NCHAR &&
456,626✔
154
      pVal->length > (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
110,768✔
155
    return TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN;
×
156
  }
157

158
  pVal->value = value->valuestring;
456,626✔
159
  return TSDB_CODE_SUCCESS;
456,626✔
160
}
161

162
static int32_t smlParseValueFromJSONObj(cJSON *root, SSmlKv *kv) {
1,398,772✔
163
  int32_t ret = TSDB_CODE_SUCCESS;
1,398,772✔
164
  int32_t size = cJSON_GetArraySize(root);
1,398,772✔
165

166
  if (size != OTD_JSON_SUB_FIELDS_NUM) {
1,398,784✔
167
    return TSDB_CODE_TSC_INVALID_JSON;
×
168
  }
169

170
  cJSON *value = cJSON_GetObjectItem(root, "value");
1,398,784✔
171
  if (value == NULL) {
1,398,772✔
172
    return TSDB_CODE_TSC_INVALID_JSON;
×
173
  }
174

175
  cJSON *type = cJSON_GetObjectItem(root, "type");
1,398,772✔
176
  if (!cJSON_IsString(type)) {
1,398,760✔
177
    return TSDB_CODE_TSC_INVALID_JSON;
×
178
  }
179

180
  switch (value->type) {
1,398,760✔
181
    case cJSON_True:
452,284✔
182
    case cJSON_False: {
183
      ret = smlConvertJSONBool(kv, type->valuestring, value);
452,284✔
184
      if (ret != TSDB_CODE_SUCCESS) {
452,284✔
185
        return ret;
×
186
      }
187
      break;
452,284✔
188
    }
189
    case cJSON_Number: {
675,537✔
190
      ret = smlConvertJSONNumber(kv, type->valuestring, value);
675,537✔
191
      if (ret != TSDB_CODE_SUCCESS) {
675,549✔
192
        return ret;
10,432✔
193
      }
194
      break;
665,117✔
195
    }
196
    case cJSON_String: {
270,939✔
197
      ret = smlConvertJSONString(kv, type->valuestring, value);
270,939✔
198
      if (ret != TSDB_CODE_SUCCESS) {
270,939✔
199
        return ret;
18,908✔
200
      }
201
      break;
252,031✔
202
    }
203
    default:
×
204
      return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
205
  }
206

207
  return TSDB_CODE_SUCCESS;
1,369,432✔
208
}
209

210
static int32_t smlParseValueFromJSON(cJSON *root, SSmlKv *kv) {
2,366,195✔
211
  switch (root->type) {
2,366,195✔
212
    case cJSON_True:
305,462✔
213
    case cJSON_False: {
214
      kv->type = TSDB_DATA_TYPE_BOOL;
305,462✔
215
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
305,462✔
216
      kv->i = root->valueint;
305,462✔
217
      break;
306,114✔
218
    }
219
    case cJSON_Number: {
456,075✔
220
      kv->type = TSDB_DATA_TYPE_DOUBLE;
456,075✔
221
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
456,075✔
222
      kv->d = root->valuedouble;
456,075✔
223
      break;
456,075✔
224
    }
225
    case cJSON_String: {
204,595✔
226
      int32_t ret = smlConvertJSONString(kv, "binary", root);
204,595✔
227
      if (ret != TSDB_CODE_SUCCESS) {
204,582✔
228
        uError("SML:Failed to parse binary value from JSON Obj");
×
229
        return ret;
×
230
      }
231
      break;
204,582✔
232
    }
233
    case cJSON_Object: {
1,398,772✔
234
      int32_t ret = smlParseValueFromJSONObj(root, kv);
1,398,772✔
235
      if (ret != TSDB_CODE_SUCCESS) {
1,398,760✔
236
        uError("SML:Failed to parse value from JSON Obj");
29,340✔
237
        return ret;
29,340✔
238
      }
239
      break;
1,369,432✔
240
    }
241
    default:
1,304✔
242
      return TSDB_CODE_TSC_INVALID_JSON;
1,304✔
243
  }
244

245
  return TSDB_CODE_SUCCESS;
2,336,203✔
246
}
247

248
static int32_t smlProcessTagJson(SSmlHandle *info, cJSON *tags){
208,800✔
249
  SArray *preLineKV = info->preLineTagKV;
208,800✔
250
  taosArrayClearEx(preLineKV, freeSSmlKv);
208,800✔
251
  int     cnt = 0;
208,800✔
252

253
  int32_t tagNum = cJSON_GetArraySize(tags);
208,800✔
254
  if (unlikely(tagNum == 0)) {
208,813✔
255
    uError("SML:Tag should not be empty");
1,304✔
256
    return TSDB_CODE_TSC_INVALID_JSON;
1,304✔
257
  }
258
  for (int32_t i = 0; i < tagNum; ++i) {
2,092,240✔
259
    cJSON *tag = cJSON_GetArrayItem(tags, i);
1,902,931✔
260
    if (unlikely(tag == NULL)) {
1,902,970✔
261
      return TSDB_CODE_TSC_INVALID_JSON;
×
262
    }
263
    size_t keyLen = strlen(tag->string);
1,902,970✔
264
    if (unlikely(IS_INVALID_COL_LEN(keyLen))) {
1,902,970✔
265
      uError("SML:Tag key length is 0 or too large than 64");
652✔
266
      return TSDB_CODE_TSC_INVALID_COLUMN_LENGTH;
652✔
267
    }
268

269
    // add kv to SSmlKv
270
    SSmlKv kv = {0};
1,902,318✔
271
    kv.key = tag->string;
1,902,318✔
272
    kv.keyLen = keyLen;
1,902,318✔
273

274
    // value
275
    int32_t ret = smlParseValueFromJSON(tag, &kv);
1,902,318✔
276
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
1,902,279✔
277
      return ret;
13,040✔
278
    }
279
    if (taosArrayPush(preLineKV, &kv) == NULL) {
1,889,265✔
280
      return terrno;
×
281
    }
282

283
    if (info->dataFormat && !isSmlTagAligned(info, cnt, &kv)) {
1,889,265✔
284
      return TSDB_CODE_TSC_INVALID_JSON;
4,508✔
285
    }
286

287
    cnt++;
1,884,731✔
288
  }
289
  return TSDB_CODE_SUCCESS;
189,309✔
290
}
291

292
static int32_t smlParseTagsFromJSON(SSmlHandle *info, cJSON *tags, SSmlLineInfo *elements) {
446,298✔
293
  if (is_same_child_table_telnet(elements, &info->preLine) == 0) {
446,298✔
294
    elements->measureTag = info->preLine.measureTag;
56,825✔
295
    return TSDB_CODE_SUCCESS;
56,825✔
296
  }
297
  int32_t code = 0;
390,176✔
298
  int32_t lino = 0;
390,176✔
299
  if(info->dataFormat){
390,176✔
300
    SML_CHECK_CODE(smlProcessSuperTable(info, elements));
200,959✔
301
  }
302
  SML_CHECK_CODE(smlProcessTagJson(info, tags));
208,800✔
303
  SML_CHECK_CODE(smlJoinMeasureTag(elements));
189,309✔
304
  return smlProcessChildTable(info, elements);
189,309✔
305

306
END:
200,880✔
307
  if(info->reRun){
200,880✔
308
    return TSDB_CODE_SUCCESS;
183,864✔
309
  }
310
  RETURN
17,016✔
311
}
312

313
static int64_t smlParseTSFromJSONObj(SSmlHandle *info, cJSON *root, int32_t toPrecision) {
145,851✔
314
  int32_t size = cJSON_GetArraySize(root);
145,851✔
315
  if (unlikely(size != OTD_JSON_SUB_FIELDS_NUM)) {
145,851✔
316
    smlBuildInvalidDataMsg(&info->msgBuf, "invalidate json", NULL);
×
317
    return TSDB_CODE_TSC_INVALID_JSON;
×
318
  }
319

320
  cJSON *value = cJSON_GetObjectItem(root, "value");
145,851✔
321
  if (unlikely(!cJSON_IsNumber(value))) {
145,851✔
322
    smlBuildInvalidDataMsg(&info->msgBuf, "invalidate json", NULL);
1,956✔
323
    return TSDB_CODE_TSC_INVALID_JSON;
1,956✔
324
  }
325

326
  cJSON *type = cJSON_GetObjectItem(root, "type");
143,895✔
327
  if (unlikely(!cJSON_IsString(type))) {
143,883✔
328
    smlBuildInvalidDataMsg(&info->msgBuf, "invalidate json", NULL);
×
329
    return TSDB_CODE_TSC_INVALID_JSON;
×
330
  }
331

332
  double timeDouble = value->valuedouble;
143,883✔
333
  if (unlikely(smlDoubleToInt64OverFlow(timeDouble))) {
143,883✔
334
    smlBuildInvalidDataMsg(&info->msgBuf, "timestamp is too large", NULL);
×
335
    return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
336
  }
337

338
  if (timeDouble == 0) {
143,895✔
339
    return taosGetTimestampNs() / smlFactorNS[toPrecision];
×
340
  }
341

342
  if (timeDouble < 0) {
143,895✔
343
    return (int64_t)timeDouble;
×
344
  }
345

346
  int64_t tsInt64 = (int64_t)timeDouble;
143,895✔
347
  size_t  typeLen = strlen(type->valuestring);
143,895✔
348
  if (typeLen == 1 && (type->valuestring[0] == 's' || type->valuestring[0] == 'S')) {
143,895✔
349
    // seconds
350
    if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
1,956✔
351
      return tsInt64 * smlFactorS[toPrecision];
1,956✔
352
    }
353
    return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
354
  } else if (typeLen == 2 && (type->valuestring[1] == 's' || type->valuestring[1] == 'S')) {
141,939✔
355
    switch (type->valuestring[0]) {
141,939✔
356
      case 'm':
20,015✔
357
      case 'M':
358
        // milliseconds
359
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MILLI, toPrecision);
20,015✔
360
      case 'u':
×
361
      case 'U':
362
        // microseconds
363
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MICRO, toPrecision);
×
364
      case 'n':
121,924✔
365
      case 'N':
366
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_NANO, toPrecision);
121,924✔
367
      default:
×
368
        return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
369
    }
370
  } else {
UNCOV
371
    return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
372
  }
373
}
374

375
uint8_t smlGetTimestampLen(int64_t num) {
95,749✔
376
  uint8_t len = 0;
95,749✔
377
  while ((num /= 10) != 0) {
1,100,222✔
378
    len++;
1,004,473✔
379
  }
380
  len++;
95,749✔
381
  return len;
95,749✔
382
}
383

384
static int64_t smlParseTSFromJSON(SSmlHandle *info, cJSON *timestamp) {
246,121✔
385
  // Timestamp must be the first KV to parse
386
  int32_t toPrecision = info->currSTableMeta ? info->currSTableMeta->tableInfo.precision : TSDB_TIME_PRECISION_NANO;
246,121✔
387
  if (cJSON_IsNumber(timestamp)) {
246,121✔
388
    // timestamp value 0 indicates current system time
389
    double timeDouble = timestamp->valuedouble;
98,301✔
390
    if (unlikely(smlDoubleToInt64OverFlow(timeDouble))) {
98,301✔
391
      smlBuildInvalidDataMsg(&info->msgBuf, "timestamp is too large", NULL);
×
392
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
393
    }
394

395
    if (unlikely(timeDouble < 0)) {
98,301✔
396
      smlBuildInvalidDataMsg(&info->msgBuf, "timestamp is negative", NULL);
×
397
      return (int64_t)timeDouble;
×
398
    } else if (unlikely(timeDouble == 0)) {
98,301✔
399
      return taosGetTimestampNs() / smlFactorNS[toPrecision];
7,824✔
400
    }
401

402
    uint8_t tsLen = smlGetTimestampLen((int64_t)timeDouble);
94,389✔
403

404
    int8_t fromPrecision = smlGetTsTypeByLen(tsLen);
94,376✔
405
    if (unlikely(fromPrecision == -1)) {
94,376✔
406
      smlBuildInvalidDataMsg(&info->msgBuf,
×
407
                             "timestamp precision can only be seconds(10 digits) or milli seconds(13 digits)", NULL);
408
      return TSDB_CODE_SML_INVALID_DATA;
×
409
    }
410
    int64_t tsInt64 = (int64_t)timeDouble;
94,376✔
411
    if (fromPrecision == TSDB_TIME_PRECISION_SECONDS) {
94,376✔
412
      if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
43,054✔
413
        return tsInt64 * smlFactorS[toPrecision];
43,054✔
414
      }
415
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
416
    } else {
417
      return convertTimePrecision(timeDouble, fromPrecision, toPrecision);
51,322✔
418
    }
419
  } else if (cJSON_IsObject(timestamp)) {
147,795✔
420
    return smlParseTSFromJSONObj(info, timestamp, toPrecision);
145,851✔
421
  } else {
422
    smlBuildInvalidDataMsg(&info->msgBuf, "invalidate json", NULL);
1,956✔
423
    return TSDB_CODE_TSC_INVALID_JSON;
1,956✔
424
  }
425
}
426

427
static int32_t smlParseJSONStringExt(SSmlHandle *info, cJSON *root, SSmlLineInfo *elements) {
467,801✔
428
  int32_t code = TSDB_CODE_SUCCESS;
467,801✔
429
  int32_t lino = 0;
467,801✔
430

431
  cJSON *metricJson = NULL;
467,801✔
432
  cJSON *tsJson = NULL;
467,801✔
433
  cJSON *valueJson = NULL;
467,801✔
434
  cJSON *tagsJson = NULL;
467,801✔
435

436
  int32_t size = cJSON_GetArraySize(root);
467,801✔
437
  // outmost json fields has to be exactly 4
438
  if (size != OTD_JSON_FIELDS_NUM) {
467,826✔
439
    uError("SML:0x%" PRIx64 " Invalid number of JSON fields in data point %d", info->id, size);
2,608✔
440
    return TSDB_CODE_TSC_INVALID_JSON;
2,608✔
441
  }
442

443
  cJSON **marks[OTD_JSON_FIELDS_NUM] = {&metricJson, &tsJson, &valueJson, &tagsJson};
465,218✔
444
  SML_CHECK_CODE(smlGetJsonElements(root, marks));
465,218✔
445
  // Parse metric
446
  SML_CHECK_CODE(smlParseMetricFromJSON(info, metricJson, elements));
465,870✔
447
  // Parse metric value
448
  SSmlKv kv = {.key = VALUE, .keyLen = VALUE_LEN};
463,902✔
449
  SML_CHECK_CODE(smlParseValueFromJSON(valueJson, &kv));
463,902✔
450

451
  // Parse tags
452
  elements->tags = cJSON_PrintUnformatted(tagsJson);
446,936✔
453
  SML_CHECK_NULL(elements->tags);
446,962✔
454

455
  elements->tagsLen = strlen(elements->tags);
446,310✔
456
  SML_CHECK_CODE(smlParseTagsFromJSON(info, tagsJson, elements));
446,962✔
457

458
  if (unlikely(info->reRun)) {
429,985✔
459
    goto END;
183,864✔
460
  }
461

462
  // Parse timestamp
463
  // notice!!! put ts back to tag to ensure get meta->precision
464
  int64_t ts = smlParseTSFromJSON(info, tsJson);
246,121✔
465
  if (unlikely(ts < 0)) {
246,108✔
466
    char* tmp = cJSON_PrintUnformatted(tsJson);
3,912✔
467
    if (tmp == NULL) {
3,912✔
468
      uError("SML:0x%" PRIx64 " Unable to parse timestamp from JSON payload %s %" PRId64, info->id, info->msgBuf.buf, ts);
×
469
    } else {
470
      uError("SML:0x%" PRIx64 " Unable to parse timestamp from JSON payload %s %s %" PRId64, info->id, info->msgBuf.buf,tmp, ts);
3,912✔
471
      taosMemoryFree(tmp);
3,912✔
472
    }
473
    SML_CHECK_CODE(TSDB_CODE_INVALID_TIMESTAMP);
3,912✔
474
  }
475
  SSmlKv kvTs = {0};
242,196✔
476
  smlBuildTsKv(&kvTs, ts);
242,196✔
477
  if (info->dataFormat){
242,184✔
478
    code = smlParseEndTelnetJsonFormat(info, elements, &kvTs, &kv);
55,444✔
479
  } else {
480
    code = smlParseEndTelnetJsonUnFormat(info, elements, &kvTs, &kv);
186,740✔
481
  }
482
  SML_CHECK_CODE(code);
242,157✔
483
  taosMemoryFreeClear(info->preLine.tags);
242,157✔
484
  info->preLine = *elements;
242,183✔
485
  elements->tags = NULL;
242,183✔
486

487
END:
465,883✔
488
  taosMemoryFree(elements->tags);
465,883✔
489
  RETURN
465,896✔
490
}
491

492
int32_t smlParseJSONExt(SSmlHandle *info, char *payload) {
222,464✔
493
  int32_t payloadNum = 0;
222,464✔
494
  int32_t ret = TSDB_CODE_SUCCESS;
222,464✔
495

496
  info->root = cJSON_Parse(payload);
222,464✔
497
  if (unlikely(info->root == NULL)) {
223,141✔
498
    uError("SML:0x%" PRIx64 " parse json failed:%s", info->id, payload);
2,608✔
499
    return TSDB_CODE_TSC_INVALID_JSON;
2,608✔
500
  }
501

502
  // multiple data points must be sent in JSON array
503
  if (cJSON_IsArray(info->root)) {
219,881✔
504
    payloadNum = cJSON_GetArraySize(info->root);
26,204✔
505
  } else if (cJSON_IsObject(info->root)) {
193,664✔
506
    payloadNum = 1;
193,664✔
507
  } else {
508
    uError("SML:0x%" PRIx64 " Invalid JSON type:%s", info->id, payload);
×
509
    return TSDB_CODE_TSC_INVALID_JSON;
×
510
  }
511

512
  info->lineNum = payloadNum;
220,507✔
513
  cJSON *head = (payloadNum == 1 && cJSON_IsObject(info->root)) ? info->root : info->root->child;
219,855✔
514

515
  int    cnt = 0;
219,855✔
516
  cJSON *dataPoint = head;
219,855✔
517
  while (dataPoint) {
645,889✔
518
    if (info->dataFormat) {
467,801✔
519
      SSmlLineInfo element = {0};
262,767✔
520
      ret = smlParseJSONStringExt(info, dataPoint, &element);
262,767✔
521
      if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
263,405✔
522
    } else {
523
      ret = smlParseJSONStringExt(info, dataPoint, info->lines + cnt);
205,034✔
524
    }
525
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
468,478✔
526
      uError("SML:0x%" PRIx64 " Invalid JSON Payload 2:%s", info->id, payload);
42,444✔
527
      return ret;
42,444✔
528
    }
529

530
    if (unlikely(info->reRun)) {
426,034✔
531
      cnt = 0;
183,864✔
532
      dataPoint = head;
183,864✔
533
      info->lineNum = payloadNum;
183,864✔
534
      ret = smlClearForRerun(info);
183,864✔
535
      if (ret != TSDB_CODE_SUCCESS) {
183,864✔
536
        return ret;
×
537
      }
538
      continue;
183,864✔
539
    }
540
    cnt++;
242,170✔
541
    dataPoint = dataPoint->next;
242,170✔
542
  }
543

544
  return TSDB_CODE_SUCCESS;
178,088✔
545
}
546

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc