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

taosdata / TDengine / #4801

16 Oct 2025 11:10AM UTC coverage: 61.198% (+7.3%) from 53.935%
#4801

push

travis-ci

web-flow
Merge 2f5c45b1e into 1afedf5da

155671 of 324467 branches covered (47.98%)

Branch coverage included in aggregate %.

56 of 68 new or added lines in 4 files covered. (82.35%)

251 existing lines in 79 files now uncovered.

207880 of 269594 relevant lines covered (77.11%)

127097734.85 hits per line

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

72.82
/source/client/src/clientSml.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

21
#include "clientSml.h"
22

23
#define RETURN_FALSE                                 \
24
  smlBuildInvalidDataMsg(msg, "invalid data", pVal); \
25
  return false;
26

27
#define SET_DOUBLE                     \
28
  kvVal->type = TSDB_DATA_TYPE_DOUBLE; \
29
  kvVal->d = result;
30

31
#define SET_FLOAT                                                                              \
32
  if (!IS_VALID_FLOAT(result)) {                                                               \
33
    smlBuildInvalidDataMsg(msg, "float out of range[-3.402823466e+38,3.402823466e+38]", pVal); \
34
    return false;                                                                              \
35
  }                                                                                            \
36
  kvVal->type = TSDB_DATA_TYPE_FLOAT;                                                          \
37
  kvVal->f = (float)result;
38

39
#define SET_BIGINT                                                                                       \
40
  SET_ERRNO(0);                                                                                          \
41
  int64_t tmp = taosStr2Int64(pVal, &endptr, 10);                                                        \
42
  if (ERRNO == ERANGE) {                                                                                 \
43
    smlBuildInvalidDataMsg(msg, "big int out of range[-9223372036854775808,9223372036854775807]", pVal); \
44
    return false;                                                                                        \
45
  }                                                                                                      \
46
  kvVal->type = TSDB_DATA_TYPE_BIGINT;                                                                   \
47
  kvVal->i = tmp;
48

49
#define SET_INT                                                                    \
50
  if (!IS_VALID_INT(result)) {                                                     \
51
    smlBuildInvalidDataMsg(msg, "int out of range[-2147483648,2147483647]", pVal); \
52
    return false;                                                                  \
53
  }                                                                                \
54
  kvVal->type = TSDB_DATA_TYPE_INT;                                                \
55
  kvVal->i = result;
56

57
#define SET_SMALL_INT                                                          \
58
  if (!IS_VALID_SMALLINT(result)) {                                            \
59
    smlBuildInvalidDataMsg(msg, "small int our of range[-32768,32767]", pVal); \
60
    return false;                                                              \
61
  }                                                                            \
62
  kvVal->type = TSDB_DATA_TYPE_SMALLINT;                                       \
63
  kvVal->i = result;
64

65
#define SET_UBIGINT                                                                             \
66
  SET_ERRNO(0);                                                                                 \
67
  uint64_t tmp = taosStr2UInt64(pVal, &endptr, 10);                                             \
68
  if (ERRNO == ERANGE || result < 0) {                                                          \
69
    smlBuildInvalidDataMsg(msg, "unsigned big int out of range[0,18446744073709551615]", pVal); \
70
    return false;                                                                               \
71
  }                                                                                             \
72
  kvVal->type = TSDB_DATA_TYPE_UBIGINT;                                                         \
73
  kvVal->u = tmp;
74

75
#define SET_UINT                                                                  \
76
  if (!IS_VALID_UINT(result)) {                                                   \
77
    smlBuildInvalidDataMsg(msg, "unsigned int out of range[0,4294967295]", pVal); \
78
    return false;                                                                 \
79
  }                                                                               \
80
  kvVal->type = TSDB_DATA_TYPE_UINT;                                              \
81
  kvVal->u = result;
82

83
#define SET_USMALL_INT                                                            \
84
  if (!IS_VALID_USMALLINT(result)) {                                              \
85
    smlBuildInvalidDataMsg(msg, "unsigned small int out of rang[0,65535]", pVal); \
86
    return false;                                                                 \
87
  }                                                                               \
88
  kvVal->type = TSDB_DATA_TYPE_USMALLINT;                                         \
89
  kvVal->u = result;
90

91
#define SET_TINYINT                                                       \
92
  if (!IS_VALID_TINYINT(result)) {                                        \
93
    smlBuildInvalidDataMsg(msg, "tiny int out of range[-128,127]", pVal); \
94
    return false;                                                         \
95
  }                                                                       \
96
  kvVal->type = TSDB_DATA_TYPE_TINYINT;                                   \
97
  kvVal->i = result;
98

99
#define SET_UTINYINT                                                            \
100
  if (!IS_VALID_UTINYINT(result)) {                                             \
101
    smlBuildInvalidDataMsg(msg, "unsigned tiny int out of range[0,255]", pVal); \
102
    return false;                                                               \
103
  }                                                                             \
104
  kvVal->type = TSDB_DATA_TYPE_UTINYINT;                                        \
105
  kvVal->u = result;
106

107
#define IS_COMMENT(protocol, data) (protocol == TSDB_SML_LINE_PROTOCOL && data == '#')
108

109
int64_t smlToMilli[] = {3600000LL, 60000LL, 1000LL};
110
int64_t smlFactorNS[] = {NANOSECOND_PER_MSEC, NANOSECOND_PER_USEC, 1};
111
int64_t smlFactorS[] = {1000LL, 1000000LL, 1000000000LL};
112

113
static int32_t smlCheckAuth(SSmlHandle *info, SRequestConnInfo *conn, const char *pTabName, AUTH_TYPE type) {
2,157,264✔
114
  SUserAuthInfo pAuth = {0};
2,157,264✔
115
  (void)snprintf(pAuth.user, sizeof(pAuth.user), "%s", info->taos->user);
2,157,264✔
116
  if (NULL == pTabName) {
2,156,397✔
117
    if (tNameSetDbName(&pAuth.tbName, info->taos->acctId, info->pRequest->pDb, strlen(info->pRequest->pDb)) != 0) {
443,244!
118
      return TSDB_CODE_SML_INVALID_DATA;
×
119
    }
120
  } else {
121
    toName(info->taos->acctId, info->pRequest->pDb, pTabName, &pAuth.tbName);
1,713,153✔
122
  }
123
  pAuth.type = type;
2,157,657✔
124

125
  int32_t      code = TSDB_CODE_SUCCESS;
2,157,657✔
126
  SUserAuthRes authRes = {0};
2,157,657✔
127

128
  code = catalogChkAuth(info->pCatalog, conn, &pAuth, &authRes);
2,157,657✔
129
  nodesDestroyNode(authRes.pCond[AUTH_RES_BASIC]);
2,153,064✔
130

131
  return (code == TSDB_CODE_SUCCESS)
132
             ? (authRes.pass[AUTH_RES_BASIC] ? TSDB_CODE_SUCCESS : TSDB_CODE_PAR_PERMISSION_DENIED)
2,153,511!
133
             : code;
4,276,548!
134
}
135

136
void smlBuildInvalidDataMsg(SSmlMsgBuf *pBuf, const char *msg1, const char *msg2) {
85,200✔
137
  if (pBuf->buf == NULL) {
85,200✔
138
    return;
846✔
139
  }
140
  pBuf->buf[0] = 0;
84,354✔
141
  if (msg1) {
84,354!
142
    (void)strncat(pBuf->buf, msg1, pBuf->len - 1);
84,354!
143
  }
144
  int32_t left = pBuf->len - strlen(pBuf->buf);
84,354!
145
  if (left > 2 && msg2) {
84,354!
146
    (void)strncat(pBuf->buf, ":", left - 1);
76,148!
147
    (void)strncat(pBuf->buf, msg2, left - 2);
76,148!
148
  }
149
}
150

151
int64_t smlGetTimeValue(const char *value, int32_t len, uint8_t fromPrecision, uint8_t toPrecision) {
11,487,665✔
152
  char   *endPtr = NULL;
11,487,665✔
153
  int64_t tsInt64 = taosStr2Int64(value, &endPtr, 10);
11,487,665✔
154
  if (unlikely(value + len != endPtr)) {
11,501,697✔
155
    return -1;
3,002✔
156
  }
157

158
  if (unlikely(fromPrecision >= TSDB_TIME_PRECISION_HOURS)) {
11,499,115✔
159
    int64_t unit = smlToMilli[fromPrecision - TSDB_TIME_PRECISION_HOURS];
12,288✔
160
    if (tsInt64 != 0 && unit > INT64_MAX / tsInt64) {
12,288!
161
      return -1;
9✔
162
    }
163
    tsInt64 *= unit;
12,279✔
164
    fromPrecision = TSDB_TIME_PRECISION_MILLI;
12,279✔
165
  }
166

167
  return convertTimePrecision(tsInt64, fromPrecision, toPrecision);
11,499,106✔
168
}
169

170
int32_t smlBuildTableInfo(int numRows, const char *measure, int32_t measureLen, SSmlTableInfo **tInfo) {
1,912,480✔
171
  int32_t        code = 0;
1,912,480✔
172
  int32_t        lino = 0;
1,912,480✔
173
  SSmlTableInfo *tag = (SSmlTableInfo *)taosMemoryCalloc(sizeof(SSmlTableInfo), 1);
1,912,480!
174
  SML_CHECK_NULL(tag)
1,912,900!
175

176
  tag->sTableName = measure;
1,912,900✔
177
  tag->sTableNameLen = measureLen;
1,913,320✔
178

179
  tag->cols = taosArrayInit(numRows, POINTER_BYTES);
1,913,320✔
180
  SML_CHECK_NULL(tag->cols)
1,913,320!
181
  *tInfo = tag;
1,913,320✔
182
  return code;
1,913,320✔
183

184
END:
×
185
  taosMemoryFree(tag);
×
186
  uError("%s failed code:%d line:%d", __FUNCTION__, code, lino);
×
187
  return code;
×
188
}
189

190
void smlBuildTsKv(SSmlKv *kv, int64_t ts) {
12,733,966✔
191
  kv->key = tsSmlTsDefaultName;
12,733,966✔
192
  kv->keyLen = strlen(tsSmlTsDefaultName);
12,734,359!
193
  kv->type = TSDB_DATA_TYPE_TIMESTAMP;
12,735,172✔
194
  kv->i = ts;
12,735,172✔
195
  kv->length = (size_t)tDataTypes[TSDB_DATA_TYPE_TIMESTAMP].bytes;
12,734,779✔
196
}
12,734,752✔
197

198
static void smlDestroySTableMeta(void *para) {
1,491,511✔
199
  if (para == NULL) {
1,491,511!
200
    return;
×
201
  }
202
  SSmlSTableMeta *meta = *(SSmlSTableMeta **)para;
1,491,511✔
203
  if (meta == NULL) {
1,491,511✔
204
    return;
494,082✔
205
  }
206
  taosHashCleanup(meta->tagHash);
997,429✔
207
  taosHashCleanup(meta->colHash);
997,429✔
208
  taosArrayDestroy(meta->tags);
997,429✔
209
  taosArrayDestroy(meta->cols);
997,846✔
210
  taosMemoryFreeClear(meta->tableMeta);
997,846!
211
  taosMemoryFree(meta);
997,846!
212
}
213

214
int32_t smlBuildSuperTableInfo(SSmlHandle *info, SSmlLineInfo *currElement, SSmlSTableMeta **sMeta) {
855,997✔
215
  int32_t     code = TSDB_CODE_SUCCESS;
855,997✔
216
  int32_t     lino = 0;
855,997✔
217
  STableMeta *pTableMeta = NULL;
855,997✔
218

219
  int   measureLen = currElement->measureLen;
855,997✔
220
  char *measure = (char *)taosMemoryMalloc(measureLen);
855,997!
221
  SML_CHECK_NULL(measure);
855,997!
222
  (void)memcpy(measure, currElement->measure, measureLen);
855,997!
223
  if (currElement->measureEscaped) {
855,997!
224
    PROCESS_SLASH_IN_MEASUREMENT(measure, measureLen);
9,288!
225
  }
226
  smlStrReplace(measure, measureLen);
855,997✔
227
  code = smlGetMeta(info, measure, measureLen, &pTableMeta);
855,997✔
228
  taosMemoryFree(measure);
855,997!
229
  if (code != TSDB_CODE_SUCCESS) {
855,997✔
230
    info->dataFormat = false;
494,082✔
231
    info->reRun = true;
494,082✔
232
    goto END;
494,082✔
233
  }
234
  SML_CHECK_CODE(smlBuildSTableMeta(info->dataFormat, sMeta));
361,915!
235
  for (int i = 0; i < pTableMeta->tableInfo.numOfTags + pTableMeta->tableInfo.numOfColumns; i++) {
7,964,796✔
236
    SSchema *col = pTableMeta->schema + i;
7,602,881✔
237
    SSmlKv   kv = {.key = col->name, .keyLen = strlen(col->name), .type = col->type};
7,602,881!
238
    if (col->type == TSDB_DATA_TYPE_NCHAR) {
7,604,132✔
239
      kv.length = (col->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
1,328,985✔
240
    } else if (col->type == TSDB_DATA_TYPE_BINARY || col->type == TSDB_DATA_TYPE_GEOMETRY ||
6,275,147✔
241
               col->type == TSDB_DATA_TYPE_VARBINARY) {
3,773,009✔
242
      kv.length = col->bytes - VARSTR_HEADER_SIZE;
2,504,202✔
243
    } else {
244
      kv.length = col->bytes;
3,770,945✔
245
    }
246

247
    if (i < pTableMeta->tableInfo.numOfColumns) {
7,603,715✔
248
      SML_CHECK_NULL(taosArrayPush((*sMeta)->cols, &kv));
10,740,043!
249
    } else {
250
      SML_CHECK_NULL(taosArrayPush((*sMeta)->tags, &kv));
4,466,970!
251
    }
252
  }
253
  SML_CHECK_CODE(taosHashPut(info->superTables, currElement->measure, currElement->measureLen, sMeta, POINTER_BYTES));
361,915!
254
  (*sMeta)->tableMeta = pTableMeta;
361,915✔
255
  return code;
361,915✔
256

257
END:
494,082✔
258
  smlDestroySTableMeta(sMeta);
494,082✔
259
  taosMemoryFreeClear(pTableMeta);
494,082!
260
  RETURN
494,082!
261
}
262

263
bool isSmlColAligned(SSmlHandle *info, int cnt, SSmlKv *kv) {
173,207✔
264
  // cnt begin 0, add ts so + 2
265
  if (unlikely(cnt + 2 > info->currSTableMeta->tableInfo.numOfColumns)) {
173,207!
266
    goto END;
×
267
  }
268
  // bind data
269
  int32_t ret =
270
      smlBuildCol(info->currTableDataCtx, info->currSTableMeta->schema, kv, cnt + 1, info->taos->optionInfo.charsetCxt);
173,207✔
271
  if (unlikely(ret != TSDB_CODE_SUCCESS)) {
173,207✔
272
    uDebug("smlBuildCol error, retry");
2,524!
273
    goto END;
2,524✔
274
  }
275
  if (cnt >= taosArrayGetSize(info->maxColKVs)) {
170,683!
276
    goto END;
×
277
  }
278
  SSmlKv *maxKV = (SSmlKv *)taosArrayGet(info->maxColKVs, cnt);
170,683✔
279
  if (maxKV == NULL) {
170,683!
280
    goto END;
×
281
  }
282
  if (unlikely(!IS_SAME_KEY)) {
170,683!
283
    goto END;
170,683✔
284
  }
285

286
  if (unlikely(IS_VAR_DATA_TYPE(kv->type) && kv->length > maxKV->length)) {
×
287
    maxKV->length = kv->length;
×
288
    info->needModifySchema = true;
×
289
  }
290
  return true;
×
291

292
END:
173,207✔
293
  info->dataFormat = false;
173,207✔
294
  info->reRun = true;
173,207✔
295
  return false;
172,790✔
296
}
297

298
bool isSmlTagAligned(SSmlHandle *info, int cnt, SSmlKv *kv) {
2,252,859✔
299
  if (unlikely(cnt + 1 > info->currSTableMeta->tableInfo.numOfTags)) {
2,252,859✔
300
    goto END;
5,852✔
301
  }
302

303
  if (unlikely(cnt >= taosArrayGetSize(info->maxTagKVs))) {
2,247,007!
304
    goto END;
×
305
  }
306
  SSmlKv *maxKV = (SSmlKv *)taosArrayGet(info->maxTagKVs, cnt);
2,247,090✔
307
  if (maxKV == NULL) {
2,247,090!
308
    goto END;
×
309
  }
310
  if (unlikely(!IS_SAME_KEY)) {
2,247,090!
311
    goto END;
6,196✔
312
  }
313

314
  if (unlikely(kv->length > maxKV->length)) {
2,240,894✔
315
    maxKV->length = kv->length;
11,020✔
316
    info->needModifySchema = true;
11,020✔
317
  }
318
  return true;
2,240,894✔
319

320
END:
12,048✔
321
  info->dataFormat = false;
12,048✔
322
  info->reRun = true;
12,048✔
323
  return false;
12,048✔
324
}
325

326
int32_t smlJoinMeasureTag(SSmlLineInfo *elements) {
518,022✔
327
  elements->measureTag = (char *)taosMemoryMalloc(elements->measureLen + elements->tagsLen);
518,022!
328
  if (elements->measureTag == NULL) {
518,022!
329
    return terrno;
×
330
  }
331
  (void)memcpy(elements->measureTag, elements->measure, elements->measureLen);
518,022!
332
  (void)memcpy(elements->measureTag + elements->measureLen, elements->tags, elements->tagsLen);
518,022!
333
  elements->measureTagsLen = elements->measureLen + elements->tagsLen;
518,022✔
334
  return TSDB_CODE_SUCCESS;
518,022✔
335
}
336

337
static bool smlIsPKTable(STableMeta *pTableMeta) {
591,661✔
338
  for (int i = 0; i < pTableMeta->tableInfo.numOfColumns; i++) {
11,280,468✔
339
    if (pTableMeta->schema[i].flags & COL_IS_KEY) {
10,693,196✔
340
      return true;
4,389✔
341
    }
342
  }
343

344
  return false;
587,272✔
345
}
346

347
int32_t smlProcessSuperTable(SSmlHandle *info, SSmlLineInfo *elements) {
861,612✔
348
  bool isSameMeasure = IS_SAME_SUPER_TABLE;
861,612!
349
  if (isSameMeasure) {
861,612✔
350
    return TSDB_CODE_SUCCESS;
5,597✔
351
  }
352
  SSmlSTableMeta **tmp = (SSmlSTableMeta **)taosHashGet(info->superTables, elements->measure, elements->measureLen);
856,015✔
353

354
  SSmlSTableMeta *sMeta = NULL;
856,015✔
355
  if (unlikely(tmp == NULL)) {
856,015✔
356
    int32_t code = smlBuildSuperTableInfo(info, elements, &sMeta);
855,997✔
357
    if (code != 0) return code;
855,997✔
358
  } else {
359
    sMeta = *tmp;
18✔
360
  }
361
  if (sMeta == NULL) {
361,933!
362
    uError("smlProcessSuperTable failed to get super table meta");
×
363
    return TSDB_CODE_SML_INTERNAL_ERROR;
×
364
  }
365
  info->currSTableMeta = sMeta->tableMeta;
361,933✔
366
  info->maxTagKVs = sMeta->tags;
361,933✔
367
  info->maxColKVs = sMeta->cols;
361,933✔
368

369
  if (smlIsPKTable(sMeta->tableMeta)) {
361,933✔
370
    return TSDB_CODE_SML_NOT_SUPPORT_PK;
4,389✔
371
  }
372
  return 0;
357,544✔
373
}
374

375
int32_t smlProcessChildTable(SSmlHandle *info, SSmlLineInfo *elements) {
1,915,262✔
376
  int32_t         code = TSDB_CODE_SUCCESS;
1,915,262✔
377
  int32_t         lino = 0;
1,915,262✔
378
  SSmlTableInfo **oneTable =
379
      (SSmlTableInfo **)taosHashGet(info->childTables, elements->measureTag, elements->measureTagsLen);
1,915,262✔
380
  SSmlTableInfo *tinfo = NULL;
1,915,262✔
381
  if (unlikely(oneTable == NULL)) {
1,915,262✔
382
    SML_CHECK_CODE(smlBuildTableInfo(1, elements->measure, elements->measureLen, &tinfo));
1,912,900!
383
    SML_CHECK_CODE(
1,912,900!
384
        taosHashPut(info->childTables, elements->measureTag, elements->measureTagsLen, &tinfo, POINTER_BYTES));
385

386
    tinfo->tags = taosArrayDup(info->preLineTagKV, NULL);
1,913,320✔
387
    SML_CHECK_NULL(tinfo->tags);
1,913,320!
388
    for (size_t i = 0; i < taosArrayGetSize(info->preLineTagKV); i++) {
12,649,839✔
389
      SSmlKv *kv = (SSmlKv *)taosArrayGet(info->preLineTagKV, i);
10,736,939✔
390
      SML_CHECK_NULL(kv);
10,737,359!
391
      if (kv->keyEscaped) kv->key = NULL;
10,737,359!
392
      if (kv->valueEscaped) kv->value = NULL;
10,737,359!
393
    }
394

395
    SML_CHECK_CODE(smlSetCTableName(tinfo, info->tbnameKey));
1,913,320!
396
    SML_CHECK_CODE(getTableUid(info, elements, tinfo));
1,911,220!
397
    if (info->dataFormat) {
1,912,480✔
398
      info->currSTableMeta->uid = tinfo->uid;
349,583✔
399
      SML_CHECK_CODE(smlInitTableDataCtx(info->pQuery, info->currSTableMeta, &tinfo->tableDataCtx));
349,583!
400
    }
401
  } else {
402
    tinfo = *oneTable;
2,362✔
403
  }
404
  if (info->dataFormat) info->currTableDataCtx = tinfo->tableDataCtx;
1,915,262✔
405
  return TSDB_CODE_SUCCESS;
1,914,425✔
406

407
END:
×
408
  smlDestroyTableInfo(&tinfo);
×
409
  RETURN
×
410
}
411

412
int32_t smlParseEndTelnetJsonFormat(SSmlHandle *info, SSmlLineInfo *elements, SSmlKv *kvTs, SSmlKv *kv) {
1,617,738✔
413
  int32_t code = 0;
1,617,738✔
414
  int32_t lino = 0;
1,617,738✔
415
  uDebug("SML:0x%" PRIx64 ", %s format true, ts:%" PRId64, info->id, __FUNCTION__, kvTs->i);
1,617,738✔
416
  SML_CHECK_CODE(
1,617,738!
417
      smlBuildCol(info->currTableDataCtx, info->currSTableMeta->schema, kvTs, 0, info->taos->optionInfo.charsetCxt));
418
  SML_CHECK_CODE(
1,616,952!
419
      smlBuildCol(info->currTableDataCtx, info->currSTableMeta->schema, kv, 1, info->taos->optionInfo.charsetCxt));
420
  SML_CHECK_CODE(smlBuildRow(info->currTableDataCtx));
1,617,738!
421

422
END:
1,617,738✔
423
  clearColValArraySml(info->currTableDataCtx->pValues);
1,617,738✔
424
  RETURN
1,617,738!
425
}
426

427
int32_t smlParseEndTelnetJsonUnFormat(SSmlHandle *info, SSmlLineInfo *elements, SSmlKv *kvTs, SSmlKv *kv) {
786,170✔
428
  int32_t code = 0;
786,170✔
429
  int32_t lino = 0;
786,170✔
430
  uDebug("SML:0x%" PRIx64 ", %s format false, ts:%" PRId64, info->id, __FUNCTION__, kvTs->i);
786,170✔
431
  if (elements->colArray == NULL) {
786,170!
432
    elements->colArray = taosArrayInit(16, sizeof(SSmlKv));
786,956✔
433
    SML_CHECK_NULL(elements->colArray);
786,956!
434
  }
435
  SML_CHECK_NULL(taosArrayPush(elements->colArray, kvTs));
1,573,519!
436
  SML_CHECK_NULL(taosArrayPush(elements->colArray, kv));
1,573,912!
437

438
END:
786,956✔
439
  RETURN
786,956!
440
}
441

442
int32_t smlParseEndLine(SSmlHandle *info, SSmlLineInfo *elements, SSmlKv *kvTs) {
10,317,281✔
443
  if (info->dataFormat) {
10,317,281!
444
    uDebug("SML:0x%" PRIx64 ", %s format true, ts:%" PRId64, info->id, __FUNCTION__, kvTs->i);
×
445
    int32_t ret =
446
        smlBuildCol(info->currTableDataCtx, info->currSTableMeta->schema, kvTs, 0, info->taos->optionInfo.charsetCxt);
×
447
    if (ret == TSDB_CODE_SUCCESS) {
×
448
      ret = smlBuildRow(info->currTableDataCtx);
×
449
    }
450

451
    clearColValArraySml(info->currTableDataCtx->pValues);
×
452
    taosArrayClearP(info->escapedStringList, NULL);
×
453
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
×
454
      uError("SML:0x%" PRIx64 ", %s smlBuildCol error:%d", info->id, __FUNCTION__, ret);
×
455
      return ret;
×
456
    }
457
  } else {
458
    uDebug("SML:0x%" PRIx64 ", %s format false, ts:%" PRId64, info->id, __FUNCTION__, kvTs->i);
10,317,281✔
459
    taosArraySet(elements->colArray, 0, kvTs);
10,317,281✔
460
  }
461
  info->preLine = *elements;
10,299,275✔
462

463
  return TSDB_CODE_SUCCESS;
10,298,435✔
464
}
465

466
static int32_t smlParseTableName(SArray *tags, char *childTableName, char *tbnameKey) {
1,912,060✔
467
  int32_t code = 0;
1,912,060✔
468
  int32_t lino = 0;
1,912,060✔
469
  bool    autoChildName = false;
1,912,060✔
470
  size_t  delimiter = strlen(tsSmlAutoChildTableNameDelimiter);
1,912,060!
471
  if (delimiter > 0 && tbnameKey == NULL) {
1,912,060!
472
    size_t totalNameLen = delimiter * (taosArrayGetSize(tags) - 1);
1,566✔
473
    for (int i = 0; i < taosArrayGetSize(tags); i++) {
4,959✔
474
      SSmlKv *tag = (SSmlKv *)taosArrayGet(tags, i);
2,871✔
475
      SML_CHECK_NULL(tag);
3,393!
476
      totalNameLen += tag->length;
3,393✔
477
    }
478
    if (totalNameLen < TSDB_TABLE_NAME_LEN) {
2,406✔
479
      autoChildName = true;
1,044✔
480
    }
481
  }
482
  if (autoChildName) {
1,912,900✔
483
    childTableName[0] = '\0';
1,044✔
484
    for (int i = 0; i < taosArrayGetSize(tags); i++) {
3,654✔
485
      SSmlKv *tag = (SSmlKv *)taosArrayGet(tags, i);
2,610✔
486
      SML_CHECK_NULL(tag);
2,610!
487
      (void)strncat(childTableName, tag->value, TMIN(tag->length, TSDB_TABLE_NAME_LEN - 1 - strlen(childTableName)));
2,610!
488
      if (i != taosArrayGetSize(tags) - 1) {
2,610✔
489
        (void)strncat(childTableName, tsSmlAutoChildTableNameDelimiter,
1,566!
490
                      TSDB_TABLE_NAME_LEN - 1 - strlen(childTableName));
1,566!
491
      }
492
    }
493
    if (tsSmlDot2Underline) {
1,044!
494
      smlStrReplace(childTableName, strlen(childTableName));
1,044!
495
    }
496
  } else {
497
    if (tbnameKey == NULL) {
1,911,856✔
498
      tbnameKey = tsSmlChildTableName;
1,911,436✔
499
    }
500
    size_t childTableNameLen = strlen(tbnameKey);
1,911,856!
501
    if (childTableNameLen <= 0) return TSDB_CODE_SUCCESS;
1,911,856✔
502

503
    for (int i = 0; i < taosArrayGetSize(tags); i++) {
707,193✔
504
      SSmlKv *tag = (SSmlKv *)taosArrayGet(tags, i);
603,471✔
505
      SML_CHECK_NULL(tag);
603,471!
506
      // handle child table name
507
      if (childTableNameLen == tag->keyLen && strncmp(tag->key, tbnameKey, tag->keyLen) == 0) {
603,471!
508
        tstrncpy(childTableName, tag->value, TMIN(TSDB_TABLE_NAME_LEN, tag->length + 1));
5,676!
509
        if (tsSmlDot2Underline) {
5,676!
510
          smlStrReplace(childTableName, strlen(childTableName));
2,871!
511
        }
512
        taosArrayRemove(tags, i);
5,676✔
513
        break;
5,676✔
514
      }
515
    }
516
  }
517

518
END:
103,722✔
519
  RETURN
110,442!
520
}
521

522
int32_t smlSetCTableName(SSmlTableInfo *oneTable, char *tbnameKey) {
1,912,060✔
523
  int32_t code = 0;
1,912,060✔
524
  int32_t lino = 0;
1,912,060✔
525
  SArray *dst = NULL;
1,912,060✔
526
  SML_CHECK_CODE(smlParseTableName(oneTable->tags, oneTable->childTableName, tbnameKey));
1,912,060!
527

528
  if (strlen(oneTable->childTableName) == 0) {
1,913,320✔
529
    dst = taosArrayDup(oneTable->tags, NULL);
1,906,180✔
530
    SML_CHECK_NULL(dst);
1,906,600!
531
    if (oneTable->sTableNameLen >= TSDB_TABLE_NAME_LEN) {
1,906,600!
532
      SML_CHECK_CODE(TSDB_CODE_SML_INTERNAL_ERROR);
×
533
    }
534
    char          superName[TSDB_TABLE_NAME_LEN] = {0};
1,906,600✔
535
    RandTableName rName = {dst, NULL, (uint8_t)oneTable->sTableNameLen, oneTable->childTableName};
1,905,760✔
536
    if (tsSmlDot2Underline) {
1,905,760✔
537
      (void)memcpy(superName, oneTable->sTableName, oneTable->sTableNameLen);
1,572,026!
538
      smlStrReplace(superName, oneTable->sTableNameLen);
1,572,446✔
539
      rName.stbFullName = superName;
1,572,866✔
540
    } else {
541
      rName.stbFullName = oneTable->sTableName;
333,734✔
542
    }
543

544
    SML_CHECK_CODE(buildChildTableName(&rName));
1,906,600!
545
  }
546

547
END:
6,720✔
548
  taosArrayDestroy(dst);
1,913,320✔
549
  RETURN
1,912,480!
550
}
551

552
int32_t getTableUid(SSmlHandle *info, SSmlLineInfo *currElement, SSmlTableInfo *tinfo) {
1,911,220✔
553
  char   key[TSDB_TABLE_NAME_LEN * 2 + 1] = {0};
1,911,220✔
554
  size_t nLen = strlen(tinfo->childTableName);
1,911,220!
555
  (void)memcpy(key, currElement->measure, currElement->measureLen);
1,912,480!
556
  if (tsSmlDot2Underline) {
1,912,480✔
557
    smlStrReplace(key, currElement->measureLen);
1,575,941✔
558
  }
559
  (void)memcpy(key + currElement->measureLen + 1, tinfo->childTableName, nLen);
1,912,900!
560
  void *uid =
561
      taosHashGet(info->tableUids, key,
1,912,900✔
562
                  currElement->measureLen + 1 + nLen);  // use \0 as separator for stable name and child table name
1,912,900✔
563
  if (uid == NULL) {
1,912,900✔
564
    tinfo->uid = info->uid++;
1,909,018✔
565
    return taosHashPut(info->tableUids, key, currElement->measureLen + 1 + nLen, &tinfo->uid, sizeof(uint64_t));
1,909,438✔
566
  } else {
567
    tinfo->uid = *(uint64_t *)uid;
3,882✔
568
  }
569
  return TSDB_CODE_SUCCESS;
3,882✔
570
}
571

572
int32_t smlBuildSTableMeta(bool isDataFormat, SSmlSTableMeta **sMeta) {
997,846✔
573
  int32_t         code = 0;
997,846✔
574
  int32_t         lino = 0;
997,846✔
575
  SSmlSTableMeta *meta = (SSmlSTableMeta *)taosMemoryCalloc(sizeof(SSmlSTableMeta), 1);
997,846!
576
  SML_CHECK_NULL(meta);
997,846!
577
  if (unlikely(!isDataFormat)) {
997,846✔
578
    meta->tagHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
635,931✔
579
    SML_CHECK_NULL(meta->tagHash);
635,931!
580
    meta->colHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
635,931✔
581
    SML_CHECK_NULL(meta->colHash);
635,931!
582
  }
583

584
  meta->tags = taosArrayInit(32, sizeof(SSmlKv));
997,846✔
585
  SML_CHECK_NULL(meta->tags);
997,846!
586

587
  meta->cols = taosArrayInit(32, sizeof(SSmlKv));
997,846✔
588
  SML_CHECK_NULL(meta->cols);
997,846!
589
  *sMeta = meta;
997,846✔
590
  return TSDB_CODE_SUCCESS;
997,846✔
591

592
END:
×
593
  smlDestroySTableMeta(&meta);
×
594
  uError("%s failed code:%d line:%d", __FUNCTION__, code, lino);
×
595
  return code;
×
596
}
597

598
int32_t smlParseNumber(SSmlKv *kvVal, SSmlMsgBuf *msg) {
332,320,494✔
599
  const char *pVal = kvVal->value;
332,320,494✔
600
  int32_t     len = kvVal->length;
332,435,994✔
601
  char       *endptr = NULL;
332,436,411✔
602
  double      result = taosStr2Double(pVal, &endptr);
332,436,828✔
603
  if (pVal == endptr) {
332,211,481✔
604
    RETURN_FALSE
845✔
605
  }
606

607
  int32_t left = len - (endptr - pVal);
332,253,547✔
608
  if (left == 0) {
332,253,547✔
609
    SET_DOUBLE
160,179✔
610
  } else if (left == 3) {
332,093,368✔
611
    if (endptr[0] == 'f' || endptr[0] == 'F') {
241,282,966✔
612
      if (endptr[1] == '6' && endptr[2] == '4') {
140,441,469!
613
        SET_DOUBLE
90,497,271✔
614
      } else if (endptr[1] == '3' && endptr[2] == '2') {
49,945,035!
615
        SET_FLOAT
50,149,032✔
616
      } else {
617
        RETURN_FALSE
9✔
618
      }
619
    } else if (endptr[0] == 'i' || endptr[0] == 'I') {
100,841,077✔
620
      if (endptr[1] == '6' && endptr[2] == '4') {
9,720,634!
621
        SET_BIGINT
496,063✔
622
      } else if (endptr[1] == '3' && endptr[2] == '2') {
9,224,571!
623
        SET_INT
8,673,005!
624
      } else if (endptr[1] == '1' && endptr[2] == '6') {
551,566!
625
        SET_SMALL_INT
490,718!
626
      } else {
627
        RETURN_FALSE
60,848✔
628
      }
629
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
91,120,443!
630
      if (endptr[1] == '6' && endptr[2] == '4') {
91,120,434!
631
        SET_UBIGINT
475,136✔
632
      } else if (endptr[1] == '3' && endptr[2] == '2') {
90,645,298!
633
        SET_UINT
90,322,649!
634
      } else if (endptr[1] == '1' && endptr[2] == '6') {
322,649!
635
        SET_USMALL_INT
322,649!
636
      } else {
637
        RETURN_FALSE
×
638
      }
639
    } else {
640
      RETURN_FALSE
9✔
641
    }
642
  } else if (left == 2) {
90,810,402✔
643
    if (endptr[0] == 'i' || endptr[0] == 'I') {
90,818,618✔
644
      if (endptr[1] == '8') {
495,924!
645
        SET_TINYINT
495,924!
646
      } else {
647
        RETURN_FALSE
×
648
      }
649
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
90,322,694!
650
      if (endptr[1] == '8') {
90,322,649!
651
        SET_UTINYINT
90,322,649!
652
      } else {
653
        RETURN_FALSE
×
654
      }
655
    } else {
656
      RETURN_FALSE
45✔
657
    }
658
  } else if (left == 1) {
12,726✔
659
    if (endptr[0] == 'i' || endptr[0] == 'I') {
4,156!
660
      SET_BIGINT
4,147✔
661
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
9!
662
      SET_UBIGINT
×
663
    } else {
664
      RETURN_FALSE
9✔
665
    }
666
  } else {
667
    RETURN_FALSE;
8,627✔
668
  }
669
  return true;
332,464,785✔
670
}
671

672
int32_t smlGetMeta(SSmlHandle *info, const void *measure, int32_t measureLen, STableMeta **pTableMeta) {
855,997✔
673
  *pTableMeta = NULL;
855,997✔
674

675
  SName pName = {TSDB_TABLE_NAME_T, info->taos->acctId, {0}, {0}};
855,997✔
676
  tstrncpy(pName.dbname, info->pRequest->pDb, sizeof(pName.dbname));
855,997!
677

678
  SRequestConnInfo conn = {0};
855,997✔
679
  conn.pTrans = info->taos->pAppInfo->pTransporter;
855,997✔
680
  conn.requestId = info->pRequest->requestId;
855,997✔
681
  conn.requestObjRefId = info->pRequest->self;
855,997✔
682
  conn.mgmtEps = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
855,997✔
683
  int32_t len = TMIN(measureLen, TSDB_TABLE_NAME_LEN - 1);
855,997✔
684
  (void)memcpy(pName.tname, measure, measureLen);
855,997!
685
  pName.tname[len] = 0;
855,997✔
686

687
  return catalogGetSTableMeta(info->pCatalog, &conn, &pName, pTableMeta);
855,997✔
688
}
689

690
static int64_t smlGenId() {
922,418✔
691
  static volatile int64_t linesSmlHandleId = 0;
692

693
  int64_t id = 0;
922,418✔
694
  do {
695
    id = atomic_add_fetch_64(&linesSmlHandleId, 1);
922,418✔
696
  } while (id == 0);
922,418!
697

698
  return id;
922,418✔
699
}
700

701
static int32_t smlGenerateSchemaAction(SSchema *colField, SHashObj *colHash, SSmlKv *kv, bool isTag,
15,432,291✔
702
                                       ESchemaAction *action, SSmlHandle *info) {
703
  uint16_t *index = colHash ? (uint16_t *)taosHashGet(colHash, kv->key, kv->keyLen) : NULL;
15,432,291✔
704
  if (index) {
15,431,871✔
705
    if (colField[*index].type != kv->type) {
6,366,160✔
706
      snprintf(info->msgBuf.buf, info->msgBuf.len,
7,572!
707
               "SML:0x%" PRIx64 ", %s point type and db type mismatch, db type:%s, point type:%s, key:%s", info->id,
708
               __FUNCTION__, tDataTypes[colField[*index].type].name, tDataTypes[kv->type].name, kv->key);
5,048✔
709
      uError("%s", info->msgBuf.buf);
2,524!
710
      return TSDB_CODE_SML_INVALID_DATA;
2,524✔
711
    }
712

713
    if (((colField[*index].type == TSDB_DATA_TYPE_VARCHAR || colField[*index].type == TSDB_DATA_TYPE_VARBINARY ||
6,363,636!
714
          colField[*index].type == TSDB_DATA_TYPE_GEOMETRY) &&
3,955,169✔
715
         (colField[*index].bytes - VARSTR_HEADER_SIZE) < kv->length) ||
2,408,983✔
716
        (colField[*index].type == TSDB_DATA_TYPE_NCHAR &&
6,359,620✔
717
         ((colField[*index].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE < kv->length))) {
1,096,502✔
718
      if (isTag) {
36,512✔
719
        *action = SCHEMA_ACTION_CHANGE_TAG_SIZE;
32,496✔
720
      } else {
721
        *action = SCHEMA_ACTION_CHANGE_COLUMN_SIZE;
4,016✔
722
      }
723
    }
724
  } else {
725
    if (isTag) {
9,065,711✔
726
      *action = SCHEMA_ACTION_ADD_TAG;
3,982,529✔
727
    } else {
728
      *action = SCHEMA_ACTION_ADD_COLUMN;
5,083,182✔
729
    }
730
  }
731
  return TSDB_CODE_SUCCESS;
15,429,347✔
732
}
733

734
#define BOUNDARY 1024
735
static int32_t smlFindNearestPowerOf2(int32_t length, uint8_t type) {
2,848,973✔
736
  int32_t result = 1;
2,848,973✔
737
  if (length >= BOUNDARY) {
2,848,973✔
738
    result = length;
12,738✔
739
  } else {
740
    while (result <= length) {
13,585,972✔
741
      result <<= 1;
10,749,737✔
742
    }
743
  }
744

745
  if ((type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_GEOMETRY) &&
2,848,973✔
746
      result > TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE) {
581,447!
747
    result = TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE;
×
748
  } else if (type == TSDB_DATA_TYPE_NCHAR && result > (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
2,848,973!
749
    result = (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
750
  }
751

752
  if (type == TSDB_DATA_TYPE_NCHAR) {
2,848,973✔
753
    result = result * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
2,267,526✔
754
  } else if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_GEOMETRY) {
581,447!
755
    result = result + VARSTR_HEADER_SIZE;
581,447✔
756
  }
757
  return result;
2,848,973✔
758
}
759

760
static int32_t smlProcessSchemaAction(SSmlHandle *info, SSchema *schemaField, SHashObj *schemaHash, SArray *cols,
454,292✔
761
                                      SHashObj *schemaHashCheck, ESchemaAction *action, bool isTag) {
762
  int32_t code = TSDB_CODE_SUCCESS;
454,292✔
763
  int32_t lino = 0;
454,292✔
764
  for (int j = 0; j < taosArrayGetSize(cols); ++j) {
6,894,066✔
765
    if (j == 0 && !isTag) continue;
6,443,962✔
766
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, j);
6,219,398✔
767
    SML_CHECK_NULL(kv);
6,219,364!
768
    SML_CHECK_CODE(smlGenerateSchemaAction(schemaField, schemaHash, kv, isTag, action, info));
6,219,364✔
769
    if (taosHashGet(schemaHashCheck, kv->key, kv->keyLen) != NULL) {
6,216,454✔
770
      uError("SML:0x%" PRIx64 ", %s duplicated column %s", info->id, __FUNCTION__, kv->key);
1,664!
771
      SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
1,664!
772
    }
773
  }
774

775
END:
450,104✔
776
  RETURN
454,292!
777
}
778

779
static int32_t smlCheckMeta(SSchema *schema, int32_t length, SArray *cols) {
441,701✔
780
  int32_t   code = TSDB_CODE_SUCCESS;
441,701✔
781
  int32_t   lino = 0;
441,701✔
782
  SHashObj *hashTmp = taosHashInit(length, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
441,701✔
783
  SML_CHECK_NULL(hashTmp);
441,701!
784
  for (int32_t i = 0; i < length; i++) {
6,861,353✔
785
    SML_CHECK_CODE(taosHashPut(hashTmp, schema[i].name, strlen(schema[i].name), &schema[i], sizeof(SSchema)));
6,419,676!
786
  }
787
  for (int32_t i = 0; i < taosArrayGetSize(cols); i++) {
6,855,464✔
788
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
6,414,214✔
789
    SML_CHECK_NULL(kv);
6,414,214!
790
    SSchema *sTmp = taosHashGet(hashTmp, kv->key, kv->keyLen);
6,414,214✔
791
    if (sTmp == NULL) {
6,414,214!
792
      SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
793
    }
794
    if ((kv->type == TSDB_DATA_TYPE_VARCHAR && kv->length + VARSTR_HEADER_SIZE > sTmp->bytes) ||
6,414,214!
795
        (kv->type == TSDB_DATA_TYPE_NCHAR && kv->length * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE > sTmp->bytes)) {
6,414,214✔
796
      uError("column %s (type %s) bytes invalid. db bytes:%d, kv bytes:%zu", sTmp->name,
427!
797
             tDataTypes[sTmp->type].name, sTmp->bytes, kv->length);
798
      SML_CHECK_CODE(TSDB_CODE_MND_INVALID_SCHEMA_VER);
427!
799
    }
800
  }
801

802
END:
441,274✔
803
  taosHashCleanup(hashTmp);
441,701✔
804
  RETURN
441,701!
805
}
806

807
static int32_t getBytes(uint8_t type, int32_t length) {
9,065,897✔
808
  if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_NCHAR ||
9,065,897✔
809
      type == TSDB_DATA_TYPE_GEOMETRY) {
810
    return smlFindNearestPowerOf2(length, type);
2,848,973✔
811
  } else {
812
    return tDataTypes[type].bytes;
6,216,924✔
813
  }
814
}
815

816
static int32_t smlBuildFieldsList(SSmlHandle *info, SSchema *schemaField, SHashObj *schemaHash, SArray *cols,
908,605✔
817
                                  SArray *results, int32_t numOfCols, bool isTag) {
818
  int32_t code = TSDB_CODE_SUCCESS;
908,605✔
819
  int32_t lino = 0;
908,605✔
820
  for (int j = 0; j < taosArrayGetSize(cols); ++j) {
10,121,498✔
821
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, j);
9,212,893✔
822
    SML_CHECK_NULL(kv);
9,212,893!
823
    ESchemaAction action = SCHEMA_ACTION_NULL;
9,212,893✔
824
    SML_CHECK_CODE(smlGenerateSchemaAction(schemaField, schemaHash, kv, isTag, &action, info));
9,212,893!
825
    if (action == SCHEMA_ACTION_ADD_COLUMN || action == SCHEMA_ACTION_ADD_TAG) {
18,260,534✔
826
      SField field = {0};
9,047,641✔
827
      field.type = kv->type;
9,047,641✔
828
      field.bytes = getBytes(kv->type, kv->length);
9,047,641✔
829
      (void)memcpy(field.name, kv->key, TMIN(kv->keyLen, sizeof(field.name) - 1));
9,047,641!
830
      SML_CHECK_NULL(taosArrayPush(results, &field));
9,047,641!
831
    } else if (action == SCHEMA_ACTION_CHANGE_COLUMN_SIZE || action == SCHEMA_ACTION_CHANGE_TAG_SIZE) {
165,252✔
832
      uint16_t *index = (uint16_t *)taosHashGet(schemaHash, kv->key, kv->keyLen);
18,256✔
833
      if (index == NULL) {
18,256!
834
        SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
835
      }
836
      uint16_t newIndex = *index;
18,256✔
837
      if (isTag) newIndex -= numOfCols;
18,256✔
838
      SField *field = (SField *)taosArrayGet(results, newIndex);
18,256✔
839
      SML_CHECK_NULL(field);
18,256!
840
      field->bytes = getBytes(kv->type, kv->length);
18,256✔
841
    }
842
  }
843

844
  int32_t maxLen = isTag ? TSDB_MAX_TAGS_LEN : TSDB_MAX_BYTES_PER_ROW;
908,605✔
845
  int32_t len = 0;
908,605✔
846
  for (int j = 0; j < taosArrayGetSize(results); ++j) {
10,123,562✔
847
    SField *field = taosArrayGet(results, j);
9,214,957✔
848
    SML_CHECK_NULL(field);
9,214,957!
849
    len += field->bytes;
9,214,957✔
850
  }
851
  if (len > maxLen) {
908,605✔
852
    return isTag ? TSDB_CODE_PAR_INVALID_TAGS_LENGTH : TSDB_CODE_PAR_INVALID_ROW_LENGTH;
4,992✔
853
  }
854

855
END:
903,613✔
856
  RETURN
903,613!
857
}
858

859
static FORCE_INLINE void smlBuildCreateStbReq(SMCreateStbReq *pReq, int32_t colVer, int32_t tagVer, tb_uid_t suid,
860
                                              int8_t source) {
861
  pReq->colVer = colVer;
460,885✔
862
  pReq->tagVer = tagVer;
460,885✔
863
  pReq->suid = suid;
460,885✔
864
  pReq->source = source;
460,885✔
865
}
460,885✔
866
static int32_t smlSendMetaMsg(SSmlHandle *info, SName *pName, SArray *pColumns, SArray **pTags, STableMeta *pTableMeta,
460,885✔
867
                              ESchemaAction action) {
868
  SRequestObj   *pRequest = NULL;
460,885✔
869
  SMCreateStbReq pReq = {0};
460,885✔
870
  int32_t        code = TSDB_CODE_SUCCESS;
460,885✔
871
  int32_t        lino = 0;
460,885✔
872
  SCmdMsgInfo    pCmdMsg = {0};
460,885✔
873
  char          *pSql = NULL;
460,885✔
874

875
  // put front for free
876
  pReq.numOfColumns = taosArrayGetSize(pColumns);
460,885✔
877
  pReq.pTags = *pTags;
460,885✔
878
  pReq.numOfTags = taosArrayGetSize(*pTags);
460,885✔
879
  *pTags = NULL;
460,885✔
880

881
  pReq.pColumns = taosArrayInit(pReq.numOfColumns, sizeof(SFieldWithOptions));
460,885✔
882
  SML_CHECK_NULL(pReq.pColumns);
460,885!
883
  for (int32_t i = 0; i < pReq.numOfColumns; ++i) {
5,647,507✔
884
    SField *pField = taosArrayGet(pColumns, i);
5,186,622✔
885
    SML_CHECK_NULL(pField);
5,186,622!
886
    SFieldWithOptions fieldWithOption = {0};
5,186,622✔
887
    setFieldWithOptions(&fieldWithOption, pField);
5,186,622✔
888
    setDefaultOptionsForField(&fieldWithOption);
5,186,622✔
889
    SML_CHECK_NULL(taosArrayPush(pReq.pColumns, &fieldWithOption));
10,373,244!
890
  }
891

892
  if (action == SCHEMA_ACTION_CREATE_STABLE) {
460,885✔
893
    pSql = "sml_create_stable";
442,212✔
894
    smlBuildCreateStbReq(&pReq, 1, 1, 0, TD_REQ_FROM_APP);
895
  } else if (action == SCHEMA_ACTION_ADD_TAG || action == SCHEMA_ACTION_CHANGE_TAG_SIZE) {
18,673✔
896
    pSql = (action == SCHEMA_ACTION_ADD_TAG) ? "sml_add_tag" : "sml_modify_tag_size";
13,855✔
897
    smlBuildCreateStbReq(&pReq, pTableMeta->sversion, pTableMeta->tversion + 1, pTableMeta->uid, TD_REQ_FROM_SML);
13,855✔
898
  } else if (action == SCHEMA_ACTION_ADD_COLUMN || action == SCHEMA_ACTION_CHANGE_COLUMN_SIZE) {
4,818!
899
    pSql = (action == SCHEMA_ACTION_ADD_COLUMN) ? "sml_add_column" : "sml_modify_column_size";
4,818✔
900
    smlBuildCreateStbReq(&pReq, pTableMeta->sversion + 1, pTableMeta->tversion, pTableMeta->uid, TD_REQ_FROM_SML);
4,818✔
901
  } else {
902
    SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
903
  }
904

905
  SML_CHECK_CODE(buildRequest(info->taos->id, pSql, strlen(pSql), NULL, false, &pRequest, 0));
460,885!
906

907
  pRequest->syncQuery = true;
460,885✔
908
  if (!pRequest->pDb) {
460,885!
909
    SML_CHECK_CODE(TSDB_CODE_PAR_DB_NOT_SPECIFIED);
×
910
  }
911

912
  if (pReq.numOfTags == 0) {
460,885✔
913
    pReq.numOfTags = 1;
516✔
914
    SField field = {0};
516✔
915
    field.type = TSDB_DATA_TYPE_NCHAR;
516✔
916
    field.bytes = TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
516✔
917
    tstrncpy(field.name, tsSmlTagName, sizeof(field.name));
516!
918
    SML_CHECK_NULL(taosArrayPush(pReq.pTags, &field));
1,032!
919
  }
920

921
  pReq.commentLen = -1;
460,885✔
922
  pReq.igExists = true;
460,885✔
923
  SML_CHECK_CODE(tNameExtractFullName(pName, pReq.name));
460,885!
924

925
  pCmdMsg.epSet = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
460,885✔
926
  pCmdMsg.msgType = TDMT_MND_CREATE_STB;
460,885✔
927
  pCmdMsg.msgLen = tSerializeSMCreateStbReq(NULL, 0, &pReq);
460,885✔
928
  if (pCmdMsg.msgLen < 0) {
460,885!
929
    uError("failed to serialize create stable request1, code:%d, terrno:%d", pCmdMsg.msgLen, terrno);
×
930
    SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
931
  }
932
  pCmdMsg.pMsg = taosMemoryMalloc(pCmdMsg.msgLen);
460,885!
933
  SML_CHECK_NULL(pCmdMsg.pMsg);
460,492!
934
  code = tSerializeSMCreateStbReq(pCmdMsg.pMsg, pCmdMsg.msgLen, &pReq);
460,492✔
935
  if (code < 0) {
460,885!
936
    taosMemoryFree(pCmdMsg.pMsg);
×
937
    uError("failed to serialize create stable request2, code:%d, terrno:%d", code, terrno);
×
938
    SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
939
  }
940

941
  SQuery pQuery = {0};
460,885✔
942
  pQuery.execMode = QUERY_EXEC_MODE_RPC;
460,885✔
943
  pQuery.pCmdMsg = &pCmdMsg;
460,885✔
944
  pQuery.msgType = pQuery.pCmdMsg->msgType;
460,885✔
945
  pQuery.stableQuery = true;
460,885✔
946

947
  launchQueryImpl(pRequest, &pQuery, true, NULL);  // no need to check return value
460,885✔
948

949
  if (pRequest->code == TSDB_CODE_SUCCESS) {
460,885✔
950
    SML_CHECK_CODE(catalogRemoveTableMeta(info->pCatalog, pName));
421,223!
951
  }
952
  code = pRequest->code;
460,885✔
953

954
END:
460,885✔
955
  destroyRequest(pRequest);
460,885✔
956
  tFreeSMCreateStbReq(&pReq);
460,885✔
957
  RETURN
460,885!
958
}
959

960
static int32_t smlCreateTable(SSmlHandle *info, SRequestConnInfo *conn, SSmlSTableMeta *sTableData, SName *pName,
443,244✔
961
                              STableMeta **pTableMeta) {
962
  int32_t code = 0;
443,244✔
963
  int32_t lino = 0;
443,244✔
964
  SArray *pColumns = NULL;
443,244✔
965
  SArray *pTags = NULL;
443,244✔
966
  SML_CHECK_CODE(smlCheckAuth(info, conn, NULL, AUTH_TYPE_WRITE));
443,244✔
967
  uDebug("SML:0x%" PRIx64 ", %s create table:%s", info->id, __FUNCTION__, pName->tname);
442,728✔
968
  pColumns = taosArrayInit(taosArrayGetSize(sTableData->cols), sizeof(SField));
442,728✔
969
  SML_CHECK_NULL(pColumns);
442,728!
970
  pTags = taosArrayInit(taosArrayGetSize(sTableData->tags), sizeof(SField));
442,728✔
971
  SML_CHECK_NULL(pTags);
442,728!
972
  SML_CHECK_CODE(smlBuildFieldsList(info, NULL, NULL, sTableData->tags, pTags, 0, true));
442,728!
973
  SML_CHECK_CODE(smlBuildFieldsList(info, NULL, NULL, sTableData->cols, pColumns, 0, false));
442,728✔
974
  SML_CHECK_CODE(smlSendMetaMsg(info, pName, pColumns, &pTags, NULL, SCHEMA_ACTION_CREATE_STABLE));
442,212✔
975
  info->cost.numOfCreateSTables++;
402,550✔
976
  taosMemoryFreeClear(*pTableMeta);
402,550!
977

978
  SML_CHECK_CODE(catalogGetSTableMeta(info->pCatalog, conn, pName, pTableMeta));
402,550!
979

980
END:
441,463✔
981
  taosArrayDestroy(pColumns);
443,244✔
982
  taosArrayDestroy(pTags);
443,244✔
983
  RETURN
443,244!
984
}
985

986
static int32_t smlBuildFields(SArray **pColumns, SArray **pTags, STableMeta *pTableMeta, SSmlSTableMeta *sTableData) {
23,149✔
987
  int32_t code = 0;
23,149✔
988
  int32_t lino = 0;
23,149✔
989
  *pColumns = taosArrayInit(taosArrayGetSize(sTableData->cols) + (pTableMeta)->tableInfo.numOfColumns, sizeof(SField));
23,149✔
990
  SML_CHECK_NULL(pColumns);
23,149!
991
  *pTags = taosArrayInit(taosArrayGetSize(sTableData->tags) + (pTableMeta)->tableInfo.numOfTags, sizeof(SField));
23,149✔
992
  SML_CHECK_NULL(pTags);
23,149!
993
  for (uint16_t i = 0; i < (pTableMeta)->tableInfo.numOfColumns + (pTableMeta)->tableInfo.numOfTags; i++) {
311,231✔
994
    SField field = {0};
288,082✔
995
    field.type = (pTableMeta)->schema[i].type;
288,082✔
996
    field.bytes = (pTableMeta)->schema[i].bytes;
288,082✔
997
    tstrncpy(field.name, (pTableMeta)->schema[i].name, sizeof(field.name));
288,082!
998
    if (i < (pTableMeta)->tableInfo.numOfColumns) {
288,082✔
999
      SML_CHECK_NULL(taosArrayPush(*pColumns, &field));
256,676!
1000
    } else {
1001
      SML_CHECK_NULL(taosArrayPush(*pTags, &field));
319,488!
1002
    }
1003
  }
1004
END:
23,149✔
1005
  RETURN
23,149!
1006
}
1007
static int32_t smlModifyTag(SSmlHandle *info, SHashObj *hashTmpCheck, SHashObj *hashTmp, SRequestConnInfo *conn,
229,728✔
1008
                            SSmlSTableMeta *sTableData, SName *pName, STableMeta **pTableMeta) {
1009
  ESchemaAction action = SCHEMA_ACTION_NULL;
229,728✔
1010
  SArray       *pColumns = NULL;
229,728✔
1011
  SArray       *pTags = NULL;
229,728✔
1012
  int32_t       code = 0;
229,728✔
1013
  int32_t       lino = 0;
229,728✔
1014
  SML_CHECK_CODE(
229,728✔
1015
      smlProcessSchemaAction(info, (*pTableMeta)->schema, hashTmp, sTableData->tags, hashTmpCheck, &action, true));
1016

1017
  if (action != SCHEMA_ACTION_NULL) {
227,548✔
1018
    SML_CHECK_CODE(smlCheckAuth(info, conn, pName->tname, AUTH_TYPE_WRITE));
16,839!
1019
    uDebug("SML:0x%" PRIx64 ", %s change table tag, table:%s, action:%d", info->id, __FUNCTION__, pName->tname, action);
16,839✔
1020
    SML_CHECK_CODE(smlBuildFields(&pColumns, &pTags, *pTableMeta, sTableData));
16,839!
1021
    SML_CHECK_CODE(smlBuildFieldsList(info, (*pTableMeta)->schema, hashTmp, sTableData->tags, pTags,
16,839✔
1022
                                      (*pTableMeta)->tableInfo.numOfColumns, true));
1023

1024
    SML_CHECK_CODE(smlSendMetaMsg(info, pName, pColumns, &pTags, (*pTableMeta), action));
13,855!
1025

1026
    info->cost.numOfAlterTagSTables++;
13,855✔
1027
    taosMemoryFreeClear(*pTableMeta);
13,855!
1028
    SML_CHECK_CODE(catalogRefreshTableMeta(info->pCatalog, conn, pName, -1));
13,855!
1029
    SML_CHECK_CODE(catalogGetSTableMeta(info->pCatalog, conn, pName, pTableMeta));
13,855!
1030
  }
1031

1032
END:
229,728✔
1033
  taosArrayDestroy(pColumns);
229,728✔
1034
  taosArrayDestroy(pTags);
229,728✔
1035
  RETURN
229,728!
1036
}
1037

1038
static int32_t smlModifyCols(SSmlHandle *info, SHashObj *hashTmpCheck, SHashObj *hashTmp, SRequestConnInfo *conn,
224,564✔
1039
                             SSmlSTableMeta *sTableData, SName *pName, STableMeta **pTableMeta) {
1040
  ESchemaAction action = SCHEMA_ACTION_NULL;
224,564✔
1041
  SArray       *pColumns = NULL;
224,564✔
1042
  SArray       *pTags = NULL;
224,564✔
1043
  int32_t       code = 0;
224,564✔
1044
  int32_t       lino = 0;
224,564✔
1045
  SML_CHECK_CODE(
224,564✔
1046
      smlProcessSchemaAction(info, (*pTableMeta)->schema, hashTmp, sTableData->cols, hashTmpCheck, &action, false));
1047

1048
  if (action != SCHEMA_ACTION_NULL) {
222,556✔
1049
    SML_CHECK_CODE(smlCheckAuth(info, conn, pName->tname, AUTH_TYPE_WRITE));
6,310!
1050
    uDebug("SML:0x%" PRIx64 ", %s change table col, table:%s, action:%d", info->id, __FUNCTION__, pName->tname, action);
6,310!
1051
    SML_CHECK_CODE(smlBuildFields(&pColumns, &pTags, *pTableMeta, sTableData));
6,310!
1052
    SML_CHECK_CODE(smlBuildFieldsList(info, (*pTableMeta)->schema, hashTmp, sTableData->cols, pColumns,
6,310✔
1053
                                      (*pTableMeta)->tableInfo.numOfColumns, false));
1054

1055
    SML_CHECK_CODE(smlSendMetaMsg(info, pName, pColumns, &pTags, (*pTableMeta), action));
4,818!
1056

1057
    info->cost.numOfAlterColSTables++;
4,818✔
1058
    taosMemoryFreeClear(*pTableMeta);
4,818!
1059
    SML_CHECK_CODE(catalogRefreshTableMeta(info->pCatalog, conn, pName, -1));
4,818!
1060
    SML_CHECK_CODE(catalogGetSTableMeta(info->pCatalog, conn, pName, pTableMeta));
4,818!
1061
  }
1062

1063
END:
224,564✔
1064
  taosArrayDestroy(pColumns);
224,564✔
1065
  taosArrayDestroy(pTags);
224,564✔
1066
  RETURN
224,564!
1067
}
1068

1069
static int32_t smlBuildTempHash(SHashObj *hashTmp, STableMeta *pTableMeta, uint16_t start, uint16_t end) {
459,456✔
1070
  int32_t code = 0;
459,456✔
1071
  int32_t lino = 0;
459,456✔
1072
  for (uint16_t i = start; i < end; i++) {
6,908,491✔
1073
    SML_CHECK_CODE(
6,449,035!
1074
        taosHashPut(hashTmp, pTableMeta->schema[i].name, strlen(pTableMeta->schema[i].name), &i, SHORT_BYTES));
1075
  }
1076

1077
END:
459,456✔
1078
  return code;
459,456✔
1079
}
1080

1081
static int32_t smlModifyDBSchemas(SSmlHandle *info) {
832,722✔
1082
  uDebug("SML:0x%" PRIx64 ", %s start, format:%d, needModifySchema:%d", info->id, __FUNCTION__, info->dataFormat,
832,722!
1083
         info->needModifySchema);
1084
  if (info->dataFormat && !info->needModifySchema) {
832,722!
1085
    return TSDB_CODE_SUCCESS;
166,438✔
1086
  }
1087
  int32_t     code = 0;
666,284✔
1088
  int32_t     lino = 0;
666,284✔
1089
  SHashObj   *colHashTmp = NULL;
666,284✔
1090
  SHashObj   *tagHashTmp = NULL;
666,284✔
1091
  STableMeta *pTableMeta = NULL;
666,284✔
1092

1093
  SName pName = {TSDB_TABLE_NAME_T, info->taos->acctId, {0}, {0}};
666,284✔
1094
  tstrncpy(pName.dbname, info->pRequest->pDb, sizeof(pName.dbname));
666,284!
1095

1096
  SRequestConnInfo conn = {0};
666,284✔
1097
  conn.pTrans = info->taos->pAppInfo->pTransporter;
666,284✔
1098
  conn.requestId = info->pRequest->requestId;
666,284✔
1099
  conn.requestObjRefId = info->pRequest->self;
666,284✔
1100
  conn.mgmtEps = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
666,284✔
1101

1102
  SSmlSTableMeta **tmp = (SSmlSTableMeta **)taosHashIterate(info->superTables, NULL);
666,284✔
1103
  while (tmp) {
1,289,471✔
1104
    SSmlSTableMeta *sTableData = *tmp;
672,972✔
1105
    bool            needCheckMeta = false;  // for multi thread
672,972✔
1106

1107
    size_t superTableLen = 0;
672,972✔
1108
    void  *superTable = taosHashGetKey(tmp, &superTableLen);
672,972✔
1109
    char  *measure = taosMemoryMalloc(superTableLen);
672,972!
1110
    SML_CHECK_NULL(measure);
674,787!
1111
    (void)memcpy(measure, superTable, superTableLen);
672,972!
1112
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
672,972✔
1113
      PROCESS_SLASH_IN_MEASUREMENT(measure, superTableLen);
2,472,242!
1114
    }
1115
    smlStrReplace(measure, superTableLen);
672,972✔
1116
    size_t nameLen = TMIN(superTableLen, TSDB_TABLE_NAME_LEN - 1);
672,972✔
1117
    (void)memcpy(pName.tname, measure, nameLen);
672,972!
1118
    pName.tname[nameLen] = '\0';
672,972✔
1119
    taosMemoryFree(measure);
672,972!
1120

1121
    code = catalogGetSTableMeta(info->pCatalog, &conn, &pName, &pTableMeta);
672,972✔
1122

1123
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_MND_STB_NOT_EXIST) {
672,972!
1124
      SML_CHECK_CODE(smlCreateTable(info, &conn, sTableData, &pName, &pTableMeta));
443,244✔
1125
    } else if (code == TSDB_CODE_SUCCESS) {
229,728!
1126
      if (smlIsPKTable(pTableMeta)) {
229,728!
1127
        SML_CHECK_CODE(TSDB_CODE_SML_NOT_SUPPORT_PK);
×
1128
      }
1129

1130
      colHashTmp = taosHashInit(pTableMeta->tableInfo.numOfColumns, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY),
229,728✔
1131
                                true, HASH_NO_LOCK);
1132
      tagHashTmp = taosHashInit(pTableMeta->tableInfo.numOfTags, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY),
229,728✔
1133
                                true, HASH_NO_LOCK);
1134
      SML_CHECK_NULL(colHashTmp);
229,728!
1135
      SML_CHECK_NULL(tagHashTmp);
229,728!
1136
      SML_CHECK_CODE(smlBuildTempHash(tagHashTmp, pTableMeta, pTableMeta->tableInfo.numOfColumns,
229,728!
1137
                                      pTableMeta->tableInfo.numOfColumns + pTableMeta->tableInfo.numOfTags));
1138
      SML_CHECK_CODE(smlBuildTempHash(colHashTmp, pTableMeta, 0, pTableMeta->tableInfo.numOfColumns));
229,728!
1139

1140
      SML_CHECK_CODE(smlModifyTag(info, colHashTmp, tagHashTmp, &conn, sTableData, &pName, &pTableMeta));
229,728✔
1141
      SML_CHECK_CODE(smlModifyCols(info, tagHashTmp, colHashTmp, &conn, sTableData, &pName, &pTableMeta));
224,564✔
1142

1143
      needCheckMeta = true;
221,064✔
1144
      taosHashCleanup(colHashTmp);
221,064✔
1145
      taosHashCleanup(tagHashTmp);
221,064✔
1146
      colHashTmp = NULL;
221,064✔
1147
      tagHashTmp = NULL;
221,064✔
1148
    } else {
1149
      uError("SML:0x%" PRIx64 ", %s load table meta error:%s", info->id, __FUNCTION__, tstrerror(code));
×
1150
      goto END;
×
1151
    }
1152

1153
    if (needCheckMeta) {
623,614✔
1154
      SML_CHECK_CODE(smlCheckMeta(&(pTableMeta->schema[pTableMeta->tableInfo.numOfColumns]),
221,064✔
1155
                                  pTableMeta->tableInfo.numOfTags, sTableData->tags));
1156
      SML_CHECK_CODE(smlCheckMeta(&(pTableMeta->schema[0]), pTableMeta->tableInfo.numOfColumns, sTableData->cols));
220,637!
1157
    }
1158

1159
    taosMemoryFreeClear(sTableData->tableMeta);
623,187!
1160
    sTableData->tableMeta = pTableMeta;
623,187✔
1161
    uDebug("SML:0x%" PRIx64 ", %s modify schema uid:%" PRIu64 ", sversion:%d, tversion:%d", info->id, __FUNCTION__,
623,187✔
1162
           pTableMeta->uid, pTableMeta->sversion, pTableMeta->tversion);
1163
    tmp = (SSmlSTableMeta **)taosHashIterate(info->superTables, tmp);
623,187✔
1164
  }
1165
  uDebug("SML:0x%" PRIx64 ", %s end success, format:%d, needModifySchema:%d", info->id, __FUNCTION__, info->dataFormat,
616,499!
1166
         info->needModifySchema);
1167

1168
  return TSDB_CODE_SUCCESS;
616,499✔
1169

1170
END:
49,785✔
1171
  taosHashCancelIterate(info->superTables, tmp);
49,785✔
1172
  taosHashCleanup(colHashTmp);
49,785✔
1173
  taosHashCleanup(tagHashTmp);
49,785✔
1174
  taosMemoryFreeClear(pTableMeta);
49,785!
1175
  (void)catalogRefreshTableMeta(info->pCatalog, &conn, &pName, 1);  // ignore refresh meta code if there is an error
49,785✔
1176
  uError("SML:0x%" PRIx64 ", %s end failed:%d:%s, format:%d, needModifySchema:%d", info->id, __FUNCTION__, code,
49,785!
1177
         tstrerror(code), info->dataFormat, info->needModifySchema);
1178

1179
  return code;
49,785✔
1180
}
1181

1182
static int32_t smlInsertMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, SHashObj *checkDuplicate) {
1,267,098✔
1183
  int32_t code = 0;
1,267,098✔
1184
  int32_t lino = 0;
1,267,098✔
1185
  terrno = 0;
1,267,098✔
1186
  for (int16_t i = 0; i < taosArrayGetSize(cols); ++i) {
16,099,484✔
1187
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
14,839,330✔
1188
    SML_CHECK_NULL(kv);
14,839,330!
1189
    int ret = taosHashPut(metaHash, kv->key, kv->keyLen, &i, SHORT_BYTES);
14,839,330✔
1190
    if (ret == 0) {
14,839,747✔
1191
      SML_CHECK_NULL(taosArrayPush(metaArray, kv));
14,834,983!
1192
      if (taosHashGet(checkDuplicate, kv->key, kv->keyLen) != NULL) {
14,834,983✔
1193
        uError("%s duplicated column %s", __FUNCTION__, kv->key);
2,180!
1194
        SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
1,763!
1195
      }
1196
    } else if (terrno == TSDB_CODE_DUP_KEY) {
4,764!
1197
      uError("%s duplicated column %s", __FUNCTION__, kv->key);
4,764!
1198
      return TSDB_CODE_PAR_DUPLICATED_COLUMN;
4,764✔
1199
    }
1200
  }
1201

1202
END:
1,262,334✔
1203
  RETURN
1,262,334!
1204
}
1205

1206
static int32_t smlUpdateMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, bool isTag, SSmlMsgBuf *msg,
21,032,155✔
1207
                             SHashObj *checkDuplicate) {
1208
  int32_t code = 0;
21,032,155✔
1209
  int32_t lino = 0;
21,032,155✔
1210
  for (int i = 0; i < taosArrayGetSize(cols); ++i) {
151,254,181✔
1211
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
129,287,810✔
1212
    SML_CHECK_NULL(kv);
128,352,265!
1213
    int16_t *index = (int16_t *)taosHashGet(metaHash, kv->key, kv->keyLen);
128,352,265✔
1214
    if (index) {
129,895,529✔
1215
      SSmlKv *value = (SSmlKv *)taosArrayGet(metaArray, *index);
129,896,767✔
1216
      SML_CHECK_NULL(value);
129,599,480!
1217

1218
      if (isTag) {
130,208,036✔
1219
        if (kv->length > value->length) {
28,725,118✔
1220
          value->length = kv->length;
95,263✔
1221
        }
1222
        continue;
28,725,118✔
1223
      }
1224
      if (kv->type != value->type) {
101,482,918!
1225
        smlBuildInvalidDataMsg(msg, "the type is not the same like before", kv->key);
×
1226
        SML_CHECK_CODE(TSDB_CODE_SML_NOT_SAME_TYPE);
×
1227
      }
1228

1229
      if (IS_VAR_DATA_TYPE(kv->type) && (kv->length > value->length)) {  // update string len, if bigger
101,465,404!
1230
        value->length = kv->length;
2,983,757✔
1231
      }
1232
    } else {
1233
      size_t tmp = taosArrayGetSize(metaArray);
13,453✔
1234
      if (tmp > INT16_MAX) {
13,453!
1235
        smlBuildInvalidDataMsg(msg, "too many cols or tags", kv->key);
×
1236
        SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
1237
      }
1238
      int16_t size = tmp;
13,453✔
1239
      SML_CHECK_CODE(taosHashPut(metaHash, kv->key, kv->keyLen, &size, SHORT_BYTES));
13,453!
1240
      SML_CHECK_NULL(taosArrayPush(metaArray, kv));
13,453!
1241
      if (taosHashGet(checkDuplicate, kv->key, kv->keyLen) != NULL) {
13,453!
1242
        uError("%s duplicated column %s", __FUNCTION__, kv->key);
×
1243
        SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
×
1244
      }
1245
    }
1246
  }
1247

1248
END:
18,931,424✔
1249
  RETURN
18,934,247!
1250
}
1251

1252
void smlDestroyTableInfo(void *para) {
1,913,320✔
1253
  SSmlTableInfo *tag = *(SSmlTableInfo **)para;
1,913,320✔
1254
  for (size_t i = 0; i < taosArrayGetSize(tag->cols); i++) {
13,138,991✔
1255
    SHashObj *kvHash = (SHashObj *)taosArrayGetP(tag->cols, i);
11,215,047✔
1256
    taosHashCleanup(kvHash);
11,202,763✔
1257
  }
1258

1259
  taosArrayDestroy(tag->cols);
1,914,150✔
1260
  taosArrayDestroyEx(tag->tags, freeSSmlKv);
1,912,483✔
1261
  taosMemoryFree(tag);
1,912,903!
1262
}
1,912,903✔
1263

1264
void freeSSmlKv(void *data) {
136,925,852✔
1265
  SSmlKv *kv = (SSmlKv *)data;
136,925,852✔
1266
  if (kv->keyEscaped) taosMemoryFreeClear(kv->key);
136,925,852!
1267
  if (kv->valueEscaped) taosMemoryFreeClear(kv->value);
136,925,852!
1268
#ifdef USE_GEOS
1269
  if (kv->type == TSDB_DATA_TYPE_GEOMETRY) geosFreeBuffer((void *)(kv->value));
136,921,253✔
1270
#endif
1271
  if (kv->type == TSDB_DATA_TYPE_VARBINARY) taosMemoryFreeClear(kv->value);
136,923,755!
1272
}
136,923,335✔
1273

1274
void smlDestroyInfo(SSmlHandle *info) {
1,844,836✔
1275
  if (info == NULL) return;
1,844,836✔
1276

1277
  taosHashCleanup(info->pVgHash);
922,418✔
1278
  taosHashCleanup(info->childTables);
922,418✔
1279
  taosHashCleanup(info->superTables);
922,418✔
1280
  taosHashCleanup(info->tableUids);
922,418✔
1281

1282
  for (int i = 0; i < taosArrayGetSize(info->tagJsonArray); i++) {
922,418!
1283
    cJSON *tags = (cJSON *)taosArrayGetP(info->tagJsonArray, i);
×
1284
    cJSON_Delete(tags);
×
1285
  }
1286
  taosArrayDestroy(info->tagJsonArray);
922,418✔
1287

1288
  for (int i = 0; i < taosArrayGetSize(info->valueJsonArray); i++) {
922,418!
1289
    cJSON *value = (cJSON *)taosArrayGetP(info->valueJsonArray, i);
×
1290
    cJSON_Delete(value);
×
1291
  }
1292
  taosArrayDestroy(info->valueJsonArray);
922,418✔
1293

1294
  taosArrayDestroyEx(info->preLineTagKV, freeSSmlKv);
922,418✔
1295
  taosArrayDestroyP(info->escapedStringList, NULL);
922,418✔
1296

1297
  if (!info->dataFormat) {
922,418✔
1298
    for (int i = 0; i < info->lineNum; i++) {
11,998,404✔
1299
      taosArrayDestroyEx(info->lines[i].colArray, freeSSmlKv);
11,319,022✔
1300
      if (info->lines[i].measureTagsLen != 0 && info->protocol != TSDB_SML_LINE_PROTOCOL) {
11,319,356✔
1301
        taosMemoryFree(info->lines[i].measureTag);
341,619!
1302
      }
1303
    }
1304
    taosMemoryFree(info->lines);
679,382!
1305
  }
1306
  if (info->protocol == TSDB_SML_JSON_PROTOCOL) {
922,418✔
1307
    taosMemoryFreeClear(info->preLine.tags);
360,945!
1308
  }
1309
  cJSON_Delete(info->root);
922,418✔
1310
  taosMemoryFreeClear(info);
922,418!
1311
}
1312

1313
int32_t smlBuildSmlInfo(TAOS *taos, SSmlHandle **handle) {
922,418✔
1314
  int32_t     code = TSDB_CODE_SUCCESS;
922,418✔
1315
  int32_t     lino = 0;
922,418✔
1316
  SSmlHandle *info = (SSmlHandle *)taosMemoryCalloc(1, sizeof(SSmlHandle));
922,418!
1317
  SML_CHECK_NULL(info);
922,418!
1318
  if (taos != NULL) {
922,418✔
1319
    info->taos = acquireTscObj(*(int64_t *)taos);
922,373✔
1320
    SML_CHECK_NULL(info->taos);
922,373!
1321
    SML_CHECK_CODE(catalogGetHandle(info->taos->pAppInfo->clusterId, &info->pCatalog));
922,373!
1322
  }
1323

1324
  info->pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
922,418✔
1325
  SML_CHECK_NULL(info->pVgHash);
922,418!
1326
  info->childTables = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
922,418✔
1327
  SML_CHECK_NULL(info->childTables);
922,418!
1328
  info->tableUids = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
922,418✔
1329
  SML_CHECK_NULL(info->tableUids);
922,418!
1330
  info->superTables = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
922,418✔
1331
  SML_CHECK_NULL(info->superTables);
922,418!
1332
  taosHashSetFreeFp(info->superTables, smlDestroySTableMeta);
922,418✔
1333
  taosHashSetFreeFp(info->childTables, smlDestroyTableInfo);
922,418✔
1334

1335
  info->id = smlGenId();
922,418✔
1336
  SML_CHECK_CODE(smlInitHandle(&info->pQuery));
922,418!
1337
  info->dataFormat = true;
922,418✔
1338
  info->tagJsonArray = taosArrayInit(8, POINTER_BYTES);
922,418✔
1339
  SML_CHECK_NULL(info->tagJsonArray);
922,418!
1340
  info->valueJsonArray = taosArrayInit(8, POINTER_BYTES);
922,418✔
1341
  SML_CHECK_NULL(info->valueJsonArray);
922,418!
1342
  info->preLineTagKV = taosArrayInit(8, sizeof(SSmlKv));
922,418✔
1343
  SML_CHECK_NULL(info->preLineTagKV);
922,418!
1344
  info->escapedStringList = taosArrayInit(8, POINTER_BYTES);
922,418✔
1345
  SML_CHECK_NULL(info->escapedStringList);
922,418!
1346

1347
  *handle = info;
922,418✔
1348
  info = NULL;
922,418✔
1349

1350
END:
922,418✔
1351
  smlDestroyInfo(info);
922,418✔
1352
  RETURN
922,418!
1353
}
1354

1355
static int32_t smlPushCols(SArray *colsArray, SArray *cols) {
11,236,435✔
1356
  int32_t   code = TSDB_CODE_SUCCESS;
11,236,435✔
1357
  int32_t   lino = 0;
11,236,435✔
1358
  SHashObj *kvHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
11,236,435✔
1359
  SML_CHECK_NULL(kvHash);
11,248,697!
1360
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
123,353,383✔
1361
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
111,650,990✔
1362
    SML_CHECK_NULL(kv);
111,618,758!
1363
    terrno = 0;
111,618,758✔
1364
    code = taosHashPut(kvHash, kv->key, kv->keyLen, &kv, POINTER_BYTES);
109,751,485✔
1365
    if (terrno == TSDB_CODE_DUP_KEY) {
112,155,483✔
1366
      uError("%s duplicated column %s", __FUNCTION__, kv->key);
1,778!
1367
      SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
7,396✔
1368
    }
1369
    SML_CHECK_CODE(code);
112,098,827!
1370
  }
1371

1372
  SML_CHECK_NULL(taosArrayPush(colsArray, &kvHash));
11,243,848!
1373
  return code;
11,243,848✔
1374
END:
326✔
1375
  taosHashCleanup(kvHash);
1,778✔
1376
  RETURN
1,778!
1377
}
1378

1379
static int32_t smlParseEnd(SSmlHandle *info) {
805,512✔
1380
  uDebug("SML:0x%" PRIx64 ", %s start, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat, info->lineNum);
805,512!
1381
  int32_t code = 0;
805,512✔
1382
  int32_t lino = 0;
805,512✔
1383
  if (info->dataFormat) return TSDB_CODE_SUCCESS;
805,512✔
1384

1385
  for (int32_t i = 0; i < info->lineNum; i++) {
11,846,506✔
1386
    SSmlLineInfo  *elements = info->lines + i;
11,223,128✔
1387
    SSmlTableInfo *tinfo = NULL;
11,223,128✔
1388
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
11,223,128✔
1389
      SSmlTableInfo **tmp =
1390
          (SSmlTableInfo **)taosHashGet(info->childTables, elements->measure, elements->measureTagsLen);
10,436,181✔
1391
      if (tmp) tinfo = *tmp;
10,444,315!
1392
    } else {
1393
      SSmlTableInfo **tmp = (SSmlTableInfo **)taosHashGet(info->childTables, elements->measureTag,
786,947✔
1394
                                                          elements->measureLen + elements->tagsLen);
786,947✔
1395
      if (tmp) tinfo = *tmp;
786,947!
1396
    }
1397

1398
    if (tinfo == NULL) {
11,231,262!
1399
      uError("SML:0x%" PRIx64 ", get oneTable failed, line num:%d", info->id, i);
×
1400
      smlBuildInvalidDataMsg(&info->msgBuf, "get oneTable failed", elements->measure);
×
1401
      return TSDB_CODE_SML_INVALID_DATA;
×
1402
    }
1403

1404
    if (taosArrayGetSize(tinfo->tags) > TSDB_MAX_TAGS) {
11,231,262✔
1405
      smlBuildInvalidDataMsg(&info->msgBuf, "too many tags than 128", NULL);
2,984✔
1406
      return TSDB_CODE_PAR_INVALID_TAGS_NUM;
2,984✔
1407
    }
1408

1409
    if (taosArrayGetSize(elements->colArray) + taosArrayGetSize(tinfo->tags) > TSDB_MAX_COLUMNS) {
11,236,351✔
1410
      smlBuildInvalidDataMsg(&info->msgBuf, "too many columns than 4096", NULL);
746✔
1411
      return TSDB_CODE_PAR_TOO_MANY_COLUMNS;
746✔
1412
    }
1413

1414
    SML_CHECK_CODE(smlPushCols(tinfo->cols, elements->colArray));
11,238,405✔
1415

1416
    SSmlSTableMeta **tableMeta =
1417
        (SSmlSTableMeta **)taosHashGet(info->superTables, elements->measure, elements->measureLen);
11,240,777✔
1418
    if (tableMeta) {  // update meta
11,230,983✔
1419
      uDebug("SML:0x%" PRIx64 ", %s update meta, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
10,595,301!
1420
             info->lineNum);
1421
      SML_CHECK_CODE(smlUpdateMeta((*tableMeta)->colHash, (*tableMeta)->cols, elements->colArray, false, &info->msgBuf,
10,595,301!
1422
                                   (*tableMeta)->tagHash));
1423
      SML_CHECK_CODE(smlUpdateMeta((*tableMeta)->tagHash, (*tableMeta)->tags, tinfo->tags, true, &info->msgBuf,
10,582,685!
1424
                                   (*tableMeta)->colHash));
1425
    } else {
1426
      uDebug("SML:0x%" PRIx64 ", %s add meta, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
635,682!
1427
             info->lineNum);
1428
      SSmlSTableMeta *meta = NULL;
635,682✔
1429
      SML_CHECK_CODE(smlBuildSTableMeta(info->dataFormat, &meta));
635,682!
1430
      code = taosHashPut(info->superTables, elements->measure, elements->measureLen, &meta, POINTER_BYTES);
635,931✔
1431
      if (code != TSDB_CODE_SUCCESS) {
635,931!
1432
        smlDestroySTableMeta(&meta);
×
1433
        SML_CHECK_CODE(code);
×
1434
      }
1435
      SML_CHECK_CODE(smlInsertMeta(meta->tagHash, meta->tags, tinfo->tags, NULL));
635,931✔
1436
      SML_CHECK_CODE(smlInsertMeta(meta->colHash, meta->cols, elements->colArray, meta->tagHash));
631,167✔
1437
    }
1438
  }
1439
  uDebug("SML:0x%" PRIx64 ", %s end, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat, info->lineNum);
623,378!
1440

1441
END:
205,265✔
1442
  RETURN
632,100!
1443
}
1444

1445
static int32_t smlInsertData(SSmlHandle *info) {
782,937✔
1446
  int32_t         code = TSDB_CODE_SUCCESS;
782,937✔
1447
  int32_t         lino = 0;
782,937✔
1448
  char           *measure = NULL;
782,937✔
1449
  SSmlTableInfo **oneTable = NULL;
782,937✔
1450
  uDebug("SML:0x%" PRIx64 ", %s start, format:%d", info->id, __FUNCTION__, info->dataFormat);
782,937!
1451

1452
  if (info->pRequest->dbList == NULL) {
782,937!
1453
    info->pRequest->dbList = taosArrayInit(1, TSDB_DB_FNAME_LEN);
782,937✔
1454
    SML_CHECK_NULL(info->pRequest->dbList);
782,937!
1455
  }
1456
  char *data = (char *)taosArrayReserve(info->pRequest->dbList, 1);
782,937✔
1457
  SML_CHECK_NULL(data);
782,937!
1458
  SName pName = {TSDB_TABLE_NAME_T, info->taos->acctId, {0}, {0}};
782,937✔
1459
  tstrncpy(pName.dbname, info->pRequest->pDb, sizeof(pName.dbname));
782,937!
1460
  (void)tNameGetFullDbName(&pName, data);  // ignore
782,937✔
1461

1462
  oneTable = (SSmlTableInfo **)taosHashIterate(info->childTables, NULL);
782,937✔
1463
  while (oneTable) {
2,471,936✔
1464
    SSmlTableInfo *tableData = *oneTable;
1,690,031✔
1465

1466
    int measureLen = tableData->sTableNameLen;
1,690,031✔
1467
    measure = (char *)taosMemoryMalloc(tableData->sTableNameLen);
1,691,264!
1468
    SML_CHECK_NULL(measure);
1,690,844!
1469
    (void)memcpy(measure, tableData->sTableName, tableData->sTableNameLen);
1,690,844!
1470
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
1,690,424✔
1471
      PROCESS_SLASH_IN_MEASUREMENT(measure, measureLen);
11,437,286!
1472
    }
1473
    smlStrReplace(measure, measureLen);
1,691,264✔
1474
    (void)memcpy(pName.tname, measure, measureLen);
1,690,871!
1475
    pName.tname[measureLen] = '\0';
1,690,871✔
1476

1477
    if (info->pRequest->tableList == NULL) {
1,690,871✔
1478
      info->pRequest->tableList = taosArrayInit(1, sizeof(SName));
782,937✔
1479
      SML_CHECK_NULL(info->pRequest->tableList);
782,544!
1480
    }
1481
    SML_CHECK_NULL(taosArrayPush(info->pRequest->tableList, &pName));
3,382,135!
1482
    tstrncpy(pName.tname, tableData->childTableName, sizeof(pName.tname));
1,690,871!
1483

1484
    SRequestConnInfo conn = {0};
1,690,871✔
1485
    conn.pTrans = info->taos->pAppInfo->pTransporter;
1,690,844✔
1486
    conn.requestId = info->pRequest->requestId;
1,690,871✔
1487
    conn.requestObjRefId = info->pRequest->self;
1,690,871✔
1488
    conn.mgmtEps = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
1,690,451✔
1489

1490
    SML_CHECK_CODE(smlCheckAuth(info, &conn, pName.tname, AUTH_TYPE_WRITE));
1,691,264✔
1491

1492
    SVgroupInfo vg = {0};
1,685,639✔
1493
    SML_CHECK_CODE(catalogGetTableHashVgroup(info->pCatalog, &conn, &pName, &vg));
1,687,712!
1494
    SML_CHECK_CODE(taosHashPut(info->pVgHash, (const char *)&vg.vgId, sizeof(vg.vgId), (char *)&vg, sizeof(vg)));
1,689,839!
1495

1496
    SSmlSTableMeta **pMeta =
1497
        (SSmlSTableMeta **)taosHashGet(info->superTables, tableData->sTableName, tableData->sTableNameLen);
1,689,392✔
1498
    if (unlikely(NULL == pMeta || NULL == *pMeta || NULL == (*pMeta)->tableMeta)) {
1,690,232!
1499
      uError("SML:0x%" PRIx64 ", %s NULL == pMeta. table name:%s", info->id, __FUNCTION__, tableData->childTableName);
×
1500
      SML_CHECK_CODE(TSDB_CODE_SML_INTERNAL_ERROR);
×
1501
    }
1502

1503
    // use tablemeta of stable to save vgid and uid of child table
1504
    (*pMeta)->tableMeta->vgId = vg.vgId;
1,689,839✔
1505
    (*pMeta)->tableMeta->uid = tableData->uid;  // one table merge data block together according uid
1,688,999✔
1506
    uDebug("SML:0x%" PRIx64 ", %s table:%s, uid:%" PRIu64 ", format:%d", info->id, __FUNCTION__, pName.tname,
1,689,419!
1507
           tableData->uid, info->dataFormat);
1508

1509
    SML_CHECK_CODE(smlBindData(info->pQuery, info->dataFormat, tableData->tags, (*pMeta)->cols, tableData->cols,
1,689,419!
1510
                               (*pMeta)->tableMeta, tableData->childTableName, measure, measureLen, info->ttl,
1511
                               info->msgBuf.buf, info->msgBuf.len, info->taos->optionInfo.charsetCxt));
1512
    taosMemoryFreeClear(measure);
1,688,159!
1513
    oneTable = (SSmlTableInfo **)taosHashIterate(info->childTables, oneTable);
1,687,739✔
1514
  }
1515

1516
  SML_CHECK_CODE(smlBuildOutput(info->pQuery, info->pVgHash));
781,905!
1517
  info->cost.insertRpcTime = taosGetTimestampUs();
781,905✔
1518

1519
  SAppClusterSummary *pActivity = &info->taos->pAppInfo->summary;
781,905✔
1520
  (void)atomic_add_fetch_64((int64_t *)&pActivity->numOfInsertsReq, 1);  // no need to check return code
781,905✔
1521

1522
  launchQueryImpl(info->pRequest, info->pQuery, true, NULL);  // no need to check return code
781,905✔
1523

1524
  uDebug("SML:0x%" PRIx64 ", %s end, format:%d, code:%d,%s", info->id, __FUNCTION__, info->dataFormat,
781,905!
1525
         info->pRequest->code, tstrerror(info->pRequest->code));
1526

1527
  return info->pRequest->code;
781,905✔
1528

1529
END:
1,452✔
1530
  taosMemoryFree(measure);
1,032!
1531
  taosHashCancelIterate(info->childTables, oneTable);
1,032✔
1532
  RETURN
1,032!
1533
}
1534

1535
static void smlPrintStatisticInfo(SSmlHandle *info) {
921,553✔
1536
  uDebug(
921,553✔
1537
      "SML:0x%" PRIx64
1538
      " smlInsertLines result, code:%d, msg:%s, lineNum:%d,stable num:%d,ctable num:%d,create stable num:%d,alter stable tag num:%d,alter stable col num:%d \
1539
        parse cost:%" PRId64 ",schema cost:%" PRId64 ",bind cost:%" PRId64 ",rpc cost:%" PRId64 ",total cost:%" PRId64,
1540
      info->id, info->cost.code, tstrerror(info->cost.code), info->cost.lineNum, info->cost.numOfSTables,
1541
      info->cost.numOfCTables, info->cost.numOfCreateSTables, info->cost.numOfAlterTagSTables,
1542
      info->cost.numOfAlterColSTables, info->cost.schemaTime - info->cost.parseTime,
1543
      info->cost.insertBindTime - info->cost.schemaTime, info->cost.insertRpcTime - info->cost.insertBindTime,
1544
      info->cost.endTime - info->cost.insertRpcTime, info->cost.endTime - info->cost.parseTime);
1545
}
921,553✔
1546

1547
int32_t smlClearForRerun(SSmlHandle *info) {
679,346✔
1548
  int32_t code = 0;
679,346✔
1549
  int32_t lino = 0;
679,346✔
1550
  info->reRun = false;
679,346✔
1551

1552
  taosHashClear(info->childTables);
679,346✔
1553
  taosHashClear(info->superTables);
679,346✔
1554
  taosHashClear(info->tableUids);
679,346✔
1555

1556
  if (!info->dataFormat) {
679,346!
1557
    info->lines = (SSmlLineInfo *)taosMemoryCalloc(info->lineNum, sizeof(SSmlLineInfo));
679,346!
1558
    SML_CHECK_NULL(info->lines);
678,929!
1559
  }
1560

1561
  taosArrayClearP(info->escapedStringList, NULL);
678,929✔
1562
  if (info->protocol == TSDB_SML_JSON_PROTOCOL) {
679,346✔
1563
    taosMemoryFreeClear(info->preLine.tags);
226,927!
1564
  }
1565
  (void)memset(&info->preLine, 0, sizeof(SSmlLineInfo));
679,346!
1566
  info->currSTableMeta = NULL;
679,346✔
1567
  info->currTableDataCtx = NULL;
679,346✔
1568

1569
  SVnodeModifyOpStmt *stmt = (SVnodeModifyOpStmt *)(info->pQuery->pRoot);
679,346✔
1570
  stmt->freeHashFunc(stmt->pTableBlockHashObj);
679,346✔
1571
  stmt->pTableBlockHashObj = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
678,926✔
1572
  SML_CHECK_NULL(stmt->pTableBlockHashObj);
679,346!
1573

1574
END:
679,346✔
1575
  RETURN
679,346!
1576
}
1577

1578
static void printRaw(int64_t id, int lineNum, int numLines, ELogLevel level, char *data, int32_t len) {
9,288✔
1579
  char *print = taosMemoryMalloc(len + 1);
9,288!
1580
  if (print == NULL) {
9,288!
1581
    uError("SML:0x%" PRIx64 ", smlParseLine failed. code :%d", id, terrno);
×
1582
    return;
×
1583
  }
1584
  (void)memcpy(print, data, len);
9,288!
1585
  print[len] = '\0';
9,288✔
1586
  if (level == DEBUG_DEBUG) {
9,288!
1587
    uDebug("SML:0x%" PRIx64 ", smlParseLine is raw, line %d/%d :%s", id, lineNum, numLines, print);
9,288!
1588
  } else if (level == DEBUG_ERROR) {
×
1589
    uError("SML:0x%" PRIx64 ", smlParseLine failed. line %d/%d :%s", id, lineNum, numLines, print);
×
1590
  }
1591
  taosMemoryFree(print);
9,288!
1592
}
1593

1594
static bool getLine(SSmlHandle *info, char *lines[], char **rawLine, char *rawLineEnd, int numLines, int i, char **tmp,
11,609,734✔
1595
                    int *len) {
1596
  if (lines) {
11,609,734✔
1597
    *tmp = lines[i];
11,605,803✔
1598
    *len = strlen(*tmp);
11,606,643!
1599
  } else if (*rawLine) {
10,224!
1600
    *tmp = *rawLine;
10,151✔
1601
    while (*rawLine < rawLineEnd) {
83,509,183✔
1602
      if (*((*rawLine)++) == '\n') {
83,503,415✔
1603
        break;
4,383✔
1604
      }
1605
      (*len)++;
83,499,032✔
1606
    }
1607
    if (IS_COMMENT(info->protocol, (*tmp)[0])) {  // this line is comment
10,151✔
1608
      return false;
516✔
1609
    }
1610
  }
1611

1612
  if (*rawLine != NULL && (uDebugFlag & DEBUG_DEBUG)) {
11,610,058✔
1613
    printRaw(info->id, i, numLines, DEBUG_DEBUG, *tmp, *len);
9,288✔
1614
  } else {
1615
    uDebug("SML:0x%" PRIx64 ", smlParseLine is not raw, line %d/%d :%s", info->id, i, numLines, *tmp);
11,600,770✔
1616
  }
1617
  return true;
11,780,457✔
1618
}
1619

1620
static int32_t smlParseJson(SSmlHandle *info, char *lines[], char *rawLine) {
360,945✔
1621
  int32_t code = TSDB_CODE_SUCCESS;
360,945✔
1622
  if (lines) {
360,945✔
1623
    code = smlParseJSONExt(info, *lines);
360,921✔
1624
  } else if (rawLine) {
24!
1625
    code = smlParseJSONExt(info, rawLine);
24✔
1626
  }
1627
  if (code != TSDB_CODE_SUCCESS) {
360,945✔
1628
    uError("%s failed code:%d", __FUNCTION__, code);
50,527!
1629
  }
1630
  return code;
360,945✔
1631
}
1632

1633
static int32_t smlParseStart(SSmlHandle *info, char *lines[], char *rawLine, char *rawLineEnd, int numLines) {
922,373✔
1634
  uDebug("SML:0x%" PRIx64 ", %s start", info->id, __FUNCTION__);
922,373✔
1635
  int32_t code = TSDB_CODE_SUCCESS;
922,373✔
1636
  if (info->protocol == TSDB_SML_JSON_PROTOCOL) {
922,373✔
1637
    return smlParseJson(info, lines, rawLine);
360,945✔
1638
  }
1639

1640
  char   *oldRaw = rawLine;
561,428✔
1641
  int32_t i = 0;
561,428✔
1642
  while (i < numLines) {
12,164,256✔
1643
    char *tmp = NULL;
11,649,076✔
1644
    int   len = 0;
11,649,076✔
1645
    if (!getLine(info, lines, &rawLine, rawLineEnd, numLines, i, &tmp, &len)) {
11,649,076✔
1646
      continue;
3,828✔
1647
    }
1648
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
11,736,467✔
1649
      if (info->dataFormat) {
10,624,405✔
1650
        SSmlLineInfo element = {0};
326,540✔
1651
        code = smlParseInfluxString(info, tmp, tmp + len, &element);
326,540✔
1652
      } else {
1653
        code = smlParseInfluxString(info, tmp, tmp + len, info->lines + i);
10,297,865✔
1654
      }
1655
    } else if (info->protocol == TSDB_SML_TELNET_PROTOCOL) {
1,112,062!
1656
      if (info->dataFormat) {
1,121,109✔
1657
        SSmlLineInfo element = {0};
806,434✔
1658
        code = smlParseTelnetString(info, (char *)tmp, (char *)tmp + len, &element);
806,434✔
1659
        if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
806,434!
1660
      } else {
1661
        code = smlParseTelnetString(info, (char *)tmp, (char *)tmp + len, info->lines + i);
314,675✔
1662
      }
1663
    }
1664
    if (code != TSDB_CODE_SUCCESS) {
11,668,646✔
1665
      if (rawLine != NULL) {
66,334!
1666
        printRaw(info->id, i, numLines, DEBUG_ERROR, tmp, len);
×
1667
      } else {
1668
        uError("SML:0x%" PRIx64 ", %s failed. line %d :%s", info->id, __FUNCTION__, i, tmp);
66,334!
1669
      }
1670
      return code;
66,334✔
1671
    }
1672
    if (info->reRun) {
11,602,312✔
1673
      uDebug("SML:0x%" PRIx64 ", %s re run", info->id, __FUNCTION__);
452,410✔
1674
      i = 0;
452,410✔
1675
      rawLine = oldRaw;
452,410✔
1676
      code = smlClearForRerun(info);
452,410✔
1677
      if (code != TSDB_CODE_SUCCESS) {
452,410!
1678
        return code;
×
1679
      }
1680
      continue;
452,410✔
1681
    }
1682
    i++;
11,150,322✔
1683
  }
1684
  uDebug("SML:0x%" PRIx64 ", %s end", info->id, __FUNCTION__);
515,180✔
1685

1686
  return code;
495,094✔
1687
}
1688

1689
static int smlProcess(SSmlHandle *info, char *lines[], char *rawLine, char *rawLineEnd, int numLines) {
922,373✔
1690
  int32_t code = TSDB_CODE_SUCCESS;
922,373✔
1691
  int32_t lino = 0;
922,373✔
1692
  int32_t retryNum = 0;
922,373✔
1693

1694
  info->cost.parseTime = taosGetTimestampUs();
922,373✔
1695

1696
  SML_CHECK_CODE(smlParseStart(info, lines, rawLine, rawLineEnd, numLines));
922,373✔
1697
  SML_CHECK_CODE(smlParseEnd(info));
805,512✔
1698

1699
  info->cost.lineNum = info->lineNum;
793,060✔
1700
  info->cost.numOfSTables = taosHashGetSize(info->superTables);
793,060✔
1701
  info->cost.numOfCTables = taosHashGetSize(info->childTables);
793,060✔
1702
  info->cost.schemaTime = taosGetTimestampUs();
793,060✔
1703

1704
  do {
1705
    code = smlModifyDBSchemas(info);
832,722✔
1706
    if (code != TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER && code != TSDB_CODE_SDB_OBJ_CREATING && code != TSDB_CODE_SYN_NOT_LEADER &&
832,722!
1707
        code != TSDB_CODE_MND_TRANS_CONFLICT) {
1708
      break;
793,060✔
1709
    }
1710
    taosMsleep(100);
39,662✔
1711
    uInfo("SML:0x%" PRIx64 ", smlModifyDBSchemas retry code:%s, times:%d", info->id, tstrerror(code), retryNum);
39,662!
1712
  } while (retryNum++ < taosHashGetSize(info->superTables) * MAX_RETRY_TIMES);
39,662!
1713

1714
  SML_CHECK_CODE(code);
793,060✔
1715
  info->cost.insertBindTime = taosGetTimestampUs();
782,937✔
1716
  SML_CHECK_CODE(smlInsertData(info));
782,937✔
1717

1718
END:
781,512✔
1719
  RETURN
922,373!
1720
}
1721

1722
void smlSetReqSQL(SRequestObj *request, char *lines[], char *rawLine, char *rawLineEnd) {
922,373✔
1723
  if (request->pTscObj->pAppInfo->serverCfg.monitorParas.tsSlowLogScope & SLOW_LOG_TYPE_INSERT) {
922,373!
1724
    int32_t len = 0;
×
1725
    int32_t rlen = 0;
×
1726
    char   *p = NULL;
×
1727

1728
    if (lines && lines[0]) {
×
1729
      len = strlen(lines[0]);
×
1730
      p = lines[0];
×
1731
    } else if (rawLine) {
×
1732
      if (rawLineEnd) {
×
1733
        len = rawLineEnd - rawLine;
×
1734
      } else {
1735
        len = strlen(rawLine);
×
1736
      }
1737
      p = rawLine;
×
1738
    }
1739

1740
    if (NULL == p) {
×
1741
      return;
×
1742
    }
1743

1744
    rlen = TMIN(len, TSDB_MAX_ALLOWED_SQL_LEN);
×
1745
    rlen = TMAX(rlen, 0);
×
1746

1747
    char *sql = taosMemoryMalloc(rlen + 1);
×
1748
    if (NULL == sql) {
×
1749
      uError("malloc %d for sml sql failed", rlen + 1);
×
1750
      return;
×
1751
    }
1752
    (void)memcpy(sql, p, rlen);
×
1753
    sql[rlen] = 0;
×
1754

1755
    request->sqlstr = sql;
×
1756
    request->sqlLen = rlen;
×
1757
  }
1758
}
1759

1760
TAOS_RES *taos_schemaless_insert_inner(TAOS *taos, char *lines[], char *rawLine, char *rawLineEnd, int numLines,
921,553✔
1761
                                       int protocol, int precision, int32_t ttl, int64_t reqid, char *tbnameKey) {
1762
  int32_t      code = TSDB_CODE_SUCCESS;
921,553✔
1763
  int32_t      lino = 0;
921,553✔
1764
  SRequestObj *request = NULL;
921,553✔
1765
  SSmlHandle  *info = NULL;
921,553✔
1766
  int          cnt = 0;
921,553✔
1767
  while (1) {
820✔
1768
    SML_CHECK_CODE(buildRequest(*(int64_t *)taos, "", 0, NULL, false, &request, reqid));
922,373!
1769
    SSmlMsgBuf msg = {request->msgBufLen, request->msgBuf};
922,373✔
1770
    request->code = smlBuildSmlInfo(taos, &info);
922,373✔
1771
    SML_CHECK_CODE(request->code);
922,373!
1772

1773
    info->pRequest = request;
922,373✔
1774
    info->pRequest->pQuery = info->pQuery;
922,373✔
1775
    info->ttl = ttl;
922,373✔
1776
    info->precision = precision;
922,373✔
1777
    info->protocol = (TSDB_SML_PROTOCOL_TYPE)protocol;
922,373✔
1778
    info->msgBuf.buf = info->pRequest->msgBuf;
922,373✔
1779
    info->msgBuf.len = ERROR_MSG_BUF_DEFAULT_SIZE;
922,373✔
1780
    info->lineNum = numLines;
922,373✔
1781
    info->tbnameKey = tbnameKey;
922,373✔
1782

1783
    smlSetReqSQL(request, lines, rawLine, rawLineEnd);
922,373✔
1784

1785
    if (request->pDb == NULL) {
922,373!
1786
      request->code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1787
      smlBuildInvalidDataMsg(&msg, "Database not specified", NULL);
×
1788
      goto END;
×
1789
    }
1790

1791
    if (protocol < TSDB_SML_LINE_PROTOCOL || protocol > TSDB_SML_JSON_PROTOCOL) {
922,373!
1792
      request->code = TSDB_CODE_SML_INVALID_PROTOCOL_TYPE;
×
1793
      smlBuildInvalidDataMsg(&msg, "protocol invalidate", NULL);
×
1794
      goto END;
×
1795
    }
1796

1797
    if (protocol == TSDB_SML_LINE_PROTOCOL &&
922,373!
1798
        (precision < TSDB_SML_TIMESTAMP_NOT_CONFIGURED || precision > TSDB_SML_TIMESTAMP_NANO_SECONDS)) {
326,540!
UNCOV
1799
      request->code = TSDB_CODE_SML_INVALID_PRECISION_TYPE;
×
UNCOV
1800
      smlBuildInvalidDataMsg(&msg, "precision invalidate for line protocol", NULL);
×
1801
      goto END;
×
1802
    }
1803

1804
    if (protocol == TSDB_SML_JSON_PROTOCOL) {
922,373✔
1805
      numLines = 1;
360,945✔
1806
    } else if (numLines <= 0) {
561,428!
1807
      request->code = TSDB_CODE_SML_INVALID_DATA;
×
1808
      smlBuildInvalidDataMsg(&msg, "line num is invalid", NULL);
×
1809
      goto END;
×
1810
    }
1811

1812
    code = smlProcess(info, lines, rawLine, rawLineEnd, numLines);
922,373✔
1813
    request->code = code;
922,373✔
1814
    info->cost.endTime = taosGetTimestampUs();
1,824,440✔
1815
    info->cost.code = code;
922,373✔
1816
    if (NEED_CLIENT_HANDLE_ERROR(code) || code == TSDB_CODE_SDB_OBJ_CREATING || code == TSDB_CODE_PAR_VALUE_TOO_LONG ||
922,373!
1817
        code == TSDB_CODE_MND_TRANS_CONFLICT || code == TSDB_CODE_SYN_NOT_LEADER) {
921,553!
1818
      if (cnt++ >= 10) {
820!
1819
        uInfo("SML:%" PRIx64 " retry:%d/10 end code:%d, msg:%s", info->id, cnt, code, tstrerror(code));
×
1820
        break;
20,272✔
1821
      }
1822
      taosMsleep(100);
820✔
1823
      uInfo("SML:%" PRIx64 " retry:%d/10,ver is old retry or object is creating code:%d, msg:%s", info->id, cnt, code,
820!
1824
            tstrerror(code));
1825
      code = refreshMeta(request->pTscObj, request);
820✔
1826
      if (code != 0) {
820✔
1827
        uInfo("SML:%" PRIx64 " refresh meta error code:%d, msg:%s", info->id, code, tstrerror(code));
427!
1828
      }
1829
      smlDestroyInfo(info);
820✔
1830
      info = NULL;
820✔
1831
      taos_free_result(request);
820✔
1832
      request = NULL;
820✔
1833
      continue;
820✔
1834
    }
1835
    smlPrintStatisticInfo(info);
921,553✔
1836
    break;
921,553✔
1837
  }
1838

1839
END:
921,553✔
1840
  smlDestroyInfo(info);
921,553✔
1841
  return (TAOS_RES *)request;
921,553✔
1842
}
1843

1844
/**
1845
 * taos_schemaless_insert() parse and insert data points into database according to
1846
 * different protocol.
1847
 *
1848
 * @param $lines input array may contain multiple lines, each line indicates a data point.
1849
 *               If protocol=2 is used input array should contain single JSON
1850
 *               string(e.g. char *lines[] = {"$JSON_string"}). If need to insert
1851
 *               multiple data points in JSON format, should include them in $JSON_string
1852
 *               as a JSON array.
1853
 * @param $numLines indicates how many data points in $lines.
1854
 *                  If protocol = 2 is used this param will be ignored as $lines should
1855
 *                  contain single JSON string.
1856
 * @param $protocol indicates which protocol to use for parsing:
1857
 *                  0 - influxDB line protocol
1858
 *                  1 - OpenTSDB telnet line protocol
1859
 *                  2 - OpenTSDB JSON format protocol
1860
 * @return TAOS_RES
1861
 */
1862

1863
TAOS_RES *taos_schemaless_insert_ttl_with_reqid_tbname_key(TAOS *taos, char *lines[], int numLines, int protocol,
917,849✔
1864
                                                           int precision, int32_t ttl, int64_t reqid, char *tbnameKey) {
1865
  if (taos == NULL || lines == NULL || numLines < 0) {
917,849!
UNCOV
1866
    terrno = TSDB_CODE_INVALID_PARA;
×
1867
    return NULL;
×
1868
  }
1869
  for (int i = 0; i < numLines; i++) {
13,753,663✔
1870
    if (lines[i] == NULL) {
12,835,814!
1871
      terrno = TSDB_CODE_INVALID_PARA;
×
1872
      return NULL;
×
1873
    }
1874
  }
1875
  return taos_schemaless_insert_inner(taos, lines, NULL, NULL, numLines, protocol, precision, ttl, reqid, tbnameKey);
917,849✔
1876
}
1877

1878
TAOS_RES *taos_schemaless_insert_ttl_with_reqid(TAOS *taos, char *lines[], int numLines, int protocol, int precision,
917,849✔
1879
                                                int32_t ttl, int64_t reqid) {
1880
  return taos_schemaless_insert_ttl_with_reqid_tbname_key(taos, lines, numLines, protocol, precision, ttl, reqid, NULL);
917,849✔
1881
}
1882

1883
TAOS_RES *taos_schemaless_insert(TAOS *taos, char *lines[], int numLines, int protocol, int precision) {
915,785✔
1884
  return taos_schemaless_insert_ttl_with_reqid(taos, lines, numLines, protocol, precision, TSDB_DEFAULT_TABLE_TTL, 0);
915,785✔
1885
}
1886

1887
TAOS_RES *taos_schemaless_insert_ttl(TAOS *taos, char *lines[], int numLines, int protocol, int precision,
2,064✔
1888
                                     int32_t ttl) {
1889
  return taos_schemaless_insert_ttl_with_reqid(taos, lines, numLines, protocol, precision, ttl, 0);
2,064✔
1890
}
1891

1892
TAOS_RES *taos_schemaless_insert_with_reqid(TAOS *taos, char *lines[], int numLines, int protocol, int precision,
×
1893
                                            int64_t reqid) {
1894
  return taos_schemaless_insert_ttl_with_reqid(taos, lines, numLines, protocol, precision, TSDB_DEFAULT_TABLE_TTL,
×
1895
                                               reqid);
1896
}
1897

1898
static int32_t getRawLineLen(char *lines, int len, int protocol) {
3,704✔
1899
  int   numLines = 0;
3,704✔
1900
  char *tmp = lines;
3,704✔
1901
  for (int i = 0; i < len; i++) {
41,852,724✔
1902
    if (lines[i] == '\n' || i == len - 1) {
41,849,020✔
1903
      if (!IS_COMMENT(protocol, tmp[0])) {  // ignore comment
6,531✔
1904
        numLines++;
6,015✔
1905
      }
1906
      tmp = lines + i + 1;
6,531✔
1907
    }
1908
  }
1909
  return numLines;
3,704✔
1910
}
1911

1912
TAOS_RES *taos_schemaless_insert_raw_ttl_with_reqid_tbname_key(TAOS *taos, char *lines, int len, int32_t *totalRows,
3,704✔
1913
                                                               int protocol, int precision, int32_t ttl, int64_t reqid,
1914
                                                               char *tbnameKey) {
1915
  if (taos == NULL || lines == NULL || len < 0) {
3,704!
1916
    terrno = TSDB_CODE_INVALID_PARA;
×
1917
    return NULL;
×
1918
  }
1919
  int numLines = getRawLineLen(lines, len, protocol);
3,704✔
1920
  if (totalRows != NULL) {
3,704!
1921
    *totalRows = numLines;
3,704✔
1922
  }
1923
  return taos_schemaless_insert_inner(taos, NULL, lines, lines + len, numLines, protocol, precision, ttl, reqid,
3,704✔
1924
                                      tbnameKey);
1925
}
1926

1927
TAOS_RES *taos_schemaless_insert_raw_ttl_with_reqid(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
3,704✔
1928
                                                    int precision, int32_t ttl, int64_t reqid) {
1929
  return taos_schemaless_insert_raw_ttl_with_reqid_tbname_key(taos, lines, len, totalRows, protocol, precision, ttl,
3,704✔
1930
                                                              reqid, NULL);
1931
}
1932

1933
TAOS_RES *taos_schemaless_insert_raw_with_reqid(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
12✔
1934
                                                int precision, int64_t reqid) {
1935
  return taos_schemaless_insert_raw_ttl_with_reqid(taos, lines, len, totalRows, protocol, precision,
12✔
1936
                                                   TSDB_DEFAULT_TABLE_TTL, reqid);
1937
}
1938
TAOS_RES *taos_schemaless_insert_raw_ttl(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
×
1939
                                         int precision, int32_t ttl) {
1940
  return taos_schemaless_insert_raw_ttl_with_reqid(taos, lines, len, totalRows, protocol, precision, ttl, 0);
×
1941
}
1942

1943
TAOS_RES *taos_schemaless_insert_raw(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
3,621✔
1944
                                     int precision) {
1945
  return taos_schemaless_insert_raw_ttl_with_reqid(taos, lines, len, totalRows, protocol, precision,
3,621✔
1946
                                                   TSDB_DEFAULT_TABLE_TTL, 0);
1947
}
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