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

taosdata / TDengine / #4881

14 Dec 2025 03:48AM UTC coverage: 60.617% (+0.5%) from 60.092%
#4881

push

travis-ci

web-flow
test: update coverage workflow time (#33918)

156854 of 258761 relevant lines covered (60.62%)

75258957.81 hits per line

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

87.98
/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
#include "thash.h"
23

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

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

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

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

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

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

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

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

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

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

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

108
#define IS_COMMENT(protocol, data) (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) {
805,147✔
115
  SUserAuthInfo pAuth = {0};
805,147✔
116
  (void)snprintf(pAuth.user, sizeof(pAuth.user), "%s", info->taos->user);
805,299✔
117
  if (NULL == pTabName) {
804,235✔
118
    if (tNameSetDbName(&pAuth.tbName, info->taos->acctId, info->pRequest->pDb, strlen(info->pRequest->pDb)) != 0) {
201,639✔
119
      return TSDB_CODE_SML_INVALID_DATA;
×
120
    }
121
  } else {
122
    toName(info->taos->acctId, info->pRequest->pDb, pTabName, &pAuth.tbName);
602,596✔
123
  }
124
  pAuth.type = type;
805,275✔
125

126
  int32_t      code = TSDB_CODE_SUCCESS;
805,275✔
127
  SUserAuthRes authRes = {0};
805,275✔
128

129
  code = catalogChkAuth(info->pCatalog, conn, &pAuth, &authRes);
805,427✔
130
  nodesDestroyNode(authRes.pCond[AUTH_RES_BASIC]);
803,749✔
131

132
  return (code == TSDB_CODE_SUCCESS)
133
             ? (authRes.pass[AUTH_RES_BASIC] ? TSDB_CODE_SUCCESS : TSDB_CODE_PAR_PERMISSION_DENIED)
803,451✔
134
             : code;
1,581,678✔
135
}
136

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

152
int64_t smlGetTimeValue(const char *value, int32_t len, uint8_t fromPrecision, uint8_t toPrecision) {
15,638,338✔
153
  char   *endPtr = NULL;
15,638,338✔
154
  int64_t tsInt64 = taosStr2Int64(value, &endPtr, 10);
15,652,106✔
155
  if (unlikely(value + len != endPtr)) {
15,630,050✔
156
    return -1;
1,576✔
157
  }
158

159
  if (unlikely(fromPrecision >= TSDB_TIME_PRECISION_HOURS)) {
15,644,014✔
160
    int64_t unit = smlToMilli[fromPrecision - TSDB_TIME_PRECISION_HOURS];
4,038✔
161
    if (tsInt64 != 0 && unit > INT64_MAX / tsInt64) {
4,038✔
162
      return -1;
×
163
    }
164
    tsInt64 *= unit;
4,038✔
165
    fromPrecision = TSDB_TIME_PRECISION_MILLI;
4,038✔
166
  }
167

168
  return convertTimePrecision(tsInt64, fromPrecision, toPrecision);
15,644,014✔
169
}
170

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

177
  tag->sTableName = measure;
677,559✔
178
  tag->sTableNameLen = measureLen;
677,711✔
179

180
  tag->cols = taosArrayInit(numRows, POINTER_BYTES);
678,015✔
181
  SML_CHECK_NULL(tag->cols)
677,711✔
182
  *tInfo = tag;
677,717✔
183
  return code;
677,717✔
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) {
15,724,187✔
192
  kv->key = tsSmlTsDefaultName;
15,724,187✔
193
  kv->keyLen = strlen(tsSmlTsDefaultName);
15,738,547✔
194
  kv->type = TSDB_DATA_TYPE_TIMESTAMP;
15,790,347✔
195
  kv->i = ts;
15,800,707✔
196
  kv->length = (size_t)tDataTypes[TSDB_DATA_TYPE_TIMESTAMP].bytes;
15,776,583✔
197
}
15,817,871✔
198

199
static void smlDestroySTableMeta(void *para) {
582,264✔
200
  if (para == NULL) {
582,264✔
201
    return;
×
202
  }
203
  SSmlSTableMeta *meta = *(SSmlSTableMeta **)para;
582,264✔
204
  if (meta == NULL) {
582,416✔
205
    return;
227,605✔
206
  }
207
  taosHashCleanup(meta->tagHash);
354,811✔
208
  taosHashCleanup(meta->colHash);
354,635✔
209
  taosArrayDestroy(meta->tags);
354,787✔
210
  taosArrayDestroy(meta->cols);
354,787✔
211
  taosMemoryFreeClear(meta->tableMeta);
354,787✔
212
  taosMemoryFree(meta);
354,635✔
213
}
214

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

220
  int   measureLen = currElement->measureLen;
312,590✔
221
  char *measure = (char *)taosMemoryMalloc(measureLen);
312,742✔
222
  SML_CHECK_NULL(measure);
312,766✔
223
  (void)memcpy(measure, currElement->measure, measureLen);
312,766✔
224
  if (currElement->measureEscaped) {
312,766✔
225
    PROCESS_SLASH_IN_MEASUREMENT(measure, measureLen);
2,016✔
226
  }
227
  smlStrReplace(measure, measureLen);
312,766✔
228
  code = smlGetMeta(info, measure, measureLen, &pTableMeta);
312,614✔
229
  taosMemoryFree(measure);
312,462✔
230
  if (code != TSDB_CODE_SUCCESS) {
312,790✔
231
    info->dataFormat = false;
227,629✔
232
    info->reRun = true;
227,629✔
233
    goto END;
227,629✔
234
  }
235
  SML_CHECK_CODE(smlBuildSTableMeta(info->dataFormat, sMeta));
85,161✔
236
  for (int i = 0; i < pTableMeta->tableInfo.numOfTags + pTableMeta->tableInfo.numOfColumns; i++) {
2,215,391✔
237
    SSchema *col = pTableMeta->schema + i;
2,130,364✔
238
    SSmlKv   kv = {.key = col->name, .keyLen = strlen(col->name), .type = col->type};
2,130,516✔
239
    if (col->type == TSDB_DATA_TYPE_NCHAR) {
2,130,668✔
240
      kv.length = (col->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
239,704✔
241
    } else if (col->type == TSDB_DATA_TYPE_BINARY || col->type == TSDB_DATA_TYPE_GEOMETRY ||
1,890,964✔
242
               col->type == TSDB_DATA_TYPE_VARBINARY) {
1,043,146✔
243
      kv.length = col->bytes - VARSTR_HEADER_SIZE;
848,418✔
244
    } else {
245
      kv.length = col->bytes;
1,042,394✔
246
    }
247

248
    if (i < pTableMeta->tableInfo.numOfColumns) {
2,130,668✔
249
      SML_CHECK_NULL(taosArrayPush((*sMeta)->cols, &kv));
3,580,946✔
250
    } else {
251
      SML_CHECK_NULL(taosArrayPush((*sMeta)->tags, &kv));
680,128✔
252
    }
253
  }
254
  SML_CHECK_CODE(taosHashPut(info->superTables, currElement->measure, currElement->measureLen, sMeta, POINTER_BYTES));
85,331✔
255
  (*sMeta)->tableMeta = pTableMeta;
85,313✔
256
  return code;
85,313✔
257

258
END:
227,629✔
259
  smlDestroySTableMeta(sMeta);
227,629✔
260
  taosMemoryFreeClear(pTableMeta);
227,629✔
261
  RETURN
227,629✔
262
}
263

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

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

293
END:
60,695✔
294
  info->dataFormat = false;
60,695✔
295
  info->reRun = true;
60,695✔
296
  return false;
60,847✔
297
}
298

299
bool isSmlTagAligned(SSmlHandle *info, int cnt, SSmlKv *kv) {
342,352✔
300
  if (unlikely(cnt + 1 > info->currSTableMeta->tableInfo.numOfTags)) {
342,352✔
301
    goto END;
2,030✔
302
  }
303

304
  if (unlikely(cnt >= taosArrayGetSize(info->maxTagKVs))) {
340,322✔
305
    goto END;
×
306
  }
307
  SSmlKv *maxKV = (SSmlKv *)taosArrayGet(info->maxTagKVs, cnt);
340,492✔
308
  if (maxKV == NULL) {
340,340✔
309
    goto END;
×
310
  }
311
  if (unlikely(!IS_SAME_KEY)) {
340,340✔
312
    goto END;
2,420✔
313
  }
314

315
  if (unlikely(kv->length > maxKV->length)) {
337,920✔
316
    maxKV->length = kv->length;
1,910✔
317
    info->needModifySchema = true;
1,910✔
318
  }
319
  return true;
337,920✔
320

321
END:
4,450✔
322
  info->dataFormat = false;
4,450✔
323
  info->reRun = true;
4,450✔
324
  return false;
4,450✔
325
}
326

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

338
static bool smlIsPKTable(STableMeta *pTableMeta) {
153,705✔
339
  for (int i = 0; i < pTableMeta->tableInfo.numOfColumns; i++) {
3,769,457✔
340
    if (pTableMeta->schema[i].flags & COL_IS_KEY) {
3,617,505✔
341
      return true;
1,753✔
342
    }
343
  }
344

345
  return false;
151,952✔
346
}
347

348
int32_t smlProcessSuperTable(SSmlHandle *info, SSmlLineInfo *elements) {
314,418✔
349
  bool isSameMeasure = IS_SAME_SUPER_TABLE;
314,418✔
350
  if (isSameMeasure) {
314,570✔
351
    return TSDB_CODE_SUCCESS;
1,714✔
352
  }
353
  SSmlSTableMeta **tmp = (SSmlSTableMeta **)taosHashGet(info->superTables, elements->measure, elements->measureLen);
312,856✔
354

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

370
  if (smlIsPKTable(sMeta->tableMeta)) {
85,403✔
371
    return TSDB_CODE_SML_NOT_SUPPORT_PK;
1,753✔
372
  }
373
  return 0;
83,674✔
374
}
375

376
int32_t smlProcessChildTable(SSmlHandle *info, SSmlLineInfo *elements) {
678,505✔
377
  int32_t         code = TSDB_CODE_SUCCESS;
678,505✔
378
  int32_t         lino = 0;
678,505✔
379
  SSmlTableInfo **oneTable =
380
      (SSmlTableInfo **)taosHashGet(info->childTables, elements->measureTag, elements->measureTagsLen);
678,505✔
381
  SSmlTableInfo *tinfo = NULL;
678,833✔
382
  if (unlikely(oneTable == NULL)) {
678,529✔
383
    SML_CHECK_CODE(smlBuildTableInfo(1, elements->measure, elements->measureLen, &tinfo));
677,717✔
384
    SML_CHECK_CODE(
677,413✔
385
        taosHashPut(info->childTables, elements->measureTag, elements->measureTagsLen, &tinfo, POINTER_BYTES));
386

387
    tinfo->tags = taosArrayDup(info->preLineTagKV, NULL);
677,997✔
388
    SML_CHECK_NULL(tinfo->tags);
678,021✔
389
    for (size_t i = 0; i < taosArrayGetSize(info->preLineTagKV); i++) {
4,408,226✔
390
      SSmlKv *kv = (SSmlKv *)taosArrayGet(info->preLineTagKV, i);
3,730,229✔
391
      SML_CHECK_NULL(kv);
3,730,229✔
392
      if (kv->keyEscaped) kv->key = NULL;
3,730,205✔
393
      if (kv->valueEscaped) kv->value = NULL;
3,730,205✔
394
    }
395

396
    SML_CHECK_CODE(smlSetCTableName(tinfo, info->tbnameKey));
678,021✔
397
    SML_CHECK_CODE(getTableUid(info, elements, tinfo));
677,565✔
398
    if (info->dataFormat) {
678,021✔
399
      info->currSTableMeta->uid = tinfo->uid;
80,014✔
400
      SML_CHECK_CODE(smlInitTableDataCtx(info->pQuery, info->currSTableMeta, &tinfo->tableDataCtx));
80,014✔
401
    }
402
  } else {
403
    tinfo = *oneTable;
812✔
404
  }
405
  if (info->dataFormat) info->currTableDataCtx = tinfo->tableDataCtx;
678,681✔
406
  return TSDB_CODE_SUCCESS;
678,681✔
407

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

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

423
END:
133,845✔
424
  clearColValArraySml(info->currTableDataCtx->pValues);
133,845✔
425
  RETURN
133,845✔
426
}
427

428
int32_t smlParseEndTelnetJsonUnFormat(SSmlHandle *info, SSmlLineInfo *elements, SSmlKv *kvTs, SSmlKv *kv) {
181,312✔
429
  int32_t code = 0;
181,312✔
430
  int32_t lino = 0;
181,312✔
431
  uDebug("SML:0x%" PRIx64 ", %s format false, ts:%" PRId64, info->id, __FUNCTION__, kvTs->i);
181,312✔
432
  if (elements->colArray == NULL) {
181,312✔
433
    elements->colArray = taosArrayInit(16, sizeof(SSmlKv));
181,312✔
434
    SML_CHECK_NULL(elements->colArray);
181,264✔
435
  }
436
  SML_CHECK_NULL(taosArrayPush(elements->colArray, kvTs));
362,528✔
437
  SML_CHECK_NULL(taosArrayPush(elements->colArray, kv));
362,528✔
438

439
END:
181,264✔
440
  RETURN
181,264✔
441
}
442

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

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

464
  return TSDB_CODE_SUCCESS;
15,445,734✔
465
}
466

467
static int32_t smlParseTableName(SArray *tags, char *childTableName, char *tbnameKey) {
677,693✔
468
  int32_t code = 0;
677,693✔
469
  int32_t lino = 0;
677,693✔
470
  bool    autoChildName = false;
677,693✔
471
  size_t  delimiter = strlen(tsSmlAutoChildTableNameDelimiter);
677,693✔
472
  if (delimiter > 0 && tbnameKey == NULL) {
677,693✔
473
    size_t totalNameLen = delimiter * (taosArrayGetSize(tags) - 1);
354✔
474
    for (int i = 0; i < taosArrayGetSize(tags); i++) {
1,121✔
475
      SSmlKv *tag = (SSmlKv *)taosArrayGet(tags, i);
919✔
476
      SML_CHECK_NULL(tag);
767✔
477
      totalNameLen += tag->length;
767✔
478
    }
479
    if (totalNameLen < TSDB_TABLE_NAME_LEN) {
295✔
480
      autoChildName = true;
236✔
481
    }
482
  }
483
  if (autoChildName) {
677,541✔
484
    childTableName[0] = '\0';
236✔
485
    for (int i = 0; i < taosArrayGetSize(tags); i++) {
826✔
486
      SSmlKv *tag = (SSmlKv *)taosArrayGet(tags, i);
590✔
487
      SML_CHECK_NULL(tag);
590✔
488
      (void)strncat(childTableName, tag->value, TMIN(tag->length, TSDB_TABLE_NAME_LEN - 1 - strlen(childTableName)));
590✔
489
      if (i != taosArrayGetSize(tags) - 1) {
590✔
490
        (void)strncat(childTableName, tsSmlAutoChildTableNameDelimiter,
354✔
491
                      TSDB_TABLE_NAME_LEN - 1 - strlen(childTableName));
354✔
492
      }
493
    }
494
    if (tsSmlDot2Underline) {
236✔
495
      smlStrReplace(childTableName, strlen(childTableName));
236✔
496
    }
497
  } else {
498
    if (tbnameKey == NULL) {
677,305✔
499
      tbnameKey = tsSmlChildTableName;
677,305✔
500
    }
501
    size_t childTableNameLen = strlen(tbnameKey);
677,305✔
502
    if (childTableNameLen <= 0) return TSDB_CODE_SUCCESS;
677,305✔
503

504
    for (int i = 0; i < taosArrayGetSize(tags); i++) {
153,511✔
505
      SSmlKv *tag = (SSmlKv *)taosArrayGet(tags, i);
130,993✔
506
      SML_CHECK_NULL(tag);
130,993✔
507
      // handle child table name
508
      if (childTableNameLen == tag->keyLen && strncmp(tag->key, tbnameKey, tag->keyLen) == 0) {
130,993✔
509
        tstrncpy(childTableName, tag->value, TMIN(TSDB_TABLE_NAME_LEN, tag->length + 1));
1,232✔
510
        if (tsSmlDot2Underline) {
1,232✔
511
          smlStrReplace(childTableName, strlen(childTableName));
638✔
512
        }
513
        taosArrayRemove(tags, i);
1,232✔
514
        break;
1,232✔
515
      }
516
    }
517
  }
518

519
END:
22,518✔
520
  RETURN
23,986✔
521
}
522

523
int32_t smlSetCTableName(SSmlTableInfo *oneTable, char *tbnameKey) {
677,693✔
524
  int32_t code = 0;
677,693✔
525
  int32_t lino = 0;
677,693✔
526
  SArray *dst = NULL;
677,693✔
527
  SML_CHECK_CODE(smlParseTableName(oneTable->tags, oneTable->childTableName, tbnameKey));
677,693✔
528

529
  if (strlen(oneTable->childTableName) == 0) {
678,021✔
530
    dst = taosArrayDup(oneTable->tags, NULL);
676,553✔
531
    SML_CHECK_NULL(dst);
676,249✔
532
    if (oneTable->sTableNameLen >= TSDB_TABLE_NAME_LEN) {
676,225✔
533
      SML_CHECK_CODE(TSDB_CODE_SML_INTERNAL_ERROR);
×
534
    }
535
    char          superName[TSDB_TABLE_NAME_LEN] = {0};
676,225✔
536
    RandTableName rName = {dst, NULL, (uint8_t)oneTable->sTableNameLen, oneTable->childTableName};
676,225✔
537
    if (tsSmlDot2Underline) {
676,225✔
538
      (void)memcpy(superName, oneTable->sTableName, oneTable->sTableNameLen);
516,478✔
539
      smlStrReplace(superName, oneTable->sTableNameLen);
516,782✔
540
      rName.stbFullName = superName;
516,648✔
541
    } else {
542
      rName.stbFullName = oneTable->sTableName;
159,747✔
543
    }
544

545
    SML_CHECK_CODE(buildChildTableName(&rName));
676,395✔
546
  }
547

548
END:
1,468✔
549
  taosArrayDestroy(dst);
677,869✔
550
  RETURN
677,717✔
551
}
552

553
int32_t getTableUid(SSmlHandle *info, SSmlLineInfo *currElement, SSmlTableInfo *tinfo) {
677,693✔
554
  char   key[TSDB_TABLE_NAME_LEN * 2 + 1] = {0};
677,693✔
555
  size_t nLen = strlen(tinfo->childTableName);
677,693✔
556
  (void)memcpy(key, currElement->measure, currElement->measureLen);
677,389✔
557
  if (tsSmlDot2Underline) {
677,845✔
558
    smlStrReplace(key, currElement->measureLen);
517,504✔
559
  }
560
  (void)memcpy(key + currElement->measureLen + 1, tinfo->childTableName, nLen);
677,869✔
561
  void *uid =
562
      taosHashGet(info->tableUids, key,
677,869✔
563
                  currElement->measureLen + 1 + nLen);  // use \0 as separator for stable name and child table name
677,717✔
564
  if (uid == NULL) {
677,869✔
565
    tinfo->uid = info->uid++;
677,027✔
566
    return taosHashPut(info->tableUids, key, currElement->measureLen + 1 + nLen, &tinfo->uid, sizeof(uint64_t));
676,875✔
567
  } else {
568
    tinfo->uid = *(uint64_t *)uid;
842✔
569
  }
570
  return TSDB_CODE_SUCCESS;
842✔
571
}
572

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

585
  meta->tags = taosArrayInit(32, sizeof(SSmlKv));
354,611✔
586
  SML_CHECK_NULL(meta->tags);
354,611✔
587

588
  meta->cols = taosArrayInit(32, sizeof(SSmlKv));
354,611✔
589
  SML_CHECK_NULL(meta->cols);
354,787✔
590
  *sMeta = meta;
354,763✔
591
  return TSDB_CODE_SUCCESS;
354,763✔
592

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

599
int32_t smlParseNumber(SSmlKv *kvVal, SSmlMsgBuf *msg) {
56,683,887✔
600
  const char *pVal = kvVal->value;
56,683,887✔
601
  int32_t     len = kvVal->length;
57,033,787✔
602
  char       *endptr = NULL;
57,073,915✔
603
  double      result = taosStr2Double(pVal, &endptr);
57,187,747✔
604
  if (pVal == endptr) {
56,652,495✔
605
    RETURN_FALSE
441✔
606
  }
607

608
  int32_t left = len - (endptr - pVal);
56,652,054✔
609
  if (left == 0) {
56,652,054✔
610
    SET_DOUBLE
41,002✔
611
  } else if (left == 3) {
56,611,052✔
612
    if (endptr[0] == 'f' || endptr[0] == 'F') {
56,699,183✔
613
      if (endptr[1] == '6' && endptr[2] == '4') {
41,584,208✔
614
        SET_DOUBLE
89,024✔
615
      } else if (endptr[1] == '3' && endptr[2] == '2') {
41,637,724✔
616
        SET_FLOAT
41,719,412✔
617
      } else {
618
        RETURN_FALSE
×
619
      }
620
    } else if (endptr[0] == 'i' || endptr[0] == 'I') {
15,117,787✔
621
      if (endptr[1] == '6' && endptr[2] == '4') {
14,963,269✔
622
        SET_BIGINT
87,372✔
623
      } else if (endptr[1] == '3' && endptr[2] == '2') {
14,939,537✔
624
        SET_INT
14,849,821✔
625
      } else if (endptr[1] == '1' && endptr[2] == '6') {
59,188✔
626
        SET_SMALL_INT
86,484✔
627
      } else {
628
        RETURN_FALSE
×
629
      }
630
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
109,378✔
631
      if (endptr[1] == '6' && endptr[2] == '4') {
109,378✔
632
        SET_UBIGINT
78,490✔
633
      } else if (endptr[1] == '3' && endptr[2] == '2') {
30,888✔
634
        SET_UINT
15,456✔
635
      } else if (endptr[1] == '1' && endptr[2] == '6') {
15,432✔
636
        SET_USMALL_INT
15,432✔
637
      } else {
638
        RETURN_FALSE
×
639
      }
640
    } else {
641
      RETURN_FALSE
×
642
    }
643
  } else if (left == 2) {
72,069✔
644
    if (endptr[0] == 'i' || endptr[0] == 'I') {
103,414✔
645
      if (endptr[1] == '8') {
88,030✔
646
        SET_TINYINT
88,030✔
647
      } else {
648
        RETURN_FALSE
×
649
      }
650
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
15,384✔
651
      if (endptr[1] == '8') {
15,384✔
652
        SET_UTINYINT
15,384✔
653
      } else {
654
        RETURN_FALSE
×
655
      }
656
    } else {
657
      RETURN_FALSE
×
658
    }
659
  } else if (left == 1) {
4,005✔
660
    if (endptr[0] == 'i' || endptr[0] == 'I') {
1,707✔
661
      SET_BIGINT
1,707✔
662
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
×
663
      SET_UBIGINT
×
664
    } else {
665
      RETURN_FALSE
×
666
    }
667
  } else {
668
    RETURN_FALSE;
3,192✔
669
  }
670
  return true;
55,077,028✔
671
}
672

673
int32_t smlGetMeta(SSmlHandle *info, const void *measure, int32_t measureLen, STableMeta **pTableMeta) {
312,742✔
674
  *pTableMeta = NULL;
312,742✔
675

676
  SName pName = {TSDB_TABLE_NAME_T, info->taos->acctId, {0}, {0}};
312,894✔
677
  tstrncpy(pName.dbname, info->pRequest->pDb, sizeof(pName.dbname));
312,894✔
678

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

688
  return catalogGetSTableMeta(info->pCatalog, &conn, &pName, pTableMeta);
312,790✔
689
}
690

691
static int64_t smlGenId() {
347,757✔
692
  static volatile int64_t linesSmlHandleId = 0;
693

694
  int64_t id = 0;
347,757✔
695
  do {
696
    id = atomic_add_fetch_64(&linesSmlHandleId, 1);
347,757✔
697
  } while (id == 0);
347,781✔
698

699
  return id;
347,781✔
700
}
701

702
static int32_t smlGenerateSchemaAction(SSchema *colField, SHashObj *colHash, SSmlKv *kv, bool isTag,
6,323,566✔
703
                                       ESchemaAction *action, SSmlHandle *info) {
704
  uint16_t *index = colHash ? (uint16_t *)taosHashGet(colHash, kv->key, kv->keyLen) : NULL;
6,323,566✔
705
  if (index) {
6,323,742✔
706
    if (colField[*index].type != kv->type) {
1,962,803✔
707
      snprintf(info->msgBuf.buf, info->msgBuf.len,
4,048✔
708
               "SML:0x%" PRIx64 ", %s point type and db type mismatch, index:%d, db type:%s, point type:%s, key:%s", info->id,
709
               __FUNCTION__, *index, tDataTypes[colField[*index].type].name, tDataTypes[kv->type].name, kv->key);
3,036✔
710
      uError("%s", info->msgBuf.buf);
1,012✔
711
      return TSDB_CODE_SML_INVALID_DATA;
1,012✔
712
    }
713

714
    if (((colField[*index].type == TSDB_DATA_TYPE_VARCHAR || colField[*index].type == TSDB_DATA_TYPE_VARBINARY ||
1,961,943✔
715
          colField[*index].type == TSDB_DATA_TYPE_GEOMETRY) &&
1,103,969✔
716
         (colField[*index].bytes - VARSTR_HEADER_SIZE) < kv->length) ||
858,086✔
717
        (colField[*index].type == TSDB_DATA_TYPE_NCHAR &&
1,961,195✔
718
         ((colField[*index].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE < kv->length))) {
199,083✔
719
      if (isTag) {
2,966✔
720
        *action = SCHEMA_ACTION_CHANGE_TAG_SIZE;
2,066✔
721
      } else {
722
        *action = SCHEMA_ACTION_CHANGE_COLUMN_SIZE;
900✔
723
      }
724
    }
725
  } else {
726
    if (isTag) {
4,360,939✔
727
      *action = SCHEMA_ACTION_ADD_TAG;
1,847,234✔
728
    } else {
729
      *action = SCHEMA_ACTION_ADD_COLUMN;
2,513,705✔
730
    }
731
  }
732
  return TSDB_CODE_SUCCESS;
6,322,730✔
733
}
734

735
#define BOUNDARY 1024
736
static int32_t smlFindNearestPowerOf2(int32_t length, uint8_t type) {
1,301,405✔
737
  int32_t result = 1;
1,301,405✔
738
  if (length >= BOUNDARY) {
1,301,405✔
739
    result = length;
6,246✔
740
  } else {
741
    while (result <= length) {
6,137,845✔
742
      result <<= 1;
4,842,686✔
743
    }
744
  }
745

746
  if ((type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_GEOMETRY) &&
1,301,405✔
747
      result > TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE) {
246,165✔
748
    result = TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE;
×
749
  } else if (type == TSDB_DATA_TYPE_NCHAR && result > (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
1,301,405✔
750
    result = (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
751
  }
752

753
  if (type == TSDB_DATA_TYPE_NCHAR) {
1,301,405✔
754
    result = result * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
1,055,240✔
755
  } else if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_GEOMETRY) {
246,165✔
756
    result = result + VARSTR_HEADER_SIZE;
246,189✔
757
  }
758
  return result;
1,301,405✔
759
}
760

761
static int32_t smlBuildFields(SArray **pColumns, SArray **pTags, STableMeta *pTableMeta) {
9,541✔
762
  int32_t code = 0;
9,541✔
763
  int32_t lino = 0;
9,541✔
764
  *pColumns = taosArrayInit((pTableMeta)->tableInfo.numOfColumns, sizeof(SField));
9,541✔
765
  SML_CHECK_NULL(pColumns);
9,541✔
766
  *pTags = taosArrayInit((pTableMeta)->tableInfo.numOfTags, sizeof(SField));
9,541✔
767
  SML_CHECK_NULL(pTags);
9,541✔
768
  for (uint16_t i = 0; i < (pTableMeta)->tableInfo.numOfColumns + (pTableMeta)->tableInfo.numOfTags; i++) {
112,233✔
769
    SField field = {0};
102,692✔
770
    field.type = (pTableMeta)->schema[i].type;
102,692✔
771
    field.bytes = (pTableMeta)->schema[i].bytes;
102,692✔
772
    tstrncpy(field.name, (pTableMeta)->schema[i].name, sizeof(field.name));
102,692✔
773
    if (i < (pTableMeta)->tableInfo.numOfColumns) {
102,692✔
774
      SML_CHECK_NULL(taosArrayPush(*pColumns, &field));
98,676✔
775
    } else {
776
      SML_CHECK_NULL(taosArrayPush(*pTags, &field));
106,708✔
777
    }
778
  }
779
END:
9,541✔
780
  RETURN
9,541✔
781
}
782

783
static int32_t getBytes(uint8_t type, int32_t length) {
4,363,929✔
784
  if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_NCHAR ||
4,363,929✔
785
      type == TSDB_DATA_TYPE_GEOMETRY) {
786
    return smlFindNearestPowerOf2(length, type);
1,301,405✔
787
  } else {
788
    return tDataTypes[type].bytes;
3,062,524✔
789
  }
790
}
791

792
static int32_t smlAddMofidyField(SHashObj *schemaHash, ESchemaAction action, SSmlKv *kv,
9,541✔
793
                                  SArray *results, int32_t preCols, bool isTag) {
794
  int32_t code = TSDB_CODE_SUCCESS;
9,541✔
795
  int32_t lino = 0;
9,541✔
796
  if (action == SCHEMA_ACTION_ADD_COLUMN || action == SCHEMA_ACTION_ADD_TAG) {
16,116✔
797
    SField field = {0};
6,575✔
798
    field.type = kv->type;
6,575✔
799
    field.bytes = getBytes(kv->type, kv->length);
6,575✔
800
    (void)memcpy(field.name, kv->key, TMIN(kv->keyLen, sizeof(field.name) - 1));
6,575✔
801
    SML_CHECK_NULL(taosArrayPush(results, &field));
6,575✔
802
  } else if (action == SCHEMA_ACTION_CHANGE_COLUMN_SIZE || action == SCHEMA_ACTION_CHANGE_TAG_SIZE) {
2,966✔
803
    uint16_t *index = (uint16_t *)taosHashGet(schemaHash, kv->key, kv->keyLen);
2,966✔
804
    if (index == NULL) {
2,966✔
805
      SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
806
    }
807
    uint16_t newIndex = *index;
2,966✔
808
    if (isTag) newIndex -= preCols;
2,966✔
809
    SField *field = (SField *)taosArrayGet(results, newIndex);
2,966✔
810
    SML_CHECK_NULL(field);
2,966✔
811
    field->bytes = getBytes(kv->type, kv->length);
2,966✔
812
  }
813

814
  int32_t maxLen = isTag ? TSDB_MAX_TAGS_LEN : TSDB_MAX_BYTES_PER_ROW;
9,541✔
815
  int32_t len = 0;
9,541✔
816
  for (int j = 0; j < taosArrayGetSize(results); ++j) {
75,094✔
817
    SField *field = taosArrayGet(results, j);
65,553✔
818
    SML_CHECK_NULL(field);
65,553✔
819
    len += field->bytes;
65,553✔
820
  }
821
  if (len > maxLen) {
9,541✔
822
    return isTag ? TSDB_CODE_PAR_INVALID_TAGS_LENGTH : TSDB_CODE_PAR_INVALID_ROW_LENGTH;
2,364✔
823
  }
824

825
END:
7,177✔
826
  RETURN
7,177✔
827
}
828

829
static FORCE_INLINE void smlBuildCreateStbReq(SMCreateStbReq *pReq, int32_t colVer, int32_t tagVer, tb_uid_t suid,
830
                                              int8_t source) {
831
  pReq->colVer = colVer;
208,592✔
832
  pReq->tagVer = tagVer;
208,592✔
833
  pReq->suid = suid;
208,592✔
834
  pReq->source = source;
208,592✔
835
}
208,592✔
836
static int32_t smlSendMetaMsg(SSmlHandle *info, SName *pName, SArray *pColumns, SArray **pTags, STableMeta *pTableMeta,
208,592✔
837
                              ESchemaAction action) {
838
  SRequestObj   *pRequest = NULL;
208,592✔
839
  SMCreateStbReq pReq = {0};
208,592✔
840
  int32_t        code = TSDB_CODE_SUCCESS;
208,592✔
841
  int32_t        lino = 0;
208,592✔
842
  SCmdMsgInfo    pCmdMsg = {0};
208,592✔
843
  char          *pSql = NULL;
208,592✔
844

845
  // put front for free
846
  pReq.numOfColumns = taosArrayGetSize(pColumns);
208,592✔
847
  pReq.pTags = *pTags;
208,592✔
848
  pReq.numOfTags = taosArrayGetSize(*pTags);
208,592✔
849
  *pTags = NULL;
208,592✔
850

851
  pReq.pColumns = taosArrayInit(pReq.numOfColumns, sizeof(SFieldWithOptions));
208,592✔
852
  SML_CHECK_NULL(pReq.pColumns);
208,568✔
853
  for (int32_t i = 0; i < pReq.numOfColumns; ++i) {
2,764,659✔
854
    SField *pField = taosArrayGet(pColumns, i);
2,556,067✔
855
    SML_CHECK_NULL(pField);
2,556,067✔
856
    SFieldWithOptions fieldWithOption = {0};
2,556,067✔
857
    setFieldWithOptions(&fieldWithOption, pField);
2,556,067✔
858
    setDefaultOptionsForField(&fieldWithOption);
2,556,067✔
859
    SML_CHECK_NULL(taosArrayPush(pReq.pColumns, &fieldWithOption));
5,112,182✔
860
  }
861

862
  if (action == SCHEMA_ACTION_CREATE_STABLE) {
208,592✔
863
    pSql = "sml_create_stable";
201,415✔
864
    smlBuildCreateStbReq(&pReq, 1, 1, 0, TD_REQ_FROM_APP);
865
  } else if (action == SCHEMA_ACTION_ADD_TAG || action == SCHEMA_ACTION_CHANGE_TAG_SIZE) {
7,177✔
866
    pSql = (action == SCHEMA_ACTION_ADD_TAG) ? "sml_add_tag" : "sml_modify_tag_size";
3,411✔
867
    smlBuildCreateStbReq(&pReq, pTableMeta->sversion, pTableMeta->tversion + 1, pTableMeta->uid, TD_REQ_FROM_SML);
3,411✔
868
  } else if (action == SCHEMA_ACTION_ADD_COLUMN || action == SCHEMA_ACTION_CHANGE_COLUMN_SIZE) {
3,766✔
869
    pSql = (action == SCHEMA_ACTION_ADD_COLUMN) ? "sml_add_column" : "sml_modify_column_size";
3,766✔
870
    smlBuildCreateStbReq(&pReq, pTableMeta->sversion + 1, pTableMeta->tversion, pTableMeta->uid, TD_REQ_FROM_SML);
3,766✔
871
  } else {
872
    SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
873
  }
874

875
  SML_CHECK_CODE(buildRequest(info->taos->id, pSql, strlen(pSql), NULL, false, &pRequest, 0));
208,592✔
876

877
  pRequest->syncQuery = true;
208,592✔
878
  if (!pRequest->pDb) {
208,592✔
879
    SML_CHECK_CODE(TSDB_CODE_PAR_DB_NOT_SPECIFIED);
×
880
  }
881

882
  if (pReq.numOfTags == 0) {
208,592✔
883
    pReq.numOfTags = 1;
112✔
884
    SField field = {0};
112✔
885
    field.type = TSDB_DATA_TYPE_NCHAR;
112✔
886
    field.bytes = TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
112✔
887
    tstrncpy(field.name, tsSmlTagName, sizeof(field.name));
112✔
888
    SML_CHECK_NULL(taosArrayPush(pReq.pTags, &field));
224✔
889
  }
890

891
  pReq.commentLen = -1;
208,592✔
892
  pReq.igExists = true;
208,592✔
893
  SML_CHECK_CODE(tNameExtractFullName(pName, pReq.name));
208,592✔
894

895
  pCmdMsg.epSet = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
208,592✔
896
  pCmdMsg.msgType = TDMT_MND_CREATE_STB;
208,592✔
897
  pCmdMsg.msgLen = tSerializeSMCreateStbReq(NULL, 0, &pReq);
208,592✔
898
  if (pCmdMsg.msgLen < 0) {
208,592✔
899
    uError("failed to serialize create stable request1, code:%d, terrno:%d", pCmdMsg.msgLen, terrno);
×
900
    SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
901
  }
902
  pCmdMsg.pMsg = taosMemoryMalloc(pCmdMsg.msgLen);
208,592✔
903
  SML_CHECK_NULL(pCmdMsg.pMsg);
208,592✔
904
  code = tSerializeSMCreateStbReq(pCmdMsg.pMsg, pCmdMsg.msgLen, &pReq);
208,592✔
905
  if (code < 0) {
208,592✔
906
    taosMemoryFree(pCmdMsg.pMsg);
×
907
    uError("failed to serialize create stable request2, code:%d, terrno:%d", code, terrno);
×
908
    SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
909
  }
910

911
  SQuery pQuery = {0};
208,592✔
912
  pQuery.execMode = QUERY_EXEC_MODE_RPC;
208,592✔
913
  pQuery.pCmdMsg = &pCmdMsg;
208,592✔
914
  pQuery.msgType = pQuery.pCmdMsg->msgType;
208,592✔
915
  pQuery.stableQuery = true;
208,592✔
916

917
  launchQueryImpl(pRequest, &pQuery, true, NULL);  // no need to check return value
208,592✔
918

919
  if (pRequest->code == TSDB_CODE_SUCCESS) {
208,592✔
920
    SML_CHECK_CODE(catalogRemoveTableMeta(info->pCatalog, pName));
205,416✔
921
  }
922
  code = pRequest->code;
208,592✔
923

924
END:
208,592✔
925
  destroyRequest(pRequest);
208,592✔
926
  tFreeSMCreateStbReq(&pReq);
208,592✔
927
  RETURN
208,568✔
928
}
929

930
static int32_t changeMeta(SSmlHandle *info, SHashObj *hashTmp, SRequestConnInfo *conn,
9,541✔
931
                             SSmlKv *kv, ESchemaAction action, STableMeta **pTableMeta, bool isTag, SName *pName) {
932
  SArray       *pColumns = NULL;
9,541✔
933
  SArray       *pTags = NULL;
9,541✔
934
  int32_t       code = 0;
9,541✔
935
  int32_t       lino = 0;
9,541✔
936
  uDebug("SML:0x%" PRIx64 ", %s change table tag, table:%s, action:%d", info->id, __FUNCTION__, pName->tname, action);
9,541✔
937
  SML_CHECK_CODE(smlBuildFields(&pColumns, &pTags, *pTableMeta));
9,541✔
938
  int32_t preCols = isTag ? (*pTableMeta)->tableInfo.numOfColumns : 0;
9,541✔
939
  SArray  *results = isTag ? pTags : pColumns;
9,541✔
940
  SML_CHECK_CODE(smlAddMofidyField(hashTmp, action, kv, results, preCols, isTag));
9,541✔
941

942
  SML_CHECK_CODE(smlSendMetaMsg(info, pName, pColumns, &pTags, (*pTableMeta), action));
7,177✔
943

944
  if (isTag) {
7,177✔
945
    info->cost.numOfAlterTagSTables++;
3,411✔
946
  } else {
947
    info->cost.numOfAlterColSTables++;
3,766✔
948
  }
949
  taosMemoryFreeClear(*pTableMeta);
7,177✔
950
  SML_CHECK_CODE(catalogRefreshTableMeta(info->pCatalog, conn, pName, -1));
7,177✔
951
  SML_CHECK_CODE(catalogGetSTableMeta(info->pCatalog, conn, pName, pTableMeta));
7,177✔
952

953
END:
9,541✔
954
  taosArrayDestroy(pColumns);
9,541✔
955
  taosArrayDestroy(pTags);
9,541✔
956
  return code;
9,541✔
957
}
958

959
static int32_t smlBuildTempHash(SHashObj *hashTmp, STableMeta *pTableMeta, uint16_t start, uint16_t end) {
3,938,998✔
960
  int32_t code = 0;
3,938,998✔
961
  int32_t lino = 0;
3,938,998✔
962
  for (uint16_t i = start; i < end; i++) {
85,270,238✔
963
    SML_CHECK_CODE(
81,318,726✔
964
        taosHashPut(hashTmp, pTableMeta->schema[i].name, strlen(pTableMeta->schema[i].name), &i, SHORT_BYTES));
965
  }
966

967
END:
3,941,936✔
968
  return code;
3,941,936✔
969
}
970

971
static int32_t smlProcessSchemaAction(SSmlHandle *info, SArray *cols, bool isTag, SRequestConnInfo *conn, STableMeta **tableMeta, SName *pName) {
134,407✔
972
  int32_t code = TSDB_CODE_SUCCESS;
134,407✔
973
  int32_t lino = 0;
134,407✔
974
  SHashObj *colHashTmp = taosHashInit((*tableMeta)->tableInfo.numOfColumns, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
134,407✔
975
  SHashObj *tagHashTmp = taosHashInit((*tableMeta)->tableInfo.numOfTags, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
134,407✔
976
  SML_CHECK_NULL(colHashTmp);
134,255✔
977
  SML_CHECK_NULL(tagHashTmp);
134,255✔
978

979
  for (int j = 0; j < taosArrayGetSize(cols); ++j) {
2,166,362✔
980
    if (j == 0 && !isTag) continue;
2,086,003✔
981
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, j);
1,969,735✔
982
    SML_CHECK_NULL(kv);
1,969,887✔
983
    SML_CHECK_CODE(smlBuildTempHash(tagHashTmp, *tableMeta, (*tableMeta)->tableInfo.numOfColumns,
1,969,887✔
984
                                      (*tableMeta)->tableInfo.numOfColumns + (*tableMeta)->tableInfo.numOfTags));
985
    SML_CHECK_CODE(smlBuildTempHash(colHashTmp, *tableMeta, 0, (*tableMeta)->tableInfo.numOfColumns));
1,969,863✔
986

987
    SHashObj *schemaHashCheck = isTag ? colHashTmp : tagHashTmp;
1,970,039✔
988
    SHashObj *schemaHash = isTag ? tagHashTmp : colHashTmp;
1,970,039✔
989
    if (taosHashGet(schemaHashCheck, kv->key, kv->keyLen) != NULL) {
1,970,039✔
990
      uError("SML:0x%" PRIx64 ", %s duplicated column %s", info->id, __FUNCTION__, kv->key);
509✔
991
      SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
509✔
992
    }
993
    ESchemaAction action = SCHEMA_ACTION_NULL;
1,969,202✔
994
    SML_CHECK_CODE(smlGenerateSchemaAction((*tableMeta)->schema, schemaHash, kv, isTag, &action, info));
1,969,202✔
995
    if (action == SCHEMA_ACTION_NULL) {
1,968,366✔
996
      continue;
1,958,825✔
997
    }
998
    SML_CHECK_CODE(smlCheckAuth(info, conn, pName->tname, AUTH_TYPE_WRITE));
9,541✔
999

1000
    SML_CHECK_CODE(changeMeta(info, schemaHash, conn, kv, action, tableMeta, isTag, pName));
9,541✔
1001
  }
1002

1003
END:
130,522✔
1004
  taosHashCleanup(colHashTmp);
134,407✔
1005
  taosHashCleanup(tagHashTmp);
134,407✔
1006
  RETURN
134,407✔
1007
}
1008

1009
static int32_t smlCheckMeta(SSchema *schema, int32_t length, SArray *cols) {
128,834✔
1010
  int32_t   code = TSDB_CODE_SUCCESS;
128,834✔
1011
  int32_t   lino = 0;
128,834✔
1012
  SHashObj *hashTmp = taosHashInit(length, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
128,834✔
1013
  SML_CHECK_NULL(hashTmp);
128,834✔
1014
  for (int32_t i = 0; i < length; i++) {
2,155,852✔
1015
    SML_CHECK_CODE(taosHashPut(hashTmp, schema[i].name, strlen(schema[i].name), &schema[i], sizeof(SSchema)));
2,026,994✔
1016
  }
1017
  for (int32_t i = 0; i < taosArrayGetSize(cols); i++) {
2,153,904✔
1018
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
2,025,198✔
1019
    SML_CHECK_NULL(kv);
2,025,198✔
1020
    SSchema *sTmp = taosHashGet(hashTmp, kv->key, kv->keyLen);
2,025,198✔
1021
    if (sTmp == NULL) {
2,025,046✔
1022
      SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
1023
    }
1024
    if ((kv->type == TSDB_DATA_TYPE_VARCHAR && kv->length + VARSTR_HEADER_SIZE > sTmp->bytes) ||
2,025,046✔
1025
        (kv->type == TSDB_DATA_TYPE_NCHAR && kv->length * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE > sTmp->bytes)) {
2,025,198✔
1026
      uError("column %s (type %s) bytes invalid. db bytes:%d, kv bytes:%zu", sTmp->name,
×
1027
             tDataTypes[sTmp->type].name, sTmp->bytes, kv->length);
1028
      SML_CHECK_CODE(TSDB_CODE_MND_INVALID_SCHEMA_VER);
×
1029
    }
1030
  }
1031

1032
END:
128,682✔
1033
  taosHashCleanup(hashTmp);
128,682✔
1034
  RETURN
128,834✔
1035
}
1036

1037
static int32_t smlBuildFieldsList(SSmlHandle *info, SSchema *schemaField, SHashObj *schemaHash, SArray *cols,
403,054✔
1038
                                  SArray *results, int32_t numOfCols, bool isTag) {
1039
  int32_t code = TSDB_CODE_SUCCESS;
403,054✔
1040
  int32_t lino = 0;
403,054✔
1041
  for (int j = 0; j < taosArrayGetSize(cols); ++j) {
4,757,466✔
1042
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, j);
4,354,364✔
1043
    SML_CHECK_NULL(kv);
4,354,364✔
1044
    ESchemaAction action = SCHEMA_ACTION_NULL;
4,354,364✔
1045
    SML_CHECK_CODE(smlGenerateSchemaAction(schemaField, schemaHash, kv, isTag, &action, info));
4,354,364✔
1046
    if (action == SCHEMA_ACTION_ADD_COLUMN || action == SCHEMA_ACTION_ADD_TAG) {
8,708,776✔
1047
      SField field = {0};
4,354,364✔
1048
      field.type = kv->type;
4,354,364✔
1049
      field.bytes = getBytes(kv->type, kv->length);
4,354,364✔
1050
      (void)memcpy(field.name, kv->key, TMIN(kv->keyLen, sizeof(field.name) - 1));
4,354,412✔
1051
      SML_CHECK_NULL(taosArrayPush(results, &field));
4,354,412✔
1052
    } else if (action == SCHEMA_ACTION_CHANGE_COLUMN_SIZE || action == SCHEMA_ACTION_CHANGE_TAG_SIZE) {
×
1053
      uint16_t *index = (uint16_t *)taosHashGet(schemaHash, kv->key, kv->keyLen);
×
1054
      if (index == NULL) {
×
1055
        SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
1056
      }
1057
      uint16_t newIndex = *index;
×
1058
      if (isTag) newIndex -= numOfCols;
×
1059
      SField *field = (SField *)taosArrayGet(results, newIndex);
×
1060
      SML_CHECK_NULL(field);
×
1061
      field->bytes = getBytes(kv->type, kv->length);
×
1062
    }
1063
  }
1064

1065
  int32_t maxLen = isTag ? TSDB_MAX_TAGS_LEN : TSDB_MAX_BYTES_PER_ROW;
403,054✔
1066
  int32_t len = 0;
403,054✔
1067
  for (int j = 0; j < taosArrayGetSize(results); ++j) {
4,757,400✔
1068
    SField *field = taosArrayGet(results, j);
4,354,346✔
1069
    SML_CHECK_NULL(field);
4,354,346✔
1070
    len += field->bytes;
4,354,346✔
1071
  }
1072
  if (len > maxLen) {
403,054✔
1073
    return isTag ? TSDB_CODE_PAR_INVALID_TAGS_LENGTH : TSDB_CODE_PAR_INVALID_ROW_LENGTH;
112✔
1074
  }
1075

1076
END:
402,942✔
1077
  RETURN
402,942✔
1078
}
1079

1080
static int32_t smlCreateTable(SSmlHandle *info, SRequestConnInfo *conn, SSmlSTableMeta *sTableData, SName *pName,
201,639✔
1081
                              STableMeta **pTableMeta) {
1082
  int32_t code = 0;
201,639✔
1083
  int32_t lino = 0;
201,639✔
1084
  SArray *pColumns = NULL;
201,639✔
1085
  SArray *pTags = NULL;
201,639✔
1086
  SML_CHECK_CODE(smlCheckAuth(info, conn, NULL, AUTH_TYPE_WRITE));
201,639✔
1087
  uDebug("SML:0x%" PRIx64 ", %s create table:%s", info->id, __FUNCTION__, pName->tname);
201,527✔
1088
  pColumns = taosArrayInit(taosArrayGetSize(sTableData->cols), sizeof(SField));
201,527✔
1089
  SML_CHECK_NULL(pColumns);
201,521✔
1090
  pTags = taosArrayInit(taosArrayGetSize(sTableData->tags), sizeof(SField));
201,521✔
1091
  SML_CHECK_NULL(pTags);
201,527✔
1092
  SML_CHECK_CODE(smlBuildFieldsList(info, NULL, NULL, sTableData->tags, pTags, 0, true));
201,527✔
1093
  SML_CHECK_CODE(smlBuildFieldsList(info, NULL, NULL, sTableData->cols, pColumns, 0, false));
201,527✔
1094
  SML_CHECK_CODE(smlSendMetaMsg(info, pName, pColumns, &pTags, NULL, SCHEMA_ACTION_CREATE_STABLE));
201,415✔
1095
  info->cost.numOfCreateSTables++;
198,239✔
1096
  taosMemoryFreeClear(*pTableMeta);
198,239✔
1097

1098
  SML_CHECK_CODE(catalogGetSTableMeta(info->pCatalog, conn, pName, pTableMeta));
198,239✔
1099

1100
END:
199,983✔
1101
  taosArrayDestroy(pColumns);
201,639✔
1102
  taosArrayDestroy(pTags);
201,639✔
1103
  RETURN
201,639✔
1104
}
1105

1106
static int32_t smlModifyTag(SSmlHandle *info, SRequestConnInfo *conn,
68,302✔
1107
                            SSmlSTableMeta *sTableData, SName *pName, STableMeta **pTableMeta) {
1108
  int32_t       code = 0;
68,302✔
1109
  int32_t       lino = 0;
68,302✔
1110
  SML_CHECK_CODE(
68,302✔
1111
      smlProcessSchemaAction(info, sTableData->tags, true, conn, pTableMeta, pName));
1112
END:
66,105✔
1113
  RETURN
68,302✔
1114
}
1115

1116
static int32_t smlModifyCols(SSmlHandle *info, SRequestConnInfo *conn,
66,105✔
1117
                             SSmlSTableMeta *sTableData, SName *pName, STableMeta **pTableMeta) {
1118
  int32_t       code = 0;
66,105✔
1119
  int32_t       lino = 0;
66,105✔
1120
  SML_CHECK_CODE(
66,105✔
1121
      smlProcessSchemaAction(info, sTableData->cols, false, conn, pTableMeta, pName));
1122
END:
64,417✔
1123
  RETURN
66,105✔
1124
}
1125

1126
static int32_t smlModifyDBSchemas(SSmlHandle *info) {
285,104✔
1127
  uDebug("SML:0x%" PRIx64 ", %s start, format:%d, needModifySchema:%d", info->id, __FUNCTION__, info->dataFormat,
285,104✔
1128
         info->needModifySchema);
1129
  if (info->dataFormat && !info->needModifySchema) {
285,104✔
1130
    return TSDB_CODE_SUCCESS;
17,195✔
1131
  }
1132
  int32_t     code = 0;
267,909✔
1133
  int32_t     lino = 0;
267,909✔
1134
  STableMeta *pTableMeta = NULL;
267,909✔
1135

1136
  SName pName = {TSDB_TABLE_NAME_T, info->taos->acctId, {0}, {0}};
267,909✔
1137
  tstrncpy(pName.dbname, info->pRequest->pDb, sizeof(pName.dbname));
267,909✔
1138

1139
  SRequestConnInfo conn = {0};
267,909✔
1140
  conn.pTrans = info->taos->pAppInfo->pTransporter;
267,909✔
1141
  conn.requestId = info->pRequest->requestId;
267,909✔
1142
  conn.requestObjRefId = info->pRequest->self;
267,909✔
1143
  conn.mgmtEps = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
267,909✔
1144

1145
  SSmlSTableMeta **tmp = (SSmlSTableMeta **)taosHashIterate(info->superTables, NULL);
267,933✔
1146
  while (tmp) {
530,589✔
1147
    SSmlSTableMeta *sTableData = *tmp;
269,941✔
1148
    bool            needCheckMeta = false;  // for multi thread
269,941✔
1149

1150
    size_t superTableLen = 0;
269,941✔
1151
    void  *superTable = taosHashGetKey(tmp, &superTableLen);
269,941✔
1152
    char  *measure = taosMemoryMalloc(superTableLen);
269,941✔
1153
    SML_CHECK_NULL(measure);
271,591✔
1154
    (void)memcpy(measure, superTable, superTableLen);
269,935✔
1155
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
269,935✔
1156
      PROCESS_SLASH_IN_MEASUREMENT(measure, superTableLen);
970,268✔
1157
    }
1158
    smlStrReplace(measure, superTableLen);
269,935✔
1159
    size_t nameLen = TMIN(superTableLen, TSDB_TABLE_NAME_LEN - 1);
269,941✔
1160
    (void)memcpy(pName.tname, measure, nameLen);
269,941✔
1161
    pName.tname[nameLen] = '\0';
269,941✔
1162
    taosMemoryFree(measure);
269,941✔
1163

1164
    pTableMeta = NULL;
269,941✔
1165
    code = catalogGetSTableMeta(info->pCatalog, &conn, &pName, &pTableMeta);
269,941✔
1166

1167
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_MND_STB_NOT_EXIST) {
269,935✔
1168
      SML_CHECK_CODE(smlCreateTable(info, &conn, sTableData, &pName, &pTableMeta));
201,639✔
1169
    } else if (code == TSDB_CODE_SUCCESS) {
68,296✔
1170
      if (smlIsPKTable(pTableMeta)) {
68,302✔
1171
        SML_CHECK_CODE(TSDB_CODE_SML_NOT_SUPPORT_PK);
×
1172
      }
1173

1174
      SML_CHECK_CODE(smlModifyTag(info, &conn, sTableData, &pName, &pTableMeta));
68,302✔
1175
      SML_CHECK_CODE(smlModifyCols(info, &conn, sTableData, &pName, &pTableMeta));
66,105✔
1176

1177
      needCheckMeta = true;
64,417✔
1178
    } else {
1179
      uError("SML:0x%" PRIx64 ", %s load table meta error:%s", info->id, __FUNCTION__, tstrerror(code));
×
1180
      goto END;
×
1181
    }
1182

1183
    if (needCheckMeta) {
262,656✔
1184
      SML_CHECK_CODE(smlCheckMeta(&(pTableMeta->schema[pTableMeta->tableInfo.numOfColumns]),
64,417✔
1185
                                  pTableMeta->tableInfo.numOfTags, sTableData->tags));
1186
      SML_CHECK_CODE(smlCheckMeta(&(pTableMeta->schema[0]), pTableMeta->tableInfo.numOfColumns, sTableData->cols));
64,417✔
1187
    }
1188

1189
    taosMemoryFreeClear(sTableData->tableMeta);
262,656✔
1190
    sTableData->tableMeta = pTableMeta;
262,656✔
1191
    uDebug("SML:0x%" PRIx64 ", %s modify schema uid:%" PRIu64 ", sversion:%d, tversion:%d", info->id, __FUNCTION__,
262,656✔
1192
           pTableMeta->uid, pTableMeta->sversion, pTableMeta->tversion);
1193
    tmp = (SSmlSTableMeta **)taosHashIterate(info->superTables, tmp);
262,656✔
1194
  }
1195
  uDebug("SML:0x%" PRIx64 ", %s end success, format:%d, needModifySchema:%d", info->id, __FUNCTION__, info->dataFormat,
260,648✔
1196
         info->needModifySchema);
1197

1198
  return TSDB_CODE_SUCCESS;
260,648✔
1199

1200
END:
7,285✔
1201
  taosHashCancelIterate(info->superTables, tmp);
7,285✔
1202
  taosMemoryFreeClear(pTableMeta);
7,285✔
1203
  (void)catalogRefreshTableMeta(info->pCatalog, &conn, &pName, 1);  // ignore refresh meta code if there is an error
7,285✔
1204
  uError("SML:0x%" PRIx64 ", %s end failed:%d:%s, format:%d, needModifySchema:%d", info->id, __FUNCTION__, code,
7,285✔
1205
         tstrerror(code), info->dataFormat, info->needModifySchema);
1206

1207
  return code;
7,285✔
1208
}
1209

1210
static int32_t smlInsertMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, SHashObj *checkDuplicate) {
536,540✔
1211
  int32_t code = 0;
536,540✔
1212
  int32_t lino = 0;
536,540✔
1213
  terrno = 0;
536,540✔
1214
  for (int16_t i = 0; i < taosArrayGetSize(cols); ++i) {
6,851,288✔
1215
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
6,317,321✔
1216
    SML_CHECK_NULL(kv);
6,317,327✔
1217
    int ret = taosHashPut(metaHash, kv->key, kv->keyLen, &i, SHORT_BYTES);
6,317,327✔
1218
    if (ret == 0) {
6,317,783✔
1219
      SML_CHECK_NULL(taosArrayPush(metaArray, kv));
6,315,679✔
1220
      if (taosHashGet(checkDuplicate, kv->key, kv->keyLen) != NULL) {
6,315,679✔
1221
        uError("%s duplicated column %s", __FUNCTION__, kv->key);
621✔
1222
        SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
621✔
1223
      }
1224
    } else if (terrno == TSDB_CODE_DUP_KEY) {
2,256✔
1225
      uError("%s duplicated column %s", __FUNCTION__, kv->key);
2,256✔
1226
      return TSDB_CODE_PAR_DUPLICATED_COLUMN;
2,256✔
1227
    }
1228
  }
1229

1230
END:
534,436✔
1231
  RETURN
534,436✔
1232
}
1233

1234
static int32_t smlUpdateMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, bool isTag, SSmlMsgBuf *msg,
30,724,142✔
1235
                             SHashObj *checkDuplicate) {
1236
  int32_t code = 0;
30,724,142✔
1237
  int32_t lino = 0;
30,724,142✔
1238
  for (int i = 0; i < taosArrayGetSize(cols); ++i) {
145,223,054✔
1239
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
114,417,480✔
1240
    SML_CHECK_NULL(kv);
114,395,812✔
1241
    int16_t *index = (int16_t *)taosHashGet(metaHash, kv->key, kv->keyLen);
114,395,812✔
1242
    if (index) {
114,592,896✔
1243
      SSmlKv *value = (SSmlKv *)taosArrayGet(metaArray, *index);
114,588,117✔
1244
      SML_CHECK_NULL(value);
114,608,517✔
1245

1246
      if (isTag) {
114,609,333✔
1247
        if (kv->length > value->length) {
31,838,360✔
1248
          value->length = kv->length;
14,216✔
1249
        }
1250
        continue;
31,835,392✔
1251
      }
1252
      if (kv->type != value->type) {
82,770,973✔
1253
        smlBuildInvalidDataMsg(msg, "the type is not the same like before", kv->key);
×
1254
        SML_CHECK_CODE(TSDB_CODE_SML_NOT_SAME_TYPE);
×
1255
      }
1256

1257
      if (IS_VAR_DATA_TYPE(kv->type) && (kv->length > value->length)) {  // update string len, if bigger
82,773,513✔
1258
        value->length = kv->length;
1,191,264✔
1259
      }
1260
    } else {
1261
      size_t tmp = taosArrayGetSize(metaArray);
4,779✔
1262
      if (tmp > INT16_MAX) {
4,779✔
1263
        smlBuildInvalidDataMsg(msg, "too many cols or tags", kv->key);
×
1264
        SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
1265
      }
1266
      int16_t size = tmp;
4,779✔
1267
      SML_CHECK_CODE(taosHashPut(metaHash, kv->key, kv->keyLen, &size, SHORT_BYTES));
4,779✔
1268
      SML_CHECK_NULL(taosArrayPush(metaArray, kv));
4,779✔
1269
      if (taosHashGet(checkDuplicate, kv->key, kv->keyLen) != NULL) {
4,779✔
1270
        uError("%s duplicated column %s", __FUNCTION__, kv->key);
×
1271
        SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
×
1272
      }
1273
    }
1274
  }
1275

1276
END:
29,987,842✔
1277
  RETURN
30,838,282✔
1278
}
1279

1280
void smlDestroyTableInfo(void *para) {
678,021✔
1281
  SSmlTableInfo *tag = *(SSmlTableInfo **)para;
678,021✔
1282
  for (size_t i = 0; i < taosArrayGetSize(tag->cols); i++) {
16,367,808✔
1283
    SHashObj *kvHash = (SHashObj *)taosArrayGetP(tag->cols, i);
15,689,195✔
1284
    taosHashCleanup(kvHash);
15,687,099✔
1285
  }
1286

1287
  taosArrayDestroy(tag->cols);
677,869✔
1288
  taosArrayDestroyEx(tag->tags, freeSSmlKv);
677,717✔
1289
  taosMemoryFree(tag);
677,717✔
1290
}
677,869✔
1291

1292
void freeSSmlKv(void *data) {
95,835,169✔
1293
  SSmlKv *kv = (SSmlKv *)data;
95,835,169✔
1294
  if (kv->keyEscaped) taosMemoryFreeClear(kv->key);
95,835,169✔
1295
  if (kv->valueEscaped) taosMemoryFreeClear(kv->value);
95,839,929✔
1296
#ifdef USE_GEOS
1297
  if (kv->type == TSDB_DATA_TYPE_GEOMETRY) geosFreeBuffer((void *)(kv->value));
95,685,865✔
1298
#endif
1299
  if (kv->type == TSDB_DATA_TYPE_VARBINARY) taosMemoryFreeClear(kv->value);
95,731,825✔
1300
}
95,750,189✔
1301

1302
void smlDestroyInfo(SSmlHandle *info) {
695,562✔
1303
  if (info == NULL) return;
695,562✔
1304

1305
  taosHashCleanup(info->pVgHash);
347,805✔
1306
  taosHashCleanup(info->childTables);
347,805✔
1307
  taosHashCleanup(info->superTables);
347,805✔
1308
  taosHashCleanup(info->tableUids);
347,805✔
1309

1310
  for (int i = 0; i < taosArrayGetSize(info->tagJsonArray); i++) {
347,805✔
1311
    cJSON *tags = (cJSON *)taosArrayGetP(info->tagJsonArray, i);
×
1312
    cJSON_Delete(tags);
×
1313
  }
1314
  taosArrayDestroy(info->tagJsonArray);
347,805✔
1315

1316
  for (int i = 0; i < taosArrayGetSize(info->valueJsonArray); i++) {
347,805✔
1317
    cJSON *value = (cJSON *)taosArrayGetP(info->valueJsonArray, i);
×
1318
    cJSON_Delete(value);
×
1319
  }
1320
  taosArrayDestroy(info->valueJsonArray);
347,805✔
1321

1322
  taosArrayDestroyEx(info->preLineTagKV, freeSSmlKv);
347,805✔
1323
  taosArrayDestroyP(info->escapedStringList, NULL);
347,805✔
1324

1325
  if (!info->dataFormat) {
347,805✔
1326
    for (int i = 0; i < info->lineNum; i++) {
16,009,516✔
1327
      taosArrayDestroyEx(info->lines[i].colArray, freeSSmlKv);
15,717,010✔
1328
      if (info->lines[i].measureTagsLen != 0 && info->protocol != TSDB_SML_LINE_PROTOCOL) {
15,709,634✔
1329
        taosMemoryFree(info->lines[i].measureTag);
153,669✔
1330
      }
1331
    }
1332
    taosMemoryFree(info->lines);
292,950✔
1333
  }
1334
  if (info->protocol == TSDB_SML_JSON_PROTOCOL) {
347,805✔
1335
    taosMemoryFreeClear(info->preLine.tags);
132,048✔
1336
  }
1337
  cJSON_Delete(info->root);
347,805✔
1338
  taosMemoryFreeClear(info);
347,805✔
1339
}
1340

1341
int32_t smlBuildSmlInfo(TAOS *taos, SSmlHandle **handle) {
347,781✔
1342
  int32_t     code = TSDB_CODE_SUCCESS;
347,781✔
1343
  int32_t     lino = 0;
347,781✔
1344
  SSmlHandle *info = (SSmlHandle *)taosMemoryCalloc(1, sizeof(SSmlHandle));
347,781✔
1345
  SML_CHECK_NULL(info);
347,781✔
1346
  if (taos != NULL) {
347,781✔
1347
    info->taos = acquireTscObj(*(int64_t *)taos);
347,781✔
1348
    SML_CHECK_NULL(info->taos);
347,781✔
1349
    SML_CHECK_CODE(catalogGetHandle(info->taos->pAppInfo->clusterId, &info->pCatalog));
347,781✔
1350
  }
1351

1352
  info->pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
347,781✔
1353
  SML_CHECK_NULL(info->pVgHash);
347,781✔
1354
  info->childTables = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
347,781✔
1355
  SML_CHECK_NULL(info->childTables);
347,805✔
1356
  info->tableUids = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
347,805✔
1357
  SML_CHECK_NULL(info->tableUids);
347,805✔
1358
  info->superTables = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
347,805✔
1359
  SML_CHECK_NULL(info->superTables);
347,805✔
1360
  taosHashSetFreeFp(info->superTables, smlDestroySTableMeta);
347,805✔
1361
  taosHashSetFreeFp(info->childTables, smlDestroyTableInfo);
347,757✔
1362

1363
  info->id = smlGenId();
347,781✔
1364
  SML_CHECK_CODE(smlInitHandle(&info->pQuery));
347,629✔
1365
  info->dataFormat = true;
347,781✔
1366
  info->tagJsonArray = taosArrayInit(8, POINTER_BYTES);
347,781✔
1367
  SML_CHECK_NULL(info->tagJsonArray);
347,805✔
1368
  info->valueJsonArray = taosArrayInit(8, POINTER_BYTES);
347,805✔
1369
  SML_CHECK_NULL(info->valueJsonArray);
347,805✔
1370
  info->preLineTagKV = taosArrayInit(8, sizeof(SSmlKv));
347,805✔
1371
  SML_CHECK_NULL(info->preLineTagKV);
347,605✔
1372
  info->escapedStringList = taosArrayInit(8, POINTER_BYTES);
347,605✔
1373
  SML_CHECK_NULL(info->escapedStringList);
347,805✔
1374

1375
  *handle = info;
347,781✔
1376
  info = NULL;
347,629✔
1377

1378
END:
347,629✔
1379
  smlDestroyInfo(info);
347,629✔
1380
  RETURN
347,757✔
1381
}
1382

1383
static int32_t smlPushCols(SArray *colsArray, SArray *cols) {
15,693,957✔
1384
  int32_t   code = TSDB_CODE_SUCCESS;
15,693,957✔
1385
  int32_t   lino = 0;
15,693,957✔
1386
  SHashObj *kvHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
15,693,957✔
1387
  SML_CHECK_NULL(kvHash);
15,691,733✔
1388
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
102,750,539✔
1389
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
86,950,544✔
1390
    SML_CHECK_NULL(kv);
86,955,788✔
1391
    terrno = 0;
86,955,788✔
1392
    code = taosHashPut(kvHash, kv->key, kv->keyLen, &kv, POINTER_BYTES);
86,957,672✔
1393
    if (terrno == TSDB_CODE_DUP_KEY) {
87,046,208✔
1394
      uError("%s duplicated column %s", __FUNCTION__, kv->key);
618✔
1395
      SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
266✔
1396
    }
1397
    SML_CHECK_CODE(code);
86,866,574✔
1398
  }
1399

1400
  SML_CHECK_NULL(taosArrayPush(colsArray, &kvHash));
15,686,087✔
1401
  return code;
15,686,087✔
1402
END:
242✔
1403
  taosHashCleanup(kvHash);
618✔
1404
  RETURN
618✔
1405
}
1406

1407
static int32_t smlParseEnd(SSmlHandle *info) {
287,390✔
1408
  uDebug("SML:0x%" PRIx64 ", %s start, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat, info->lineNum);
287,390✔
1409
  int32_t code = 0;
287,390✔
1410
  int32_t lino = 0;
287,390✔
1411
  if (info->dataFormat) return TSDB_CODE_SUCCESS;
287,390✔
1412

1413
  for (int32_t i = 0; i < info->lineNum; i++) {
15,934,757✔
1414
    SSmlLineInfo  *elements = info->lines + i;
15,694,288✔
1415
    SSmlTableInfo *tinfo = NULL;
15,696,364✔
1416
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
15,696,364✔
1417
      SSmlTableInfo **tmp =
1418
          (SSmlTableInfo **)taosHashGet(info->childTables, elements->measure, elements->measureTagsLen);
15,514,608✔
1419
      if (tmp) tinfo = *tmp;
15,517,720✔
1420
    } else {
1421
      SSmlTableInfo **tmp = (SSmlTableInfo **)taosHashGet(info->childTables, elements->measureTag,
181,312✔
1422
                                                          elements->measureLen + elements->tagsLen);
181,312✔
1423
      if (tmp) tinfo = *tmp;
179,980✔
1424
    }
1425

1426
    if (tinfo == NULL) {
15,697,700✔
1427
      uError("SML:0x%" PRIx64 ", get oneTable failed, line num:%d", info->id, i);
×
1428
      smlBuildInvalidDataMsg(&info->msgBuf, "get oneTable failed", elements->measure);
×
1429
      return TSDB_CODE_SML_INVALID_DATA;
×
1430
    }
1431

1432
    if (taosArrayGetSize(tinfo->tags) > TSDB_MAX_TAGS) {
15,697,700✔
1433
      smlBuildInvalidDataMsg(&info->msgBuf, "too many tags than 128", NULL);
1,573✔
1434
      return TSDB_CODE_PAR_INVALID_TAGS_NUM;
1,573✔
1435
    }
1436

1437
    if (taosArrayGetSize(elements->colArray) + taosArrayGetSize(tinfo->tags) > TSDB_MAX_COLUMNS) {
15,695,979✔
1438
      smlBuildInvalidDataMsg(&info->msgBuf, "too many columns than 4096", NULL);
394✔
1439
      return TSDB_CODE_PAR_TOO_MANY_COLUMNS;
394✔
1440
    }
1441

1442
    SML_CHECK_CODE(smlPushCols(tinfo->cols, elements->colArray));
15,692,033✔
1443

1444
    SSmlSTableMeta **tableMeta =
1445
        (SSmlSTableMeta **)taosHashGet(info->superTables, elements->measure, elements->measureLen);
15,687,271✔
1446
    if (tableMeta) {  // update meta
15,696,743✔
1447
      uDebug("SML:0x%" PRIx64 ", %s update meta, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
15,427,269✔
1448
             info->lineNum);
1449
      SML_CHECK_CODE(smlUpdateMeta((*tableMeta)->colHash, (*tableMeta)->cols, elements->colArray, false, &info->msgBuf,
15,427,269✔
1450
                                   (*tableMeta)->tagHash));
1451
      SML_CHECK_CODE(smlUpdateMeta((*tableMeta)->tagHash, (*tableMeta)->tags, tinfo->tags, true, &info->msgBuf,
15,424,893✔
1452
                                   (*tableMeta)->colHash));
1453
    } else {
1454
      uDebug("SML:0x%" PRIx64 ", %s add meta, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
269,474✔
1455
             info->lineNum);
1456
      SSmlSTableMeta *meta = NULL;
269,474✔
1457
      SML_CHECK_CODE(smlBuildSTableMeta(info->dataFormat, &meta));
269,474✔
1458
      code = taosHashPut(info->superTables, elements->measure, elements->measureLen, &meta, POINTER_BYTES);
269,474✔
1459
      if (code != TSDB_CODE_SUCCESS) {
269,474✔
1460
        smlDestroySTableMeta(&meta);
×
1461
        SML_CHECK_CODE(code);
×
1462
      }
1463
      SML_CHECK_CODE(smlInsertMeta(meta->tagHash, meta->tags, tinfo->tags, NULL));
269,474✔
1464
      SML_CHECK_CODE(smlInsertMeta(meta->colHash, meta->cols, elements->colArray, meta->tagHash));
267,218✔
1465
    }
1466
  }
1467
  uDebug("SML:0x%" PRIx64 ", %s end, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat, info->lineNum);
264,589✔
1468

1469
END:
53,385✔
1470
  RETURN
268,084✔
1471
}
1472

1473
static int32_t smlInsertData(SSmlHandle *info) {
277,819✔
1474
  int32_t         code = TSDB_CODE_SUCCESS;
277,819✔
1475
  int32_t         lino = 0;
277,819✔
1476
  char           *measure = NULL;
277,819✔
1477
  SSmlTableInfo **oneTable = NULL;
277,819✔
1478
  uDebug("SML:0x%" PRIx64 ", %s start, format:%d", info->id, __FUNCTION__, info->dataFormat);
277,819✔
1479

1480
  if (info->pRequest->dbList == NULL) {
277,819✔
1481
    info->pRequest->dbList = taosArrayInit(1, TSDB_DB_FNAME_LEN);
277,819✔
1482
    SML_CHECK_NULL(info->pRequest->dbList);
277,819✔
1483
  }
1484
  char *data = (char *)taosArrayReserve(info->pRequest->dbList, 1);
277,819✔
1485
  SML_CHECK_NULL(data);
277,819✔
1486
  SName pName = {TSDB_TABLE_NAME_T, info->taos->acctId, {0}, {0}};
277,819✔
1487
  tstrncpy(pName.dbname, info->pRequest->pDb, sizeof(pName.dbname));
277,819✔
1488
  (void)tNameGetFullDbName(&pName, data);  // ignore
277,819✔
1489

1490
  oneTable = (SSmlTableInfo **)taosHashIterate(info->childTables, NULL);
277,819✔
1491
  while (oneTable) {
871,130✔
1492
    SSmlTableInfo *tableData = *oneTable;
593,511✔
1493

1494
    int measureLen = tableData->sTableNameLen;
593,511✔
1495
    measure = (char *)taosMemoryMalloc(tableData->sTableNameLen);
594,271✔
1496
    SML_CHECK_NULL(measure);
594,119✔
1497
    (void)memcpy(measure, tableData->sTableName, tableData->sTableNameLen);
594,119✔
1498
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
593,967✔
1499
      PROCESS_SLASH_IN_MEASUREMENT(measure, measureLen);
4,135,848✔
1500
    }
1501
    smlStrReplace(measure, measureLen);
594,575✔
1502
    (void)memcpy(pName.tname, measure, measureLen);
593,967✔
1503
    pName.tname[measureLen] = '\0';
593,967✔
1504

1505
    if (info->pRequest->tableList == NULL) {
594,119✔
1506
      info->pRequest->tableList = taosArrayInit(1, sizeof(SName));
277,843✔
1507
      SML_CHECK_NULL(info->pRequest->tableList);
277,795✔
1508
    }
1509
    SML_CHECK_NULL(taosArrayPush(info->pRequest->tableList, &pName));
1,188,014✔
1510
    tstrncpy(pName.tname, tableData->childTableName, sizeof(pName.tname));
593,943✔
1511

1512
    SRequestConnInfo conn = {0};
593,943✔
1513
    conn.pTrans = info->taos->pAppInfo->pTransporter;
593,943✔
1514
    conn.requestId = info->pRequest->requestId;
594,247✔
1515
    conn.requestObjRefId = info->pRequest->self;
594,247✔
1516
    conn.mgmtEps = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
594,247✔
1517

1518
    SML_CHECK_CODE(smlCheckAuth(info, &conn, pName.tname, AUTH_TYPE_WRITE));
594,271✔
1519

1520
    SVgroupInfo vg = {0};
592,047✔
1521
    SML_CHECK_CODE(catalogGetTableHashVgroup(info->pCatalog, &conn, &pName, &vg));
592,807✔
1522
    SML_CHECK_CODE(taosHashPut(info->pVgHash, (const char *)&vg.vgId, sizeof(vg.vgId), (char *)&vg, sizeof(vg)));
594,047✔
1523

1524
    SSmlSTableMeta **pMeta =
1525
        (SSmlSTableMeta **)taosHashGet(info->superTables, tableData->sTableName, tableData->sTableNameLen);
593,135✔
1526
    if (unlikely(NULL == pMeta || NULL == *pMeta || NULL == (*pMeta)->tableMeta)) {
593,743✔
1527
      uError("SML:0x%" PRIx64 ", %s NULL == pMeta. table name:%s", info->id, __FUNCTION__, tableData->childTableName);
×
1528
      SML_CHECK_CODE(TSDB_CODE_SML_INTERNAL_ERROR);
×
1529
    }
1530

1531
    // use tablemeta of stable to save vgid and uid of child table
1532
    (*pMeta)->tableMeta->vgId = vg.vgId;
593,895✔
1533
    (*pMeta)->tableMeta->uid = tableData->uid;  // one table merge data block together according uid
594,047✔
1534
    uDebug("SML:0x%" PRIx64 ", %s table:%s, uid:%" PRIu64 ", format:%d", info->id, __FUNCTION__, pName.tname,
594,047✔
1535
           tableData->uid, info->dataFormat);
1536

1537
    SML_CHECK_CODE(smlBindData(info->pQuery, info->dataFormat, tableData->tags, (*pMeta)->cols, tableData->cols,
594,047✔
1538
                               (*pMeta)->tableMeta, tableData->childTableName, measure, measureLen, info->ttl,
1539
                               info->msgBuf.buf, info->msgBuf.len, info->taos->optionInfo.charsetCxt));
1540
    taosMemoryFreeClear(measure);
592,983✔
1541
    oneTable = (SSmlTableInfo **)taosHashIterate(info->childTables, oneTable);
592,527✔
1542
  }
1543

1544
  SML_CHECK_CODE(smlBuildOutput(info->pQuery, info->pVgHash));
277,619✔
1545
  info->cost.insertRpcTime = taosGetTimestampUs();
277,619✔
1546

1547
  SAppClusterSummary *pActivity = &info->taos->pAppInfo->summary;
277,619✔
1548
  (void)atomic_add_fetch_64((int64_t *)&pActivity->numOfInsertsReq, 1);  // no need to check return code
277,619✔
1549

1550
  launchQueryImpl(info->pRequest, info->pQuery, true, NULL);  // no need to check return code
277,619✔
1551

1552
  uDebug("SML:0x%" PRIx64 ", %s end, format:%d, code:%d,%s", info->id, __FUNCTION__, info->dataFormat,
277,619✔
1553
         info->pRequest->code, tstrerror(info->pRequest->code));
1554

1555
  return info->pRequest->code;
277,619✔
1556

1557
END:
680✔
1558
  taosMemoryFree(measure);
224✔
1559
  taosHashCancelIterate(info->childTables, oneTable);
224✔
1560
  RETURN
224✔
1561
}
1562

1563
static void smlPrintStatisticInfo(SSmlHandle *info) {
347,805✔
1564
  uDebug(
347,805✔
1565
      "SML:0x%" PRIx64
1566
      " 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 \
1567
        parse cost:%" PRId64 ",schema cost:%" PRId64 ",bind cost:%" PRId64 ",rpc cost:%" PRId64 ",total cost:%" PRId64,
1568
      info->id, info->cost.code, tstrerror(info->cost.code), info->cost.lineNum, info->cost.numOfSTables,
1569
      info->cost.numOfCTables, info->cost.numOfCreateSTables, info->cost.numOfAlterTagSTables,
1570
      info->cost.numOfAlterColSTables, info->cost.schemaTime - info->cost.parseTime,
1571
      info->cost.insertBindTime - info->cost.schemaTime, info->cost.insertRpcTime - info->cost.insertBindTime,
1572
      info->cost.endTime - info->cost.insertRpcTime, info->cost.endTime - info->cost.parseTime);
1573
}
347,805✔
1574

1575
int32_t smlClearForRerun(SSmlHandle *info) {
292,774✔
1576
  int32_t code = 0;
292,774✔
1577
  int32_t lino = 0;
292,774✔
1578
  info->reRun = false;
292,774✔
1579

1580
  taosHashClear(info->childTables);
292,774✔
1581
  taosHashClear(info->superTables);
292,926✔
1582
  taosHashClear(info->tableUids);
292,926✔
1583

1584
  if (!info->dataFormat) {
292,926✔
1585
    info->lines = (SSmlLineInfo *)taosMemoryCalloc(info->lineNum, sizeof(SSmlLineInfo));
292,926✔
1586
    SML_CHECK_NULL(info->lines);
292,926✔
1587
  }
1588

1589
  taosArrayClearP(info->escapedStringList, NULL);
292,774✔
1590
  if (info->protocol == TSDB_SML_JSON_PROTOCOL) {
292,926✔
1591
    taosMemoryFreeClear(info->preLine.tags);
104,896✔
1592
  }
1593
  (void)memset(&info->preLine, 0, sizeof(SSmlLineInfo));
292,926✔
1594
  info->currSTableMeta = NULL;
292,926✔
1595
  info->currTableDataCtx = NULL;
292,926✔
1596

1597
  SVnodeModifyOpStmt *stmt = (SVnodeModifyOpStmt *)(info->pQuery->pRoot);
292,926✔
1598
  stmt->freeHashFunc(stmt->pTableBlockHashObj);
292,926✔
1599
  stmt->pTableBlockHashObj = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
292,926✔
1600
  SML_CHECK_NULL(stmt->pTableBlockHashObj);
292,926✔
1601

1602
END:
292,926✔
1603
  RETURN
292,926✔
1604
}
1605

1606
static void printRaw(int64_t id, int lineNum, int numLines, ELogLevel level, char *data, int32_t len) {
2,016✔
1607
  char *print = taosMemoryMalloc(len + 1);
2,016✔
1608
  if (print == NULL) {
2,016✔
1609
    uError("SML:0x%" PRIx64 ", smlParseLine failed. code :%d", id, terrno);
×
1610
    return;
×
1611
  }
1612
  (void)memcpy(print, data, len);
2,016✔
1613
  print[len] = '\0';
2,016✔
1614
  if (level == DEBUG_DEBUG) {
2,016✔
1615
    uDebug("SML:0x%" PRIx64 ", smlParseLine is raw, line %d/%d :%s", id, lineNum, numLines, print);
2,016✔
1616
  } else if (level == DEBUG_ERROR) {
×
1617
    uError("SML:0x%" PRIx64 ", smlParseLine failed. line %d/%d :%s", id, lineNum, numLines, print);
×
1618
  }
1619
  taosMemoryFree(print);
2,016✔
1620
}
1621

1622
static bool getLine(SSmlHandle *info, char *lines[], char **rawLine, char *rawLineEnd, int numLines, int i, char **tmp,
15,770,397✔
1623
                    int *len) {
1624
  if (lines) {
15,770,397✔
1625
    *tmp = lines[i];
15,788,734✔
1626
    *len = strlen(*tmp);
15,797,318✔
1627
  } else if (*rawLine) {
1,439✔
1628
    *tmp = *rawLine;
3,567✔
1629
    while (*rawLine < rawLineEnd) {
18,224,825✔
1630
      if (*((*rawLine)++) == '\n') {
18,223,249✔
1631
        break;
1,991✔
1632
      }
1633
      (*len)++;
18,221,258✔
1634
    }
1635
    if (IS_COMMENT(info->protocol, (*tmp)[0])) {  // this line is comment
3,567✔
1636
      return false;
112✔
1637
    }
1638
  }
1639

1640
  if (*rawLine != NULL && (uDebugFlag & DEBUG_DEBUG)) {
15,796,185✔
1641
    printRaw(info->id, i, numLines, DEBUG_DEBUG, *tmp, *len);
2,016✔
1642
  } else {
1643
    uDebug("SML:0x%" PRIx64 ", smlParseLine is not raw, line %d/%d :%s", info->id, i, numLines, *tmp);
15,486,329✔
1644
  }
1645
  return true;
15,777,413✔
1646
}
1647

1648
static int32_t smlParseJson(SSmlHandle *info, char *lines[], char *rawLine) {
132,048✔
1649
  int32_t code = TSDB_CODE_SUCCESS;
132,048✔
1650
  if (lines) {
132,048✔
1651
    code = smlParseJSONExt(info, *lines);
131,934✔
1652
  } else if (rawLine) {
114✔
1653
    code = smlParseJSONExt(info, rawLine);
114✔
1654
  }
1655
  if (code != TSDB_CODE_SUCCESS) {
132,024✔
1656
    uError("%s failed code:%d", __FUNCTION__, code);
26,485✔
1657
  }
1658
  return code;
132,024✔
1659
}
1660

1661
static int32_t smlParseStart(SSmlHandle *info, char *lines[], char *rawLine, char *rawLineEnd, int numLines) {
347,781✔
1662
  uDebug("SML:0x%" PRIx64 ", %s start", info->id, __FUNCTION__);
347,781✔
1663
  int32_t code = TSDB_CODE_SUCCESS;
347,799✔
1664
  if (info->protocol == TSDB_SML_JSON_PROTOCOL) {
347,799✔
1665
    return smlParseJson(info, lines, rawLine);
132,042✔
1666
  }
1667

1668
  char   *oldRaw = rawLine;
215,605✔
1669
  int32_t i = 0;
215,605✔
1670
  while (i < numLines) {
15,964,724✔
1671
    char *tmp = NULL;
15,782,849✔
1672
    int   len = 0;
15,752,361✔
1673
    if (!getLine(info, lines, &rawLine, rawLineEnd, numLines, i, &tmp, &len)) {
15,787,885✔
1674
      continue;
2,773✔
1675
    }
1676
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
15,781,433✔
1677
      if (info->dataFormat) {
15,606,425✔
1678
        SSmlLineInfo element = {0};
131,639✔
1679
        code = smlParseInfluxString(info, tmp, tmp + len, &element);
131,639✔
1680
      } else {
1681
        code = smlParseInfluxString(info, tmp, tmp + len, info->lines + i);
15,477,298✔
1682
      }
1683
    } else if (info->protocol == TSDB_SML_TELNET_PROTOCOL) {
191,588✔
1684
      if (info->dataFormat) {
191,564✔
1685
        SSmlLineInfo element = {0};
123,397✔
1686
        code = smlParseTelnetString(info, (char *)tmp, (char *)tmp + len, &element);
123,397✔
1687
        if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
123,421✔
1688
      } else {
1689
        code = smlParseTelnetString(info, (char *)tmp, (char *)tmp + len, info->lines + i);
68,167✔
1690
      }
1691
    }
1692
    if (code != TSDB_CODE_SUCCESS) {
15,765,293✔
1693
      if (rawLine != NULL) {
33,906✔
1694
        printRaw(info->id, i, numLines, DEBUG_ERROR, tmp, len);
×
1695
      } else {
1696
        uError("SML:0x%" PRIx64 ", %s failed. line %d :%s", info->id, __FUNCTION__, i, tmp);
33,906✔
1697
      }
1698
      return code;
33,906✔
1699
    }
1700
    if (info->reRun) {
15,731,387✔
1701
      uDebug("SML:0x%" PRIx64 ", %s re run", info->id, __FUNCTION__);
188,026✔
1702
      i = 0;
188,026✔
1703
      rawLine = oldRaw;
188,026✔
1704
      code = smlClearForRerun(info);
188,026✔
1705
      if (code != TSDB_CODE_SUCCESS) {
187,878✔
1706
        return code;
×
1707
      }
1708
      continue;
187,878✔
1709
    }
1710
    i++;
15,543,953✔
1711
  }
1712
  uDebug("SML:0x%" PRIx64 ", %s end", info->id, __FUNCTION__);
181,875✔
1713

1714
  return code;
181,851✔
1715
}
1716

1717
static int smlProcess(SSmlHandle *info, char *lines[], char *rawLine, char *rawLineEnd, int numLines) {
347,781✔
1718
  int32_t code = TSDB_CODE_SUCCESS;
347,781✔
1719
  int32_t lino = 0;
347,781✔
1720
  int32_t retryNum = 0;
347,781✔
1721

1722
  info->cost.parseTime = taosGetTimestampUs();
347,757✔
1723

1724
  SML_CHECK_CODE(smlParseStart(info, lines, rawLine, rawLineEnd, numLines));
347,605✔
1725
  SML_CHECK_CODE(smlParseEnd(info));
287,414✔
1726

1727
  info->cost.lineNum = info->lineNum;
281,928✔
1728
  info->cost.numOfSTables = taosHashGetSize(info->superTables);
281,928✔
1729
  info->cost.numOfCTables = taosHashGetSize(info->childTables);
281,952✔
1730
  info->cost.schemaTime = taosGetTimestampUs();
281,928✔
1731

1732
  do {
1733
    code = smlModifyDBSchemas(info);
285,128✔
1734
    if (code != TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER && code != TSDB_CODE_SDB_OBJ_CREATING && code != TSDB_CODE_SYN_NOT_LEADER &&
285,104✔
1735
        code != TSDB_CODE_MND_TRANS_CONFLICT) {
1736
      break;
281,952✔
1737
    }
1738
    taosMsleep(100);
3,152✔
1739
    uInfo("SML:0x%" PRIx64 ", smlModifyDBSchemas retry code:%s, times:%d", info->id, tstrerror(code), retryNum);
3,176✔
1740
  } while (retryNum++ < taosHashGetSize(info->superTables) * MAX_RETRY_TIMES);
3,176✔
1741

1742
  SML_CHECK_CODE(code);
281,928✔
1743
  info->cost.insertBindTime = taosGetTimestampUs();
277,819✔
1744
  SML_CHECK_CODE(smlInsertData(info));
277,819✔
1745

1746
END:
277,619✔
1747
  RETURN
347,805✔
1748
}
1749

1750
void smlSetReqSQL(SRequestObj *request, char *lines[], char *rawLine, char *rawLineEnd) {
347,781✔
1751
  if (request->pTscObj->pAppInfo->serverCfg.monitorParas.tsSlowLogScope & SLOW_LOG_TYPE_INSERT) {
347,781✔
1752
    int32_t len = 0;
×
1753
    int32_t rlen = 0;
×
1754
    char   *p = NULL;
×
1755

1756
    if (lines && lines[0]) {
×
1757
      len = strlen(lines[0]);
×
1758
      p = lines[0];
×
1759
    } else if (rawLine) {
×
1760
      if (rawLineEnd) {
×
1761
        len = rawLineEnd - rawLine;
×
1762
      } else {
1763
        len = strlen(rawLine);
×
1764
      }
1765
      p = rawLine;
×
1766
    }
1767

1768
    if (NULL == p) {
×
1769
      return;
×
1770
    }
1771

1772
    rlen = TMIN(len, tsMaxSQLLength);
×
1773
    rlen = TMAX(rlen, 0);
×
1774

1775
    char *sql = taosMemoryMalloc(rlen + 1);
×
1776
    if (NULL == sql) {
×
1777
      uError("malloc %d for sml sql failed", rlen + 1);
×
1778
      return;
×
1779
    }
1780
    (void)memcpy(sql, p, rlen);
×
1781
    sql[rlen] = 0;
×
1782

1783
    request->sqlstr = sql;
×
1784
    request->sqlLen = rlen;
×
1785
  }
1786
}
1787

1788
TAOS_RES *taos_schemaless_insert_inner(TAOS *taos, char *lines[], char *rawLine, char *rawLineEnd, int numLines,
347,629✔
1789
                                       int protocol, int precision, int32_t ttl, int64_t reqid, char *tbnameKey) {
1790
  int32_t      code = TSDB_CODE_SUCCESS;
347,629✔
1791
  int32_t      lino = 0;
347,629✔
1792
  SRequestObj *request = NULL;
347,629✔
1793
  SSmlHandle  *info = NULL;
347,781✔
1794
  int          cnt = 0;
347,781✔
1795
  while (1) {
×
1796
    SML_CHECK_CODE(buildRequest(*(int64_t *)taos, "", 0, NULL, false, &request, reqid));
347,781✔
1797
    SSmlMsgBuf msg = {request->msgBufLen, request->msgBuf};
347,805✔
1798
    request->code = smlBuildSmlInfo(taos, &info);
347,805✔
1799
    SML_CHECK_CODE(request->code);
347,781✔
1800

1801
    info->pRequest = request;
347,781✔
1802
    info->pRequest->pQuery = info->pQuery;
347,629✔
1803
    info->ttl = ttl;
347,629✔
1804
    info->precision = precision;
347,629✔
1805
    info->protocol = (TSDB_SML_PROTOCOL_TYPE)protocol;
347,781✔
1806
    info->msgBuf.buf = info->pRequest->msgBuf;
347,781✔
1807
    info->msgBuf.len = ERROR_MSG_BUF_DEFAULT_SIZE;
347,781✔
1808
    info->lineNum = numLines;
347,629✔
1809
    info->tbnameKey = tbnameKey;
347,781✔
1810

1811
    smlSetReqSQL(request, lines, rawLine, rawLineEnd);
347,629✔
1812

1813
    if (request->pDb == NULL) {
347,653✔
1814
      request->code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1815
      smlBuildInvalidDataMsg(&msg, "Database not specified", NULL);
×
1816
      goto END;
×
1817
    }
1818

1819
    if (protocol < TSDB_SML_LINE_PROTOCOL || protocol > TSDB_SML_JSON_PROTOCOL) {
347,653✔
1820
      request->code = TSDB_CODE_SML_INVALID_PROTOCOL_TYPE;
×
1821
      smlBuildInvalidDataMsg(&msg, "protocol invalidate", NULL);
×
1822
      goto END;
×
1823
    }
1824

1825
    if (protocol == TSDB_SML_LINE_PROTOCOL &&
347,805✔
1826
        (precision < TSDB_SML_TIMESTAMP_NOT_CONFIGURED || precision > TSDB_SML_TIMESTAMP_NANO_SECONDS)) {
131,639✔
1827
      request->code = TSDB_CODE_SML_INVALID_PRECISION_TYPE;
×
1828
      smlBuildInvalidDataMsg(&msg, "precision invalidate for line protocol", NULL);
×
1829
      goto END;
×
1830
    }
1831

1832
    if (protocol == TSDB_SML_JSON_PROTOCOL) {
347,805✔
1833
      numLines = 1;
132,048✔
1834
    } else if (numLines <= 0) {
215,757✔
1835
      request->code = TSDB_CODE_SML_INVALID_DATA;
×
1836
      smlBuildInvalidDataMsg(&msg, "line num is invalid", NULL);
×
1837
      goto END;
×
1838
    }
1839

1840
    code = smlProcess(info, lines, rawLine, rawLineEnd, numLines);
347,805✔
1841
    request->code = code;
347,805✔
1842
    info->cost.endTime = taosGetTimestampUs();
676,937✔
1843
    info->cost.code = code;
347,805✔
1844
    if (NEED_CLIENT_HANDLE_ERROR(code) || code == TSDB_CODE_SDB_OBJ_CREATING || code == TSDB_CODE_PAR_VALUE_TOO_LONG ||
347,805✔
1845
        code == TSDB_CODE_MND_TRANS_CONFLICT || code == TSDB_CODE_SYN_NOT_LEADER) {
347,805✔
1846
      if (cnt++ >= 10) {
×
1847
        uInfo("SML:%" PRIx64 " retry:%d/10 end code:%d, msg:%s", info->id, cnt, code, tstrerror(code));
×
1848
        break;
18,673✔
1849
      }
1850
      taosMsleep(100);
×
1851
      uInfo("SML:%" PRIx64 " retry:%d/10,ver is old retry or object is creating code:%d, msg:%s", info->id, cnt, code,
×
1852
            tstrerror(code));
1853
      code = refreshMeta(request->pTscObj, request);
×
1854
      if (code != 0) {
×
1855
        uInfo("SML:%" PRIx64 " refresh meta error code:%d, msg:%s", info->id, code, tstrerror(code));
×
1856
      }
1857
      smlDestroyInfo(info);
×
1858
      info = NULL;
×
1859
      taos_free_result(request);
×
1860
      request = NULL;
×
1861
      continue;
×
1862
    }
1863
    smlPrintStatisticInfo(info);
347,805✔
1864
    break;
347,805✔
1865
  }
1866

1867
END:
347,805✔
1868
  smlDestroyInfo(info);
347,805✔
1869
  return (TAOS_RES *)request;
347,805✔
1870
}
1871

1872
/**
1873
 * taos_schemaless_insert() parse and insert data points into database according to
1874
 * different protocol.
1875
 *
1876
 * @param $lines input array may contain multiple lines, each line indicates a data point.
1877
 *               If protocol=2 is used input array should contain single JSON
1878
 *               string(e.g. char *lines[] = {"$JSON_string"}). If need to insert
1879
 *               multiple data points in JSON format, should include them in $JSON_string
1880
 *               as a JSON array.
1881
 * @param $numLines indicates how many data points in $lines.
1882
 *                  If protocol = 2 is used this param will be ignored as $lines should
1883
 *                  contain single JSON string.
1884
 * @param $protocol indicates which protocol to use for parsing:
1885
 *                  0 - influxDB line protocol
1886
 *                  1 - OpenTSDB telnet line protocol
1887
 *                  2 - OpenTSDB JSON format protocol
1888
 * @return TAOS_RES
1889
 */
1890

1891
TAOS_RES *taos_schemaless_insert_ttl_with_reqid_tbname_key(TAOS *taos, char *lines[], int numLines, int protocol,
346,487✔
1892
                                                           int precision, int32_t ttl, int64_t reqid, char *tbnameKey) {
1893
  if (taos == NULL || lines == NULL || numLines < 0) {
346,487✔
1894
    terrno = TSDB_CODE_INVALID_PARA;
176✔
1895
    return NULL;
×
1896
  }
1897
  for (int i = 0; i < numLines; i++) {
16,306,573✔
1898
    if (lines[i] == NULL) {
15,960,566✔
1899
      terrno = TSDB_CODE_INVALID_PARA;
×
1900
      return NULL;
×
1901
    }
1902
  }
1903
  return taos_schemaless_insert_inner(taos, lines, NULL, NULL, numLines, protocol, precision, ttl, reqid, tbnameKey);
346,007✔
1904
}
1905

1906
TAOS_RES *taos_schemaless_insert_ttl_with_reqid(TAOS *taos, char *lines[], int numLines, int protocol, int precision,
346,463✔
1907
                                                int32_t ttl, int64_t reqid) {
1908
  return taos_schemaless_insert_ttl_with_reqid_tbname_key(taos, lines, numLines, protocol, precision, ttl, reqid, NULL);
346,463✔
1909
}
1910

1911
TAOS_RES *taos_schemaless_insert(TAOS *taos, char *lines[], int numLines, int protocol, int precision) {
346,039✔
1912
  return taos_schemaless_insert_ttl_with_reqid(taos, lines, numLines, protocol, precision, TSDB_DEFAULT_TABLE_TTL, 0);
346,039✔
1913
}
1914

1915
TAOS_RES *taos_schemaless_insert_ttl(TAOS *taos, char *lines[], int numLines, int protocol, int precision,
448✔
1916
                                     int32_t ttl) {
1917
  return taos_schemaless_insert_ttl_with_reqid(taos, lines, numLines, protocol, precision, ttl, 0);
448✔
1918
}
1919

1920
TAOS_RES *taos_schemaless_insert_with_reqid(TAOS *taos, char *lines[], int numLines, int protocol, int precision,
×
1921
                                            int64_t reqid) {
1922
  return taos_schemaless_insert_ttl_with_reqid(taos, lines, numLines, protocol, precision, TSDB_DEFAULT_TABLE_TTL,
×
1923
                                               reqid);
1924
}
1925

1926
static int32_t getRawLineLen(char *lines, int len, int protocol) {
1,166✔
1927
  int   numLines = 0;
1,166✔
1928
  char *tmp = lines;
1,166✔
1929
  for (int i = 0; i < len; i++) {
9,215,242✔
1930
    if (lines[i] == '\n' || i == len - 1) {
9,214,076✔
1931
      if (!IS_COMMENT(protocol, tmp[0])) {  // ignore comment
2,857✔
1932
        numLines++;
2,745✔
1933
      }
1934
      tmp = lines + i + 1;
2,857✔
1935
    }
1936
  }
1937
  return numLines;
1,166✔
1938
}
1939

1940
TAOS_RES *taos_schemaless_insert_raw_ttl_with_reqid_tbname_key(TAOS *taos, char *lines, int len, int32_t *totalRows,
1,166✔
1941
                                                               int protocol, int precision, int32_t ttl, int64_t reqid,
1942
                                                               char *tbnameKey) {
1943
  if (taos == NULL || lines == NULL || len < 0) {
1,166✔
1944
    terrno = TSDB_CODE_INVALID_PARA;
×
1945
    return NULL;
×
1946
  }
1947
  int numLines = getRawLineLen(lines, len, protocol);
1,166✔
1948
  if (totalRows != NULL) {
1,166✔
1949
    *totalRows = numLines;
1,166✔
1950
  }
1951
  return taos_schemaless_insert_inner(taos, NULL, lines, lines + len, numLines, protocol, precision, ttl, reqid,
1,166✔
1952
                                      tbnameKey);
1953
}
1954

1955
TAOS_RES *taos_schemaless_insert_raw_ttl_with_reqid(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
1,166✔
1956
                                                    int precision, int32_t ttl, int64_t reqid) {
1957
  return taos_schemaless_insert_raw_ttl_with_reqid_tbname_key(taos, lines, len, totalRows, protocol, precision, ttl,
1,166✔
1958
                                                              reqid, NULL);
1959
}
1960

1961
TAOS_RES *taos_schemaless_insert_raw_with_reqid(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
76✔
1962
                                                int precision, int64_t reqid) {
1963
  return taos_schemaless_insert_raw_ttl_with_reqid(taos, lines, len, totalRows, protocol, precision,
76✔
1964
                                                   TSDB_DEFAULT_TABLE_TTL, reqid);
1965
}
1966
TAOS_RES *taos_schemaless_insert_raw_ttl(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
×
1967
                                         int precision, int32_t ttl) {
1968
  return taos_schemaless_insert_raw_ttl_with_reqid(taos, lines, len, totalRows, protocol, precision, ttl, 0);
×
1969
}
1970

1971
TAOS_RES *taos_schemaless_insert_raw(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
841✔
1972
                                     int precision) {
1973
  return taos_schemaless_insert_raw_ttl_with_reqid(taos, lines, len, totalRows, protocol, precision,
841✔
1974
                                                   TSDB_DEFAULT_TABLE_TTL, 0);
1975
}
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