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

taosdata / TDengine / #4118

17 May 2025 06:43AM UTC coverage: 62.797% (+0.7%) from 62.054%
#4118

push

travis-ci

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

merge: from main to 3.0 branch

156841 of 318088 branches covered (49.31%)

Branch coverage included in aggregate %.

176 of 225 new or added lines in 20 files covered. (78.22%)

2989 existing lines in 163 files now uncovered.

242067 of 317143 relevant lines covered (76.33%)

6956088.31 hits per line

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

74.38
/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)                             \
108
  (protocol == TSDB_SML_LINE_PROTOCOL && data == '#')
109

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

114
static int32_t smlCheckAuth(SSmlHandle *info, SRequestConnInfo *conn, const char *pTabName, AUTH_TYPE type) {
3,560✔
115
  SUserAuthInfo pAuth = {0};
3,560✔
116
  (void)snprintf(pAuth.user, sizeof(pAuth.user), "%s", info->taos->user);
3,560✔
117
  if (NULL == pTabName) {
3,560✔
118
    if (tNameSetDbName(&pAuth.tbName, info->taos->acctId, info->pRequest->pDb, strlen(info->pRequest->pDb)) != 0) {
945!
119
      return TSDB_CODE_SML_INVALID_DATA;
×
120
    }
121
  } else {
122
    toName(info->taos->acctId, info->pRequest->pDb, pTabName, &pAuth.tbName);
2,615✔
123
  }
124
  pAuth.type = type;
3,557✔
125

126
  int32_t      code = TSDB_CODE_SUCCESS;
3,557✔
127
  SUserAuthRes authRes = {0};
3,557✔
128

129
  code = catalogChkAuth(info->pCatalog, conn, &pAuth, &authRes);
3,557✔
130
  nodesDestroyNode(authRes.pCond[AUTH_RES_BASIC]);
3,560✔
131

132
  return (code == TSDB_CODE_SUCCESS)
133
             ? (authRes.pass[AUTH_RES_BASIC] ? TSDB_CODE_SUCCESS : TSDB_CODE_PAR_PERMISSION_DENIED)
3,557✔
134
             : code;
3,557!
135
}
136

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

152
int64_t smlGetTimeValue(const char *value, int32_t len, uint8_t fromPrecision, uint8_t toPrecision) {
102,040✔
153
  char   *endPtr = NULL;
102,040✔
154
  int64_t tsInt64 = taosStr2Int64(value, &endPtr, 10);
102,040✔
155
  if (unlikely(value + len != endPtr)) {
102,262✔
156
    return -1;
6✔
157
  }
158

159
  if (unlikely(fromPrecision >= TSDB_TIME_PRECISION_HOURS)) {
102,256✔
160
    int64_t unit = smlToMilli[fromPrecision - TSDB_TIME_PRECISION_HOURS];
104✔
161
    if (tsInt64 != 0 && unit > INT64_MAX / tsInt64) {
104!
162
      return -1;
1✔
163
    }
164
    tsInt64 *= unit;
103✔
165
    fromPrecision = TSDB_TIME_PRECISION_MILLI;
103✔
166
  }
167

168
  return convertTimePrecision(tsInt64, fromPrecision, toPrecision);
102,255✔
169
}
170

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

177
  tag->sTableName = measure;
2,852✔
178
  tag->sTableNameLen = measureLen;
2,852✔
179

180
  tag->cols = taosArrayInit(numRows, POINTER_BYTES);
2,852✔
181
  SML_CHECK_NULL(tag->cols)
2,852!
182
  *tInfo = tag;
2,852✔
183
  return code;
2,852✔
184

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

191
void smlBuildTsKv(SSmlKv *kv, int64_t ts) {
104,594✔
192
  kv->key = tsSmlTsDefaultName;
104,594✔
193
  kv->keyLen = strlen(tsSmlTsDefaultName);
104,594✔
194
  kv->type = TSDB_DATA_TYPE_TIMESTAMP;
104,594✔
195
  kv->i = ts;
104,594✔
196
  kv->length = (size_t)tDataTypes[TSDB_DATA_TYPE_TIMESTAMP].bytes;
104,594✔
197
}
104,594✔
198

199
static void smlDestroySTableMeta(void *para) {
3,028✔
200
  if (para == NULL) {
3,028!
201
    return;
×
202
  }
203
  SSmlSTableMeta *meta = *(SSmlSTableMeta **)para;
3,028✔
204
  if (meta == NULL) {
3,028✔
205
    return;
1,007✔
206
  }
207
  taosHashCleanup(meta->tagHash);
2,021✔
208
  taosHashCleanup(meta->colHash);
2,022✔
209
  taosArrayDestroy(meta->tags);
2,021✔
210
  taosArrayDestroy(meta->cols);
2,022✔
211
  taosMemoryFreeClear(meta->tableMeta);
2,022!
212
  taosMemoryFree(meta);
2,022!
213
}
214

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

220
  int   measureLen = currElement->measureLen;
1,811✔
221
  char *measure = (char *)taosMemoryMalloc(measureLen);
1,811!
222
  SML_CHECK_NULL(measure);
1,816!
223
  (void)memcpy(measure, currElement->measure, measureLen);
1,816✔
224
  if (currElement->measureEscaped) {
1,816✔
225
    PROCESS_SLASH_IN_MEASUREMENT(measure, measureLen);
108!
226
  }
227
  smlStrReplace(measure, measureLen);
1,816✔
228
  code = smlGetMeta(info, measure, measureLen, &pTableMeta);
1,814✔
229
  taosMemoryFree(measure);
1,819!
230
  if (code != TSDB_CODE_SUCCESS) {
1,819✔
231
    info->dataFormat = false;
1,007✔
232
    info->reRun = true;
1,007✔
233
    goto END;
1,007✔
234
  }
235
  SML_CHECK_CODE(smlBuildSTableMeta(info->dataFormat, sMeta));
812!
236
  for (int i = 0; i < pTableMeta->tableInfo.numOfTags + pTableMeta->tableInfo.numOfColumns; i++) {
9,142✔
237
    SSchema *col = pTableMeta->schema + i;
8,328✔
238
    SSmlKv   kv = {.key = col->name, .keyLen = strlen(col->name), .type = col->type};
8,328✔
239
    if (col->type == TSDB_DATA_TYPE_NCHAR) {
8,328✔
240
      kv.length = (col->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
2,534✔
241
    } else if (col->type == TSDB_DATA_TYPE_BINARY || col->type == TSDB_DATA_TYPE_GEOMETRY ||
5,794✔
242
               col->type == TSDB_DATA_TYPE_VARBINARY) {
4,726✔
243
      kv.length = col->bytes - VARSTR_HEADER_SIZE;
1,091✔
244
    } else {
245
      kv.length = col->bytes;
4,703✔
246
    }
247

248
    if (i < pTableMeta->tableInfo.numOfColumns) {
8,328✔
249
      SML_CHECK_NULL(taosArrayPush((*sMeta)->cols, &kv));
4,997!
250
    } else {
251
      SML_CHECK_NULL(taosArrayPush((*sMeta)->tags, &kv));
11,661!
252
    }
253
  }
254
  SML_CHECK_CODE(taosHashPut(info->superTables, currElement->measure, currElement->measureLen, sMeta, POINTER_BYTES));
814!
255
  (*sMeta)->tableMeta = pTableMeta;
814✔
256
  return code;
814✔
257

258
END:
1,007✔
259
  smlDestroySTableMeta(sMeta);
1,007✔
260
  taosMemoryFreeClear(pTableMeta);
1,007!
261
  RETURN
1,007!
262
}
263

264
bool isSmlColAligned(SSmlHandle *info, int cnt, SSmlKv *kv) {
160✔
265
  // cnt begin 0, add ts so + 2
266
  if (unlikely(cnt + 2 > info->currSTableMeta->tableInfo.numOfColumns)) {
160!
267
    goto END;
×
268
  }
269
  // bind data
270
  int32_t ret = smlBuildCol(info->currTableDataCtx, info->currSTableMeta->schema, kv, cnt + 1, info->taos->optionInfo.charsetCxt);
160✔
271
  if (unlikely(ret != TSDB_CODE_SUCCESS)) {
163✔
272
    uDebug("smlBuildCol error, retry");
14!
273
    goto END;
14✔
274
  }
275
  if (cnt >= taosArrayGetSize(info->maxColKVs)) {
149!
276
    goto END;
×
277
  }
278
  SSmlKv *maxKV = (SSmlKv *)taosArrayGet(info->maxColKVs, cnt);
146✔
279
  if (maxKV == NULL) {
147!
280
    goto END;
×
281
  }
282
  if (unlikely(!IS_SAME_KEY)) {
147!
283
    goto END;
147✔
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:
161✔
293
  info->dataFormat = false;
161✔
294
  info->reRun = true;
161✔
295
  return false;
161✔
296
}
297

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

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

314
  if (unlikely(kv->length > maxKV->length)) {
6,173✔
315
    maxKV->length = kv->length;
20✔
316
    info->needModifySchema = true;
20✔
317
  }
318
  return true;
6,173✔
319

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

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

337
static bool smlIsPKTable(STableMeta *pTableMeta) {
1,185✔
338
  for (int i = 0; i < pTableMeta->tableInfo.numOfColumns; i++) {
5,270✔
339
    if (pTableMeta->schema[i].flags & COL_IS_KEY) {
4,109✔
340
      return true;
24✔
341
    }
342
  }
343

344
  return false;
1,161✔
345
}
346

347
int32_t smlProcessSuperTable(SSmlHandle *info, SSmlLineInfo *elements) {
1,879✔
348
  bool isSameMeasure = IS_SAME_SUPER_TABLE;
1,879!
349
  if (isSameMeasure) {
1,879✔
350
    return TSDB_CODE_SUCCESS;
66✔
351
  }
352
  SSmlSTableMeta **tmp = (SSmlSTableMeta **)taosHashGet(info->superTables, elements->measure, elements->measureLen);
1,813✔
353

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

369
  if (smlIsPKTable(sMeta->tableMeta)) {
814✔
370
    return TSDB_CODE_SML_NOT_SUPPORT_PK;
24✔
371
  }
372
  return 0;
790✔
373
}
374

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

384
    tinfo->tags = taosArrayDup(info->preLineTagKV, NULL);
2,853✔
385
    SML_CHECK_NULL(tinfo->tags);
2,853!
386
    for (size_t i = 0; i < taosArrayGetSize(info->preLineTagKV); i++) {
23,928✔
387
      SSmlKv *kv = (SSmlKv *)taosArrayGet(info->preLineTagKV, i);
21,085✔
388
      SML_CHECK_NULL(kv);
21,083✔
389
      if (kv->keyEscaped) kv->key = NULL;
21,075✔
390
      if (kv->valueEscaped) kv->value = NULL;
21,075✔
391
    }
392

393
    SML_CHECK_CODE(smlSetCTableName(tinfo, info->tbnameKey));
2,845!
394
    SML_CHECK_CODE(getTableUid(info, elements, tinfo));
2,853!
395
    if (info->dataFormat) {
2,853✔
396
      info->currSTableMeta->uid = tinfo->uid;
779✔
397
      SML_CHECK_CODE(smlInitTableDataCtx(info->pQuery, info->currSTableMeta, &tinfo->tableDataCtx));
779!
398
    }
399
  } else {
400
    tinfo = *oneTable;
73✔
401
  }
402
  if (info->dataFormat) info->currTableDataCtx = tinfo->tableDataCtx;
2,925✔
403
  return TSDB_CODE_SUCCESS;
2,925✔
404

405
END:
×
406
  smlDestroyTableInfo(&tinfo);
×
407
  RETURN
×
408
}
409

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

418
END:
5,091✔
419
  clearColValArraySml(info->currTableDataCtx->pValues);
5,091✔
420
  RETURN
5,078!
421
}
422

423
int32_t smlParseEndTelnetJsonUnFormat(SSmlHandle *info, SSmlLineInfo *elements, SSmlKv *kvTs, SSmlKv *kv) {
2,186✔
424
  int32_t code = 0;
2,186✔
425
  int32_t lino = 0;
2,186✔
426
  uDebug("SML:0x%" PRIx64 ", %s format false, ts:%" PRId64, info->id, __FUNCTION__, kvTs->i);
2,186!
427
  if (elements->colArray == NULL) {
2,186!
428
    elements->colArray = taosArrayInit(16, sizeof(SSmlKv));
2,186✔
429
    SML_CHECK_NULL(elements->colArray);
2,192!
430
  }
431
  SML_CHECK_NULL(taosArrayPush(elements->colArray, kvTs));
4,385!
432
  SML_CHECK_NULL (taosArrayPush(elements->colArray, kv));
4,379!
433

434
END:
2,186✔
435
  RETURN
2,186!
436
}
437

438
int32_t smlParseEndLine(SSmlHandle *info, SSmlLineInfo *elements, SSmlKv *kvTs) {
94,642✔
439
  if (info->dataFormat) {
94,642!
440
    uDebug("SML:0x%" PRIx64 ", %s format true, ts:%" PRId64, info->id, __FUNCTION__, kvTs->i);
×
441
    int32_t ret = smlBuildCol(info->currTableDataCtx, info->currSTableMeta->schema, kvTs, 0, info->taos->optionInfo.charsetCxt);
×
442
    if (ret == TSDB_CODE_SUCCESS) {
×
443
      ret = smlBuildRow(info->currTableDataCtx);
×
444
    }
445

446
    clearColValArraySml(info->currTableDataCtx->pValues);
×
447
    taosArrayClearP(info->escapedStringList, NULL);
×
448
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
×
449
      uError("SML:0x%" PRIx64 ", %s smlBuildCol error:%d", info->id, __FUNCTION__, ret);
×
450
      return ret;
×
451
    }
452
  } else {
453
    uDebug("SML:0x%" PRIx64 ", %s format false, ts:%" PRId64, info->id, __FUNCTION__, kvTs->i);
94,642!
454
    taosArraySet(elements->colArray, 0, kvTs);
94,642✔
455
  }
456
  info->preLine = *elements;
95,332✔
457

458
  return TSDB_CODE_SUCCESS;
95,332✔
459
}
460

461
static int32_t smlParseTableName(SArray *tags, char *childTableName, char *tbnameKey) {
2,849✔
462
  int32_t code = 0;
2,849✔
463
  int32_t lino = 0;
2,849✔
464
  bool    autoChildName = false;
2,849✔
465
  size_t  delimiter = strlen(tsSmlAutoChildTableNameDelimiter);
2,849✔
466
  if (delimiter > 0 && tbnameKey == NULL) {
2,849!
467
    size_t totalNameLen = delimiter * (taosArrayGetSize(tags) - 1);
6✔
468
    for (int i = 0; i < taosArrayGetSize(tags); i++) {
19✔
469
      SSmlKv *tag = (SSmlKv *)taosArrayGet(tags, i);
14✔
470
      SML_CHECK_NULL(tag);
13!
471
      totalNameLen += tag->length;
13✔
472
    }
473
    if (totalNameLen < TSDB_TABLE_NAME_LEN) {
5✔
474
      autoChildName = true;
4✔
475
    }
476
  }
477
  if (autoChildName) {
2,848✔
478
    childTableName[0] = '\0';
4✔
479
    for (int i = 0; i < taosArrayGetSize(tags); i++) {
14✔
480
      SSmlKv *tag = (SSmlKv *)taosArrayGet(tags, i);
10✔
481
      SML_CHECK_NULL(tag);
10!
482
      (void)strncat(childTableName, tag->value, TMIN(tag->length, TSDB_TABLE_NAME_LEN - 1 - strlen(childTableName)));
10✔
483
      if (i != taosArrayGetSize(tags) - 1) {
10✔
484
        (void)strncat(childTableName, tsSmlAutoChildTableNameDelimiter, TSDB_TABLE_NAME_LEN - 1 - strlen(childTableName));
6✔
485
      }
486
    }
487
    if (tsSmlDot2Underline) {
4!
488
      smlStrReplace(childTableName, strlen(childTableName));
4✔
489
    }
490
  } else {
491
    if (tbnameKey == NULL) {
2,844!
492
      tbnameKey = tsSmlChildTableName;
2,844✔
493
    }
494
    size_t childTableNameLen = strlen(tbnameKey);
2,844✔
495
    if (childTableNameLen <= 0) return TSDB_CODE_SUCCESS;
2,844✔
496

497
    for (int i = 0; i < taosArrayGetSize(tags); i++) {
8,211✔
498
      SSmlKv *tag = (SSmlKv *)taosArrayGet(tags, i);
7,011✔
499
      SML_CHECK_NULL(tag);
7,011!
500
      // handle child table name
501
      if (childTableNameLen == tag->keyLen && strncmp(tag->key, tbnameKey, tag->keyLen) == 0) {
7,011!
502
        tstrncpy(childTableName, tag->value, TMIN(TSDB_TABLE_NAME_LEN, tag->length + 1));
66✔
503
        if (tsSmlDot2Underline) {
66✔
504
          smlStrReplace(childTableName, strlen(childTableName));
11✔
505
        }
506
        taosArrayRemove(tags, i);
66✔
507
        break;
66✔
508
      }
509
    }
510
  }
511

512
END:
1,202✔
513
  RETURN
1,272!
514
}
515

516
int32_t smlSetCTableName(SSmlTableInfo *oneTable, char *tbnameKey) {
2,850✔
517
  int32_t code = 0;
2,850✔
518
  int32_t lino = 0;
2,850✔
519
  SArray *dst  = NULL;
2,850✔
520
  SML_CHECK_CODE(smlParseTableName(oneTable->tags, oneTable->childTableName, tbnameKey));
2,850!
521

522
  if (strlen(oneTable->childTableName) == 0) {
2,851✔
523
    dst = taosArrayDup(oneTable->tags, NULL);
2,783✔
524
    SML_CHECK_NULL(dst);
2,783✔
525
    if (oneTable->sTableNameLen >= TSDB_TABLE_NAME_LEN) {
2,779!
526
      SML_CHECK_CODE(TSDB_CODE_SML_INTERNAL_ERROR);
×
527
    }
528
    char          superName[TSDB_TABLE_NAME_LEN] = {0};
2,779✔
529
    RandTableName rName = {dst, NULL, (uint8_t)oneTable->sTableNameLen, oneTable->childTableName};
2,779✔
530
    if (tsSmlDot2Underline) {
2,779✔
531
      (void)memcpy(superName, oneTable->sTableName, oneTable->sTableNameLen);
1,400✔
532
      smlStrReplace(superName, oneTable->sTableNameLen);
1,400✔
533
      rName.stbFullName = superName;
1,404✔
534
    } else {
535
      rName.stbFullName = oneTable->sTableName;
1,379✔
536
    }
537

538
    SML_CHECK_CODE(buildChildTableName(&rName));
2,783!
539
  }
540

541
END:
68✔
542
  taosArrayDestroy(dst);
2,851✔
543
  RETURN
2,853!
544
}
545

546
int32_t getTableUid(SSmlHandle *info, SSmlLineInfo *currElement, SSmlTableInfo *tinfo) {
2,850✔
547
  char   key[TSDB_TABLE_NAME_LEN * 2 + 1] = {0};
2,850✔
548
  size_t nLen = strlen(tinfo->childTableName);
2,850✔
549
  (void)memcpy(key, currElement->measure, currElement->measureLen);
2,850✔
550
  if (tsSmlDot2Underline) {
2,850✔
551
    smlStrReplace(key, currElement->measureLen);
1,416✔
552
  }
553
  (void)memcpy(key + currElement->measureLen + 1, tinfo->childTableName, nLen);
2,852✔
554
  void *uid =
555
      taosHashGet(info->tableUids, key,
2,852✔
556
                  currElement->measureLen + 1 + nLen);  // use \0 as separator for stable name and child table name
2,852✔
557
  if (uid == NULL) {
2,849✔
558
    tinfo->uid = info->uid++;
2,805✔
559
    return taosHashPut(info->tableUids, key, currElement->measureLen + 1 + nLen, &tinfo->uid, sizeof(uint64_t));
2,805✔
560
  } else {
561
    tinfo->uid = *(uint64_t *)uid;
44✔
562
  }
563
  return TSDB_CODE_SUCCESS;
44✔
564
}
565

566
int32_t smlBuildSTableMeta(bool isDataFormat, SSmlSTableMeta **sMeta) {
2,017✔
567
  int32_t code = 0;
2,017✔
568
  int32_t lino = 0;
2,017✔
569
  SSmlSTableMeta *meta = (SSmlSTableMeta *)taosMemoryCalloc(sizeof(SSmlSTableMeta), 1);
2,017!
570
  SML_CHECK_NULL(meta);
2,020!
571
  if (unlikely(!isDataFormat)) {
2,020✔
572
    meta->tagHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
1,207✔
573
    SML_CHECK_NULL(meta->tagHash);
1,208!
574
    meta->colHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
1,208✔
575
    SML_CHECK_NULL(meta->colHash);
1,208!
576
  }
577

578
  meta->tags = taosArrayInit(32, sizeof(SSmlKv));
2,021✔
579
  SML_CHECK_NULL(meta->tags);
2,021!
580

581
  meta->cols = taosArrayInit(32, sizeof(SSmlKv));
2,021✔
582
  SML_CHECK_NULL(meta->cols);
2,021✔
583
  *sMeta = meta;
2,020✔
584
  return TSDB_CODE_SUCCESS;
2,020✔
585

586
END:
×
587
  smlDestroySTableMeta(&meta);
×
588
  uError("%s failed code:%d line:%d", __FUNCTION__ , code, lino);
×
589
  return code;
×
590
}
591

592
int32_t smlParseNumber(SSmlKv *kvVal, SSmlMsgBuf *msg) {
30,283,866✔
593
  const char *pVal = kvVal->value;
30,283,866✔
594
  int32_t     len = kvVal->length;
30,283,866✔
595
  char       *endptr = NULL;
30,283,866✔
596
  double      result = taosStr2Double(pVal, &endptr);
30,283,866✔
597
  if (pVal == endptr) {
30,285,189✔
598
    RETURN_FALSE
11✔
599
  }
600

601
  int32_t left = len - (endptr - pVal);
30,286,072✔
602
  if (left == 0) {
30,286,072✔
603
    SET_DOUBLE
1,704✔
604
  } else if (left == 3) {
30,284,368✔
605
    if (endptr[0] == 'f' || endptr[0] == 'F') {
20,282,258!
606
      if (endptr[1] == '6' && endptr[2] == '4') {
10,176,928!
607
        SET_DOUBLE
10,001,492✔
608
      } else if (endptr[1] == '3' && endptr[2] == '2') {
175,436!
609
        SET_FLOAT
189,214!
610
      } else {
611
        RETURN_FALSE
1✔
612
      }
613
    } else if (endptr[0] == 'i' || endptr[0] == 'I') {
10,105,330✔
614
      if (endptr[1] == '6' && endptr[2] == '4') {
102,605!
615
        SET_BIGINT
1,519✔
616
      } else if (endptr[1] == '3' && endptr[2] == '2') {
101,086!
617
        SET_INT
99,554!
618
      } else if (endptr[1] == '1' && endptr[2] == '6') {
1,532!
619
        SET_SMALL_INT
1,447✔
620
      } else {
621
        RETURN_FALSE
85✔
622
      }
623
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
10,002,725!
624
      if (endptr[1] == '6' && endptr[2] == '4') {
10,002,724!
625
        SET_UBIGINT
1,431✔
626
      } else if (endptr[1] == '3' && endptr[2] == '2') {
10,001,293!
627
        SET_UINT
10,000,645✔
628
      } else if (endptr[1] == '1' && endptr[2] == '6') {
648!
629
        SET_USMALL_INT
647✔
630
      } else {
631
        RETURN_FALSE
1✔
632
      }
633
    } else {
634
      RETURN_FALSE
1✔
635
    }
636
  } else if (left == 2) {
10,002,110✔
637
    if (endptr[0] == 'i' || endptr[0] == 'I') {
10,002,140✔
638
      if (endptr[1] == '8') {
1,488!
639
        SET_TINYINT
1,489✔
640
      } else {
641
        RETURN_FALSE
×
642
      }
643
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
10,000,652!
644
      if (endptr[1] == '8') {
10,000,647!
645
        SET_UTINYINT
10,000,647✔
646
      } else {
647
        RETURN_FALSE
×
648
      }
649
    } else {
650
      RETURN_FALSE
5✔
651
    }
652
  } else if (left == 1) {
21✔
653
    if (endptr[0] == 'i' || endptr[0] == 'I') {
35!
654
      SET_BIGINT
34✔
655
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
1!
656
      SET_UBIGINT
×
657
    } else {
658
      RETURN_FALSE
1✔
659
    }
660
  } else {
661
    RETURN_FALSE;
18✔
662
  }
663
  return true;
30,299,915✔
664
}
665

666
int32_t smlGetMeta(SSmlHandle *info, const void *measure, int32_t measureLen, STableMeta **pTableMeta) {
1,807✔
667
  *pTableMeta = NULL;
1,807✔
668

669
  SName pName = {TSDB_TABLE_NAME_T, info->taos->acctId, {0}, {0}};
1,807✔
670
  tstrncpy(pName.dbname, info->pRequest->pDb, sizeof(pName.dbname));
1,807✔
671

672
  SRequestConnInfo conn = {0};
1,807✔
673
  conn.pTrans = info->taos->pAppInfo->pTransporter;
1,807✔
674
  conn.requestId = info->pRequest->requestId;
1,807✔
675
  conn.requestObjRefId = info->pRequest->self;
1,807✔
676
  conn.mgmtEps = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
1,807✔
677
  int32_t len = TMIN(measureLen, TSDB_TABLE_NAME_LEN - 1);
1,820✔
678
  (void)memcpy(pName.tname, measure, measureLen);
1,820✔
679
  pName.tname[len] = 0;
1,820✔
680

681
  return catalogGetSTableMeta(info->pCatalog, &conn, &pName, pTableMeta);
1,820✔
682
}
683

684
static int64_t smlGenId() {
1,904✔
685
  static volatile int64_t linesSmlHandleId = 0;
686

687
  int64_t id = 0;
1,904✔
688
  do {
689
    id = atomic_add_fetch_64(&linesSmlHandleId, 1);
1,904✔
690
  } while (id == 0);
1,915!
691

692
  return id;
1,915✔
693
}
694

695
static int32_t smlGenerateSchemaAction(SSchema *colField, SHashObj *colHash, SSmlKv *kv, bool isTag,
18,963✔
696
                                       ESchemaAction *action, SSmlHandle *info) {
697
  uint16_t *index = colHash ? (uint16_t *)taosHashGet(colHash, kv->key, kv->keyLen) : NULL;
18,963✔
698
  if (index) {
18,968✔
699
    if (colField[*index].type != kv->type) {
3,923✔
700
      snprintf(info->msgBuf.buf, info->msgBuf.len, "SML:0x%" PRIx64 ", %s point type and db type mismatch, db type:%s, point type:%s, key:%s",
14✔
701
               info->id, __FUNCTION__, tDataTypes[colField[*index].type].name, tDataTypes[kv->type].name, kv->key);
14✔
702
      uError("%s", info->msgBuf.buf);
14!
703
      return TSDB_CODE_SML_INVALID_DATA;
14✔
704
    }
705

706
    if (((colField[*index].type == TSDB_DATA_TYPE_VARCHAR || colField[*index].type == TSDB_DATA_TYPE_VARBINARY ||
3,909!
707
          colField[*index].type == TSDB_DATA_TYPE_GEOMETRY) &&
3,512✔
708
         (colField[*index].bytes - VARSTR_HEADER_SIZE) < kv->length) ||
403✔
709
        (colField[*index].type == TSDB_DATA_TYPE_NCHAR &&
3,893✔
710
         ((colField[*index].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE < kv->length))) {
1,832✔
711
      if (isTag) {
84✔
712
        *action = SCHEMA_ACTION_CHANGE_TAG_SIZE;
68✔
713
      } else {
714
        *action = SCHEMA_ACTION_CHANGE_COLUMN_SIZE;
16✔
715
      }
716
    }
717
  } else {
718
    if (isTag) {
15,045✔
719
      *action = SCHEMA_ACTION_ADD_TAG;
6,972✔
720
    } else {
721
      *action = SCHEMA_ACTION_ADD_COLUMN;
8,073✔
722
    }
723
  }
724
  return TSDB_CODE_SUCCESS;
18,954✔
725
}
726

727
#define BOUNDARY 1024
728
static int32_t smlFindNearestPowerOf2(int32_t length, uint8_t type) {
4,965✔
729
  int32_t result = 1;
4,965✔
730
  if (length >= BOUNDARY) {
4,965✔
731
    result = length;
33✔
732
  } else {
733
    while (result <= length) {
23,106✔
734
      result <<= 1;
18,174✔
735
    }
736
  }
737

738
  if ((type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_GEOMETRY) &&
4,965✔
739
      result > TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE) {
1,016!
740
    result = TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE;
×
741
  } else if (type == TSDB_DATA_TYPE_NCHAR && result > (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
4,965!
742
    result = (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
743
  }
744

745
  if (type == TSDB_DATA_TYPE_NCHAR) {
4,965✔
746
    result = result * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
3,949✔
747
  } else if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_GEOMETRY) {
1,016!
748
    result = result + VARSTR_HEADER_SIZE;
1,020✔
749
  }
750
  return result;
4,965✔
751
}
752

753
static int32_t smlProcessSchemaAction(SSmlHandle *info, SSchema *schemaField, SHashObj *schemaHash, SArray *cols,
724✔
754
                                      SHashObj *schemaHashCheck, ESchemaAction *action, bool isTag) {
755
  int32_t code = TSDB_CODE_SUCCESS;
724✔
756
  int32_t lino = 0;
724✔
757
  for (int j = 0; j < taosArrayGetSize(cols); ++j) {
4,562✔
758
    if (j == 0 && !isTag) continue;
3,861✔
759
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, j);
3,510✔
760
    SML_CHECK_NULL(kv);
3,509!
761
    SML_CHECK_CODE(smlGenerateSchemaAction(schemaField, schemaHash, kv, isTag, action, info));
3,509✔
762
    if (taosHashGet(schemaHashCheck, kv->key, kv->keyLen) != NULL) {
3,499✔
763
      SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
13!
764
    }
765
  }
766

767
END:
697✔
768
  RETURN
724!
769
}
770

771
static int32_t smlCheckMeta(SSchema *schema, int32_t length, SArray *cols) {
681✔
772
  int32_t   code = TSDB_CODE_SUCCESS;
681✔
773
  int32_t   lino = 0;
681✔
774
  SHashObj *hashTmp = taosHashInit(length, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
681✔
775
  SML_CHECK_NULL(hashTmp);
680!
776
  for (int32_t i = 0; i < length; i++) {
4,530✔
777
    SML_CHECK_CODE(taosHashPut(hashTmp, schema[i].name, strlen(schema[i].name), &schema[i], sizeof(SSchema)));
3,849!
778
  }
779
  for (int32_t i = 0; i < taosArrayGetSize(cols); i++) {
4,469✔
780
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
3,787✔
781
    SML_CHECK_NULL(kv);
3,788!
782
    SSchema *sTmp = taosHashGet(hashTmp, kv->key, kv->keyLen);
3,788✔
783
    if (sTmp == NULL) {
3,789!
784
      SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
785
    }
786
    if ((kv->type == TSDB_DATA_TYPE_VARCHAR && kv->length + VARSTR_HEADER_SIZE > sTmp->bytes) ||
3,789!
787
        (kv->type == TSDB_DATA_TYPE_NCHAR && kv->length * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE > sTmp->bytes)) {
3,789✔
788
      uError("column %s (type %s) bytes invalid. db bytes:%d, kv bytes:%zu", sTmp->name,
1!
789
             tDataTypes[sTmp->type].name, sTmp->bytes, kv->length);
790
      SML_CHECK_CODE(TSDB_CODE_MND_INVALID_SCHEMA_VER);
1!
791
    }
792
  }
793

794
END:
678✔
795
  taosHashCleanup(hashTmp);
679✔
796
  RETURN
681!
797
}
798

799
static int32_t getBytes(uint8_t type, int32_t length) {
14,982✔
800
  if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_NCHAR ||
14,982✔
801
      type == TSDB_DATA_TYPE_GEOMETRY) {
802
    return smlFindNearestPowerOf2(length, type);
4,955✔
803
  } else {
804
    return tDataTypes[type].bytes;
10,027✔
805
  }
806
}
807

808
static int32_t smlBuildFieldsList(SSmlHandle *info, SSchema *schemaField, SHashObj *schemaHash, SArray *cols,
1,961✔
809
                                  SArray *results, int32_t numOfCols, bool isTag) {
810
  int32_t code = TSDB_CODE_SUCCESS;
1,961✔
811
  int32_t lino = 0;
1,961✔
812
  for (int j = 0; j < taosArrayGetSize(cols); ++j) {
17,427✔
813
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, j);
15,460✔
814
    SML_CHECK_NULL(kv);
15,454!
815
    ESchemaAction action = SCHEMA_ACTION_NULL;
15,454✔
816
    SML_CHECK_CODE(smlGenerateSchemaAction(schemaField, schemaHash, kv, isTag, &action, info));
15,454!
817
    if (action == SCHEMA_ACTION_ADD_COLUMN || action == SCHEMA_ACTION_ADD_TAG) {
30,407✔
818
      SField field = {0};
14,939✔
819
      field.type = kv->type;
14,939✔
820
      field.bytes = getBytes(kv->type, kv->length);
14,939✔
821
      (void)memcpy(field.name, kv->key, TMIN(kv->keyLen, sizeof(field.name) - 1));
14,942✔
822
      SML_CHECK_NULL(taosArrayPush(results, &field));
14,954!
823
    } else if (action == SCHEMA_ACTION_CHANGE_COLUMN_SIZE || action == SCHEMA_ACTION_CHANGE_TAG_SIZE) {
514✔
824
      uint16_t *index = (uint16_t *)taosHashGet(schemaHash, kv->key, kv->keyLen);
44✔
825
      if (index == NULL) {
42!
UNCOV
826
        SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
827
      }
828
      uint16_t newIndex = *index;
42✔
829
      if (isTag) newIndex -= numOfCols;
42✔
830
      SField *field = (SField *)taosArrayGet(results, newIndex);
42✔
831
      SML_CHECK_NULL(field);
42!
832
      field->bytes = getBytes(kv->type, kv->length);
42✔
833
    }
834
  }
835

836
  int32_t maxLen = isTag ? TSDB_MAX_TAGS_LEN : TSDB_MAX_BYTES_PER_ROW;
1,961✔
837
  int32_t len = 0;
1,961✔
838
  for (int j = 0; j < taosArrayGetSize(results); ++j) {
17,456✔
839
    SField *field = taosArrayGet(results, j);
15,493✔
840
    SML_CHECK_NULL(field);
15,495!
841
    len += field->bytes;
15,495✔
842
  }
843
  if (len > maxLen) {
1,960✔
844
    return isTag ? TSDB_CODE_PAR_INVALID_TAGS_LENGTH : TSDB_CODE_PAR_INVALID_ROW_LENGTH;
12✔
845
  }
846

847
END:
1,948✔
848
  RETURN
1,948!
849
}
850

851
static FORCE_INLINE void smlBuildCreateStbReq(SMCreateStbReq *pReq, int32_t colVer, int32_t tagVer, tb_uid_t suid, int8_t source){
852
  pReq->colVer = colVer;
1,009✔
853
  pReq->tagVer = tagVer;
1,009✔
854
  pReq->suid = suid;
1,009✔
855
  pReq->source = source;
1,009✔
856
}
1,009✔
857
static int32_t smlSendMetaMsg(SSmlHandle *info, SName *pName, SArray *pColumns, SArray **pTags, STableMeta *pTableMeta,
1,010✔
858
                              ESchemaAction action) {
859
  SRequestObj   *pRequest = NULL;
1,010✔
860
  SMCreateStbReq pReq = {0};
1,010✔
861
  int32_t        code = TSDB_CODE_SUCCESS;
1,010✔
862
  int32_t        lino = 0;
1,010✔
863
  SCmdMsgInfo    pCmdMsg = {0};
1,010✔
864
  char          *pSql = NULL;
1,010✔
865

866
  // put front for free
867
  pReq.numOfColumns = taosArrayGetSize(pColumns);
1,010✔
868
  pReq.pTags = *pTags;
1,010✔
869
  pReq.numOfTags = taosArrayGetSize(*pTags);
1,010✔
870
  *pTags = NULL;
1,010✔
871

872
  pReq.pColumns = taosArrayInit(pReq.numOfColumns, sizeof(SFieldWithOptions));
1,010✔
873
  SML_CHECK_NULL(pReq.pColumns);
1,010!
874
  for (int32_t i = 0; i < pReq.numOfColumns; ++i) {
9,406✔
875
    SField *pField = taosArrayGet(pColumns, i);
8,397✔
876
    SML_CHECK_NULL(pField);
8,395!
877
    SFieldWithOptions fieldWithOption = {0};
8,395✔
878
    setFieldWithOptions(&fieldWithOption, pField);
8,395✔
879
    setDefaultOptionsForField(&fieldWithOption);
8,396✔
880
    SML_CHECK_NULL(taosArrayPush(pReq.pColumns, &fieldWithOption));
16,787!
881
  }
882

883
  if (action == SCHEMA_ACTION_CREATE_STABLE) {
1,009✔
884
    pSql = "sml_create_stable";
932✔
885
    smlBuildCreateStbReq(&pReq, 1, 1, 0, TD_REQ_FROM_APP);
886
  } else if (action == SCHEMA_ACTION_ADD_TAG || action == SCHEMA_ACTION_CHANGE_TAG_SIZE) {
77✔
887
    pSql = (action == SCHEMA_ACTION_ADD_TAG) ? "sml_add_tag" : "sml_modify_tag_size";
44✔
888
    smlBuildCreateStbReq(&pReq, pTableMeta->sversion, pTableMeta->tversion + 1, pTableMeta->uid, TD_REQ_FROM_TAOX);
44✔
889
  } else if (action == SCHEMA_ACTION_ADD_COLUMN || action == SCHEMA_ACTION_CHANGE_COLUMN_SIZE) {
33!
890
    pSql = (action == SCHEMA_ACTION_ADD_COLUMN) ? "sml_add_column" : "sml_modify_column_size";
33✔
891
    smlBuildCreateStbReq(&pReq, pTableMeta->sversion + 1, pTableMeta->tversion, pTableMeta->uid, TD_REQ_FROM_TAOX);
33✔
892
  } else {
UNCOV
893
    SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
894
  }
895

896
  SML_CHECK_CODE(buildRequest(info->taos->id, pSql, strlen(pSql), NULL, false, &pRequest, 0));
1,009!
897

898
  pRequest->syncQuery = true;
1,010✔
899
  if (!pRequest->pDb) {
1,010!
UNCOV
900
    SML_CHECK_CODE(TSDB_CODE_PAR_DB_NOT_SPECIFIED);
×
901
  }
902

903
  if (pReq.numOfTags == 0) {
1,010✔
904
    pReq.numOfTags = 1;
6✔
905
    SField field = {0};
6✔
906
    field.type = TSDB_DATA_TYPE_NCHAR;
6✔
907
    field.bytes = TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
6✔
908
    tstrncpy(field.name, tsSmlTagName, sizeof(field.name));
6✔
909
    SML_CHECK_NULL(taosArrayPush(pReq.pTags, &field));
12!
910
  }
911

912
  pReq.commentLen = -1;
1,010✔
913
  pReq.igExists = true;
1,010✔
914
  SML_CHECK_CODE(tNameExtractFullName(pName, pReq.name));
1,010!
915

916
  pCmdMsg.epSet = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
1,010✔
917
  pCmdMsg.msgType = TDMT_MND_CREATE_STB;
1,010✔
918
  pCmdMsg.msgLen = tSerializeSMCreateStbReq(NULL, 0, &pReq);
1,010✔
919
  if (pCmdMsg.msgLen < 0) {
1,008!
UNCOV
920
    uError("failed to serialize create stable request1, code:%d, terrno:%d", pCmdMsg.msgLen, terrno);
×
921
    SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
922
  }
923
  pCmdMsg.pMsg = taosMemoryMalloc(pCmdMsg.msgLen);
1,008!
924
  SML_CHECK_NULL(pCmdMsg.pMsg);
1,010!
925
  code = tSerializeSMCreateStbReq(pCmdMsg.pMsg, pCmdMsg.msgLen, &pReq);
1,010✔
926
  if (code < 0) {
1,008!
UNCOV
927
    taosMemoryFree(pCmdMsg.pMsg);
×
928
    uError("failed to serialize create stable request2, code:%d, terrno:%d", code, terrno);
×
929
    SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
930
  }
931

932
  SQuery pQuery = {0};
1,008✔
933
  pQuery.execMode = QUERY_EXEC_MODE_RPC;
1,008✔
934
  pQuery.pCmdMsg = &pCmdMsg;
1,008✔
935
  pQuery.msgType = pQuery.pCmdMsg->msgType;
1,008✔
936
  pQuery.stableQuery = true;
1,008✔
937

938
  launchQueryImpl(pRequest, &pQuery, true, NULL);  // no need to check return value
1,008✔
939

940
  if (pRequest->code == TSDB_CODE_SUCCESS) {
1,010✔
941
    SML_CHECK_CODE(catalogRemoveTableMeta(info->pCatalog, pName));
875!
942
  }
943
  code = pRequest->code;
1,010✔
944

945
END:
1,010✔
946
  destroyRequest(pRequest);
1,010✔
947
  tFreeSMCreateStbReq(&pReq);
1,010✔
948
  RETURN
1,010!
949
}
950

951
static int32_t smlCreateTable(SSmlHandle *info, SRequestConnInfo *conn, SSmlSTableMeta *sTableData,
945✔
952
                              SName *pName, STableMeta **pTableMeta){
953
  int32_t code = 0;
945✔
954
  int32_t lino = 0;
945✔
955
  SArray *pColumns = NULL;
945✔
956
  SArray *pTags = NULL;
945✔
957
  SML_CHECK_CODE(smlCheckAuth(info, conn, NULL, AUTH_TYPE_WRITE));
945✔
958
  uDebug("SML:0x%" PRIx64 ", %s create table:%s", info->id, __FUNCTION__, pName->tname);
939!
959
  pColumns = taosArrayInit(taosArrayGetSize(sTableData->cols), sizeof(SField));
939✔
960
  SML_CHECK_NULL(pColumns);
939!
961
  pTags = taosArrayInit(taosArrayGetSize(sTableData->tags), sizeof(SField));
939✔
962
  SML_CHECK_NULL(pTags);
939!
963
  SML_CHECK_CODE(smlBuildFieldsList(info, NULL, NULL, sTableData->tags, pTags, 0, true));
939!
964
  SML_CHECK_CODE(smlBuildFieldsList(info, NULL, NULL, sTableData->cols, pColumns, 0, false));
939✔
965
  SML_CHECK_CODE(smlSendMetaMsg(info, pName, pColumns, &pTags, NULL, SCHEMA_ACTION_CREATE_STABLE));
933✔
966
  info->cost.numOfCreateSTables++;
798✔
967
  taosMemoryFreeClear(*pTableMeta);
798!
968

969
  SML_CHECK_CODE(catalogGetSTableMeta(info->pCatalog, conn, pName, pTableMeta));
798!
970

971
END:
798✔
972
  taosArrayDestroy(pColumns);
945✔
973
  taosArrayDestroy(pTags);
945✔
974
  RETURN
945!
975
}
976

977
static int32_t smlBuildFields(SArray **pColumns, SArray **pTags, STableMeta *pTableMeta, SSmlSTableMeta *sTableData){
83✔
978
  int32_t code = 0;
83✔
979
  int32_t lino = 0;
83✔
980
  *pColumns = taosArrayInit(taosArrayGetSize(sTableData->cols) + (pTableMeta)->tableInfo.numOfColumns, sizeof(SField));
83✔
981
  SML_CHECK_NULL(pColumns);
83!
982
  *pTags = taosArrayInit(taosArrayGetSize(sTableData->tags) + (pTableMeta)->tableInfo.numOfTags, sizeof(SField));
83✔
983
  SML_CHECK_NULL(pTags);
83!
984
  for (uint16_t i = 0; i < (pTableMeta)->tableInfo.numOfColumns + (pTableMeta)->tableInfo.numOfTags; i++) {
1,013✔
985
    SField field = {0};
930✔
986
    field.type = (pTableMeta)->schema[i].type;
930✔
987
    field.bytes = (pTableMeta)->schema[i].bytes;
930✔
988
    tstrncpy(field.name, (pTableMeta)->schema[i].name, sizeof(field.name));
930✔
989
    if (i < (pTableMeta)->tableInfo.numOfColumns) {
930✔
990
      SML_CHECK_NULL(taosArrayPush(*pColumns, &field));
868!
991
    } else {
992
      SML_CHECK_NULL(taosArrayPush(*pTags, &field));
992!
993
    }
994
  }
995
END:
83✔
996
  RETURN
83!
997
}
998
static int32_t smlModifyTag(SSmlHandle *info, SHashObj* hashTmpCheck, SHashObj* hashTmp, SRequestConnInfo *conn,
374✔
999
                            SSmlSTableMeta *sTableData, SName *pName, STableMeta **pTableMeta){
1000
  ESchemaAction action = SCHEMA_ACTION_NULL;
374✔
1001
  SArray *pColumns = NULL;
374✔
1002
  SArray *pTags = NULL;
374✔
1003
  int32_t code = 0;
374✔
1004
  int32_t lino = 0;
374✔
1005
  SML_CHECK_CODE(smlProcessSchemaAction(info, (*pTableMeta)->schema, hashTmp, sTableData->tags, hashTmpCheck, &action, true));
374✔
1006

1007
  if (action != SCHEMA_ACTION_NULL) {
355✔
1008
    SML_CHECK_CODE(smlCheckAuth(info, conn, pName->tname, AUTH_TYPE_WRITE));
48!
1009
    uDebug("SML:0x%" PRIx64 ", %s change table tag, table:%s, action:%d", info->id, __FUNCTION__, pName->tname,
48!
1010
           action);
1011
    SML_CHECK_CODE(smlBuildFields(&pColumns, &pTags, *pTableMeta, sTableData));
48!
1012
    SML_CHECK_CODE(smlBuildFieldsList(info, (*pTableMeta)->schema, hashTmp, sTableData->tags, pTags,
48✔
1013
                              (*pTableMeta)->tableInfo.numOfColumns, true));
1014

1015
    SML_CHECK_CODE(smlSendMetaMsg(info, pName, pColumns, &pTags, (*pTableMeta), action));
44!
1016

1017
    info->cost.numOfAlterTagSTables++;
44✔
1018
    taosMemoryFreeClear(*pTableMeta);
44!
1019
    SML_CHECK_CODE(catalogRefreshTableMeta(info->pCatalog, conn, pName, -1));
44!
1020
    SML_CHECK_CODE(catalogGetSTableMeta(info->pCatalog, conn, pName, pTableMeta));
44!
1021
  }
1022

1023
END:
351✔
1024
  taosArrayDestroy(pColumns);
374✔
1025
  taosArrayDestroy(pTags);
373✔
1026
  RETURN
374!
1027
}
1028

1029
static int32_t smlModifyCols(SSmlHandle *info, SHashObj* hashTmpCheck, SHashObj* hashTmp, SRequestConnInfo *conn,
351✔
1030
                            SSmlSTableMeta *sTableData, SName *pName, STableMeta **pTableMeta){
1031
  ESchemaAction action = SCHEMA_ACTION_NULL;
351✔
1032
  SArray *pColumns = NULL;
351✔
1033
  SArray *pTags = NULL;
351✔
1034
  int32_t code = 0;
351✔
1035
  int32_t lino = 0;
351✔
1036
  SML_CHECK_CODE(smlProcessSchemaAction(info, (*pTableMeta)->schema, hashTmp, sTableData->cols, hashTmpCheck, &action, false));
351✔
1037

1038
  if (action != SCHEMA_ACTION_NULL) {
343✔
1039
    SML_CHECK_CODE(smlCheckAuth(info, conn, pName->tname, AUTH_TYPE_WRITE));
35!
1040
    uDebug("SML:0x%" PRIx64 ", %s change table col, table:%s, action:%d", info->id, __FUNCTION__, pName->tname,
35!
1041
           action);
1042
    SML_CHECK_CODE(smlBuildFields(&pColumns, &pTags, *pTableMeta, sTableData));
35!
1043
    SML_CHECK_CODE(smlBuildFieldsList(info, (*pTableMeta)->schema, hashTmp, sTableData->cols, pColumns,
35✔
1044
                                      (*pTableMeta)->tableInfo.numOfColumns, false));
1045

1046
    SML_CHECK_CODE(smlSendMetaMsg(info, pName, pColumns, &pTags, (*pTableMeta), action));
33!
1047

1048
    info->cost.numOfAlterColSTables++;
33✔
1049
    taosMemoryFreeClear(*pTableMeta);
33!
1050
    SML_CHECK_CODE(catalogRefreshTableMeta(info->pCatalog, conn, pName, -1));
33!
1051
    SML_CHECK_CODE(catalogGetSTableMeta(info->pCatalog, conn, pName, pTableMeta));
33!
1052
  }
1053

1054
END:
341✔
1055
  taosArrayDestroy(pColumns);
351✔
1056
  taosArrayDestroy(pTags);
349✔
1057
  RETURN
351!
1058
}
1059

1060
static int32_t smlBuildTempHash(SHashObj *hashTmp, STableMeta *pTableMeta, uint16_t start, uint16_t end){
748✔
1061
  int32_t code = 0;
748✔
1062
  int32_t lino = 0;
748✔
1063
  for (uint16_t i = start; i < end; i++) {
4,669✔
1064
    SML_CHECK_CODE(taosHashPut(hashTmp, pTableMeta->schema[i].name, strlen(pTableMeta->schema[i].name), &i, SHORT_BYTES));
3,920!
1065
  }
1066

1067
END:
749✔
1068
  return code;
749✔
1069
}
1070

1071
static int32_t smlModifyDBSchemas(SSmlHandle *info) {
1,799✔
1072
  uDebug("SML:0x%" PRIx64 ", %s start, format:%d, needModifySchema:%d", info->id, __FUNCTION__, info->dataFormat,
1,799!
1073
         info->needModifySchema);
1074
  if (info->dataFormat && !info->needModifySchema) {
1,799✔
1075
    return TSDB_CODE_SUCCESS;
543✔
1076
  }
1077
  int32_t     code = 0;
1,256✔
1078
  int32_t     lino = 0;
1,256✔
1079
  SHashObj   *colHashTmp = NULL;
1,256✔
1080
  SHashObj   *tagHashTmp = NULL;
1,256✔
1081
  STableMeta *pTableMeta = NULL;
1,256✔
1082

1083
  SName pName = {TSDB_TABLE_NAME_T, info->taos->acctId, {0}, {0}};
1,256✔
1084
  tstrncpy(pName.dbname, info->pRequest->pDb, sizeof(pName.dbname));
1,256✔
1085

1086
  SRequestConnInfo conn = {0};
1,256✔
1087
  conn.pTrans = info->taos->pAppInfo->pTransporter;
1,256✔
1088
  conn.requestId = info->pRequest->requestId;
1,256✔
1089
  conn.requestObjRefId = info->pRequest->self;
1,256✔
1090
  conn.mgmtEps = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
1,256✔
1091

1092
  SSmlSTableMeta **tmp = (SSmlSTableMeta **)taosHashIterate(info->superTables, NULL);
1,257✔
1093
  while (tmp) {
2,392✔
1094
    SSmlSTableMeta *sTableData = *tmp;
1,316✔
1095
    bool            needCheckMeta = false;  // for multi thread
1,316✔
1096

1097
    size_t superTableLen = 0;
1,316✔
1098
    void  *superTable = taosHashGetKey(tmp, &superTableLen);
1,316✔
1099
    char  *measure = taosMemoryMalloc(superTableLen);
1,318!
1100
    SML_CHECK_NULL(measure);
1,499!
1101
    (void)memcpy(measure, superTable, superTableLen);
1,318✔
1102
    if (info->protocol == TSDB_SML_LINE_PROTOCOL){
1,318✔
1103
      PROCESS_SLASH_IN_MEASUREMENT(measure, superTableLen);
3,593✔
1104
    }
1105
    smlStrReplace(measure, superTableLen);
1,318✔
1106
    size_t nameLen = TMIN(superTableLen, TSDB_TABLE_NAME_LEN - 1);
1,319✔
1107
    (void)memcpy(pName.tname, measure, nameLen);
1,319✔
1108
    pName.tname[nameLen] = '\0';
1,319✔
1109
    taosMemoryFree(measure);
1,319!
1110

1111
    code = catalogGetSTableMeta(info->pCatalog, &conn, &pName, &pTableMeta);
1,318✔
1112

1113
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_MND_STB_NOT_EXIST) {
1,317!
1114
      SML_CHECK_CODE(smlCreateTable(info, &conn, sTableData, &pName, &pTableMeta));
944✔
1115
    } else if (code == TSDB_CODE_SUCCESS) {
373✔
1116
      if (smlIsPKTable(pTableMeta)) {
372!
UNCOV
1117
        SML_CHECK_CODE(TSDB_CODE_SML_NOT_SUPPORT_PK);
×
1118
      }
1119

1120
      colHashTmp = taosHashInit(pTableMeta->tableInfo.numOfColumns, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
373✔
1121
      tagHashTmp = taosHashInit(pTableMeta->tableInfo.numOfTags, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
374✔
1122
      SML_CHECK_NULL(colHashTmp);
374!
1123
      SML_CHECK_NULL(tagHashTmp);
374!
1124
      SML_CHECK_CODE(smlBuildTempHash(tagHashTmp, pTableMeta, pTableMeta->tableInfo.numOfColumns, pTableMeta->tableInfo.numOfColumns + pTableMeta->tableInfo.numOfTags));
374!
1125
      SML_CHECK_CODE(smlBuildTempHash(colHashTmp, pTableMeta, 0, pTableMeta->tableInfo.numOfColumns));
374!
1126

1127
      SML_CHECK_CODE(smlModifyTag(info, colHashTmp, tagHashTmp, &conn, sTableData, &pName, &pTableMeta));
374✔
1128
      SML_CHECK_CODE(smlModifyCols(info, tagHashTmp, colHashTmp, &conn, sTableData, &pName, &pTableMeta));
350✔
1129

1130
      needCheckMeta = true;
341✔
1131
      taosHashCleanup(colHashTmp);
341✔
1132
      taosHashCleanup(tagHashTmp);
341✔
1133
      colHashTmp = NULL;
341✔
1134
      tagHashTmp = NULL;
341✔
1135
    } else {
1136
      uError("SML:0x%" PRIx64 ", %s load table meta error:%s", info->id, __FUNCTION__, tstrerror(code));
1!
1137
      goto END;
×
1138
    }
1139

1140
    if (needCheckMeta) {
1,139✔
1141
      SML_CHECK_CODE(smlCheckMeta(&(pTableMeta->schema[pTableMeta->tableInfo.numOfColumns]), pTableMeta->tableInfo.numOfTags, sTableData->tags));
341✔
1142
      SML_CHECK_CODE(smlCheckMeta(&(pTableMeta->schema[0]), pTableMeta->tableInfo.numOfColumns, sTableData->cols));
340!
1143
    }
1144

1145
    taosMemoryFreeClear(sTableData->tableMeta);
1,138!
1146
    sTableData->tableMeta = pTableMeta;
1,138✔
1147
    uDebug("SML:0x%" PRIx64 ", %s modify schema uid:%" PRIu64 ", sversion:%d, tversion:%d", info->id, __FUNCTION__, pTableMeta->uid,
1,138!
1148
           pTableMeta->sversion, pTableMeta->tversion);
1149
    tmp = (SSmlSTableMeta **)taosHashIterate(info->superTables, tmp);
1,138✔
1150
  }
1151
  uDebug("SML:0x%" PRIx64 ", %s end success, format:%d, needModifySchema:%d", info->id, __FUNCTION__, info->dataFormat,
1,076!
1152
         info->needModifySchema);
1153

1154
  return TSDB_CODE_SUCCESS;
1,076✔
1155

1156
END:
181✔
1157
  taosHashCancelIterate(info->superTables, tmp);
181✔
1158
  taosHashCleanup(colHashTmp);
181✔
1159
  taosHashCleanup(tagHashTmp);
181✔
1160
  taosMemoryFreeClear(pTableMeta);
181!
1161
  (void)catalogRefreshTableMeta(info->pCatalog, &conn, &pName, 1);  // ignore refresh meta code if there is an error
181✔
1162
  uError("SML:0x%" PRIx64 ", %s end failed:%d:%s, format:%d, needModifySchema:%d", info->id, __FUNCTION__, code,
181!
1163
         tstrerror(code), info->dataFormat, info->needModifySchema);
1164

1165
  return code;
181✔
1166
}
1167

1168
static int32_t smlInsertMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, SHashObj *checkDuplicate) {
2,404✔
1169
  int32_t code = 0;
2,404✔
1170
  int32_t lino = 0;
2,404✔
1171
  terrno = 0;
2,404✔
1172
  for (int16_t i = 0; i < taosArrayGetSize(cols); ++i) {
19,597✔
1173
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
17,216✔
1174
    SML_CHECK_NULL(kv);
17,238!
1175
    int ret = taosHashPut(metaHash, kv->key, kv->keyLen, &i, SHORT_BYTES);
17,219✔
1176
    if (ret == 0) {
17,237✔
1177
      SML_CHECK_NULL(taosArrayPush(metaArray, kv));
17,219!
1178
      if (taosHashGet(checkDuplicate, kv->key, kv->keyLen) != NULL) {
17,219✔
1179
        SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
19!
1180
      }
1181
    } else if (terrno == TSDB_CODE_DUP_KEY) {
12!
1182
      return TSDB_CODE_PAR_DUPLICATED_COLUMN;
12✔
1183
    }
1184
  }
1185

1186
END:
2,392✔
1187
  RETURN
2,392!
1188
}
1189

1190
static int32_t smlUpdateMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, bool isTag, SSmlMsgBuf *msg,
170,485✔
1191
                             SHashObj *checkDuplicate) {
1192
  int32_t code = 0;
170,485✔
1193
  int32_t lino = 0;
170,485✔
1194
  for (int i = 0; i < taosArrayGetSize(cols); ++i) {
674,507✔
1195
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
438,611✔
1196
    SML_CHECK_NULL(kv);
426,123!
1197
    int16_t *index = (int16_t *)taosHashGet(metaHash, kv->key, kv->keyLen);
426,123✔
1198
    if (index) {
463,877!
1199
      SSmlKv *value = (SSmlKv *)taosArrayGet(metaArray, *index);
463,882✔
1200
      SML_CHECK_NULL(value);
452,105!
1201

1202
      if (isTag) {
503,929✔
1203
        if (kv->length > value->length) {
190,300✔
1204
          value->length = kv->length;
100✔
1205
        }
1206
        continue;
190,300✔
1207
      }
1208
      if (kv->type != value->type) {
313,629!
UNCOV
1209
        smlBuildInvalidDataMsg(msg, "the type is not the same like before", kv->key);
×
1210
        SML_CHECK_CODE(TSDB_CODE_SML_NOT_SAME_TYPE);
×
1211
      }
1212

1213
      if (IS_VAR_DATA_TYPE(kv->type) && (kv->length > value->length)) {  // update string len, if bigger
313,629!
1214
        value->length = kv->length;
116✔
1215
      }
1216
    } else {
UNCOV
1217
      size_t tmp = taosArrayGetSize(metaArray);
×
1218
      if (tmp > INT16_MAX) {
93!
UNCOV
1219
        smlBuildInvalidDataMsg(msg, "too many cols or tags", kv->key);
×
1220
        SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
1221
      }
1222
      int16_t size = tmp;
93✔
1223
      SML_CHECK_CODE(taosHashPut(metaHash, kv->key, kv->keyLen, &size, SHORT_BYTES));
93!
1224
      SML_CHECK_NULL(taosArrayPush(metaArray, kv));
93!
1225
      if (taosHashGet(checkDuplicate, kv->key, kv->keyLen) != NULL) {
93!
UNCOV
1226
        SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
×
1227
      }
1228
    }
1229
  }
1230

1231
END:
119,281✔
1232
  RETURN
119,281!
1233
}
1234

1235
void smlDestroyTableInfo(void *para) {
2,851✔
1236
  SSmlTableInfo *tag = *(SSmlTableInfo **)para;
2,851✔
1237
  for (size_t i = 0; i < taosArrayGetSize(tag->cols); i++) {
104,694✔
1238
    SHashObj *kvHash = (SHashObj *)taosArrayGetP(tag->cols, i);
100,739✔
1239
    taosHashCleanup(kvHash);
97,678✔
1240
  }
1241

1242
  taosArrayDestroy(tag->cols);
2,713✔
1243
  taosArrayDestroyEx(tag->tags, freeSSmlKv);
2,852✔
1244
  taosMemoryFree(tag);
2,853!
1245
}
2,853✔
1246

1247
void freeSSmlKv(void *data) {
326,696✔
1248
  SSmlKv *kv = (SSmlKv *)data;
326,696✔
1249
  if (kv->keyEscaped) taosMemoryFreeClear(kv->key);
326,696!
1250
  if (kv->valueEscaped) taosMemoryFreeClear(kv->value);
326,696!
1251
#ifdef USE_GEOS
1252
  if (kv->type == TSDB_DATA_TYPE_GEOMETRY) geosFreeBuffer((void *)(kv->value));
326,696✔
1253
#endif
1254
  if (kv->type == TSDB_DATA_TYPE_VARBINARY) taosMemoryFreeClear(kv->value);
326,696!
1255
}
326,696✔
1256

1257
void smlDestroyInfo(SSmlHandle *info) {
3,823✔
1258
  if (info == NULL) return;
3,823✔
1259

1260
  taosHashCleanup(info->pVgHash);
1,914✔
1261
  taosHashCleanup(info->childTables);
1,915✔
1262
  taosHashCleanup(info->superTables);
1,915✔
1263
  taosHashCleanup(info->tableUids);
1,915✔
1264

1265
  for (int i = 0; i < taosArrayGetSize(info->tagJsonArray); i++) {
1,915!
UNCOV
1266
    cJSON *tags = (cJSON *)taosArrayGetP(info->tagJsonArray, i);
×
1267
    cJSON_Delete(tags);
×
1268
  }
1269
  taosArrayDestroy(info->tagJsonArray);
1,915✔
1270

1271
  for (int i = 0; i < taosArrayGetSize(info->valueJsonArray); i++) {
1,915!
UNCOV
1272
    cJSON *value = (cJSON *)taosArrayGetP(info->valueJsonArray, i);
×
1273
    cJSON_Delete(value);
×
1274
  }
1275
  taosArrayDestroy(info->valueJsonArray);
1,915✔
1276

1277
  taosArrayDestroyEx(info->preLineTagKV, freeSSmlKv);
1,915✔
1278
  taosArrayDestroyP(info->escapedStringList, NULL);
1,915✔
1279

1280
  if (!info->dataFormat) {
1,915✔
1281
    for (int i = 0; i < info->lineNum; i++) {
93,789✔
1282
      taosArrayDestroyEx(info->lines[i].colArray, freeSSmlKv);
92,551✔
1283
      if (info->lines[i].measureTagsLen != 0 && info->protocol != TSDB_SML_LINE_PROTOCOL) {
92,758✔
1284
        taosMemoryFree(info->lines[i].measureTag);
691!
1285
      }
1286
    }
1287
    taosMemoryFree(info->lines);
1,238!
1288
  }
1289
  if(info->protocol == TSDB_SML_JSON_PROTOCOL)  {
1,915✔
1290
    taosMemoryFreeClear(info->preLine.tags);
844!
1291
  }
1292
  cJSON_Delete(info->root);
1,915✔
1293
  taosMemoryFreeClear(info);
1,915!
1294
}
1295

1296
int32_t smlBuildSmlInfo(TAOS *taos, SSmlHandle **handle) {
1,902✔
1297
  int32_t     code = TSDB_CODE_SUCCESS;
1,902✔
1298
  int32_t     lino = 0;
1,902✔
1299
  SSmlHandle *info = (SSmlHandle *)taosMemoryCalloc(1, sizeof(SSmlHandle));
1,902!
1300
  SML_CHECK_NULL(info);
1,912!
1301
  if (taos != NULL){
1,912✔
1302
    info->taos = acquireTscObj(*(int64_t *)taos);
1,907✔
1303
    SML_CHECK_NULL(info->taos);
1,910!
1304
    SML_CHECK_CODE(catalogGetHandle(info->taos->pAppInfo->clusterId, &info->pCatalog));
1,910!
1305
  }
1306

1307
  info->pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
1,914✔
1308
  SML_CHECK_NULL(info->pVgHash);
1,914!
1309
  info->childTables = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,914✔
1310
  SML_CHECK_NULL(info->childTables);
1,914!
1311
  info->tableUids = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,914✔
1312
  SML_CHECK_NULL(info->tableUids);
1,915!
1313
  info->superTables = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,915✔
1314
  SML_CHECK_NULL(info->superTables);
1,915!
1315
  taosHashSetFreeFp(info->superTables, smlDestroySTableMeta);
1,915✔
1316
  taosHashSetFreeFp(info->childTables, smlDestroyTableInfo);
1,915✔
1317

1318
  info->id = smlGenId();
1,915✔
1319
  SML_CHECK_CODE(smlInitHandle(&info->pQuery));
1,915!
1320
  info->dataFormat = true;
1,914✔
1321
  info->tagJsonArray = taosArrayInit(8, POINTER_BYTES);
1,914✔
1322
  SML_CHECK_NULL(info->tagJsonArray);
1,915!
1323
  info->valueJsonArray = taosArrayInit(8, POINTER_BYTES);
1,915✔
1324
  SML_CHECK_NULL(info->valueJsonArray);
1,915!
1325
  info->preLineTagKV = taosArrayInit(8, sizeof(SSmlKv));
1,915✔
1326
  SML_CHECK_NULL(info->preLineTagKV);
1,915!
1327
  info->escapedStringList = taosArrayInit(8, POINTER_BYTES);
1,915✔
1328
  SML_CHECK_NULL(info->escapedStringList);
1,915!
1329

1330
  *handle = info;
1,915✔
1331
  info = NULL;
1,915✔
1332

1333
END:
1,915✔
1334
  smlDestroyInfo(info);
1,915✔
1335
  RETURN
1,910!
1336
}
1337

1338
static int32_t smlPushCols(SArray *colsArray, SArray *cols) {
93,506✔
1339
  int32_t code = TSDB_CODE_SUCCESS;
93,506✔
1340
  int32_t lino = 0;
93,506✔
1341
  SHashObj *kvHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
93,506✔
1342
  SML_CHECK_NULL(kvHash);
99,040!
1343
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
479,735✔
1344
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
360,936✔
1345
    SML_CHECK_NULL(kv);
350,152!
1346
    terrno = 0;
350,139✔
1347
    code = taosHashPut(kvHash, kv->key, kv->keyLen, &kv, POINTER_BYTES);
339,808✔
1348
    if (terrno == TSDB_CODE_DUP_KEY) {
389,904✔
1349
      SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
13!
1350
    }
1351
    SML_CHECK_CODE(code);
380,695!
1352
  }
1353

1354
  SML_CHECK_NULL(taosArrayPush(colsArray, &kvHash));
96,727!
1355
  return code;
96,727✔
1356
END:
13✔
1357
  taosHashCleanup(kvHash);
13✔
1358
  RETURN
13!
1359
}
1360

1361
static int32_t smlParseEnd(SSmlHandle *info) {
1,713✔
1362
  uDebug("SML:0x%" PRIx64 ", %s start, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
1,713!
1363
         info->lineNum);
1364
  int32_t code = 0;
1,713✔
1365
  int32_t lino = 0;
1,713✔
1366
  if (info->dataFormat) return TSDB_CODE_SUCCESS;
1,713✔
1367

1368
  for (int32_t i = 0; i < info->lineNum; i++) {
94,274✔
1369
    SSmlLineInfo  *elements = info->lines + i;
93,058✔
1370
    SSmlTableInfo *tinfo = NULL;
93,058✔
1371
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
93,058✔
1372
      SSmlTableInfo **tmp =
1373
          (SSmlTableInfo **)taosHashGet(info->childTables, elements->measure, elements->measureTagsLen);
90,868✔
1374
      if (tmp) tinfo = *tmp;
94,591!
1375
    } else {
1376
      SSmlTableInfo **tmp = (SSmlTableInfo **)taosHashGet(info->childTables, elements->measureTag,
2,190✔
1377
                                                          elements->measureLen + elements->tagsLen);
2,190✔
1378
      if (tmp) tinfo = *tmp;
2,193!
1379
    }
1380

1381
    if (tinfo == NULL) {
96,784!
UNCOV
1382
      uError("SML:0x%" PRIx64 ", get oneTable failed, line num:%d", info->id, i);
×
1383
      smlBuildInvalidDataMsg(&info->msgBuf, "get oneTable failed", elements->measure);
×
1384
      return TSDB_CODE_SML_INVALID_DATA;
×
1385
    }
1386

1387
    if (taosArrayGetSize(tinfo->tags) > TSDB_MAX_TAGS) {
96,784✔
1388
      smlBuildInvalidDataMsg(&info->msgBuf, "too many tags than 128", NULL);
4✔
1389
      return TSDB_CODE_PAR_INVALID_TAGS_NUM;
4✔
1390
    }
1391

1392
    if (taosArrayGetSize(elements->colArray) + taosArrayGetSize(tinfo->tags) > TSDB_MAX_COLUMNS) {
95,664✔
1393
      smlBuildInvalidDataMsg(&info->msgBuf, "too many columns than 4096", NULL);
1✔
1394
      return TSDB_CODE_PAR_TOO_MANY_COLUMNS;
1✔
1395
    }
1396

1397
    SML_CHECK_CODE(smlPushCols(tinfo->cols, elements->colArray));
93,564✔
1398

1399
    SSmlSTableMeta **tableMeta =
1400
        (SSmlSTableMeta **)taosHashGet(info->superTables, elements->measure, elements->measureLen);
96,654✔
1401
    if (tableMeta) {  // update meta
93,148✔
1402
      uDebug("SML:0x%" PRIx64 ", %s update meta, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
91,951!
1403
             info->lineNum);
1404
      SML_CHECK_CODE(smlUpdateMeta((*tableMeta)->colHash, (*tableMeta)->cols, elements->colArray, false, &info->msgBuf,
91,951!
1405
                          (*tableMeta)->tagHash));
1406
      SML_CHECK_CODE(smlUpdateMeta((*tableMeta)->tagHash, (*tableMeta)->tags, tinfo->tags, true, &info->msgBuf,
91,834!
1407
                          (*tableMeta)->colHash));
1408
    } else {
1409
      uDebug("SML:0x%" PRIx64 ", %s add meta, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
1,197!
1410
             info->lineNum);
1411
      SSmlSTableMeta *meta = NULL;
1,197✔
1412
      SML_CHECK_CODE(smlBuildSTableMeta(info->dataFormat, &meta));
1,228!
1413
      code = taosHashPut(info->superTables, elements->measure, elements->measureLen, &meta, POINTER_BYTES);
1,207✔
1414
      if (code != TSDB_CODE_SUCCESS) {
1,208!
UNCOV
1415
        smlDestroySTableMeta(&meta);
×
1416
        SML_CHECK_CODE(code);
×
1417
      }
1418
      SML_CHECK_CODE(smlInsertMeta(meta->tagHash, meta->tags, tinfo->tags, NULL));
1,208✔
1419
      SML_CHECK_CODE(smlInsertMeta(meta->colHash, meta->cols, elements->colArray, meta->tagHash));
1,196✔
1420
    }
1421
  }
1422
  uDebug("SML:0x%" PRIx64 ", %s end, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat, info->lineNum);
1,216!
1423

1424
END:
1,216✔
1425
  RETURN
1,260!
1426
}
1427

1428
static int32_t smlInsertData(SSmlHandle *info) {
1,617✔
1429
  int32_t         code      = TSDB_CODE_SUCCESS;
1,617✔
1430
  int32_t         lino      = 0;
1,617✔
1431
  char           *measure   = NULL;
1,617✔
1432
  SSmlTableInfo **oneTable  = NULL;
1,617✔
1433
  uDebug("SML:0x%" PRIx64 ", %s start, format:%d", info->id, __FUNCTION__, info->dataFormat);
1,617!
1434

1435
  if (info->pRequest->dbList == NULL) {
1,617!
1436
    info->pRequest->dbList = taosArrayInit(1, TSDB_DB_FNAME_LEN);
1,619✔
1437
    SML_CHECK_NULL(info->pRequest->dbList);
1,619!
1438
  }
1439
  char *data = (char *)taosArrayReserve(info->pRequest->dbList, 1);
1,617✔
1440
  SML_CHECK_NULL(data);
1,618!
1441
  SName pName = {TSDB_TABLE_NAME_T, info->taos->acctId, {0}, {0}};
1,618✔
1442
  tstrncpy(pName.dbname, info->pRequest->pDb, sizeof(pName.dbname));
1,618✔
1443
  (void)tNameGetFullDbName(&pName, data);  // ignore
1,618✔
1444

1445
  oneTable = (SSmlTableInfo **)taosHashIterate(info->childTables, NULL);
1,618✔
1446
  while (oneTable) {
4,139✔
1447
    SSmlTableInfo *tableData = *oneTable;
2,532✔
1448

1449
    int   measureLen = tableData->sTableNameLen;
2,532✔
1450
    measure = (char *)taosMemoryMalloc(tableData->sTableNameLen);
2,532!
1451
    SML_CHECK_NULL(measure);
2,543!
1452
    (void)memcpy(measure, tableData->sTableName, tableData->sTableNameLen);
2,531✔
1453
    if (info->protocol == TSDB_SML_LINE_PROTOCOL){
2,531✔
1454
      PROCESS_SLASH_IN_MEASUREMENT(measure, measureLen);
8,960✔
1455
    }
1456
    smlStrReplace(measure, measureLen);
2,531✔
1457
    (void)memcpy(pName.tname, measure, measureLen);
2,532✔
1458
    pName.tname[measureLen] = '\0';
2,532✔
1459

1460
    if (info->pRequest->tableList == NULL) {
2,532✔
1461
      info->pRequest->tableList = taosArrayInit(1, sizeof(SName));
1,619✔
1462
      SML_CHECK_NULL(info->pRequest->tableList);
1,617!
1463
    }
1464
    SML_CHECK_NULL(taosArrayPush(info->pRequest->tableList, &pName));
5,060!
1465
    tstrncpy(pName.tname, tableData->childTableName, sizeof(pName.tname));
2,530✔
1466

1467
    SRequestConnInfo conn = {0};
2,530✔
1468
    conn.pTrans = info->taos->pAppInfo->pTransporter;
2,530✔
1469
    conn.requestId = info->pRequest->requestId;
2,530✔
1470
    conn.requestObjRefId = info->pRequest->self;
2,530✔
1471
    conn.mgmtEps = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
2,530✔
1472

1473
    SML_CHECK_CODE(smlCheckAuth(info, &conn, pName.tname, AUTH_TYPE_WRITE));
2,532✔
1474

1475
    SVgroupInfo vg = {0};
2,518✔
1476
    SML_CHECK_CODE(catalogGetTableHashVgroup(info->pCatalog, &conn, &pName, &vg));
2,518!
1477
    SML_CHECK_CODE(taosHashPut(info->pVgHash, (const char *)&vg.vgId, sizeof(vg.vgId), (char *)&vg, sizeof(vg)));
2,520!
1478

1479
    SSmlSTableMeta **pMeta =
1480
        (SSmlSTableMeta **)taosHashGet(info->superTables, tableData->sTableName, tableData->sTableNameLen);
2,520✔
1481
    if (unlikely(NULL == pMeta || NULL == *pMeta || NULL == (*pMeta)->tableMeta)) {
2,519!
UNCOV
1482
      uError("SML:0x%" PRIx64 ", %s NULL == pMeta. table name:%s", info->id, __FUNCTION__, tableData->childTableName);
×
1483
      SML_CHECK_CODE(TSDB_CODE_SML_INTERNAL_ERROR);
×
1484
    }
1485

1486
    // use tablemeta of stable to save vgid and uid of child table
1487
    (*pMeta)->tableMeta->vgId = vg.vgId;
2,519✔
1488
    (*pMeta)->tableMeta->uid = tableData->uid;  // one table merge data block together according uid
2,519✔
1489
    uDebug("SML:0x%" PRIx64 ", %s table:%s, uid:%" PRIu64 ", format:%d", info->id, __FUNCTION__, pName.tname,
2,519!
1490
           tableData->uid, info->dataFormat);
1491

1492
    SML_CHECK_CODE(smlBindData(info->pQuery, info->dataFormat, tableData->tags, (*pMeta)->cols, tableData->cols,
2,519!
1493
                       (*pMeta)->tableMeta, tableData->childTableName, measure, measureLen, info->ttl, info->msgBuf.buf,
1494
                       info->msgBuf.len, info->taos->optionInfo.charsetCxt));
1495
    taosMemoryFreeClear(measure);
2,519!
1496
    oneTable = (SSmlTableInfo **)taosHashIterate(info->childTables, oneTable);
2,519✔
1497
  }
1498

1499
  SML_CHECK_CODE(smlBuildOutput(info->pQuery, info->pVgHash));
1,607!
1500
  info->cost.insertRpcTime = taosGetTimestampUs();
1,607✔
1501

1502
  SAppClusterSummary *pActivity = &info->taos->pAppInfo->summary;
1,607✔
1503
  (void)atomic_add_fetch_64((int64_t *)&pActivity->numOfInsertsReq, 1);  // no need to check return code
1,607✔
1504

1505
  launchQueryImpl(info->pRequest, info->pQuery, true, NULL);  // no need to check return code
1,607✔
1506

1507
  uDebug("SML:0x%" PRIx64 ", %s end, format:%d, code:%d,%s", info->id, __FUNCTION__, info->dataFormat, info->pRequest->code,
1,606!
1508
         tstrerror(info->pRequest->code));
1509

1510
  return info->pRequest->code;
1,607✔
1511

1512
END:
12✔
1513
  taosMemoryFree(measure);
12!
1514
  taosHashCancelIterate(info->childTables, oneTable);
12✔
1515
  RETURN
12!
1516
}
1517

1518
static void smlPrintStatisticInfo(SSmlHandle *info) {
1,908✔
1519
  uDebug(
1,908!
1520
      "SML:0x%" PRIx64
1521
      " 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 \
1522
        parse cost:%" PRId64 ",schema cost:%" PRId64 ",bind cost:%" PRId64 ",rpc cost:%" PRId64 ",total cost:%" PRId64,
1523
      info->id, info->cost.code, tstrerror(info->cost.code), info->cost.lineNum, info->cost.numOfSTables,
1524
      info->cost.numOfCTables, info->cost.numOfCreateSTables, info->cost.numOfAlterTagSTables,
1525
      info->cost.numOfAlterColSTables, info->cost.schemaTime - info->cost.parseTime,
1526
      info->cost.insertBindTime - info->cost.schemaTime, info->cost.insertRpcTime - info->cost.insertBindTime,
1527
      info->cost.endTime - info->cost.insertRpcTime, info->cost.endTime - info->cost.parseTime);
1528
}
1,908✔
1529

1530
int32_t smlClearForRerun(SSmlHandle *info) {
1,243✔
1531
  int32_t code = 0;
1,243✔
1532
  int32_t lino = 0;
1,243✔
1533
  info->reRun = false;
1,243✔
1534

1535
  taosHashClear(info->childTables);
1,243✔
1536
  taosHashClear(info->superTables);
1,246✔
1537
  taosHashClear(info->tableUids);
1,246✔
1538

1539
  if (!info->dataFormat) {
1,246✔
1540
    info->lines = (SSmlLineInfo *)taosMemoryCalloc(info->lineNum, sizeof(SSmlLineInfo));
1,245!
1541
    SML_CHECK_NULL(info->lines);
1,243!
1542
  }
1543

1544
  taosArrayClearP(info->escapedStringList, NULL);
1,244✔
1545
  if(info->protocol == TSDB_SML_JSON_PROTOCOL)  {
1,242✔
1546
    taosMemoryFreeClear(info->preLine.tags);
427!
1547
  }
1548
  (void)memset(&info->preLine, 0, sizeof(SSmlLineInfo));
1,242✔
1549
  info->currSTableMeta = NULL;
1,242✔
1550
  info->currTableDataCtx = NULL;
1,242✔
1551

1552
  SVnodeModifyOpStmt *stmt = (SVnodeModifyOpStmt *)(info->pQuery->pRoot);
1,242✔
1553
  stmt->freeHashFunc(stmt->pTableBlockHashObj);
1,242✔
1554
  stmt->pTableBlockHashObj = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
1,246✔
1555
  SML_CHECK_NULL(stmt->pTableBlockHashObj);
1,243!
1556

1557
END:
1,243✔
1558
  RETURN
1,243!
1559
}
1560

UNCOV
1561
static void printRaw(int64_t id, int lineNum, int numLines, ELogLevel level, char* data, int32_t len){
×
1562
  char *print = taosMemoryMalloc(len + 1);
×
1563
  if (print == NULL) {
×
1564
    uError("SML:0x%" PRIx64 ", smlParseLine failed. code :%d", id, terrno);
×
1565
    return;
×
1566
  }
UNCOV
1567
  (void)memcpy(print, data, len);
×
1568
  print[len] = '\0';
×
1569
  if (level == DEBUG_DEBUG){
×
1570
    uDebug("SML:0x%" PRIx64 ", smlParseLine is raw, line %d/%d :%s", id, lineNum, numLines, print);
×
1571
  }else if (level == DEBUG_ERROR){
×
1572
    uError("SML:0x%" PRIx64 ", smlParseLine failed. line %d/%d :%s", id, lineNum, numLines, print);
×
1573
  }
UNCOV
1574
  taosMemoryFree(print);
×
1575
}
1576

1577
static bool getLine(SSmlHandle *info, char *lines[], char **rawLine, char *rawLineEnd, int numLines, int i, char **tmp,
98,071✔
1578
                    int *len) {
1579
  if (lines) {
98,071✔
1580
    *tmp = lines[i];
97,966✔
1581
    *len = strlen(*tmp);
97,966✔
1582
  } else if (*rawLine) {
105!
1583
    *tmp = *rawLine;
114✔
1584
    while (*rawLine < rawLineEnd) {
970,728✔
1585
      if (*((*rawLine)++) == '\n') {
970,662✔
1586
        break;
48✔
1587
      }
1588
      (*len)++;
970,614✔
1589
    }
1590
    if (IS_COMMENT(info->protocol,(*tmp)[0])) {  // this line is comment
114✔
1591
      return false;
6✔
1592
    }
1593
  }
1594

1595
  if (*rawLine != NULL && (uDebugFlag & DEBUG_DEBUG)) {
98,065!
UNCOV
1596
    printRaw(info->id, i, numLines, DEBUG_DEBUG, *tmp, *len);
×
1597
  } else {
1598
    uDebug("SML:0x%" PRIx64 ", smlParseLine is not raw, line %d/%d :%s", info->id, i, numLines, *tmp);
98,065!
1599
  }
1600
  return true;
98,560✔
1601
}
1602

1603

1604
static int32_t smlParseJson(SSmlHandle *info, char *lines[], char *rawLine) {
844✔
1605
  int32_t code = TSDB_CODE_SUCCESS;
844✔
1606
  if (lines) {
844!
1607
    code = smlParseJSONExt(info, *lines);
844✔
UNCOV
1608
  } else if (rawLine) {
×
1609
    code = smlParseJSONExt(info, rawLine);
×
1610
  }
1611
  if (code != TSDB_CODE_SUCCESS) {
843✔
1612
    uError("%s failed code:%d", __FUNCTION__ , code);
72!
1613
  }
1614
  return code;
843✔
1615
}
1616

1617
static int32_t smlParseStart(SSmlHandle *info, char *lines[], char *rawLine, char *rawLineEnd, int numLines) {
1,904✔
1618
  uDebug("SML:0x%" PRIx64 ", %s start", info->id, __FUNCTION__);
1,904!
1619
  int32_t code = TSDB_CODE_SUCCESS;
1,902✔
1620
  if (info->protocol == TSDB_SML_JSON_PROTOCOL) {
1,902✔
1621
    return smlParseJson(info, lines, rawLine);
842✔
1622
  }
1623

1624
  char   *oldRaw = rawLine;
1,060✔
1625
  int32_t i = 0;
1,060✔
1626
  while (i < numLines) {
98,942✔
1627
    char *tmp = NULL;
97,983✔
1628
    int   len = 0;
97,983✔
1629
    if (!getLine(info, lines, &rawLine, rawLineEnd, numLines, i, &tmp, &len)) {
97,983✔
1630
      continue;
822✔
1631
    }
1632
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
98,612✔
1633
      if (info->dataFormat) {
95,881✔
1634
        SSmlLineInfo element = {0};
605✔
1635
        code = smlParseInfluxString(info, tmp, tmp + len, &element);
605✔
1636
      } else {
1637
        code = smlParseInfluxString(info, tmp, tmp + len, info->lines + i);
95,276✔
1638
      }
1639
    } else if (info->protocol == TSDB_SML_TELNET_PROTOCOL) {
2,731!
1640
      if (info->dataFormat) {
2,741✔
1641
        SSmlLineInfo element = {0};
1,974✔
1642
        code = smlParseTelnetString(info, (char *)tmp, (char *)tmp + len, &element);
1,974✔
1643
        if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
1,974!
1644
      } else {
1645
        code = smlParseTelnetString(info, (char *)tmp, (char *)tmp + len, info->lines + i);
767✔
1646
      }
1647
    }
1648
    if (code != TSDB_CODE_SUCCESS) {
97,998✔
1649
      if (rawLine != NULL) {
124!
UNCOV
1650
        printRaw(info->id, i, numLines, DEBUG_ERROR, tmp, len);
×
1651
      } else {
1652
        uError("SML:0x%" PRIx64 ", %s failed. line %d :%s", info->id, __FUNCTION__, i, tmp);
124!
1653
      }
1654
      return code;
124✔
1655
    }
1656
    if (info->reRun) {
97,874✔
1657
      uDebug("SML:0x%" PRIx64 ", %s re run", info->id, __FUNCTION__);
814!
1658
      i = 0;
814✔
1659
      rawLine = oldRaw;
814✔
1660
      code = smlClearForRerun(info);
814✔
1661
      if (code != TSDB_CODE_SUCCESS) {
816!
UNCOV
1662
        return code;
×
1663
      }
1664
      continue;
816✔
1665
    }
1666
    i++;
97,060✔
1667
  }
1668
  uDebug("SML:0x%" PRIx64 ", %s end", info->id, __FUNCTION__);
959!
1669

1670
  return code;
942✔
1671
}
1672

1673
static int smlProcess(SSmlHandle *info, char *lines[], char *rawLine, char *rawLineEnd, int numLines) {
1,903✔
1674
  int32_t code = TSDB_CODE_SUCCESS;
1,903✔
1675
  int32_t lino = 0;
1,903✔
1676
  int32_t retryNum = 0;
1,903✔
1677

1678
  info->cost.parseTime = taosGetTimestampUs();
1,903✔
1679

1680
  SML_CHECK_CODE(smlParseStart(info, lines, rawLine, rawLineEnd, numLines));
1,903✔
1681
  SML_CHECK_CODE(smlParseEnd(info));
1,713✔
1682

1683
  info->cost.lineNum = info->lineNum;
1,665✔
1684
  info->cost.numOfSTables = taosHashGetSize(info->superTables);
1,665✔
1685
  info->cost.numOfCTables = taosHashGetSize(info->childTables);
1,664✔
1686
  info->cost.schemaTime = taosGetTimestampUs();
1,665✔
1687

1688
  do {
1689
    code = smlModifyDBSchemas(info);
1,801✔
1690
    if (code != TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER && code != TSDB_CODE_SDB_OBJ_CREATING &&
1,800!
1691
        code != TSDB_CODE_MND_TRANS_CONFLICT) {
1692
      break;
1,665✔
1693
    }
1694
    taosMsleep(100);
135✔
1695
    uInfo("SML:0x%" PRIx64 ", smlModifyDBSchemas retry code:%s, times:%d", info->id, tstrerror(code), retryNum);
134!
1696
  } while (retryNum++ < taosHashGetSize(info->superTables) * MAX_RETRY_TIMES);
135!
1697

1698
  SML_CHECK_CODE(code);
1,664✔
1699
  info->cost.insertBindTime = taosGetTimestampUs();
1,618✔
1700
  SML_CHECK_CODE(smlInsertData(info));
1,618✔
1701

1702
END:
1,606✔
1703
  RETURN
1,909!
1704
}
1705

1706
void smlSetReqSQL(SRequestObj *request, char *lines[], char *rawLine, char *rawLineEnd) {
1,902✔
1707
  if (request->pTscObj->pAppInfo->serverCfg.monitorParas.tsSlowLogScope & SLOW_LOG_TYPE_INSERT) {
1,902!
UNCOV
1708
    int32_t len = 0;
×
1709
    int32_t rlen = 0;
×
1710
    char   *p = NULL;
×
1711

UNCOV
1712
    if (lines && lines[0]) {
×
1713
      len = strlen(lines[0]);
×
1714
      p = lines[0];
×
1715
    } else if (rawLine) {
×
1716
      if (rawLineEnd) {
×
1717
        len = rawLineEnd - rawLine;
×
1718
      } else {
UNCOV
1719
        len = strlen(rawLine);
×
1720
      }
UNCOV
1721
      p = rawLine;
×
1722
    }
1723

UNCOV
1724
    if (NULL == p) {
×
1725
      return;
×
1726
    }
1727

UNCOV
1728
    rlen = TMIN(len, TSDB_MAX_ALLOWED_SQL_LEN);
×
1729
    rlen = TMAX(rlen, 0);
×
1730

UNCOV
1731
    char *sql = taosMemoryMalloc(rlen + 1);
×
1732
    if (NULL == sql) {
×
1733
      uError("malloc %d for sml sql failed", rlen + 1);
×
1734
      return;
×
1735
    }
UNCOV
1736
    (void)memcpy(sql, p, rlen);
×
1737
    sql[rlen] = 0;
×
1738

UNCOV
1739
    request->sqlstr = sql;
×
1740
    request->sqlLen = rlen;
×
1741
  }
1742
}
1743

1744
TAOS_RES *taos_schemaless_insert_inner(TAOS *taos, char *lines[], char *rawLine, char *rawLineEnd, int numLines,
1,902✔
1745
                                       int protocol, int precision, int32_t ttl, int64_t reqid, char *tbnameKey) {
1746
  int32_t      code    = TSDB_CODE_SUCCESS;
1,902✔
1747
  int32_t      lino    = 0;
1,902✔
1748
  SRequestObj *request = NULL;
1,902✔
1749
  SSmlHandle  *info    = NULL;
1,902✔
1750
  int          cnt     = 0;
1,902✔
1751
  while (1) {
1✔
1752
    SML_CHECK_CODE(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &request, reqid));
1,903!
1753
    SSmlMsgBuf msg = {request->msgBufLen, request->msgBuf};
1,908✔
1754
    request->code = smlBuildSmlInfo(taos, &info);
1,908✔
1755
    SML_CHECK_CODE(request->code);
1,904!
1756

1757
    info->pRequest = request;
1,904✔
1758
    info->pRequest->pQuery = info->pQuery;
1,904✔
1759
    info->ttl = ttl;
1,904✔
1760
    info->precision = precision;
1,904✔
1761
    info->protocol = (TSDB_SML_PROTOCOL_TYPE)protocol;
1,904✔
1762
    info->msgBuf.buf = info->pRequest->msgBuf;
1,904✔
1763
    info->msgBuf.len = ERROR_MSG_BUF_DEFAULT_SIZE;
1,904✔
1764
    info->lineNum = numLines;
1,904✔
1765
    info->tbnameKey = tbnameKey;
1,904✔
1766

1767
    smlSetReqSQL(request, lines, rawLine, rawLineEnd);
1,904✔
1768

1769
    if (request->pDb == NULL) {
1,902!
UNCOV
1770
      request->code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1771
      smlBuildInvalidDataMsg(&msg, "Database not specified", NULL);
×
1772
      goto END;
×
1773
    }
1774

1775
    if (protocol < TSDB_SML_LINE_PROTOCOL || protocol > TSDB_SML_JSON_PROTOCOL) {
1,902!
UNCOV
1776
      request->code = TSDB_CODE_SML_INVALID_PROTOCOL_TYPE;
×
1777
      smlBuildInvalidDataMsg(&msg, "protocol invalidate", NULL);
×
1778
      goto END;
×
1779
    }
1780

1781
    if (protocol == TSDB_SML_LINE_PROTOCOL &&
1,908✔
1782
        (precision < TSDB_SML_TIMESTAMP_NOT_CONFIGURED || precision > TSDB_SML_TIMESTAMP_NANO_SECONDS)) {
603✔
1783
      request->code = TSDB_CODE_SML_INVALID_PRECISION_TYPE;
3✔
1784
      smlBuildInvalidDataMsg(&msg, "precision invalidate for line protocol", NULL);
3✔
1785
      goto END;
×
1786
    }
1787

1788
    if (protocol == TSDB_SML_JSON_PROTOCOL) {
1,905✔
1789
      numLines = 1;
842✔
1790
    } else if (numLines <= 0) {
1,063!
UNCOV
1791
      request->code = TSDB_CODE_SML_INVALID_DATA;
×
1792
      smlBuildInvalidDataMsg(&msg, "line num is invalid", NULL);
×
1793
      goto END;
×
1794
    }
1795

1796
    code = smlProcess(info, lines, rawLine, rawLineEnd, numLines);
1,905✔
1797
    request->code = code;
1,908✔
1798
    info->cost.endTime = taosGetTimestampUs();
1,908✔
1799
    info->cost.code = code;
1,910✔
1800
    if (NEED_CLIENT_HANDLE_ERROR(code) || code == TSDB_CODE_SDB_OBJ_CREATING || code == TSDB_CODE_PAR_VALUE_TOO_LONG ||
1,910!
1801
        code == TSDB_CODE_MND_TRANS_CONFLICT) {
1802
      if (cnt++ >= 10) {
2!
UNCOV
1803
        uInfo("SML:%" PRIx64 " retry:%d/10 end code:%d, msg:%s", info->id, cnt, code, tstrerror(code));
×
1804
        break;
1,907✔
1805
      }
1806
      taosMsleep(100);
2✔
1807
      uInfo("SML:%" PRIx64 " retry:%d/10,ver is old retry or object is creating code:%d, msg:%s", info->id, cnt, code,
1!
1808
            tstrerror(code));
1809
      code = refreshMeta(request->pTscObj, request);
1✔
1810
      if (code != 0) {
1!
1811
        uInfo("SML:%" PRIx64 " refresh meta error code:%d, msg:%s", info->id, code, tstrerror(code));
1!
1812
      }
1813
      smlDestroyInfo(info);
1✔
1814
      info = NULL;
1✔
1815
      taos_free_result(request);
1✔
1816
      request = NULL;
1✔
1817
      continue;
1✔
1818
    }
1819
    smlPrintStatisticInfo(info);
1,908✔
1820
    break;
1,907✔
1821
  }
1822

1823
END:
1,907✔
1824
  smlDestroyInfo(info);
1,907✔
1825
  return (TAOS_RES *)request;
1,909✔
1826
}
1827

1828
/**
1829
 * taos_schemaless_insert() parse and insert data points into database according to
1830
 * different protocol.
1831
 *
1832
 * @param $lines input array may contain multiple lines, each line indicates a data point.
1833
 *               If protocol=2 is used input array should contain single JSON
1834
 *               string(e.g. char *lines[] = {"$JSON_string"}). If need to insert
1835
 *               multiple data points in JSON format, should include them in $JSON_string
1836
 *               as a JSON array.
1837
 * @param $numLines indicates how many data points in $lines.
1838
 *                  If protocol = 2 is used this param will be ignored as $lines should
1839
 *                  contain single JSON string.
1840
 * @param $protocol indicates which protocol to use for parsing:
1841
 *                  0 - influxDB line protocol
1842
 *                  1 - OpenTSDB telnet line protocol
1843
 *                  2 - OpenTSDB JSON format protocol
1844
 * @return TAOS_RES
1845
 */
1846

1847
TAOS_RES *taos_schemaless_insert_ttl_with_reqid_tbname_key(TAOS *taos, char *lines[], int numLines, int protocol,
1,863✔
1848
                                                           int precision, int32_t ttl, int64_t reqid, char *tbnameKey) {
1849
  if (taos == NULL || lines == NULL || numLines < 0) {
1,863!
1850
    terrno = TSDB_CODE_INVALID_PARA;
1✔
1851
    return NULL;
×
1852
  }
1853
  for (int i = 0; i < numLines; i++){
88,131✔
1854
    if (lines[i] == NULL){
86,269!
UNCOV
1855
      terrno = TSDB_CODE_INVALID_PARA;
×
1856
      return NULL;
×
1857
    }
1858
  }
1859
  return taos_schemaless_insert_inner(taos, lines, NULL, NULL, numLines, protocol, precision, ttl, reqid, tbnameKey);
1,862✔
1860
}
1861

1862
TAOS_RES *taos_schemaless_insert_ttl_with_reqid(TAOS *taos, char *lines[], int numLines, int protocol, int precision,
1,865✔
1863
                                                int32_t ttl, int64_t reqid) {
1864
  return taos_schemaless_insert_ttl_with_reqid_tbname_key(taos, lines, numLines, protocol, precision, ttl, reqid, NULL);
1,865✔
1865
}
1866

1867
TAOS_RES *taos_schemaless_insert(TAOS *taos, char *lines[], int numLines, int protocol, int precision) {
1,838✔
1868
  return taos_schemaless_insert_ttl_with_reqid(taos, lines, numLines, protocol, precision, TSDB_DEFAULT_TABLE_TTL, 0);
1,838✔
1869
}
1870

1871
TAOS_RES *taos_schemaless_insert_ttl(TAOS *taos, char *lines[], int numLines, int protocol, int precision,
24✔
1872
                                     int32_t ttl) {
1873
  return taos_schemaless_insert_ttl_with_reqid(taos, lines, numLines, protocol, precision, ttl, 0);
24✔
1874
}
1875

UNCOV
1876
TAOS_RES *taos_schemaless_insert_with_reqid(TAOS *taos, char *lines[], int numLines, int protocol, int precision,
×
1877
                                            int64_t reqid) {
UNCOV
1878
  return taos_schemaless_insert_ttl_with_reqid(taos, lines, numLines, protocol, precision, TSDB_DEFAULT_TABLE_TTL,
×
1879
                                               reqid);
1880
}
1881

1882
static int32_t getRawLineLen(char *lines, int len, int protocol) {
42✔
1883
  int numLines = 0;
42✔
1884
  char *tmp = lines;
42✔
1885
  for (int i = 0; i < len; i++) {
486,306✔
1886
    if (lines[i] == '\n' || i == len - 1) {
486,264✔
1887
      if (!IS_COMMENT(protocol, tmp[0])) {  // ignore comment
72✔
1888
        numLines++;
66✔
1889
      }
1890
      tmp = lines + i + 1;
72✔
1891
    }
1892
  }
1893
  return numLines;
42✔
1894
}
1895

1896
TAOS_RES *taos_schemaless_insert_raw_ttl_with_reqid_tbname_key(TAOS *taos, char *lines, int len, int32_t *totalRows,
42✔
1897
                                                               int protocol, int precision, int32_t ttl, int64_t reqid,
1898
                                                               char *tbnameKey) {
1899
  if (taos == NULL || lines == NULL || len < 0) {
42!
UNCOV
1900
    terrno = TSDB_CODE_INVALID_PARA;
×
1901
    return NULL;
×
1902
  }
1903
  int numLines = getRawLineLen(lines, len, protocol);
42✔
1904
  if (totalRows != NULL){
42!
1905
    *totalRows = numLines;
42✔
1906
  }
1907
  return taos_schemaless_insert_inner(taos, NULL, lines, lines + len, numLines, protocol, precision, ttl, reqid,
42✔
1908
                                      tbnameKey);
1909
}
1910

1911
TAOS_RES *taos_schemaless_insert_raw_ttl_with_reqid(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
42✔
1912
                                                    int precision, int32_t ttl, int64_t reqid) {
1913
  return taos_schemaless_insert_raw_ttl_with_reqid_tbname_key(taos, lines, len, totalRows, protocol, precision, ttl,
42✔
1914
                                                              reqid, NULL);
1915
}
1916

UNCOV
1917
TAOS_RES *taos_schemaless_insert_raw_with_reqid(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
×
1918
                                                int precision, int64_t reqid) {
UNCOV
1919
  return taos_schemaless_insert_raw_ttl_with_reqid(taos, lines, len, totalRows, protocol, precision,
×
1920
                                                   TSDB_DEFAULT_TABLE_TTL, reqid);
1921
}
UNCOV
1922
TAOS_RES *taos_schemaless_insert_raw_ttl(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
×
1923
                                         int precision, int32_t ttl) {
UNCOV
1924
  return taos_schemaless_insert_raw_ttl_with_reqid(taos, lines, len, totalRows, protocol, precision, ttl, 0);
×
1925
}
1926

1927
TAOS_RES *taos_schemaless_insert_raw(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
42✔
1928
                                     int precision) {
1929
  return taos_schemaless_insert_raw_ttl_with_reqid(taos, lines, len, totalRows, protocol, precision,
42✔
1930
                                                   TSDB_DEFAULT_TABLE_TTL, 0);
1931
}
1932

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