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

taosdata / TDengine / #5044

06 May 2026 02:35AM UTC coverage: 73.169% (+0.06%) from 73.107%
#5044

push

travis-ci

web-flow
feat: [6659794715] cpu limit (#35153)

244 of 275 new or added lines in 23 files covered. (88.73%)

526 existing lines in 141 files now uncovered.

277745 of 379596 relevant lines covered (73.17%)

133740972.66 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) {
447,626✔
25
  elements->measureLen = strlen(metric->valuestring);
447,626✔
26
  if (IS_INVALID_TABLE_LEN(elements->measureLen)) {
447,626✔
27
    uError("SML:0x%" PRIx64 " Metric length is 0 or large than 192", info->id);
1,268✔
28
    return TSDB_CODE_TSC_INVALID_TABLE_ID_LENGTH;
1,256✔
29
  }
30

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

35
const char    *jsonName[OTD_JSON_FIELDS_NUM] = {"metric", "timestamp", "value", "tags"};
36
static int32_t smlGetJsonElements(cJSON *root, cJSON ***marks) {
447,614✔
37
  for (int i = 0; i < OTD_JSON_FIELDS_NUM; ++i) {
2,237,998✔
38
    cJSON *child = root->child;
1,790,384✔
39
    while (child != NULL) {
4,475,792✔
40
      if (strcasecmp(child->string, jsonName[i]) == 0) {
4,475,840✔
41
        *marks[i] = child;
1,790,432✔
42
        break;
1,790,432✔
43
      }
44
      child = child->next;
2,685,408✔
45
    }
46
    if (*marks[i] == NULL) {
1,790,384✔
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;
447,614✔
52
}
53

54
static int32_t smlConvertJSONBool(SSmlKv *pVal, char *typeStr, cJSON *value) {
435,748✔
55
  if (strcasecmp(typeStr, "bool") != 0) {
435,748✔
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;
435,748✔
60
  pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
435,748✔
61
  pVal->i = value->valueint;
435,748✔
62

63
  return TSDB_CODE_SUCCESS;
435,748✔
64
}
65

66
static int32_t smlConvertJSONNumber(SSmlKv *pVal, char *typeStr, cJSON *value) {
651,477✔
67
  // tinyint
68
  if (strcasecmp(typeStr, "i8") == 0 || strcasecmp(typeStr, "tinyint") == 0) {
651,477✔
69
    if (!IS_VALID_TINYINT(value->valuedouble)) {
113,608✔
70
      uError("SML:JSON value(%f) cannot fit in type(tinyint)", value->valuedouble);
2,512✔
71
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,512✔
72
    }
73
    pVal->type = TSDB_DATA_TYPE_TINYINT;
111,096✔
74
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
111,096✔
75
    pVal->i = value->valuedouble;
111,096✔
76
    return TSDB_CODE_SUCCESS;
111,096✔
77
  }
78
  // smallint
79
  if (strcasecmp(typeStr, "i16") == 0 || strcasecmp(typeStr, "smallint") == 0) {
537,869✔
80
    if (!IS_VALID_SMALLINT(value->valuedouble)) {
109,840✔
81
      uError("SML:JSON value(%f) cannot fit in type(smallint)", value->valuedouble);
2,512✔
82
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,512✔
83
    }
84
    pVal->type = TSDB_DATA_TYPE_SMALLINT;
107,328✔
85
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
107,328✔
86
    pVal->i = value->valuedouble;
107,328✔
87
    return TSDB_CODE_SUCCESS;
107,328✔
88
  }
89
  // int
90
  if (strcasecmp(typeStr, "i32") == 0 || strcasecmp(typeStr, "int") == 0) {
428,029✔
91
    if (!IS_VALID_INT(value->valuedouble)) {
108,562✔
92
      uError("SML:JSON value(%f) cannot fit in type(int)", value->valuedouble);
2,512✔
93
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,512✔
94
    }
95
    pVal->type = TSDB_DATA_TYPE_INT;
106,050✔
96
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
106,050✔
97
    pVal->i = value->valuedouble;
106,050✔
98
    return TSDB_CODE_SUCCESS;
106,050✔
99
  }
100
  // bigint
101
  if (strcasecmp(typeStr, "i64") == 0 || strcasecmp(typeStr, "bigint") == 0) {
319,467✔
102
    pVal->type = TSDB_DATA_TYPE_BIGINT;
108,575✔
103
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
108,575✔
104
    if (value->valuedouble >= (double)INT64_MAX) {
108,575✔
105
      pVal->i = INT64_MAX;
99,852✔
106
    } else if (value->valuedouble <= (double)INT64_MIN) {
8,723✔
107
      pVal->i = INT64_MIN;
1,256✔
108
    } else {
109
      pVal->i = value->valuedouble;
7,467✔
110
    }
111
    return TSDB_CODE_SUCCESS;
108,575✔
112
  }
113
  // float
114
  if (strcasecmp(typeStr, "f32") == 0 || strcasecmp(typeStr, "float") == 0) {
210,892✔
115
    if (!IS_VALID_FLOAT(value->valuedouble)) {
107,332✔
116
      uError("SML:JSON value(%f) cannot fit in type(float)", value->valuedouble);
2,512✔
117
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,512✔
118
    }
119
    pVal->type = TSDB_DATA_TYPE_FLOAT;
104,820✔
120
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
104,820✔
121
    pVal->f = value->valuedouble;
104,820✔
122
    return TSDB_CODE_SUCCESS;
104,820✔
123
  }
124
  // double
125
  if (strcasecmp(typeStr, "f64") == 0 || strcasecmp(typeStr, "double") == 0) {
103,560✔
126
    pVal->type = TSDB_DATA_TYPE_DOUBLE;
103,560✔
127
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
103,560✔
128
    pVal->d = value->valuedouble;
103,560✔
129
    return TSDB_CODE_SUCCESS;
103,560✔
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) {
457,549✔
138
  if (strcasecmp(typeStr, "binary") == 0) {
457,549✔
139
    pVal->type = TSDB_DATA_TYPE_BINARY;
332,517✔
140
  } else if (strcasecmp(typeStr, "varbinary") == 0) {
125,032✔
141
    pVal->type = TSDB_DATA_TYPE_VARBINARY;
×
142
  } else if (strcasecmp(typeStr, "nchar") == 0) {
125,032✔
143
    pVal->type = TSDB_DATA_TYPE_NCHAR;
106,820✔
144
  } else {
145
    uError("SML:invalid type(%s) for JSON String", typeStr);
18,212✔
146
    return TSDB_CODE_TSC_INVALID_JSON_TYPE;
18,212✔
147
  }
148
  pVal->length = strlen(value->valuestring);
439,337✔
149

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

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

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

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

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

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

180
  switch (value->type) {
1,348,472✔
181
    case cJSON_True:
435,748✔
182
    case cJSON_False: {
183
      ret = smlConvertJSONBool(kv, type->valuestring, value);
435,748✔
184
      if (ret != TSDB_CODE_SUCCESS) {
435,748✔
185
        return ret;
×
186
      }
187
      break;
435,748✔
188
    }
189
    case cJSON_Number: {
651,477✔
190
      ret = smlConvertJSONNumber(kv, type->valuestring, value);
651,477✔
191
      if (ret != TSDB_CODE_SUCCESS) {
651,465✔
192
        return ret;
10,048✔
193
      }
194
      break;
641,417✔
195
    }
196
    case cJSON_String: {
261,247✔
197
      ret = smlConvertJSONString(kv, type->valuestring, value);
261,247✔
198
      if (ret != TSDB_CODE_SUCCESS) {
261,247✔
199
        return ret;
18,212✔
200
      }
201
      break;
243,035✔
202
    }
203
    default:
×
204
      return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
205
  }
206

207
  return TSDB_CODE_SUCCESS;
1,320,200✔
208
}
209

210
static int32_t smlParseValueFromJSON(cJSON *root, SSmlKv *kv) {
2,276,521✔
211
  switch (root->type) {
2,276,521✔
212
    case cJSON_True:
294,561✔
213
    case cJSON_False: {
214
      kv->type = TSDB_DATA_TYPE_BOOL;
294,561✔
215
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
294,561✔
216
      kv->i = root->valueint;
294,561✔
217
      break;
294,561✔
218
    }
219
    case cJSON_Number: {
436,565✔
220
      kv->type = TSDB_DATA_TYPE_DOUBLE;
436,565✔
221
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
436,565✔
222
      kv->d = root->valuedouble;
436,565✔
223
      break;
436,565✔
224
    }
225
    case cJSON_String: {
196,302✔
226
      int32_t ret = smlConvertJSONString(kv, "binary", root);
196,302✔
227
      if (ret != TSDB_CODE_SUCCESS) {
196,302✔
228
        uError("SML:Failed to parse binary value from JSON Obj");
×
229
        return ret;
×
230
      }
231
      break;
196,302✔
232
    }
233
    case cJSON_Object: {
1,348,472✔
234
      int32_t ret = smlParseValueFromJSONObj(root, kv);
1,348,472✔
235
      if (ret != TSDB_CODE_SUCCESS) {
1,348,460✔
236
        uError("SML:Failed to parse value from JSON Obj");
28,260✔
237
        return ret;
28,260✔
238
      }
239
      break;
1,320,200✔
240
    }
241
    default:
1,256✔
242
      return TSDB_CODE_TSC_INVALID_JSON;
1,256✔
243
  }
244

245
  return TSDB_CODE_SUCCESS;
2,247,628✔
246
}
247

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

253
  int32_t tagNum = cJSON_GetArraySize(tags);
200,947✔
254
  if (unlikely(tagNum == 0)) {
200,947✔
255
    uError("SML:Tag should not be empty");
1,256✔
256
    return TSDB_CODE_TSC_INVALID_JSON;
1,256✔
257
  }
258
  for (int32_t i = 0; i < tagNum; ++i) {
2,013,599✔
259
    cJSON *tag = cJSON_GetArrayItem(tags, i);
1,831,438✔
260
    if (unlikely(tag == NULL)) {
1,831,450✔
261
      return TSDB_CODE_TSC_INVALID_JSON;
×
262
    }
263
    size_t keyLen = strlen(tag->string);
1,831,450✔
264
    if (unlikely(IS_INVALID_COL_LEN(keyLen))) {
1,831,450✔
265
      uError("SML:Tag key length is 0 or too large than 64");
628✔
266
      return TSDB_CODE_TSC_INVALID_COLUMN_LENGTH;
628✔
267
    }
268

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

274
    // value
275
    int32_t ret = smlParseValueFromJSON(tag, &kv);
1,830,822✔
276
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
1,830,822✔
277
      return ret;
12,560✔
278
    }
279
    if (taosArrayPush(preLineKV, &kv) == NULL) {
1,818,250✔
280
      return terrno;
×
281
    }
282

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

287
    cnt++;
1,813,908✔
288
  }
289
  return TSDB_CODE_SUCCESS;
182,161✔
290
}
291

292
static int32_t smlParseTagsFromJSON(SSmlHandle *info, cJSON *tags, SSmlLineInfo *elements) {
429,390✔
293
  if (is_same_child_table_telnet(elements, &info->preLine) == 0) {
429,390✔
294
    elements->measureTag = info->preLine.measureTag;
53,865✔
295
    return TSDB_CODE_SUCCESS;
53,865✔
296
  }
297
  int32_t code = 0;
375,537✔
298
  int32_t lino = 0;
375,537✔
299
  if(info->dataFormat){
375,537✔
300
    SML_CHECK_CODE(smlProcessSuperTable(info, elements));
193,438✔
301
  }
302
  SML_CHECK_CODE(smlProcessTagJson(info, tags));
200,935✔
303
  SML_CHECK_CODE(smlJoinMeasureTag(elements));
182,173✔
304
  return smlProcessChildTable(info, elements);
182,149✔
305

306
END:
193,376✔
307
  if(info->reRun){
193,376✔
308
    return TSDB_CODE_SUCCESS;
176,994✔
309
  }
310
  RETURN
16,382✔
311
}
312

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

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

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

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

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

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

346
  int64_t tsInt64 = (int64_t)timeDouble;
139,225✔
347
  size_t  typeLen = strlen(type->valuestring);
139,225✔
348
  if (typeLen == 1 && (type->valuestring[0] == 's' || type->valuestring[0] == 'S')) {
139,225✔
349
    // seconds
350
    if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
1,881✔
351
      return tsInt64 * smlFactorS[toPrecision];
1,881✔
352
    }
353
    return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
354
  } else if (typeLen == 2 && (type->valuestring[1] == 's' || type->valuestring[1] == 'S')) {
137,344✔
355
    switch (type->valuestring[0]) {
137,344✔
356
      case 'm':
19,908✔
357
      case 'M':
358
        // milliseconds
359
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MILLI, toPrecision);
19,908✔
360
      case 'u':
×
361
      case 'U':
362
        // microseconds
363
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MICRO, toPrecision);
×
364
      case 'n':
117,436✔
365
      case 'N':
366
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_NANO, toPrecision);
117,436✔
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) {
89,962✔
376
  uint8_t len = 0;
89,962✔
377
  while ((num /= 10) != 0) {
1,037,140✔
378
    len++;
947,178✔
379
  }
380
  len++;
89,962✔
381
  return len;
89,962✔
382
}
383

384
static int64_t smlParseTSFromJSON(SSmlHandle *info, cJSON *timestamp) {
236,038✔
385
  // Timestamp must be the first KV to parse
386
  int32_t toPrecision = info->currSTableMeta ? info->currSTableMeta->tableInfo.precision : TSDB_TIME_PRECISION_NANO;
236,038✔
387
  if (cJSON_IsNumber(timestamp)) {
236,038✔
388
    // timestamp value 0 indicates current system time
389
    double timeDouble = timestamp->valuedouble;
93,033✔
390
    if (unlikely(smlDoubleToInt64OverFlow(timeDouble))) {
93,033✔
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)) {
93,045✔
396
      smlBuildInvalidDataMsg(&info->msgBuf, "timestamp is negative", NULL);
×
397
      return (int64_t)timeDouble;
×
398
    } else if (unlikely(timeDouble == 0)) {
93,045✔
399
      return taosGetTimestampNs() / smlFactorNS[toPrecision];
7,518✔
400
    }
401

402
    uint8_t tsLen = smlGetTimestampLen((int64_t)timeDouble);
89,286✔
403

404
    int8_t fromPrecision = smlGetTsTypeByLen(tsLen);
89,298✔
405
    if (unlikely(fromPrecision == -1)) {
89,298✔
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;
89,298✔
411
    if (fromPrecision == TSDB_TIME_PRECISION_SECONDS) {
89,298✔
412
      if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
41,442✔
413
        return tsInt64 * smlFactorS[toPrecision];
41,442✔
414
      }
415
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
416
    } else {
417
      return convertTimePrecision(timeDouble, fromPrecision, toPrecision);
47,856✔
418
    }
419
  } else if (cJSON_IsObject(timestamp)) {
143,005✔
420
    return smlParseTSFromJSONObj(info, timestamp, toPrecision);
141,109✔
421
  } else {
422
    smlBuildInvalidDataMsg(&info->msgBuf, "invalidate json", NULL);
1,884✔
423
    return TSDB_CODE_TSC_INVALID_JSON;
1,884✔
424
  }
425
}
426

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

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

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

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

451
  // Parse tags
452
  elements->tags = cJSON_PrintUnformatted(tagsJson);
429,402✔
453
  SML_CHECK_NULL(elements->tags);
429,402✔
454

455
  elements->tagsLen = strlen(elements->tags);
429,402✔
456
  SML_CHECK_CODE(smlParseTagsFromJSON(info, tagsJson, elements));
429,402✔
457

458
  if (unlikely(info->reRun)) {
413,020✔
459
    goto END;
176,994✔
460
  }
461

462
  // Parse timestamp
463
  // notice!!! put ts back to tag to ensure get meta->precision
464
  int64_t ts = smlParseTSFromJSON(info, tsJson);
236,026✔
465
  if (unlikely(ts < 0)) {
236,014✔
466
    char* tmp = cJSON_PrintUnformatted(tsJson);
3,768✔
467
    if (tmp == NULL) {
3,768✔
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,768✔
471
      taosMemoryFree(tmp);
3,768✔
472
    }
473
    SML_CHECK_CODE(TSDB_CODE_INVALID_TIMESTAMP);
3,768✔
474
  }
475
  SSmlKv kvTs = {0};
232,246✔
476
  smlBuildTsKv(&kvTs, ts);
232,246✔
477
  if (info->dataFormat){
232,258✔
478
    code = smlParseEndTelnetJsonFormat(info, elements, &kvTs, &kv);
52,754✔
479
  } else {
480
    code = smlParseEndTelnetJsonUnFormat(info, elements, &kvTs, &kv);
179,504✔
481
  }
482
  SML_CHECK_CODE(code);
232,234✔
483
  taosMemoryFreeClear(info->preLine.tags);
232,234✔
484
  info->preLine = *elements;
232,246✔
485
  elements->tags = NULL;
232,246✔
486

487
END:
447,602✔
488
  taosMemoryFree(elements->tags);
447,602✔
489
  RETURN
447,626✔
490
}
491

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

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

502
  // multiple data points must be sent in JSON array
503
  if (cJSON_IsArray(info->root)) {
212,234✔
504
    payloadNum = cJSON_GetArraySize(info->root);
25,703✔
505
  } else if (cJSON_IsObject(info->root)) {
186,531✔
506
    payloadNum = 1;
186,531✔
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;
212,234✔
513
  cJSON *head = (payloadNum == 1 && cJSON_IsObject(info->root)) ? info->root : info->root->child;
212,234✔
514

515
  int    cnt = 0;
212,234✔
516
  cJSON *dataPoint = head;
212,234✔
517
  while (dataPoint) {
621,510✔
518
    if (info->dataFormat) {
450,138✔
519
      SSmlLineInfo element = {0};
253,050✔
520
      ret = smlParseJSONStringExt(info, dataPoint, &element);
253,050✔
521
      if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
253,038✔
522
    } else {
523
      ret = smlParseJSONStringExt(info, dataPoint, info->lines + cnt);
197,088✔
524
    }
525
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
450,150✔
526
      uError("SML:0x%" PRIx64 " Invalid JSON Payload 2:%s", info->id, payload);
40,874✔
527
      return ret;
40,874✔
528
    }
529

530
    if (unlikely(info->reRun)) {
409,276✔
531
      cnt = 0;
176,994✔
532
      dataPoint = head;
176,994✔
533
      info->lineNum = payloadNum;
176,994✔
534
      ret = smlClearForRerun(info);
176,994✔
535
      if (ret != TSDB_CODE_SUCCESS) {
176,994✔
536
        return ret;
×
537
      }
538
      continue;
176,994✔
539
    }
540
    cnt++;
232,282✔
541
    dataPoint = dataPoint->next;
232,282✔
542
  }
543

544
  return TSDB_CODE_SUCCESS;
171,372✔
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