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

taosdata / TDengine / #4541

19 Jul 2025 01:13AM UTC coverage: 56.753% (-1.6%) from 58.31%
#4541

push

travis-ci

web-flow
fix: subquery memleak (#32024)

124299 of 282344 branches covered (44.02%)

Branch coverage included in aggregate %.

181106 of 255787 relevant lines covered (70.8%)

24937406.43 hits per line

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

73.69
/source/client/src/clientSml.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include <ctype.h>
17
#include <stdio.h>
18
#include <stdlib.h>
19
#include <string.h>
20

21
#include "clientSml.h"
22

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

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

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

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

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

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

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

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

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

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

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

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

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

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

125
  int32_t      code = TSDB_CODE_SUCCESS;
3,562✔
126
  SUserAuthRes authRes = {0};
3,562✔
127

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

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

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

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

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

167
  return convertTimePrecision(tsInt64, fromPrecision, toPrecision);
102,143✔
168
}
169

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

176
  tag->sTableName = measure;
2,850✔
177
  tag->sTableNameLen = measureLen;
2,850✔
178

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

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

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

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

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

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

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

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

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

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

292
END:
162✔
293
  info->dataFormat = false;
162✔
294
  info->reRun = true;
162✔
295
  return false;
162✔
296
}
297

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

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

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

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

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

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

344
  return false;
1,162✔
345
}
346

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

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

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

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

386
    tinfo->tags = taosArrayDup(info->preLineTagKV, NULL);
2,852✔
387
    SML_CHECK_NULL(tinfo->tags);
2,850!
388
    for (size_t i = 0; i < taosArrayGetSize(info->preLineTagKV); i++) {
23,927✔
389
      SSmlKv *kv = (SSmlKv *)taosArrayGet(info->preLineTagKV, i);
21,080✔
390
      SML_CHECK_NULL(kv);
21,079✔
391
      if (kv->keyEscaped) kv->key = NULL;
21,077✔
392
      if (kv->valueEscaped) kv->value = NULL;
21,077✔
393
    }
394

395
    SML_CHECK_CODE(smlSetCTableName(tinfo, info->tbnameKey));
2,850!
396
    SML_CHECK_CODE(getTableUid(info, elements, tinfo));
2,850!
397
    if (info->dataFormat) {
2,852✔
398
      info->currSTableMeta->uid = tinfo->uid;
777✔
399
      SML_CHECK_CODE(smlInitTableDataCtx(info->pQuery, info->currSTableMeta, &tinfo->tableDataCtx));
777!
400
    }
401
  } else {
402
    tinfo = *oneTable;
73✔
403
  }
404
  if (info->dataFormat) info->currTableDataCtx = tinfo->tableDataCtx;
2,924✔
405
  return TSDB_CODE_SUCCESS;
2,924✔
406

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

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

422
END:
5,091✔
423
  clearColValArraySml(info->currTableDataCtx->pValues);
5,091✔
424
  RETURN
5,083!
425
}
426

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

438
END:
2,193✔
439
  RETURN
2,193!
440
}
441

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

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

463
  return TSDB_CODE_SUCCESS;
97,673✔
464
}
465

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

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

518
END:
1,202✔
519
  RETURN
1,272!
520
}
521

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

528
  if (strlen(oneTable->childTableName) == 0) {
2,850✔
529
    dst = taosArrayDup(oneTable->tags, NULL);
2,780✔
530
    SML_CHECK_NULL(dst);
2,781✔
531
    if (oneTable->sTableNameLen >= TSDB_TABLE_NAME_LEN) {
2,779!
532
      SML_CHECK_CODE(TSDB_CODE_SML_INTERNAL_ERROR);
×
533
    }
534
    char          superName[TSDB_TABLE_NAME_LEN] = {0};
2,779✔
535
    RandTableName rName = {dst, NULL, (uint8_t)oneTable->sTableNameLen, oneTable->childTableName};
2,779✔
536
    if (tsSmlDot2Underline) {
2,779✔
537
      (void)memcpy(superName, oneTable->sTableName, oneTable->sTableNameLen);
1,400✔
538
      smlStrReplace(superName, oneTable->sTableNameLen);
1,400✔
539
      rName.stbFullName = superName;
1,402✔
540
    } else {
541
      rName.stbFullName = oneTable->sTableName;
1,379✔
542
    }
543

544
    SML_CHECK_CODE(buildChildTableName(&rName));
2,781!
545
  }
546

547
END:
70✔
548
  taosArrayDestroy(dst);
2,852✔
549
  RETURN
2,852!
550
}
551

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

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

584
  meta->tags = taosArrayInit(32, sizeof(SSmlKv));
2,021✔
585
  SML_CHECK_NULL(meta->tags);
2,019!
586

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

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

598
int32_t smlParseNumber(SSmlKv *kvVal, SSmlMsgBuf *msg) {
30,225,428✔
599
  const char *pVal = kvVal->value;
30,225,428✔
600
  int32_t     len = kvVal->length;
30,225,428✔
601
  char       *endptr = NULL;
30,225,428✔
602
  double      result = taosStr2Double(pVal, &endptr);
30,225,428✔
603
  if (pVal == endptr) {
30,290,096✔
604
    RETURN_FALSE
1,376✔
605
  }
606

607
  int32_t left = len - (endptr - pVal);
30,288,720✔
608
  if (left == 0) {
30,288,720✔
609
    SET_DOUBLE
1,704✔
610
  } else if (left == 3) {
30,287,016✔
611
    if (endptr[0] == 'f' || endptr[0] == 'F') {
20,284,951!
612
      if (endptr[1] == '6' && endptr[2] == '4') {
10,179,760!
613
        SET_DOUBLE
10,001,502✔
614
      } else if (endptr[1] == '3' && endptr[2] == '2') {
178,258!
615
        SET_FLOAT
189,162!
616
      } else {
617
        RETURN_FALSE
1✔
618
      }
619
    } else if (endptr[0] == 'i' || endptr[0] == 'I') {
10,105,191✔
620
      if (endptr[1] == '6' && endptr[2] == '4') {
102,436!
621
        SET_BIGINT
1,529✔
622
      } else if (endptr[1] == '3' && endptr[2] == '2') {
100,907!
623
        SET_INT
99,494!
624
      } else if (endptr[1] == '1' && endptr[2] == '6') {
1,413!
625
        SET_SMALL_INT
1,459✔
626
      } else {
627
        RETURN_FALSE
1✔
628
      }
629
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
10,002,755!
630
      if (endptr[1] == '6' && endptr[2] == '4') {
10,002,754!
631
        SET_UBIGINT
1,441✔
632
      } else if (endptr[1] == '3' && endptr[2] == '2') {
10,001,313!
633
        SET_UINT
10,000,657✔
634
      } else if (endptr[1] == '1' && endptr[2] == '6') {
656!
635
        SET_USMALL_INT
657✔
636
      } else {
637
        RETURN_FALSE
×
638
      }
639
    } else {
640
      RETURN_FALSE
1✔
641
    }
642
  } else if (left == 2) {
10,002,065✔
643
    if (endptr[0] == 'i' || endptr[0] == 'I') {
10,002,164✔
644
      if (endptr[1] == '8') {
1,502!
645
        SET_TINYINT
1,503✔
646
      } else {
647
        RETURN_FALSE
×
648
      }
649
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
10,000,662!
650
      if (endptr[1] == '8') {
10,000,657!
651
        SET_UTINYINT
10,000,657✔
652
      } else {
653
        RETURN_FALSE
×
654
      }
655
    } else {
656
      RETURN_FALSE
5✔
657
    }
658
  } else if (left == 1) {
21✔
659
    if (endptr[0] == 'i' || endptr[0] == 'I') {
35!
660
      SET_BIGINT
34✔
661
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
1!
662
      SET_UBIGINT
×
663
    } else {
664
      RETURN_FALSE
1✔
665
    }
666
  } else {
667
    RETURN_FALSE;
18✔
668
  }
669
  return true;
30,299,880✔
670
}
671

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

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

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

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

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

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

698
  return id;
1,915✔
699
}
700

701
static int32_t smlGenerateSchemaAction(SSchema *colField, SHashObj *colHash, SSmlKv *kv, bool isTag,
19,124✔
702
                                       ESchemaAction *action, SSmlHandle *info) {
703
  uint16_t *index = colHash ? (uint16_t *)taosHashGet(colHash, kv->key, kv->keyLen) : NULL;
19,124✔
704
  if (index) {
19,128✔
705
    if (colField[*index].type != kv->type) {
4,075✔
706
      snprintf(info->msgBuf.buf, info->msgBuf.len,
14✔
707
               "SML:0x%" PRIx64 ", %s point type and db type mismatch, db type:%s, point type:%s, key:%s", info->id,
708
               __FUNCTION__, tDataTypes[colField[*index].type].name, tDataTypes[kv->type].name, kv->key);
14✔
709
      uError("%s", info->msgBuf.buf);
14!
710
      return TSDB_CODE_SML_INVALID_DATA;
14✔
711
    }
712

713
    if (((colField[*index].type == TSDB_DATA_TYPE_VARCHAR || colField[*index].type == TSDB_DATA_TYPE_VARBINARY ||
4,061!
714
          colField[*index].type == TSDB_DATA_TYPE_GEOMETRY) &&
3,661✔
715
         (colField[*index].bytes - VARSTR_HEADER_SIZE) < kv->length) ||
406✔
716
        (colField[*index].type == TSDB_DATA_TYPE_NCHAR &&
4,044✔
717
         ((colField[*index].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE < kv->length))) {
1,963✔
718
      if (isTag) {
92✔
719
        *action = SCHEMA_ACTION_CHANGE_TAG_SIZE;
76✔
720
      } else {
721
        *action = SCHEMA_ACTION_CHANGE_COLUMN_SIZE;
16✔
722
      }
723
    }
724
  } else {
725
    if (isTag) {
15,053✔
726
      *action = SCHEMA_ACTION_ADD_TAG;
6,981✔
727
    } else {
728
      *action = SCHEMA_ACTION_ADD_COLUMN;
8,072✔
729
    }
730
  }
731
  return TSDB_CODE_SUCCESS;
19,114✔
732
}
733

734
#define BOUNDARY 1024
735
static int32_t smlFindNearestPowerOf2(int32_t length, uint8_t type) {
4,979✔
736
  int32_t result = 1;
4,979✔
737
  if (length >= BOUNDARY) {
4,979✔
738
    result = length;
33✔
739
  } else {
740
    while (result <= length) {
23,187✔
741
      result <<= 1;
18,241✔
742
    }
743
  }
744

745
  if ((type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_GEOMETRY) &&
4,979✔
746
      result > TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE) {
1,014!
747
    result = TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE;
×
748
  } else if (type == TSDB_DATA_TYPE_NCHAR && result > (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
4,979!
749
    result = (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
750
  }
751

752
  if (type == TSDB_DATA_TYPE_NCHAR) {
4,979✔
753
    result = result * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
3,965✔
754
  } else if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_GEOMETRY) {
1,014!
755
    result = result + VARSTR_HEADER_SIZE;
1,022✔
756
  }
757
  return result;
4,979✔
758
}
759

760
static int32_t smlProcessSchemaAction(SSmlHandle *info, SSchema *schemaField, SHashObj *schemaHash, SArray *cols,
737✔
761
                                      SHashObj *schemaHashCheck, ESchemaAction *action, bool isTag) {
762
  int32_t code = TSDB_CODE_SUCCESS;
737✔
763
  int32_t lino = 0;
737✔
764
  for (int j = 0; j < taosArrayGetSize(cols); ++j) {
4,678✔
765
    if (j == 0 && !isTag) continue;
3,968✔
766
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, j);
3,609✔
767
    SML_CHECK_NULL(kv);
3,608!
768
    SML_CHECK_CODE(smlGenerateSchemaAction(schemaField, schemaHash, kv, isTag, action, info));
3,608✔
769
    if (taosHashGet(schemaHashCheck, kv->key, kv->keyLen) != NULL) {
3,595✔
770
      SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
13!
771
    }
772
  }
773

774
END:
714✔
775
  RETURN
741!
776
}
777

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

801
END:
696✔
802
  taosHashCleanup(hashTmp);
697✔
803
  RETURN
697!
804
}
805

806
static int32_t getBytes(uint8_t type, int32_t length) {
14,984✔
807
  if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_NCHAR ||
14,984✔
808
      type == TSDB_DATA_TYPE_GEOMETRY) {
809
    return smlFindNearestPowerOf2(length, type);
4,963✔
810
  } else {
811
    return tDataTypes[type].bytes;
10,021✔
812
  }
813
}
814

815
static int32_t smlBuildFieldsList(SSmlHandle *info, SSchema *schemaField, SHashObj *schemaHash, SArray *cols,
1,965✔
816
                                  SArray *results, int32_t numOfCols, bool isTag) {
817
  int32_t code = TSDB_CODE_SUCCESS;
1,965✔
818
  int32_t lino = 0;
1,965✔
819
  for (int j = 0; j < taosArrayGetSize(cols); ++j) {
17,505✔
820
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, j);
15,525✔
821
    SML_CHECK_NULL(kv);
15,523!
822
    ESchemaAction action = SCHEMA_ACTION_NULL;
15,523✔
823
    SML_CHECK_CODE(smlGenerateSchemaAction(schemaField, schemaHash, kv, isTag, &action, info));
15,523!
824
    if (action == SCHEMA_ACTION_ADD_COLUMN || action == SCHEMA_ACTION_ADD_TAG) {
30,478✔
825
      SField field = {0};
14,936✔
826
      field.type = kv->type;
14,936✔
827
      field.bytes = getBytes(kv->type, kv->length);
14,936✔
828
      (void)memcpy(field.name, kv->key, TMIN(kv->keyLen, sizeof(field.name) - 1));
14,936✔
829
      SML_CHECK_NULL(taosArrayPush(results, &field));
14,971!
830
    } else if (action == SCHEMA_ACTION_CHANGE_COLUMN_SIZE || action == SCHEMA_ACTION_CHANGE_TAG_SIZE) {
571✔
831
      uint16_t *index = (uint16_t *)taosHashGet(schemaHash, kv->key, kv->keyLen);
48✔
832
      if (index == NULL) {
46!
833
        SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
834
      }
835
      uint16_t newIndex = *index;
46✔
836
      if (isTag) newIndex -= numOfCols;
46✔
837
      SField *field = (SField *)taosArrayGet(results, newIndex);
46✔
838
      SML_CHECK_NULL(field);
46!
839
      field->bytes = getBytes(kv->type, kv->length);
46✔
840
    }
841
  }
842

843
  int32_t maxLen = isTag ? TSDB_MAX_TAGS_LEN : TSDB_MAX_BYTES_PER_ROW;
1,965✔
844
  int32_t len = 0;
1,965✔
845
  for (int j = 0; j < taosArrayGetSize(results); ++j) {
17,484✔
846
    SField *field = taosArrayGet(results, j);
15,509✔
847
    SML_CHECK_NULL(field);
15,519!
848
    len += field->bytes;
15,519✔
849
  }
850
  if (len > maxLen) {
1,962✔
851
    return isTag ? TSDB_CODE_PAR_INVALID_TAGS_LENGTH : TSDB_CODE_PAR_INVALID_ROW_LENGTH;
12✔
852
  }
853

854
END:
1,950✔
855
  RETURN
1,950!
856
}
857

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

874
  // put front for free
875
  pReq.numOfColumns = taosArrayGetSize(pColumns);
1,013✔
876
  pReq.pTags = *pTags;
1,014✔
877
  pReq.numOfTags = taosArrayGetSize(*pTags);
1,014✔
878
  *pTags = NULL;
1,014✔
879

880
  pReq.pColumns = taosArrayInit(pReq.numOfColumns, sizeof(SFieldWithOptions));
1,014✔
881
  SML_CHECK_NULL(pReq.pColumns);
1,013!
882
  for (int32_t i = 0; i < pReq.numOfColumns; ++i) {
9,484✔
883
    SField *pField = taosArrayGet(pColumns, i);
8,473✔
884
    SML_CHECK_NULL(pField);
8,470!
885
    SFieldWithOptions fieldWithOption = {0};
8,470✔
886
    setFieldWithOptions(&fieldWithOption, pField);
8,470✔
887
    setDefaultOptionsForField(&fieldWithOption);
8,452✔
888
    SML_CHECK_NULL(taosArrayPush(pReq.pColumns, &fieldWithOption));
16,902!
889
  }
890

891
  if (action == SCHEMA_ACTION_CREATE_STABLE) {
1,011✔
892
    pSql = "sml_create_stable";
932✔
893
    smlBuildCreateStbReq(&pReq, 1, 1, 0, TD_REQ_FROM_APP);
894
  } else if (action == SCHEMA_ACTION_ADD_TAG || action == SCHEMA_ACTION_CHANGE_TAG_SIZE) {
79✔
895
    pSql = (action == SCHEMA_ACTION_ADD_TAG) ? "sml_add_tag" : "sml_modify_tag_size";
46✔
896
    smlBuildCreateStbReq(&pReq, pTableMeta->sversion, pTableMeta->tversion + 1, pTableMeta->uid, TD_REQ_FROM_SML);
46✔
897
  } else if (action == SCHEMA_ACTION_ADD_COLUMN || action == SCHEMA_ACTION_CHANGE_COLUMN_SIZE) {
33!
898
    pSql = (action == SCHEMA_ACTION_ADD_COLUMN) ? "sml_add_column" : "sml_modify_column_size";
33✔
899
    smlBuildCreateStbReq(&pReq, pTableMeta->sversion + 1, pTableMeta->tversion, pTableMeta->uid, TD_REQ_FROM_SML);
33✔
900
  } else {
901
    SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
902
  }
903

904
  SML_CHECK_CODE(buildRequest(info->taos->id, pSql, strlen(pSql), NULL, false, &pRequest, 0));
1,011!
905

906
  pRequest->syncQuery = true;
1,014✔
907
  if (!pRequest->pDb) {
1,014!
908
    SML_CHECK_CODE(TSDB_CODE_PAR_DB_NOT_SPECIFIED);
×
909
  }
910

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

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

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

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

946
  launchQueryImpl(pRequest, &pQuery, true, NULL);  // no need to check return value
1,013✔
947

948
  if (pRequest->code == TSDB_CODE_SUCCESS) {
1,014✔
949
    SML_CHECK_CODE(catalogRemoveTableMeta(info->pCatalog, pName));
879!
950
  }
951
  code = pRequest->code;
1,014✔
952

953
END:
1,014✔
954
  destroyRequest(pRequest);
1,014✔
955
  tFreeSMCreateStbReq(&pReq);
1,014✔
956
  RETURN
1,014!
957
}
958

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

977
  SML_CHECK_CODE(catalogGetSTableMeta(info->pCatalog, conn, pName, pTableMeta));
798!
978

979
END:
798✔
980
  taosArrayDestroy(pColumns);
945✔
981
  taosArrayDestroy(pTags);
945✔
982
  RETURN
945!
983
}
984

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

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

1023
    SML_CHECK_CODE(smlSendMetaMsg(info, pName, pColumns, &pTags, (*pTableMeta), action));
48!
1024

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

1031
END:
359✔
1032
  taosArrayDestroy(pColumns);
382✔
1033
  taosArrayDestroy(pTags);
379✔
1034
  RETURN
381!
1035
}
1036

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

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

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

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

1062
END:
349✔
1063
  taosArrayDestroy(pColumns);
359✔
1064
  taosArrayDestroy(pTags);
357✔
1065
  RETURN
359!
1066
}
1067

1068
static int32_t smlBuildTempHash(SHashObj *hashTmp, STableMeta *pTableMeta, uint16_t start, uint16_t end) {
763✔
1069
  int32_t code = 0;
763✔
1070
  int32_t lino = 0;
763✔
1071
  for (uint16_t i = start; i < end; i++) {
4,782✔
1072
    SML_CHECK_CODE(
4,018!
1073
        taosHashPut(hashTmp, pTableMeta->schema[i].name, strlen(pTableMeta->schema[i].name), &i, SHORT_BYTES));
1074
  }
1075

1076
END:
764✔
1077
  return code;
764✔
1078
}
1079

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

1092
  SName pName = {TSDB_TABLE_NAME_T, info->taos->acctId, {0}, {0}};
1,264✔
1093
  tstrncpy(pName.dbname, info->pRequest->pDb, sizeof(pName.dbname));
1,264✔
1094

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

1101
  SSmlSTableMeta **tmp = (SSmlSTableMeta **)taosHashIterate(info->superTables, NULL);
1,265✔
1102
  while (tmp) {
2,408✔
1103
    SSmlSTableMeta *sTableData = *tmp;
1,325✔
1104
    bool            needCheckMeta = false;  // for multi thread
1,325✔
1105

1106
    size_t superTableLen = 0;
1,325✔
1107
    void  *superTable = taosHashGetKey(tmp, &superTableLen);
1,325✔
1108
    char  *measure = taosMemoryMalloc(superTableLen);
1,327!
1109
    SML_CHECK_NULL(measure);
1,508!
1110
    (void)memcpy(measure, superTable, superTableLen);
1,327✔
1111
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
1,327✔
1112
      PROCESS_SLASH_IN_MEASUREMENT(measure, superTableLen);
3,602✔
1113
    }
1114
    smlStrReplace(measure, superTableLen);
1,327✔
1115
    size_t nameLen = TMIN(superTableLen, TSDB_TABLE_NAME_LEN - 1);
1,326✔
1116
    (void)memcpy(pName.tname, measure, nameLen);
1,326✔
1117
    pName.tname[nameLen] = '\0';
1,326✔
1118
    taosMemoryFree(measure);
1,326!
1119

1120
    code = catalogGetSTableMeta(info->pCatalog, &conn, &pName, &pTableMeta);
1,325✔
1121

1122
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_MND_STB_NOT_EXIST) {
1,326!
1123
      SML_CHECK_CODE(smlCreateTable(info, &conn, sTableData, &pName, &pTableMeta));
945✔
1124
    } else if (code == TSDB_CODE_SUCCESS) {
381!
1125
      if (smlIsPKTable(pTableMeta)) {
382!
1126
        SML_CHECK_CODE(TSDB_CODE_SML_NOT_SUPPORT_PK);
×
1127
      }
1128

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

1139
      SML_CHECK_CODE(smlModifyTag(info, colHashTmp, tagHashTmp, &conn, sTableData, &pName, &pTableMeta));
382✔
1140
      SML_CHECK_CODE(smlModifyCols(info, tagHashTmp, colHashTmp, &conn, sTableData, &pName, &pTableMeta));
358✔
1141

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

1152
    if (needCheckMeta) {
1,147✔
1153
      SML_CHECK_CODE(smlCheckMeta(&(pTableMeta->schema[pTableMeta->tableInfo.numOfColumns]),
348✔
1154
                                  pTableMeta->tableInfo.numOfTags, sTableData->tags));
1155
      SML_CHECK_CODE(smlCheckMeta(&(pTableMeta->schema[0]), pTableMeta->tableInfo.numOfColumns, sTableData->cols));
348!
1156
    }
1157

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

1167
  return TSDB_CODE_SUCCESS;
1,083✔
1168

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

1178
  return code;
181✔
1179
}
1180

1181
static int32_t smlInsertMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, SHashObj *checkDuplicate) {
2,406✔
1182
  int32_t code = 0;
2,406✔
1183
  int32_t lino = 0;
2,406✔
1184
  terrno = 0;
2,406✔
1185
  for (int16_t i = 0; i < taosArrayGetSize(cols); ++i) {
19,628✔
1186
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
17,248✔
1187
    SML_CHECK_NULL(kv);
17,270!
1188
    int ret = taosHashPut(metaHash, kv->key, kv->keyLen, &i, SHORT_BYTES);
17,251✔
1189
    if (ret == 0) {
17,263✔
1190
      SML_CHECK_NULL(taosArrayPush(metaArray, kv));
17,250!
1191
      if (taosHashGet(checkDuplicate, kv->key, kv->keyLen) != NULL) {
17,250✔
1192
        SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
19!
1193
      }
1194
    } else if (terrno == TSDB_CODE_DUP_KEY) {
12!
1195
      return TSDB_CODE_PAR_DUPLICATED_COLUMN;
12✔
1196
    }
1197
  }
1198

1199
END:
2,394✔
1200
  RETURN
2,394!
1201
}
1202

1203
static int32_t smlUpdateMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, bool isTag, SSmlMsgBuf *msg,
170,820✔
1204
                             SHashObj *checkDuplicate) {
1205
  int32_t code = 0;
170,820✔
1206
  int32_t lino = 0;
170,820✔
1207
  for (int i = 0; i < taosArrayGetSize(cols); ++i) {
664,958✔
1208
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
434,402✔
1209
    SML_CHECK_NULL(kv);
420,016!
1210
    int16_t *index = (int16_t *)taosHashGet(metaHash, kv->key, kv->keyLen);
420,016✔
1211
    if (index) {
450,135✔
1212
      SSmlKv *value = (SSmlKv *)taosArrayGet(metaArray, *index);
450,133✔
1213
      SML_CHECK_NULL(value);
444,087!
1214

1215
      if (isTag) {
494,045✔
1216
        if (kv->length > value->length) {
188,528✔
1217
          value->length = kv->length;
73✔
1218
        }
1219
        continue;
188,528✔
1220
      }
1221
      if (kv->type != value->type) {
305,517!
1222
        smlBuildInvalidDataMsg(msg, "the type is not the same like before", kv->key);
×
1223
        SML_CHECK_CODE(TSDB_CODE_SML_NOT_SAME_TYPE);
×
1224
      }
1225

1226
      if (IS_VAR_DATA_TYPE(kv->type) && (kv->length > value->length)) {  // update string len, if bigger
305,517!
1227
        value->length = kv->length;
172✔
1228
      }
1229
    } else {
1230
      size_t tmp = taosArrayGetSize(metaArray);
2✔
1231
      if (tmp > INT16_MAX) {
93!
1232
        smlBuildInvalidDataMsg(msg, "too many cols or tags", kv->key);
×
1233
        SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
1234
      }
1235
      int16_t size = tmp;
93✔
1236
      SML_CHECK_CODE(taosHashPut(metaHash, kv->key, kv->keyLen, &size, SHORT_BYTES));
93!
1237
      SML_CHECK_NULL(taosArrayPush(metaArray, kv));
93!
1238
      if (taosHashGet(checkDuplicate, kv->key, kv->keyLen) != NULL) {
93!
1239
        SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
×
1240
      }
1241
    }
1242
  }
1243

1244
END:
114,512✔
1245
  RETURN
114,512!
1246
}
1247

1248
void smlDestroyTableInfo(void *para) {
2,852✔
1249
  SSmlTableInfo *tag = *(SSmlTableInfo **)para;
2,852✔
1250
  for (size_t i = 0; i < taosArrayGetSize(tag->cols); i++) {
105,222✔
1251
    SHashObj *kvHash = (SHashObj *)taosArrayGetP(tag->cols, i);
102,176✔
1252
    taosHashCleanup(kvHash);
101,752✔
1253
  }
1254

1255
  taosArrayDestroy(tag->cols);
2,862✔
1256
  taosArrayDestroyEx(tag->tags, freeSSmlKv);
2,852✔
1257
  taosMemoryFree(tag);
2,852!
1258
}
2,852✔
1259

1260
void freeSSmlKv(void *data) {
415,224✔
1261
  SSmlKv *kv = (SSmlKv *)data;
415,224✔
1262
  if (kv->keyEscaped) taosMemoryFreeClear(kv->key);
415,224!
1263
  if (kv->valueEscaped) taosMemoryFreeClear(kv->value);
415,224!
1264
#ifdef USE_GEOS
1265
  if (kv->type == TSDB_DATA_TYPE_GEOMETRY) geosFreeBuffer((void *)(kv->value));
415,224✔
1266
#endif
1267
  if (kv->type == TSDB_DATA_TYPE_VARBINARY) taosMemoryFreeClear(kv->value);
415,224!
1268
}
415,224✔
1269

1270
void smlDestroyInfo(SSmlHandle *info) {
3,825✔
1271
  if (info == NULL) return;
3,825✔
1272

1273
  taosHashCleanup(info->pVgHash);
1,914✔
1274
  taosHashCleanup(info->childTables);
1,915✔
1275
  taosHashCleanup(info->superTables);
1,915✔
1276
  taosHashCleanup(info->tableUids);
1,915✔
1277

1278
  for (int i = 0; i < taosArrayGetSize(info->tagJsonArray); i++) {
1,915!
1279
    cJSON *tags = (cJSON *)taosArrayGetP(info->tagJsonArray, i);
×
1280
    cJSON_Delete(tags);
×
1281
  }
1282
  taosArrayDestroy(info->tagJsonArray);
1,915✔
1283

1284
  for (int i = 0; i < taosArrayGetSize(info->valueJsonArray); i++) {
1,915!
1285
    cJSON *value = (cJSON *)taosArrayGetP(info->valueJsonArray, i);
×
1286
    cJSON_Delete(value);
×
1287
  }
1288
  taosArrayDestroy(info->valueJsonArray);
1,914✔
1289

1290
  taosArrayDestroyEx(info->preLineTagKV, freeSSmlKv);
1,915✔
1291
  taosArrayDestroyP(info->escapedStringList, NULL);
1,915✔
1292

1293
  if (!info->dataFormat) {
1,915✔
1294
    for (int i = 0; i < info->lineNum; i++) {
102,226✔
1295
      taosArrayDestroyEx(info->lines[i].colArray, freeSSmlKv);
100,951✔
1296
      if (info->lines[i].measureTagsLen != 0 && info->protocol != TSDB_SML_LINE_PROTOCOL) {
100,914✔
1297
        taosMemoryFree(info->lines[i].measureTag);
692!
1298
      }
1299
    }
1300
    taosMemoryFree(info->lines);
1,275!
1301
  }
1302
  if (info->protocol == TSDB_SML_JSON_PROTOCOL) {
1,915✔
1303
    taosMemoryFreeClear(info->preLine.tags);
843!
1304
  }
1305
  cJSON_Delete(info->root);
1,916✔
1306
  taosMemoryFreeClear(info);
1,915!
1307
}
1308

1309
int32_t smlBuildSmlInfo(TAOS *taos, SSmlHandle **handle) {
1,904✔
1310
  int32_t     code = TSDB_CODE_SUCCESS;
1,904✔
1311
  int32_t     lino = 0;
1,904✔
1312
  SSmlHandle *info = (SSmlHandle *)taosMemoryCalloc(1, sizeof(SSmlHandle));
1,904!
1313
  SML_CHECK_NULL(info);
1,915!
1314
  if (taos != NULL) {
1,915✔
1315
    info->taos = acquireTscObj(*(int64_t *)taos);
1,910✔
1316
    SML_CHECK_NULL(info->taos);
1,910!
1317
    SML_CHECK_CODE(catalogGetHandle(info->taos->pAppInfo->clusterId, &info->pCatalog));
1,910!
1318
  }
1319

1320
  info->pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
1,912✔
1321
  SML_CHECK_NULL(info->pVgHash);
1,915!
1322
  info->childTables = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,915✔
1323
  SML_CHECK_NULL(info->childTables);
1,913!
1324
  info->tableUids = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,913✔
1325
  SML_CHECK_NULL(info->tableUids);
1,914!
1326
  info->superTables = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,914✔
1327
  SML_CHECK_NULL(info->superTables);
1,915!
1328
  taosHashSetFreeFp(info->superTables, smlDestroySTableMeta);
1,915✔
1329
  taosHashSetFreeFp(info->childTables, smlDestroyTableInfo);
1,913✔
1330

1331
  info->id = smlGenId();
1,915✔
1332
  SML_CHECK_CODE(smlInitHandle(&info->pQuery));
1,915!
1333
  info->dataFormat = true;
1,912✔
1334
  info->tagJsonArray = taosArrayInit(8, POINTER_BYTES);
1,912✔
1335
  SML_CHECK_NULL(info->tagJsonArray);
1,915!
1336
  info->valueJsonArray = taosArrayInit(8, POINTER_BYTES);
1,915✔
1337
  SML_CHECK_NULL(info->valueJsonArray);
1,915!
1338
  info->preLineTagKV = taosArrayInit(8, sizeof(SSmlKv));
1,915✔
1339
  SML_CHECK_NULL(info->preLineTagKV);
1,915!
1340
  info->escapedStringList = taosArrayInit(8, POINTER_BYTES);
1,915✔
1341
  SML_CHECK_NULL(info->escapedStringList);
1,915!
1342

1343
  *handle = info;
1,915✔
1344
  info = NULL;
1,915✔
1345

1346
END:
1,915✔
1347
  smlDestroyInfo(info);
1,915✔
1348
  RETURN
1,913!
1349
}
1350

1351
static int32_t smlPushCols(SArray *colsArray, SArray *cols) {
92,051✔
1352
  int32_t   code = TSDB_CODE_SUCCESS;
92,051✔
1353
  int32_t   lino = 0;
92,051✔
1354
  SHashObj *kvHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
92,051✔
1355
  SML_CHECK_NULL(kvHash);
99,883!
1356
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
472,072✔
1357
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
352,667✔
1358
    SML_CHECK_NULL(kv);
345,508!
1359
    terrno = 0;
345,495✔
1360
    code = taosHashPut(kvHash, kv->key, kv->keyLen, &kv, POINTER_BYTES);
333,247✔
1361
    if (terrno == TSDB_CODE_DUP_KEY) {
392,091✔
1362
      SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
13!
1363
    }
1364
    SML_CHECK_CODE(code);
372,189!
1365
  }
1366

1367
  SML_CHECK_NULL(taosArrayPush(colsArray, &kvHash));
97,561!
1368
  return code;
97,561✔
1369
END:
13✔
1370
  taosHashCleanup(kvHash);
13✔
1371
  RETURN
13!
1372
}
1373

1374
static int32_t smlParseEnd(SSmlHandle *info) {
1,709✔
1375
  uDebug("SML:0x%" PRIx64 ", %s start, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat, info->lineNum);
1,709!
1376
  int32_t code = 0;
1,711✔
1377
  int32_t lino = 0;
1,711✔
1378
  if (info->dataFormat) return TSDB_CODE_SUCCESS;
1,711✔
1379

1380
  for (int32_t i = 0; i < info->lineNum; i++) {
94,376✔
1381
    SSmlLineInfo  *elements = info->lines + i;
93,160✔
1382
    SSmlTableInfo *tinfo = NULL;
93,160✔
1383
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
93,160✔
1384
      SSmlTableInfo **tmp =
1385
          (SSmlTableInfo **)taosHashGet(info->childTables, elements->measure, elements->measureTagsLen);
90,969✔
1386
      if (tmp) tinfo = *tmp;
93,672!
1387
    } else {
1388
      SSmlTableInfo **tmp = (SSmlTableInfo **)taosHashGet(info->childTables, elements->measureTag,
2,191✔
1389
                                                          elements->measureLen + elements->tagsLen);
2,191✔
1390
      if (tmp) tinfo = *tmp;
2,194!
1391
    }
1392

1393
    if (tinfo == NULL) {
95,866!
1394
      uError("SML:0x%" PRIx64 ", get oneTable failed, line num:%d", info->id, i);
×
1395
      smlBuildInvalidDataMsg(&info->msgBuf, "get oneTable failed", elements->measure);
×
1396
      return TSDB_CODE_SML_INVALID_DATA;
×
1397
    }
1398

1399
    if (taosArrayGetSize(tinfo->tags) > TSDB_MAX_TAGS) {
95,866✔
1400
      smlBuildInvalidDataMsg(&info->msgBuf, "too many tags than 128", NULL);
4✔
1401
      return TSDB_CODE_PAR_INVALID_TAGS_NUM;
4✔
1402
    }
1403

1404
    if (taosArrayGetSize(elements->colArray) + taosArrayGetSize(tinfo->tags) > TSDB_MAX_COLUMNS) {
94,822✔
1405
      smlBuildInvalidDataMsg(&info->msgBuf, "too many columns than 4096", NULL);
1✔
1406
      return TSDB_CODE_PAR_TOO_MANY_COLUMNS;
1✔
1407
    }
1408

1409
    SML_CHECK_CODE(smlPushCols(tinfo->cols, elements->colArray));
92,715✔
1410

1411
    SSmlSTableMeta **tableMeta =
1412
        (SSmlSTableMeta **)taosHashGet(info->superTables, elements->measure, elements->measureLen);
97,206✔
1413
    if (tableMeta) {  // update meta
91,698✔
1414
      uDebug("SML:0x%" PRIx64 ", %s update meta, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
90,491!
1415
             info->lineNum);
1416
      SML_CHECK_CODE(smlUpdateMeta((*tableMeta)->colHash, (*tableMeta)->cols, elements->colArray, false, &info->msgBuf,
90,491!
1417
                                   (*tableMeta)->tagHash));
1418
      SML_CHECK_CODE(smlUpdateMeta((*tableMeta)->tagHash, (*tableMeta)->tags, tinfo->tags, true, &info->msgBuf,
91,788!
1419
                                   (*tableMeta)->colHash));
1420
    } else {
1421
      uDebug("SML:0x%" PRIx64 ", %s add meta, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
1,207!
1422
             info->lineNum);
1423
      SSmlSTableMeta *meta = NULL;
1,207✔
1424
      SML_CHECK_CODE(smlBuildSTableMeta(info->dataFormat, &meta));
1,238!
1425
      code = taosHashPut(info->superTables, elements->measure, elements->measureLen, &meta, POINTER_BYTES);
1,209✔
1426
      if (code != TSDB_CODE_SUCCESS) {
1,209!
1427
        smlDestroySTableMeta(&meta);
×
1428
        SML_CHECK_CODE(code);
×
1429
      }
1430
      SML_CHECK_CODE(smlInsertMeta(meta->tagHash, meta->tags, tinfo->tags, NULL));
1,209✔
1431
      SML_CHECK_CODE(smlInsertMeta(meta->colHash, meta->cols, elements->colArray, meta->tagHash));
1,197✔
1432
    }
1433
  }
1434
  uDebug("SML:0x%" PRIx64 ", %s end, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat, info->lineNum);
1,216!
1435

1436
END:
1,216✔
1437
  RETURN
1,260!
1438
}
1439

1440
static int32_t smlInsertData(SSmlHandle *info) {
1,615✔
1441
  int32_t         code = TSDB_CODE_SUCCESS;
1,615✔
1442
  int32_t         lino = 0;
1,615✔
1443
  char           *measure = NULL;
1,615✔
1444
  SSmlTableInfo **oneTable = NULL;
1,615✔
1445
  uDebug("SML:0x%" PRIx64 ", %s start, format:%d", info->id, __FUNCTION__, info->dataFormat);
1,615!
1446

1447
  if (info->pRequest->dbList == NULL) {
1,615!
1448
    info->pRequest->dbList = taosArrayInit(1, TSDB_DB_FNAME_LEN);
1,619✔
1449
    SML_CHECK_NULL(info->pRequest->dbList);
1,618!
1450
  }
1451
  char *data = (char *)taosArrayReserve(info->pRequest->dbList, 1);
1,614✔
1452
  SML_CHECK_NULL(data);
1,617!
1453
  SName pName = {TSDB_TABLE_NAME_T, info->taos->acctId, {0}, {0}};
1,617✔
1454
  tstrncpy(pName.dbname, info->pRequest->pDb, sizeof(pName.dbname));
1,617✔
1455
  (void)tNameGetFullDbName(&pName, data);  // ignore
1,617✔
1456

1457
  oneTable = (SSmlTableInfo **)taosHashIterate(info->childTables, NULL);
1,615✔
1458
  while (oneTable) {
4,133✔
1459
    SSmlTableInfo *tableData = *oneTable;
2,527✔
1460

1461
    int measureLen = tableData->sTableNameLen;
2,527✔
1462
    measure = (char *)taosMemoryMalloc(tableData->sTableNameLen);
2,527!
1463
    SML_CHECK_NULL(measure);
2,542!
1464
    (void)memcpy(measure, tableData->sTableName, tableData->sTableNameLen);
2,530✔
1465
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
2,530✔
1466
      PROCESS_SLASH_IN_MEASUREMENT(measure, measureLen);
8,970✔
1467
    }
1468
    smlStrReplace(measure, measureLen);
2,530✔
1469
    (void)memcpy(pName.tname, measure, measureLen);
2,530✔
1470
    pName.tname[measureLen] = '\0';
2,530✔
1471

1472
    if (info->pRequest->tableList == NULL) {
2,530✔
1473
      info->pRequest->tableList = taosArrayInit(1, sizeof(SName));
1,618✔
1474
      SML_CHECK_NULL(info->pRequest->tableList);
1,618!
1475
    }
1476
    SML_CHECK_NULL(taosArrayPush(info->pRequest->tableList, &pName));
5,059!
1477
    tstrncpy(pName.tname, tableData->childTableName, sizeof(pName.tname));
2,529✔
1478

1479
    SRequestConnInfo conn = {0};
2,529✔
1480
    conn.pTrans = info->taos->pAppInfo->pTransporter;
2,529✔
1481
    conn.requestId = info->pRequest->requestId;
2,529✔
1482
    conn.requestObjRefId = info->pRequest->self;
2,529✔
1483
    conn.mgmtEps = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
2,529✔
1484

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

1487
    SVgroupInfo vg = {0};
2,513✔
1488
    SML_CHECK_CODE(catalogGetTableHashVgroup(info->pCatalog, &conn, &pName, &vg));
2,513!
1489
    SML_CHECK_CODE(taosHashPut(info->pVgHash, (const char *)&vg.vgId, sizeof(vg.vgId), (char *)&vg, sizeof(vg)));
2,519!
1490

1491
    SSmlSTableMeta **pMeta =
1492
        (SSmlSTableMeta **)taosHashGet(info->superTables, tableData->sTableName, tableData->sTableNameLen);
2,520✔
1493
    if (unlikely(NULL == pMeta || NULL == *pMeta || NULL == (*pMeta)->tableMeta)) {
2,520!
1494
      uError("SML:0x%" PRIx64 ", %s NULL == pMeta. table name:%s", info->id, __FUNCTION__, tableData->childTableName);
×
1495
      SML_CHECK_CODE(TSDB_CODE_SML_INTERNAL_ERROR);
×
1496
    }
1497

1498
    // use tablemeta of stable to save vgid and uid of child table
1499
    (*pMeta)->tableMeta->vgId = vg.vgId;
2,520✔
1500
    (*pMeta)->tableMeta->uid = tableData->uid;  // one table merge data block together according uid
2,520✔
1501
    uDebug("SML:0x%" PRIx64 ", %s table:%s, uid:%" PRIu64 ", format:%d", info->id, __FUNCTION__, pName.tname,
2,520!
1502
           tableData->uid, info->dataFormat);
1503

1504
    SML_CHECK_CODE(smlBindData(info->pQuery, info->dataFormat, tableData->tags, (*pMeta)->cols, tableData->cols,
2,520!
1505
                               (*pMeta)->tableMeta, tableData->childTableName, measure, measureLen, info->ttl,
1506
                               info->msgBuf.buf, info->msgBuf.len, info->taos->optionInfo.charsetCxt));
1507
    taosMemoryFreeClear(measure);
2,518!
1508
    oneTable = (SSmlTableInfo **)taosHashIterate(info->childTables, oneTable);
2,517✔
1509
  }
1510

1511
  SML_CHECK_CODE(smlBuildOutput(info->pQuery, info->pVgHash));
1,606!
1512
  info->cost.insertRpcTime = taosGetTimestampUs();
1,602✔
1513

1514
  SAppClusterSummary *pActivity = &info->taos->pAppInfo->summary;
1,602✔
1515
  (void)atomic_add_fetch_64((int64_t *)&pActivity->numOfInsertsReq, 1);  // no need to check return code
1,602✔
1516

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

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

1522
  return info->pRequest->code;
1,606✔
1523

1524
END:
12✔
1525
  taosMemoryFree(measure);
12!
1526
  taosHashCancelIterate(info->childTables, oneTable);
12✔
1527
  RETURN
12!
1528
}
1529

1530
static void smlPrintStatisticInfo(SSmlHandle *info) {
1,908✔
1531
  uDebug(
1,908!
1532
      "SML:0x%" PRIx64
1533
      " 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 \
1534
        parse cost:%" PRId64 ",schema cost:%" PRId64 ",bind cost:%" PRId64 ",rpc cost:%" PRId64 ",total cost:%" PRId64,
1535
      info->id, info->cost.code, tstrerror(info->cost.code), info->cost.lineNum, info->cost.numOfSTables,
1536
      info->cost.numOfCTables, info->cost.numOfCreateSTables, info->cost.numOfAlterTagSTables,
1537
      info->cost.numOfAlterColSTables, info->cost.schemaTime - info->cost.parseTime,
1538
      info->cost.insertBindTime - info->cost.schemaTime, info->cost.insertRpcTime - info->cost.insertBindTime,
1539
      info->cost.endTime - info->cost.insertRpcTime, info->cost.endTime - info->cost.parseTime);
1540
}
1,908✔
1541

1542
int32_t smlClearForRerun(SSmlHandle *info) {
1,246✔
1543
  int32_t code = 0;
1,246✔
1544
  int32_t lino = 0;
1,246✔
1545
  info->reRun = false;
1,246✔
1546

1547
  taosHashClear(info->childTables);
1,246✔
1548
  taosHashClear(info->superTables);
1,247✔
1549
  taosHashClear(info->tableUids);
1,247✔
1550

1551
  if (!info->dataFormat) {
1,247!
1552
    info->lines = (SSmlLineInfo *)taosMemoryCalloc(info->lineNum, sizeof(SSmlLineInfo));
1,247!
1553
    SML_CHECK_NULL(info->lines);
1,247!
1554
  }
1555

1556
  taosArrayClearP(info->escapedStringList, NULL);
1,247✔
1557
  if (info->protocol == TSDB_SML_JSON_PROTOCOL) {
1,246✔
1558
    taosMemoryFreeClear(info->preLine.tags);
427!
1559
  }
1560
  (void)memset(&info->preLine, 0, sizeof(SSmlLineInfo));
1,246✔
1561
  info->currSTableMeta = NULL;
1,246✔
1562
  info->currTableDataCtx = NULL;
1,246✔
1563

1564
  SVnodeModifyOpStmt *stmt = (SVnodeModifyOpStmt *)(info->pQuery->pRoot);
1,246✔
1565
  stmt->freeHashFunc(stmt->pTableBlockHashObj);
1,246✔
1566
  stmt->pTableBlockHashObj = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
1,246✔
1567
  SML_CHECK_NULL(stmt->pTableBlockHashObj);
1,246!
1568

1569
END:
1,246✔
1570
  RETURN
1,246!
1571
}
1572

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

1589
static bool getLine(SSmlHandle *info, char *lines[], char **rawLine, char *rawLineEnd, int numLines, int i, char **tmp,
100,395✔
1590
                    int *len) {
1591
  if (lines) {
100,395✔
1592
    *tmp = lines[i];
100,300✔
1593
    *len = strlen(*tmp);
100,300✔
1594
  } else if (*rawLine) {
95!
1595
    *tmp = *rawLine;
114✔
1596
    while (*rawLine < rawLineEnd) {
970,728✔
1597
      if (*((*rawLine)++) == '\n') {
970,662✔
1598
        break;
48✔
1599
      }
1600
      (*len)++;
970,614✔
1601
    }
1602
    if (IS_COMMENT(info->protocol, (*tmp)[0])) {  // this line is comment
114✔
1603
      return false;
6✔
1604
    }
1605
  }
1606

1607
  if (*rawLine != NULL && (uDebugFlag & DEBUG_DEBUG)) {
100,389!
1608
    printRaw(info->id, i, numLines, DEBUG_DEBUG, *tmp, *len);
×
1609
  } else {
1610
    uDebug("SML:0x%" PRIx64 ", smlParseLine is not raw, line %d/%d :%s", info->id, i, numLines, *tmp);
100,389!
1611
  }
1612
  return true;
101,106✔
1613
}
1614

1615
static int32_t smlParseJson(SSmlHandle *info, char *lines[], char *rawLine) {
837✔
1616
  int32_t code = TSDB_CODE_SUCCESS;
837✔
1617
  if (lines) {
837!
1618
    code = smlParseJSONExt(info, *lines);
839✔
1619
  } else if (rawLine) {
×
1620
    code = smlParseJSONExt(info, rawLine);
×
1621
  }
1622
  if (code != TSDB_CODE_SUCCESS) {
840✔
1623
    uError("%s failed code:%d", __FUNCTION__, code);
72!
1624
  }
1625
  return code;
841✔
1626
}
1627

1628
static int32_t smlParseStart(SSmlHandle *info, char *lines[], char *rawLine, char *rawLineEnd, int numLines) {
1,902✔
1629
  uDebug("SML:0x%" PRIx64 ", %s start", info->id, __FUNCTION__);
1,902!
1630
  int32_t code = TSDB_CODE_SUCCESS;
1,902✔
1631
  if (info->protocol == TSDB_SML_JSON_PROTOCOL) {
1,902✔
1632
    return smlParseJson(info, lines, rawLine);
838✔
1633
  }
1634

1635
  char   *oldRaw = rawLine;
1,064✔
1636
  int32_t i = 0;
1,064✔
1637
  while (i < numLines) {
101,384✔
1638
    char *tmp = NULL;
100,433✔
1639
    int   len = 0;
100,433✔
1640
    if (!getLine(info, lines, &rawLine, rawLineEnd, numLines, i, &tmp, &len)) {
100,433✔
1641
      continue;
824✔
1642
    }
1643
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
101,196✔
1644
      if (info->dataFormat) {
98,473✔
1645
        SSmlLineInfo element = {0};
606✔
1646
        code = smlParseInfluxString(info, tmp, tmp + len, &element);
606✔
1647
      } else {
1648
        code = smlParseInfluxString(info, tmp, tmp + len, info->lines + i);
97,867✔
1649
      }
1650
    } else if (info->protocol == TSDB_SML_TELNET_PROTOCOL) {
2,723!
1651
      if (info->dataFormat) {
2,736✔
1652
        SSmlLineInfo element = {0};
1,971✔
1653
        code = smlParseTelnetString(info, (char *)tmp, (char *)tmp + len, &element);
1,971✔
1654
        if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
1,971!
1655
      } else {
1656
        code = smlParseTelnetString(info, (char *)tmp, (char *)tmp + len, info->lines + i);
765✔
1657
      }
1658
    }
1659
    if (code != TSDB_CODE_SUCCESS) {
100,438✔
1660
      if (rawLine != NULL) {
124!
1661
        printRaw(info->id, i, numLines, DEBUG_ERROR, tmp, len);
×
1662
      } else {
1663
        uError("SML:0x%" PRIx64 ", %s failed. line %d :%s", info->id, __FUNCTION__, i, tmp);
124!
1664
      }
1665
      return code;
124✔
1666
    }
1667
    if (info->reRun) {
100,314✔
1668
      uDebug("SML:0x%" PRIx64 ", %s re run", info->id, __FUNCTION__);
818!
1669
      i = 0;
818✔
1670
      rawLine = oldRaw;
818✔
1671
      code = smlClearForRerun(info);
818✔
1672
      if (code != TSDB_CODE_SUCCESS) {
818!
1673
        return code;
×
1674
      }
1675
      continue;
818✔
1676
    }
1677
    i++;
99,496✔
1678
  }
1679
  uDebug("SML:0x%" PRIx64 ", %s end", info->id, __FUNCTION__);
951!
1680

1681
  return code;
942✔
1682
}
1683

1684
static int smlProcess(SSmlHandle *info, char *lines[], char *rawLine, char *rawLineEnd, int numLines) {
1,902✔
1685
  int32_t code = TSDB_CODE_SUCCESS;
1,902✔
1686
  int32_t lino = 0;
1,902✔
1687
  int32_t retryNum = 0;
1,902✔
1688

1689
  info->cost.parseTime = taosGetTimestampUs();
1,902✔
1690

1691
  SML_CHECK_CODE(smlParseStart(info, lines, rawLine, rawLineEnd, numLines));
1,902✔
1692
  SML_CHECK_CODE(smlParseEnd(info));
1,710✔
1693

1694
  info->cost.lineNum = info->lineNum;
1,660✔
1695
  info->cost.numOfSTables = taosHashGetSize(info->superTables);
1,660✔
1696
  info->cost.numOfCTables = taosHashGetSize(info->childTables);
1,663✔
1697
  info->cost.schemaTime = taosGetTimestampUs();
1,661✔
1698

1699
  do {
1700
    code = smlModifyDBSchemas(info);
1,800✔
1701
    if (code != TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER && code != TSDB_CODE_SDB_OBJ_CREATING &&
1,796!
1702
        code != TSDB_CODE_MND_TRANS_CONFLICT) {
1703
      break;
1,665✔
1704
    }
1705
    taosMsleep(100);
131✔
1706
    uInfo("SML:0x%" PRIx64 ", smlModifyDBSchemas retry code:%s, times:%d", info->id, tstrerror(code), retryNum);
134!
1707
  } while (retryNum++ < taosHashGetSize(info->superTables) * MAX_RETRY_TIMES);
134!
1708

1709
  SML_CHECK_CODE(code);
1,661✔
1710
  info->cost.insertBindTime = taosGetTimestampUs();
1,617✔
1711
  SML_CHECK_CODE(smlInsertData(info));
1,617✔
1712

1713
END:
1,605✔
1714
  RETURN
1,908!
1715
}
1716

1717
void smlSetReqSQL(SRequestObj *request, char *lines[], char *rawLine, char *rawLineEnd) {
1,904✔
1718
  if (request->pTscObj->pAppInfo->serverCfg.monitorParas.tsSlowLogScope & SLOW_LOG_TYPE_INSERT) {
1,904!
1719
    int32_t len = 0;
×
1720
    int32_t rlen = 0;
×
1721
    char   *p = NULL;
×
1722

1723
    if (lines && lines[0]) {
×
1724
      len = strlen(lines[0]);
×
1725
      p = lines[0];
×
1726
    } else if (rawLine) {
×
1727
      if (rawLineEnd) {
×
1728
        len = rawLineEnd - rawLine;
×
1729
      } else {
1730
        len = strlen(rawLine);
×
1731
      }
1732
      p = rawLine;
×
1733
    }
1734

1735
    if (NULL == p) {
×
1736
      return;
×
1737
    }
1738

1739
    rlen = TMIN(len, TSDB_MAX_ALLOWED_SQL_LEN);
×
1740
    rlen = TMAX(rlen, 0);
×
1741

1742
    char *sql = taosMemoryMalloc(rlen + 1);
×
1743
    if (NULL == sql) {
×
1744
      uError("malloc %d for sml sql failed", rlen + 1);
×
1745
      return;
×
1746
    }
1747
    (void)memcpy(sql, p, rlen);
×
1748
    sql[rlen] = 0;
×
1749

1750
    request->sqlstr = sql;
×
1751
    request->sqlLen = rlen;
×
1752
  }
1753
}
1754

1755
TAOS_RES *taos_schemaless_insert_inner(TAOS *taos, char *lines[], char *rawLine, char *rawLineEnd, int numLines,
1,900✔
1756
                                       int protocol, int precision, int32_t ttl, int64_t reqid, char *tbnameKey) {
1757
  int32_t      code = TSDB_CODE_SUCCESS;
1,900✔
1758
  int32_t      lino = 0;
1,900✔
1759
  SRequestObj *request = NULL;
1,900✔
1760
  SSmlHandle  *info = NULL;
1,900✔
1761
  int          cnt = 0;
1,900✔
1762
  while (1) {
1✔
1763
    SML_CHECK_CODE(buildRequest(*(int64_t *)taos, "", 0, NULL, false, &request, reqid));
1,901!
1764
    SSmlMsgBuf msg = {request->msgBufLen, request->msgBuf};
1,907✔
1765
    request->code = smlBuildSmlInfo(taos, &info);
1,907✔
1766
    SML_CHECK_CODE(request->code);
1,908!
1767

1768
    info->pRequest = request;
1,908✔
1769
    info->pRequest->pQuery = info->pQuery;
1,908✔
1770
    info->ttl = ttl;
1,908✔
1771
    info->precision = precision;
1,908✔
1772
    info->protocol = (TSDB_SML_PROTOCOL_TYPE)protocol;
1,908✔
1773
    info->msgBuf.buf = info->pRequest->msgBuf;
1,908✔
1774
    info->msgBuf.len = ERROR_MSG_BUF_DEFAULT_SIZE;
1,908✔
1775
    info->lineNum = numLines;
1,908✔
1776
    info->tbnameKey = tbnameKey;
1,908✔
1777

1778
    smlSetReqSQL(request, lines, rawLine, rawLineEnd);
1,908✔
1779

1780
    if (request->pDb == NULL) {
1,900!
1781
      request->code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1782
      smlBuildInvalidDataMsg(&msg, "Database not specified", NULL);
×
1783
      goto END;
×
1784
    }
1785

1786
    if (protocol < TSDB_SML_LINE_PROTOCOL || protocol > TSDB_SML_JSON_PROTOCOL) {
1,900!
1787
      request->code = TSDB_CODE_SML_INVALID_PROTOCOL_TYPE;
×
1788
      smlBuildInvalidDataMsg(&msg, "protocol invalidate", NULL);
×
1789
      goto END;
×
1790
    }
1791

1792
    if (protocol == TSDB_SML_LINE_PROTOCOL &&
1,903!
1793
        (precision < TSDB_SML_TIMESTAMP_NOT_CONFIGURED || precision > TSDB_SML_TIMESTAMP_NANO_SECONDS)) {
606!
1794
      request->code = TSDB_CODE_SML_INVALID_PRECISION_TYPE;
×
1795
      smlBuildInvalidDataMsg(&msg, "precision invalidate for line protocol", NULL);
×
1796
      goto END;
×
1797
    }
1798

1799
    if (protocol == TSDB_SML_JSON_PROTOCOL) {
1,904✔
1800
      numLines = 1;
840✔
1801
    } else if (numLines <= 0) {
1,064!
1802
      request->code = TSDB_CODE_SML_INVALID_DATA;
×
1803
      smlBuildInvalidDataMsg(&msg, "line num is invalid", NULL);
×
1804
      goto END;
×
1805
    }
1806

1807
    code = smlProcess(info, lines, rawLine, rawLineEnd, numLines);
1,904✔
1808
    request->code = code;
1,908✔
1809
    info->cost.endTime = taosGetTimestampUs();
1,908✔
1810
    info->cost.code = code;
1,909✔
1811
    if (NEED_CLIENT_HANDLE_ERROR(code) || code == TSDB_CODE_SDB_OBJ_CREATING || code == TSDB_CODE_PAR_VALUE_TOO_LONG ||
1,909!
1812
        code == TSDB_CODE_MND_TRANS_CONFLICT) {
1813
      if (cnt++ >= 10) {
×
1814
        uInfo("SML:%" PRIx64 " retry:%d/10 end code:%d, msg:%s", info->id, cnt, code, tstrerror(code));
×
1815
        break;
1,908✔
1816
      }
1817
      taosMsleep(100);
×
1818
      uInfo("SML:%" PRIx64 " retry:%d/10,ver is old retry or object is creating code:%d, msg:%s", info->id, cnt, code,
1!
1819
            tstrerror(code));
1820
      code = refreshMeta(request->pTscObj, request);
1✔
1821
      if (code != 0) {
1!
1822
        uInfo("SML:%" PRIx64 " refresh meta error code:%d, msg:%s", info->id, code, tstrerror(code));
1!
1823
      }
1824
      smlDestroyInfo(info);
1✔
1825
      info = NULL;
1✔
1826
      taos_free_result(request);
1✔
1827
      request = NULL;
1✔
1828
      continue;
1✔
1829
    }
1830
    smlPrintStatisticInfo(info);
1,909✔
1831
    break;
1,908✔
1832
  }
1833

1834
END:
1,908✔
1835
  smlDestroyInfo(info);
1,908✔
1836
  return (TAOS_RES *)request;
1,909✔
1837
}
1838

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

1858
TAOS_RES *taos_schemaless_insert_ttl_with_reqid_tbname_key(TAOS *taos, char *lines[], int numLines, int protocol,
1,858✔
1859
                                                           int precision, int32_t ttl, int64_t reqid, char *tbnameKey) {
1860
  if (taos == NULL || lines == NULL || numLines < 0) {
1,858!
1861
    terrno = TSDB_CODE_INVALID_PARA;
×
1862
    return NULL;
×
1863
  }
1864
  for (int i = 0; i < numLines; i++) {
108,131✔
1865
    if (lines[i] == NULL) {
106,265!
1866
      terrno = TSDB_CODE_INVALID_PARA;
×
1867
      return NULL;
×
1868
    }
1869
  }
1870
  return taos_schemaless_insert_inner(taos, lines, NULL, NULL, numLines, protocol, precision, ttl, reqid, tbnameKey);
1,866✔
1871
}
1872

1873
TAOS_RES *taos_schemaless_insert_ttl_with_reqid(TAOS *taos, char *lines[], int numLines, int protocol, int precision,
1,863✔
1874
                                                int32_t ttl, int64_t reqid) {
1875
  return taos_schemaless_insert_ttl_with_reqid_tbname_key(taos, lines, numLines, protocol, precision, ttl, reqid, NULL);
1,863✔
1876
}
1877

1878
TAOS_RES *taos_schemaless_insert(TAOS *taos, char *lines[], int numLines, int protocol, int precision) {
1,834✔
1879
  return taos_schemaless_insert_ttl_with_reqid(taos, lines, numLines, protocol, precision, TSDB_DEFAULT_TABLE_TTL, 0);
1,834✔
1880
}
1881

1882
TAOS_RES *taos_schemaless_insert_ttl(TAOS *taos, char *lines[], int numLines, int protocol, int precision,
24✔
1883
                                     int32_t ttl) {
1884
  return taos_schemaless_insert_ttl_with_reqid(taos, lines, numLines, protocol, precision, ttl, 0);
24✔
1885
}
1886

1887
TAOS_RES *taos_schemaless_insert_with_reqid(TAOS *taos, char *lines[], int numLines, int protocol, int precision,
×
1888
                                            int64_t reqid) {
1889
  return taos_schemaless_insert_ttl_with_reqid(taos, lines, numLines, protocol, precision, TSDB_DEFAULT_TABLE_TTL,
×
1890
                                               reqid);
1891
}
1892

1893
static int32_t getRawLineLen(char *lines, int len, int protocol) {
42✔
1894
  int   numLines = 0;
42✔
1895
  char *tmp = lines;
42✔
1896
  for (int i = 0; i < len; i++) {
486,306✔
1897
    if (lines[i] == '\n' || i == len - 1) {
486,264✔
1898
      if (!IS_COMMENT(protocol, tmp[0])) {  // ignore comment
72✔
1899
        numLines++;
66✔
1900
      }
1901
      tmp = lines + i + 1;
72✔
1902
    }
1903
  }
1904
  return numLines;
42✔
1905
}
1906

1907
TAOS_RES *taos_schemaless_insert_raw_ttl_with_reqid_tbname_key(TAOS *taos, char *lines, int len, int32_t *totalRows,
42✔
1908
                                                               int protocol, int precision, int32_t ttl, int64_t reqid,
1909
                                                               char *tbnameKey) {
1910
  if (taos == NULL || lines == NULL || len < 0) {
42!
1911
    terrno = TSDB_CODE_INVALID_PARA;
×
1912
    return NULL;
×
1913
  }
1914
  int numLines = getRawLineLen(lines, len, protocol);
42✔
1915
  if (totalRows != NULL) {
42!
1916
    *totalRows = numLines;
42✔
1917
  }
1918
  return taos_schemaless_insert_inner(taos, NULL, lines, lines + len, numLines, protocol, precision, ttl, reqid,
42✔
1919
                                      tbnameKey);
1920
}
1921

1922
TAOS_RES *taos_schemaless_insert_raw_ttl_with_reqid(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
42✔
1923
                                                    int precision, int32_t ttl, int64_t reqid) {
1924
  return taos_schemaless_insert_raw_ttl_with_reqid_tbname_key(taos, lines, len, totalRows, protocol, precision, ttl,
42✔
1925
                                                              reqid, NULL);
1926
}
1927

1928
TAOS_RES *taos_schemaless_insert_raw_with_reqid(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
×
1929
                                                int precision, int64_t reqid) {
1930
  return taos_schemaless_insert_raw_ttl_with_reqid(taos, lines, len, totalRows, protocol, precision,
×
1931
                                                   TSDB_DEFAULT_TABLE_TTL, reqid);
1932
}
1933
TAOS_RES *taos_schemaless_insert_raw_ttl(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
×
1934
                                         int precision, int32_t ttl) {
1935
  return taos_schemaless_insert_raw_ttl_with_reqid(taos, lines, len, totalRows, protocol, precision, ttl, 0);
×
1936
}
1937

1938
TAOS_RES *taos_schemaless_insert_raw(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
42✔
1939
                                     int precision) {
1940
  return taos_schemaless_insert_raw_ttl_with_reqid(taos, lines, len, totalRows, protocol, precision,
42✔
1941
                                                   TSDB_DEFAULT_TABLE_TTL, 0);
1942
}
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