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

taosdata / TDengine / #4956

09 Feb 2026 01:16AM UTC coverage: 66.85% (-0.03%) from 66.884%
#4956

push

travis-ci

web-flow
docs: add support for recording STMT to CSV files (#34276)

* docs: add support for recording STMT to CSV files

* docs: update version for STMT recording feature in CSV files

205696 of 307696 relevant lines covered (66.85%)

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

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

35
const char    *jsonName[OTD_JSON_FIELDS_NUM] = {"metric", "timestamp", "value", "tags"};
36
static int32_t smlGetJsonElements(cJSON *root, cJSON ***marks) {
409,687✔
37
  for (int i = 0; i < OTD_JSON_FIELDS_NUM; ++i) {
2,048,291✔
38
    cJSON *child = root->child;
1,638,604✔
39
    while (child != NULL) {
4,096,510✔
40
      if (strcasecmp(child->string, jsonName[i]) == 0) {
4,096,510✔
41
        *marks[i] = child;
1,638,604✔
42
        break;
1,638,604✔
43
      }
44
      child = child->next;
2,457,906✔
45
    }
46
    if (*marks[i] == NULL) {
1,638,604✔
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;
409,687✔
52
}
53

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

63
  return TSDB_CODE_SUCCESS;
393,780✔
64
}
65

66
static int32_t smlConvertJSONNumber(SSmlKv *pVal, char *typeStr, cJSON *value) {
590,451✔
67
  // tinyint
68
  if (strcasecmp(typeStr, "i8") == 0 || strcasecmp(typeStr, "tinyint") == 0) {
590,451✔
69
    if (!IS_VALID_TINYINT(value->valuedouble)) {
102,921✔
70
      uError("SML:JSON value(%f) cannot fit in type(tinyint)", value->valuedouble);
2,268✔
71
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,268✔
72
    }
73
    pVal->type = TSDB_DATA_TYPE_TINYINT;
100,653✔
74
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
100,653✔
75
    pVal->i = value->valuedouble;
100,653✔
76
    return TSDB_CODE_SUCCESS;
100,653✔
77
  }
78
  // smallint
79
  if (strcasecmp(typeStr, "i16") == 0 || strcasecmp(typeStr, "smallint") == 0) {
487,530✔
80
    if (!IS_VALID_SMALLINT(value->valuedouble)) {
99,507✔
81
      uError("SML:JSON value(%f) cannot fit in type(smallint)", value->valuedouble);
2,268✔
82
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,268✔
83
    }
84
    pVal->type = TSDB_DATA_TYPE_SMALLINT;
97,239✔
85
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
97,239✔
86
    pVal->i = value->valuedouble;
97,239✔
87
    return TSDB_CODE_SUCCESS;
97,239✔
88
  }
89
  // int
90
  if (strcasecmp(typeStr, "i32") == 0 || strcasecmp(typeStr, "int") == 0) {
388,023✔
91
    if (!IS_VALID_INT(value->valuedouble)) {
98,436✔
92
      uError("SML:JSON value(%f) cannot fit in type(int)", value->valuedouble);
2,268✔
93
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,268✔
94
    }
95
    pVal->type = TSDB_DATA_TYPE_INT;
96,168✔
96
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
96,168✔
97
    pVal->i = value->valuedouble;
96,168✔
98
    return TSDB_CODE_SUCCESS;
96,168✔
99
  }
100
  // bigint
101
  if (strcasecmp(typeStr, "i64") == 0 || strcasecmp(typeStr, "bigint") == 0) {
289,587✔
102
    pVal->type = TSDB_DATA_TYPE_BIGINT;
98,385✔
103
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
98,385✔
104
    if (value->valuedouble >= (double)INT64_MAX) {
98,385✔
105
      pVal->i = INT64_MAX;
90,153✔
106
    } else if (value->valuedouble <= (double)INT64_MIN) {
8,232✔
107
      pVal->i = INT64_MIN;
1,134✔
108
    } else {
109
      pVal->i = value->valuedouble;
7,098✔
110
    }
111
    return TSDB_CODE_SUCCESS;
98,385✔
112
  }
113
  // float
114
  if (strcasecmp(typeStr, "f32") == 0 || strcasecmp(typeStr, "float") == 0) {
191,202✔
115
    if (!IS_VALID_FLOAT(value->valuedouble)) {
97,341✔
116
      uError("SML:JSON value(%f) cannot fit in type(float)", value->valuedouble);
2,268✔
117
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,268✔
118
    }
119
    pVal->type = TSDB_DATA_TYPE_FLOAT;
95,073✔
120
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
95,073✔
121
    pVal->f = value->valuedouble;
95,073✔
122
    return TSDB_CODE_SUCCESS;
95,073✔
123
  }
124
  // double
125
  if (strcasecmp(typeStr, "f64") == 0 || strcasecmp(typeStr, "double") == 0) {
93,861✔
126
    pVal->type = TSDB_DATA_TYPE_DOUBLE;
93,861✔
127
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
93,861✔
128
    pVal->d = value->valuedouble;
93,861✔
129
    return TSDB_CODE_SUCCESS;
93,861✔
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) {
415,837✔
138
  if (strcasecmp(typeStr, "binary") == 0) {
415,837✔
139
    pVal->type = TSDB_DATA_TYPE_BINARY;
302,614✔
140
  } else if (strcasecmp(typeStr, "varbinary") == 0) {
113,223✔
141
    pVal->type = TSDB_DATA_TYPE_VARBINARY;
×
142
  } else if (strcasecmp(typeStr, "nchar") == 0) {
113,223✔
143
    pVal->type = TSDB_DATA_TYPE_NCHAR;
96,804✔
144
  } else {
145
    uError("SML:invalid type(%s) for JSON String", typeStr);
16,443✔
146
    return TSDB_CODE_TSC_INVALID_JSON_TYPE;
16,443✔
147
  }
148
  pVal->length = strlen(value->valuestring);
399,418✔
149

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

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

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

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

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

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

180
  switch (value->type) {
1,220,834✔
181
    case cJSON_True:
393,780✔
182
    case cJSON_False: {
183
      ret = smlConvertJSONBool(kv, type->valuestring, value);
393,780✔
184
      if (ret != TSDB_CODE_SUCCESS) {
393,780✔
185
        return ret;
×
186
      }
187
      break;
393,780✔
188
    }
189
    case cJSON_Number: {
590,451✔
190
      ret = smlConvertJSONNumber(kv, type->valuestring, value);
590,451✔
191
      if (ret != TSDB_CODE_SUCCESS) {
590,451✔
192
        return ret;
9,072✔
193
      }
194
      break;
581,379✔
195
    }
196
    case cJSON_String: {
236,603✔
197
      ret = smlConvertJSONString(kv, type->valuestring, value);
236,603✔
198
      if (ret != TSDB_CODE_SUCCESS) {
236,603✔
199
        return ret;
16,443✔
200
      }
201
      break;
220,160✔
202
    }
203
    default:
×
204
      return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
205
  }
206

207
  return TSDB_CODE_SUCCESS;
1,195,319✔
208
}
209

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

245
  return TSDB_CODE_SUCCESS;
2,041,156✔
246
}
247

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

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

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

274
    // value
275
    int32_t ret = smlParseValueFromJSON(tag, &kv);
1,659,240✔
276
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
1,659,264✔
277
      return ret;
11,340✔
278
    }
279
    if (taosArrayPush(preLineKV, &kv) == NULL) {
1,647,888✔
280
      return terrno;
×
281
    }
282

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

287
    cnt++;
1,643,974✔
288
  }
289
  return TSDB_CODE_SUCCESS;
165,230✔
290
}
291

292
static int32_t smlParseTagsFromJSON(SSmlHandle *info, cJSON *tags, SSmlLineInfo *elements) {
393,208✔
293
  if (is_same_child_table_telnet(elements, &info->preLine) == 0) {
393,208✔
294
    elements->measureTag = info->preLine.measureTag;
53,294✔
295
    return TSDB_CODE_SUCCESS;
53,294✔
296
  }
297
  int32_t code = 0;
339,938✔
298
  int32_t lino = 0;
339,938✔
299
  if(info->dataFormat){
339,938✔
300
    SML_CHECK_CODE(smlProcessSuperTable(info, elements));
175,384✔
301
  }
302
  SML_CHECK_CODE(smlProcessTagJson(info, tags));
182,185✔
303
  SML_CHECK_CODE(smlJoinMeasureTag(elements));
165,230✔
304
  return smlProcessChildTable(info, elements);
165,230✔
305

306
END:
174,708✔
307
  if(info->reRun){
174,708✔
308
    return TSDB_CODE_SUCCESS;
159,905✔
309
  }
310
  RETURN
14,803✔
311
}
312

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

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

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

332
  double timeDouble = value->valuedouble;
127,425✔
333
  if (unlikely(smlDoubleToInt64OverFlow(timeDouble))) {
127,425✔
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,413✔
339
    return taosGetTimestampNs() / smlFactorNS[toPrecision];
×
340
  }
341

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

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

375
uint8_t smlGetTimestampLen(int64_t num) {
84,307✔
376
  uint8_t len = 0;
84,307✔
377
  while ((num /= 10) != 0) {
983,647✔
378
    len++;
899,340✔
379
  }
380
  len++;
84,307✔
381
  return len;
84,307✔
382
}
383

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

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

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

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

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

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

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

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

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

458
  if (unlikely(info->reRun)) {
378,429✔
459
    goto END;
159,905✔
460
  }
461

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

487
END:
409,675✔
488
  taosMemoryFree(elements->tags);
409,675✔
489
  RETURN
409,687✔
490
}
491

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

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

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

515
  int    cnt = 0;
192,314✔
516
  cJSON *dataPoint = head;
192,314✔
517
  while (dataPoint) {
567,353✔
518
    if (info->dataFormat) {
411,931✔
519
      SSmlLineInfo element = {0};
234,019✔
520
      ret = smlParseJSONStringExt(info, dataPoint, &element);
234,019✔
521
      if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
234,031✔
522
    } else {
523
      ret = smlParseJSONStringExt(info, dataPoint, info->lines + cnt);
177,912✔
524
    }
525
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
411,955✔
526
      uError("SML:0x%" PRIx64 " Invalid JSON Payload 2:%s", info->id, payload);
36,916✔
527
      return ret;
36,916✔
528
    }
529

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

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