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

taosdata / TDengine / #3563

21 Dec 2024 05:37AM UTC coverage: 61.045% (+34.4%) from 26.655%
#3563

push

travis-ci

web-flow
Merge pull request #29256 from taosdata/merge/mainto3.0

merge: from main t 3.0

135730 of 287838 branches covered (47.15%)

Branch coverage included in aggregate %.

9 of 28 new or added lines in 5 files covered. (32.14%)

784 existing lines in 21 files now uncovered.

213302 of 283921 relevant lines covered (75.13%)

9176355.9 hits per line

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

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

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

35
const char    *jsonName[OTD_JSON_FIELDS_NUM] = {"metric", "timestamp", "value", "tags"};
36
static int32_t smlGetJsonElements(cJSON *root, cJSON ***marks) {
1,553✔
37
  for (int i = 0; i < OTD_JSON_FIELDS_NUM; ++i) {
7,757✔
38
    cJSON *child = root->child;
6,204✔
39
    while (child != NULL) {
15,514!
40
      if (strcasecmp(child->string, jsonName[i]) == 0) {
15,520✔
41
        *marks[i] = child;
6,210✔
42
        break;
6,210✔
43
      }
44
      child = child->next;
9,310✔
45
    }
46
    if (*marks[i] == NULL) {
6,204!
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;
1,553✔
52
}
53

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

63
  return TSDB_CODE_SUCCESS;
×
64
}
65

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

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

158
  pVal->value = value->valuestring;
688✔
159
  return TSDB_CODE_SUCCESS;
688✔
160
}
161

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

166
  if (size != OTD_JSON_SUB_FIELDS_NUM) {
12!
167
    return TSDB_CODE_TSC_INVALID_JSON;
×
168
  }
169

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

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

180
  switch (value->type) {
12!
181
    case cJSON_True:
×
182
    case cJSON_False: {
183
      ret = smlConvertJSONBool(kv, type->valuestring, value);
×
184
      if (ret != TSDB_CODE_SUCCESS) {
×
185
        return ret;
×
186
      }
187
      break;
×
188
    }
189
    case cJSON_Number: {
9✔
190
      ret = smlConvertJSONNumber(kv, type->valuestring, value);
9✔
191
      if (ret != TSDB_CODE_SUCCESS) {
9!
192
        return ret;
×
193
      }
194
      break;
9✔
195
    }
196
    case cJSON_String: {
3✔
197
      ret = smlConvertJSONString(kv, type->valuestring, value);
3✔
198
      if (ret != TSDB_CODE_SUCCESS) {
3!
199
        return ret;
×
200
      }
201
      break;
3✔
202
    }
203
    default:
×
204
      return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
205
  }
206

207
  return TSDB_CODE_SUCCESS;
12✔
208
}
209

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

245
  return TSDB_CODE_SUCCESS;
2,960✔
246
}
247

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

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

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

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

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

287
    cnt++;
1,398✔
288
  }
289
  return TSDB_CODE_SUCCESS;
190✔
290
}
291

292
static int32_t smlParseTagsFromJSON(SSmlHandle *info, cJSON *tags, SSmlLineInfo *elements) {
1,554✔
293
  if (is_same_child_table_telnet(elements, &info->preLine) == 0) {
1,554✔
294
    elements->measureTag = info->preLine.measureTag;
1,299✔
295
    return TSDB_CODE_SUCCESS;
1,299✔
296
  }
297
  int32_t code = 0;
255✔
298
  int32_t lino = 0;
255✔
299
  if(info->dataFormat){
255✔
300
    SML_CHECK_CODE(smlProcessSuperTable(info, elements));
180✔
301
  }
302
  SML_CHECK_CODE(smlProcessTagJson(info, tags));
198✔
303
  SML_CHECK_CODE(smlJoinMeasureTag(elements));
189!
304
  return smlProcessChildTable(info, elements);
189✔
305

306
END:
66✔
307
  if(info->reRun){
66✔
308
    return TSDB_CODE_SUCCESS;
63✔
309
  }
310
  RETURN
3!
311
}
312

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

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

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

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

338
  if (timeDouble == 0) {
9!
339
    return taosGetTimestampNs() / smlFactorNS[toPrecision];
×
340
  }
341

342
  if (timeDouble < 0) {
9!
343
    return timeDouble;
×
344
  }
345

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

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

402
    uint8_t tsLen = smlGetTimestampLen((int64_t)timeDouble);
1,470✔
403

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

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

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

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

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

451
  // Parse tags
452
  elements->tags = cJSON_PrintUnformatted(tagsJson);
1,553✔
453
  SML_CHECK_NULL(elements->tags);
1,554!
454

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

458
  if (unlikely(info->reRun)) {
1,551✔
459
    goto END;
63✔
460
  }
461

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

487
END:
1,553✔
488
  taosMemoryFree(elements->tags);
1,553!
489
  RETURN
1,553!
490
}
491

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

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

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

515
  int    cnt = 0;
177✔
516
  cJSON *dataPoint = head;
177✔
517
  while (dataPoint) {
1,727✔
518
    if (info->dataFormat) {
1,554✔
519
      SSmlLineInfo element = {0};
1,152✔
520
      ret = smlParseJSONStringExt(info, dataPoint, &element);
1,152✔
521
      if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
1,152!
522
    } else {
523
      ret = smlParseJSONStringExt(info, dataPoint, info->lines + cnt);
402✔
524
    }
525
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
1,553✔
526
      uError("SML:0x%" PRIx64 " Invalid JSON Payload 2:%s", info->id, payload);
3!
527
      return ret;
3✔
528
    }
529

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

544
  return TSDB_CODE_SUCCESS;
173✔
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