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

taosdata / TDengine / #3942

25 Apr 2025 11:21AM UTC coverage: 62.853% (+0.3%) from 62.507%
#3942

push

travis-ci

web-flow
docs: jdbc tmq supports database subscription. [TS-6222] (#30819)

* docs: jdbc tmq supports database subscription. [TS-6222]

* Update docs/zh/07-develop/07-tmq.md

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update 07-tmq.md

---------

Co-authored-by: haoranchen <haoran920c@163.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

156603 of 317531 branches covered (49.32%)

Branch coverage included in aggregate %.

241895 of 316485 relevant lines covered (76.43%)

6664240.48 hits per line

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

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

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

35
const char    *jsonName[OTD_JSON_FIELDS_NUM] = {"metric", "timestamp", "value", "tags"};
36
static int32_t smlGetJsonElements(cJSON *root, cJSON ***marks) {
5,317✔
37
  for (int i = 0; i < OTD_JSON_FIELDS_NUM; ++i) {
26,553✔
38
    cJSON *child = root->child;
21,236✔
39
    while (child != NULL) {
53,101!
40
      if (strcasecmp(child->string, jsonName[i]) == 0) {
53,126✔
41
        *marks[i] = child;
21,261✔
42
        break;
21,261✔
43
      }
44
      child = child->next;
31,865✔
45
    }
46
    if (*marks[i] == NULL) {
21,236!
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;
5,317✔
52
}
53

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

63
  return TSDB_CODE_SUCCESS;
995✔
64
}
65

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

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

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

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

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

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

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

180
  switch (value->type) {
4,905!
181
    case cJSON_True:
996✔
182
    case cJSON_False: {
183
      ret = smlConvertJSONBool(kv, type->valuestring, value);
996✔
184
      if (ret != TSDB_CODE_SUCCESS) {
997!
185
        return ret;
×
186
      }
187
      break;
997✔
188
    }
189
    case cJSON_Number: {
2,871✔
190
      ret = smlConvertJSONNumber(kv, type->valuestring, value);
2,871✔
191
      if (ret != TSDB_CODE_SUCCESS) {
2,868✔
192
        return ret;
16✔
193
      }
194
      break;
2,852✔
195
    }
196
    case cJSON_String: {
1,038✔
197
      ret = smlConvertJSONString(kv, type->valuestring, value);
1,038✔
198
      if (ret != TSDB_CODE_SUCCESS) {
1,037✔
199
        return ret;
29✔
200
      }
201
      break;
1,008✔
202
    }
203
    default:
×
204
      return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
205
  }
206

207
  return TSDB_CODE_SUCCESS;
4,857✔
208
}
209

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

245
  return TSDB_CODE_SUCCESS;
13,089✔
246
}
247

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

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

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

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

283
    if (info->dataFormat && !isSmlTagAligned(info, cnt, &kv)) {
7,793✔
284
      return TSDB_CODE_TSC_INVALID_JSON;
19✔
285
    }
286

287
    cnt++;
7,773✔
288
  }
289
  return TSDB_CODE_SUCCESS;
861✔
290
}
291

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

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

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

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

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

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

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

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

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

375
uint8_t smlGetTimestampLen(int64_t num) {
3,164✔
376
  uint8_t len = 0;
3,164✔
377
  while ((num /= 10) != 0) {
40,700✔
378
    len++;
37,536✔
379
  }
380
  len++;
3,164✔
381
  return len;
3,164✔
382
}
383

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

402
    uint8_t tsLen = smlGetTimestampLen((int64_t)timeDouble);
3,157✔
403

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

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

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

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

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

451
  // Parse tags
452
  elements->tags = cJSON_PrintUnformatted(tagsJson);
5,290✔
453
  SML_CHECK_NULL(elements->tags);
5,294!
454

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

458
  if (unlikely(info->reRun)) {
5,263✔
459
    goto END;
428✔
460
  }
461

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

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

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

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

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

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

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

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