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

taosdata / TDengine / #4976

06 Mar 2026 09:48AM UTC coverage: 68.446% (+0.08%) from 68.37%
#4976

push

travis-ci

web-flow
feat(TDgpt): support multiple input data columns for anomaly detection. (#34606)

0 of 93 new or added lines in 9 files covered. (0.0%)

5718 existing lines in 144 files now uncovered.

211146 of 308486 relevant lines covered (68.45%)

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

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

35
const char    *jsonName[OTD_JSON_FIELDS_NUM] = {"metric", "timestamp", "value", "tags"};
36
static int32_t smlGetJsonElements(cJSON *root, cJSON ***marks) {
411,048✔
37
  for (int i = 0; i < OTD_JSON_FIELDS_NUM; ++i) {
2,055,192✔
38
    cJSON *child = root->child;
1,644,144✔
39
    while (child != NULL) {
4,110,396✔
40
      if (strcasecmp(child->string, jsonName[i]) == 0) {
4,110,444✔
41
        *marks[i] = child;
1,644,192✔
42
        break;
1,644,192✔
43
      }
44
      child = child->next;
2,466,252✔
45
    }
46
    if (*marks[i] == NULL) {
1,644,144✔
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;
411,048✔
52
}
53

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

63
  return TSDB_CODE_SUCCESS;
395,168✔
64
}
65

66
static int32_t smlConvertJSONNumber(SSmlKv *pVal, char *typeStr, cJSON *value) {
592,434✔
67
  // tinyint
68
  if (strcasecmp(typeStr, "i8") == 0 || strcasecmp(typeStr, "tinyint") == 0) {
592,434✔
69
    if (!IS_VALID_TINYINT(value->valuedouble)) {
103,271✔
70
      uError("SML:JSON value(%f) cannot fit in type(tinyint)", value->valuedouble);
2,276✔
71
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,276✔
72
    }
73
    pVal->type = TSDB_DATA_TYPE_TINYINT;
101,007✔
74
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
101,007✔
75
    pVal->i = value->valuedouble;
101,007✔
76
    return TSDB_CODE_SUCCESS;
101,007✔
77
  }
78
  // smallint
79
  if (strcasecmp(typeStr, "i16") == 0 || strcasecmp(typeStr, "smallint") == 0) {
489,163✔
80
    if (!IS_VALID_SMALLINT(value->valuedouble)) {
99,857✔
81
      uError("SML:JSON value(%f) cannot fit in type(smallint)", value->valuedouble);
2,276✔
82
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,276✔
83
    }
84
    pVal->type = TSDB_DATA_TYPE_SMALLINT;
97,581✔
85
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
97,581✔
86
    pVal->i = value->valuedouble;
97,581✔
87
    return TSDB_CODE_SUCCESS;
97,581✔
88
  }
89
  // int
90
  if (strcasecmp(typeStr, "i32") == 0 || strcasecmp(typeStr, "int") == 0) {
389,306✔
91
    if (!IS_VALID_INT(value->valuedouble)) {
98,761✔
92
      uError("SML:JSON value(%f) cannot fit in type(int)", value->valuedouble);
2,276✔
93
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,276✔
94
    }
95
    pVal->type = TSDB_DATA_TYPE_INT;
96,485✔
96
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
96,485✔
97
    pVal->i = value->valuedouble;
96,485✔
98
    return TSDB_CODE_SUCCESS;
96,485✔
99
  }
100
  // bigint
101
  if (strcasecmp(typeStr, "i64") == 0 || strcasecmp(typeStr, "bigint") == 0) {
290,545✔
102
    pVal->type = TSDB_DATA_TYPE_BIGINT;
98,737✔
103
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
98,737✔
104
    if (value->valuedouble >= (double)INT64_MAX) {
98,737✔
105
      pVal->i = INT64_MAX;
90,471✔
106
    } else if (value->valuedouble <= (double)INT64_MIN) {
8,266✔
107
      pVal->i = INT64_MIN;
1,138✔
108
    } else {
109
      pVal->i = value->valuedouble;
7,128✔
110
    }
111
    return TSDB_CODE_SUCCESS;
98,737✔
112
  }
113
  // float
114
  if (strcasecmp(typeStr, "f32") == 0 || strcasecmp(typeStr, "float") == 0) {
191,808✔
115
    if (!IS_VALID_FLOAT(value->valuedouble)) {
97,629✔
116
      uError("SML:JSON value(%f) cannot fit in type(float)", value->valuedouble);
2,276✔
117
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,276✔
118
    }
119
    pVal->type = TSDB_DATA_TYPE_FLOAT;
95,353✔
120
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
95,353✔
121
    pVal->f = value->valuedouble;
95,353✔
122
    return TSDB_CODE_SUCCESS;
95,353✔
123
  }
124
  // double
125
  if (strcasecmp(typeStr, "f64") == 0 || strcasecmp(typeStr, "double") == 0) {
94,179✔
126
    pVal->type = TSDB_DATA_TYPE_DOUBLE;
94,179✔
127
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
94,179✔
128
    pVal->d = value->valuedouble;
94,179✔
129
    return TSDB_CODE_SUCCESS;
94,179✔
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) {
417,337✔
138
  if (strcasecmp(typeStr, "binary") == 0) {
417,337✔
139
    pVal->type = TSDB_DATA_TYPE_BINARY;
303,692✔
140
  } else if (strcasecmp(typeStr, "varbinary") == 0) {
113,645✔
141
    pVal->type = TSDB_DATA_TYPE_VARBINARY;
×
142
  } else if (strcasecmp(typeStr, "nchar") == 0) {
113,645✔
143
    pVal->type = TSDB_DATA_TYPE_NCHAR;
97,156✔
144
  } else {
145
    uError("SML:invalid type(%s) for JSON String", typeStr);
16,501✔
146
    return TSDB_CODE_TSC_INVALID_JSON_TYPE;
16,501✔
147
  }
148
  pVal->length = strlen(value->valuestring);
400,848✔
149

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

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

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

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

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

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

180
  switch (value->type) {
1,225,046✔
181
    case cJSON_True:
395,168✔
182
    case cJSON_False: {
183
      ret = smlConvertJSONBool(kv, type->valuestring, value);
395,168✔
184
      if (ret != TSDB_CODE_SUCCESS) {
395,168✔
185
        return ret;
×
186
      }
187
      break;
395,168✔
188
    }
189
    case cJSON_Number: {
592,434✔
190
      ret = smlConvertJSONNumber(kv, type->valuestring, value);
592,434✔
191
      if (ret != TSDB_CODE_SUCCESS) {
592,446✔
192
        return ret;
9,104✔
193
      }
194
      break;
583,342✔
195
    }
196
    case cJSON_String: {
237,444✔
197
      ret = smlConvertJSONString(kv, type->valuestring, value);
237,444✔
198
      if (ret != TSDB_CODE_SUCCESS) {
237,444✔
199
        return ret;
16,501✔
200
      }
201
      break;
220,943✔
202
    }
203
    default:
×
204
      return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
205
  }
206

207
  return TSDB_CODE_SUCCESS;
1,199,453✔
208
}
209

210
static int32_t smlParseValueFromJSON(cJSON *root, SSmlKv *kv) {
2,074,892✔
211
  switch (root->type) {
2,074,892✔
212
    case cJSON_True:
267,373✔
213
    case cJSON_False: {
214
      kv->type = TSDB_DATA_TYPE_BOOL;
267,373✔
215
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
267,373✔
216
      kv->i = root->valueint;
267,373✔
217
      break;
267,373✔
218
    }
219
    case cJSON_Number: {
401,418✔
220
      kv->type = TSDB_DATA_TYPE_DOUBLE;
401,418✔
221
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
401,418✔
222
      kv->d = root->valuedouble;
401,418✔
223
      break;
401,418✔
224
    }
225
    case cJSON_String: {
179,905✔
226
      int32_t ret = smlConvertJSONString(kv, "binary", root);
179,905✔
227
      if (ret != TSDB_CODE_SUCCESS) {
179,905✔
228
        uError("SML:Failed to parse binary value from JSON Obj");
×
229
        return ret;
×
230
      }
231
      break;
179,905✔
232
    }
233
    case cJSON_Object: {
1,225,058✔
234
      int32_t ret = smlParseValueFromJSONObj(root, kv);
1,225,058✔
235
      if (ret != TSDB_CODE_SUCCESS) {
1,225,058✔
236
        uError("SML:Failed to parse value from JSON Obj");
25,605✔
237
        return ret;
25,605✔
238
      }
239
      break;
1,199,453✔
240
    }
241
    default:
1,138✔
242
      return TSDB_CODE_TSC_INVALID_JSON;
1,138✔
243
  }
244

245
  return TSDB_CODE_SUCCESS;
2,048,149✔
246
}
247

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

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

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

274
    // value
275
    int32_t ret = smlParseValueFromJSON(tag, &kv);
1,664,982✔
276
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
1,664,982✔
277
      return ret;
11,380✔
278
    }
279
    if (taosArrayPush(preLineKV, &kv) == NULL) {
1,653,590✔
280
      return terrno;
×
281
    }
282

283
    if (info->dataFormat && !isSmlTagAligned(info, cnt, &kv)) {
1,653,590✔
284
      return TSDB_CODE_TSC_INVALID_JSON;
3,941✔
285
    }
286

287
    cnt++;
1,649,661✔
288
  }
289
  return TSDB_CODE_SUCCESS;
165,834✔
290
}
291

292
static int32_t smlParseTagsFromJSON(SSmlHandle *info, cJSON *tags, SSmlLineInfo *elements) {
394,559✔
293
  if (is_same_child_table_telnet(elements, &info->preLine) == 0) {
394,559✔
294
    elements->measureTag = info->preLine.measureTag;
53,324✔
295
    return TSDB_CODE_SUCCESS;
53,324✔
296
  }
297
  int32_t code = 0;
341,223✔
298
  int32_t lino = 0;
341,223✔
299
  if(info->dataFormat){
341,223✔
300
    SML_CHECK_CODE(smlProcessSuperTable(info, elements));
175,973✔
301
  }
302
  SML_CHECK_CODE(smlProcessTagJson(info, tags));
182,862✔
303
  SML_CHECK_CODE(smlJoinMeasureTag(elements));
165,834✔
304
  return smlProcessChildTable(info, elements);
165,834✔
305

306
END:
175,401✔
307
  if(info->reRun){
175,401✔
308
    return TSDB_CODE_SUCCESS;
160,549✔
309
  }
310
  RETURN
14,852✔
311
}
312

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

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

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

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

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

342
  if (timeDouble < 0) {
127,822✔
343
    return timeDouble;
×
344
  }
345

346
  int64_t tsInt64 = timeDouble;
127,822✔
347
  size_t  typeLen = strlen(type->valuestring);
127,822✔
348
  if (typeLen == 1 && (type->valuestring[0] == 's' || type->valuestring[0] == 'S')) {
127,822✔
349
    // seconds
350
    if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
1,709✔
351
      return tsInt64 * smlFactorS[toPrecision];
1,709✔
352
    }
353
    return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
354
  } else if (typeLen == 2 && (type->valuestring[1] == 's' || type->valuestring[1] == 'S')) {
126,113✔
355
    switch (type->valuestring[0]) {
126,113✔
356
      case 'm':
19,710✔
357
      case 'M':
358
        // milliseconds
359
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MILLI, toPrecision);
19,710✔
360
      case 'u':
×
361
      case 'U':
362
        // microseconds
363
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MICRO, toPrecision);
×
364
      case 'n':
106,403✔
365
      case 'N':
366
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_NANO, toPrecision);
106,403✔
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) {
84,502✔
376
  uint8_t len = 0;
84,502✔
377
  while ((num /= 10) != 0) {
985,636✔
378
    len++;
901,134✔
379
  }
380
  len++;
84,502✔
381
  return len;
84,502✔
382
}
383

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

402
    uint8_t tsLen = smlGetTimestampLen((int64_t)timeDouble);
84,502✔
403

404
    int8_t fromPrecision = smlGetTsTypeByLen(tsLen);
84,502✔
405
    if (unlikely(fromPrecision == -1)) {
84,502✔
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 = timeDouble;
84,502✔
411
    if (fromPrecision == TSDB_TIME_PRECISION_SECONDS) {
84,502✔
412
      if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
37,598✔
413
        return tsInt64 * smlFactorS[toPrecision];
37,598✔
414
      }
415
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
416
    } else {
417
      return convertTimePrecision(timeDouble, fromPrecision, toPrecision);
46,904✔
418
    }
419
  } else if (cJSON_IsObject(timestamp)) {
131,236✔
420
    return smlParseTSFromJSONObj(info, timestamp, toPrecision);
129,529✔
421
  } else {
422
    smlBuildInvalidDataMsg(&info->msgBuf, "invalidate json", NULL);
1,707✔
423
    return TSDB_CODE_TSC_INVALID_JSON;
1,707✔
424
  }
425
}
426

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

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

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

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

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

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

458
  if (unlikely(info->reRun)) {
379,707✔
459
    goto END;
160,549✔
460
  }
461

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

487
END:
411,048✔
488
  taosMemoryFree(elements->tags);
411,048✔
489
  RETURN
411,060✔
490
}
491

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

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

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

515
  int    cnt = 0;
192,983✔
516
  cJSON *dataPoint = head;
192,983✔
517
  while (dataPoint) {
569,276✔
518
    if (info->dataFormat) {
413,336✔
519
      SSmlLineInfo element = {0};
234,150✔
520
      ret = smlParseJSONStringExt(info, dataPoint, &element);
234,150✔
521
      if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
234,150✔
522
    } else {
523
      ret = smlParseJSONStringExt(info, dataPoint, info->lines + cnt);
179,186✔
524
    }
525
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
413,336✔
526
      uError("SML:0x%" PRIx64 " Invalid JSON Payload 2:%s", info->id, payload);
37,043✔
527
      return ret;
37,043✔
528
    }
529

530
    if (unlikely(info->reRun)) {
376,293✔
531
      cnt = 0;
160,549✔
532
      dataPoint = head;
160,549✔
533
      info->lineNum = payloadNum;
160,549✔
534
      ret = smlClearForRerun(info);
160,549✔
535
      if (ret != TSDB_CODE_SUCCESS) {
160,549✔
536
        return ret;
×
537
      }
538
      continue;
160,549✔
539
    }
540
    cnt++;
215,744✔
541
    dataPoint = dataPoint->next;
215,744✔
542
  }
543

544
  return TSDB_CODE_SUCCESS;
155,940✔
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