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

taosdata / TDengine / #4852

14 Nov 2025 08:06AM UTC coverage: 63.812% (+0.06%) from 63.754%
#4852

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

355 of 675 new or added lines in 18 files covered. (52.59%)

3142 existing lines in 128 files now uncovered.

149263 of 233910 relevant lines covered (63.81%)

118719500.98 hits per line

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

89.72
/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) {
1,569,072✔
114
  SUserAuthInfo pAuth = {0};
1,569,072✔
115
  (void)snprintf(pAuth.user, sizeof(pAuth.user), "%s", info->taos->user);
1,569,072✔
116
  if (NULL == pTabName) {
1,567,787✔
117
    if (tNameSetDbName(&pAuth.tbName, info->taos->acctId, info->pRequest->pDb, strlen(info->pRequest->pDb)) != 0) {
360,440✔
118
      return TSDB_CODE_SML_INVALID_DATA;
×
119
    }
120
  } else {
121
    toName(info->taos->acctId, info->pRequest->pDb, pTabName, &pAuth.tbName);
1,207,347✔
122
  }
123
  pAuth.type = type;
1,568,144✔
124

125
  int32_t      code = TSDB_CODE_SUCCESS;
1,568,144✔
126
  SUserAuthRes authRes = {0};
1,568,144✔
127

128
  code = catalogChkAuth(info->pCatalog, conn, &pAuth, &authRes);
1,568,144✔
129
  nodesDestroyNode(authRes.pCond[AUTH_RES_BASIC]);
1,567,473✔
130

131
  return (code == TSDB_CODE_SUCCESS)
132
             ? (authRes.pass[AUTH_RES_BASIC] ? TSDB_CODE_SUCCESS : TSDB_CODE_PAR_PERMISSION_DENIED)
1,568,221✔
133
             : code;
2,856,549✔
134
}
135

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

151
int64_t smlGetTimeValue(const char *value, int32_t len, uint8_t fromPrecision, uint8_t toPrecision) {
27,654,527✔
152
  char   *endPtr = NULL;
27,654,527✔
153
  int64_t tsInt64 = taosStr2Int64(value, &endPtr, 10);
27,723,660✔
154
  if (unlikely(value + len != endPtr)) {
27,671,600✔
155
    return -1;
2,452✔
156
  }
157

158
  if (unlikely(fromPrecision >= TSDB_TIME_PRECISION_HOURS)) {
27,707,698✔
159
    int64_t unit = smlToMilli[fromPrecision - TSDB_TIME_PRECISION_HOURS];
8,457✔
160
    if (tsInt64 != 0 && unit > INT64_MAX / tsInt64) {
8,457✔
161
      return -1;
×
162
    }
163
    tsInt64 *= unit;
8,457✔
164
    fromPrecision = TSDB_TIME_PRECISION_MILLI;
8,457✔
165
  }
166

167
  return convertTimePrecision(tsInt64, fromPrecision, toPrecision);
27,707,698✔
168
}
169

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

176
  tag->sTableName = measure;
1,342,432✔
177
  tag->sTableNameLen = measureLen;
1,342,432✔
178

179
  tag->cols = taosArrayInit(numRows, POINTER_BYTES);
1,342,945✔
180
  SML_CHECK_NULL(tag->cols)
1,343,360✔
181
  *tInfo = tag;
1,342,846✔
182
  return code;
1,342,332✔
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) {
28,818,962✔
191
  kv->key = tsSmlTsDefaultName;
28,818,962✔
192
  kv->keyLen = strlen(tsSmlTsDefaultName);
28,831,298✔
193
  kv->type = TSDB_DATA_TYPE_TIMESTAMP;
28,828,985✔
194
  kv->i = ts;
28,886,039✔
195
  kv->length = (size_t)tDataTypes[TSDB_DATA_TYPE_TIMESTAMP].bytes;
28,909,169✔
196
}
28,955,172✔
197

198
static void smlDestroySTableMeta(void *para) {
1,170,529✔
199
  if (para == NULL) {
1,170,529✔
200
    return;
×
201
  }
202
  SSmlSTableMeta *meta = *(SSmlSTableMeta **)para;
1,170,529✔
203
  if (meta == NULL) {
1,170,529✔
204
    return;
405,698✔
205
  }
206
  taosHashCleanup(meta->tagHash);
764,831✔
207
  taosHashCleanup(meta->colHash);
764,831✔
208
  taosArrayDestroy(meta->tags);
764,831✔
209
  taosArrayDestroy(meta->cols);
764,831✔
210
  taosMemoryFreeClear(meta->tableMeta);
764,831✔
211
  taosMemoryFree(meta);
764,484✔
212
}
213

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

219
  int   measureLen = currElement->measureLen;
677,974✔
220
  char *measure = (char *)taosMemoryMalloc(measureLen);
677,974✔
221
  SML_CHECK_NULL(measure);
677,819✔
222
  (void)memcpy(measure, currElement->measure, measureLen);
677,819✔
223
  if (currElement->measureEscaped) {
677,819✔
224
    PROCESS_SLASH_IN_MEASUREMENT(measure, measureLen);
5,742✔
225
  }
226
  smlStrReplace(measure, measureLen);
677,562✔
227
  code = smlGetMeta(info, measure, measureLen, &pTableMeta);
677,858✔
228
  taosMemoryFree(measure);
678,091✔
229
  if (code != TSDB_CODE_SUCCESS) {
678,191✔
230
    info->dataFormat = false;
405,698✔
231
    info->reRun = true;
405,698✔
232
    goto END;
405,698✔
233
  }
234
  SML_CHECK_CODE(smlBuildSTableMeta(info->dataFormat, sMeta));
272,493✔
235
  for (int i = 0; i < pTableMeta->tableInfo.numOfTags + pTableMeta->tableInfo.numOfColumns; i++) {
5,374,197✔
236
    SSchema *col = pTableMeta->schema + i;
5,100,789✔
237
    SSmlKv   kv = {.key = col->name, .keyLen = strlen(col->name), .type = col->type};
5,101,046✔
238
    if (col->type == TSDB_DATA_TYPE_NCHAR) {
5,101,304✔
239
      kv.length = (col->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
854,800✔
240
    } else if (col->type == TSDB_DATA_TYPE_BINARY || col->type == TSDB_DATA_TYPE_GEOMETRY ||
4,246,247✔
241
               col->type == TSDB_DATA_TYPE_VARBINARY) {
2,625,540✔
242
      kv.length = col->bytes - VARSTR_HEADER_SIZE;
1,621,826✔
243
    } else {
244
      kv.length = col->bytes;
2,624,421✔
245
    }
246

247
    if (i < pTableMeta->tableInfo.numOfColumns) {
5,101,816✔
248
      SML_CHECK_NULL(taosArrayPush((*sMeta)->cols, &kv));
6,787,840✔
249
    } else {
250
      SML_CHECK_NULL(taosArrayPush((*sMeta)->tags, &kv));
3,414,845✔
251
    }
252
  }
253
  SML_CHECK_CODE(taosHashPut(info->superTables, currElement->measure, currElement->measureLen, sMeta, POINTER_BYTES));
273,151✔
254
  (*sMeta)->tableMeta = pTableMeta;
272,649✔
255
  return code;
272,906✔
256

257
END:
405,698✔
258
  smlDestroySTableMeta(sMeta);
405,698✔
259
  taosMemoryFreeClear(pTableMeta);
405,698✔
260
  RETURN
405,698✔
261
}
262

263
bool isSmlColAligned(SSmlHandle *info, int cnt, SSmlKv *kv) {
112,411✔
264
  // cnt begin 0, add ts so + 2
265
  if (unlikely(cnt + 2 > info->currSTableMeta->tableInfo.numOfColumns)) {
112,411✔
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);
112,411✔
271
  if (unlikely(ret != TSDB_CODE_SUCCESS)) {
112,411✔
272
    uDebug("smlBuildCol error, retry");
1,864✔
273
    goto END;
1,864✔
274
  }
275
  if (cnt >= taosArrayGetSize(info->maxColKVs)) {
110,547✔
276
    goto END;
×
277
  }
278
  SSmlKv *maxKV = (SSmlKv *)taosArrayGet(info->maxColKVs, cnt);
110,290✔
279
  if (maxKV == NULL) {
110,290✔
280
    goto END;
×
281
  }
282
  if (unlikely(!IS_SAME_KEY)) {
110,290✔
283
    goto END;
110,547✔
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:
112,411✔
293
  info->dataFormat = false;
112,411✔
294
  info->reRun = true;
112,411✔
295
  return false;
112,154✔
296
}
297

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

303
  if (unlikely(cnt >= taosArrayGetSize(info->maxTagKVs))) {
1,823,141✔
304
    goto END;
×
305
  }
306
  SSmlKv *maxKV = (SSmlKv *)taosArrayGet(info->maxTagKVs, cnt);
1,823,545✔
307
  if (maxKV == NULL) {
1,823,219✔
308
    goto END;
×
309
  }
310
  if (unlikely(!IS_SAME_KEY)) {
1,823,219✔
311
    goto END;
4,551✔
312
  }
313

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

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

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

337
static bool smlIsPKTable(STableMeta *pTableMeta) {
429,735✔
338
  for (int i = 0; i < pTableMeta->tableInfo.numOfColumns; i++) {
7,086,300✔
339
    if (pTableMeta->schema[i].flags & COL_IS_KEY) {
6,659,982✔
340
      return true;
4,957✔
341
    }
342
  }
343

344
  return false;
424,778✔
345
}
346

347
int32_t smlProcessSuperTable(SSmlHandle *info, SSmlLineInfo *elements) {
696,810✔
348
  bool isSameMeasure = IS_SAME_SUPER_TABLE;
696,810✔
349
  if (isSameMeasure) {
696,810✔
350
    return TSDB_CODE_SUCCESS;
17,266✔
351
  }
352
  SSmlSTableMeta **tmp = (SSmlSTableMeta **)taosHashGet(info->superTables, elements->measure, elements->measureLen);
679,544✔
353

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

369
  if (smlIsPKTable(sMeta->tableMeta)) {
274,041✔
370
    return TSDB_CODE_SML_NOT_SUPPORT_PK;
4,957✔
371
  }
372
  return 0;
269,184✔
373
}
374

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

386
    tinfo->tags = taosArrayDup(info->preLineTagKV, NULL);
1,343,616✔
387
    SML_CHECK_NULL(tinfo->tags);
1,343,616✔
388
    for (size_t i = 0; i < taosArrayGetSize(info->preLineTagKV); i++) {
9,296,776✔
389
      SSmlKv *kv = (SSmlKv *)taosArrayGet(info->preLineTagKV, i);
7,952,389✔
390
      SML_CHECK_NULL(kv);
7,952,389✔
391
      if (kv->keyEscaped) kv->key = NULL;
7,952,389✔
392
      if (kv->valueEscaped) kv->value = NULL;
7,952,646✔
393
    }
394

395
    SML_CHECK_CODE(smlSetCTableName(tinfo, info->tbnameKey));
1,342,867✔
396
    SML_CHECK_CODE(getTableUid(info, elements, tinfo));
1,342,846✔
397
    if (info->dataFormat) {
1,342,846✔
398
      info->currSTableMeta->uid = tinfo->uid;
275,034✔
399
      SML_CHECK_CODE(smlInitTableDataCtx(info->pQuery, info->currSTableMeta, &tinfo->tableDataCtx));
275,035✔
400
    }
401
  } else {
402
    tinfo = *oneTable;
3,577✔
403
  }
404
  if (info->dataFormat) info->currTableDataCtx = tinfo->tableDataCtx;
1,346,523✔
405
  return TSDB_CODE_SUCCESS;
1,346,008✔
406

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

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

422
END:
1,301,872✔
423
  clearColValArraySml(info->currTableDataCtx->pValues);
1,301,872✔
424
  RETURN
1,301,269✔
425
}
426

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

438
END:
635,092✔
439
  RETURN
635,092✔
440
}
441

442
int32_t smlParseEndLine(SSmlHandle *info, SSmlLineInfo *elements, SSmlKv *kvTs) {
26,920,105✔
443
  if (info->dataFormat) {
26,920,105✔
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);
26,961,225✔
459
    taosArraySet(elements->colArray, 0, kvTs);
26,961,225✔
460
  }
461
  info->preLine = *elements;
26,982,813✔
462

463
  return TSDB_CODE_SUCCESS;
27,006,200✔
464
}
465

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

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

518
END:
64,128✔
519
  RETURN
68,293✔
520
}
521

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

528
  if (strlen(oneTable->childTableName) == 0) {
1,342,498✔
529
    dst = taosArrayDup(oneTable->tags, NULL);
1,338,590✔
530
    SML_CHECK_NULL(dst);
1,338,680✔
531
    if (oneTable->sTableNameLen >= TSDB_TABLE_NAME_LEN) {
1,338,523✔
532
      SML_CHECK_CODE(TSDB_CODE_SML_INTERNAL_ERROR);
×
533
    }
534
    char          superName[TSDB_TABLE_NAME_LEN] = {0};
1,338,780✔
535
    RandTableName rName = {dst, NULL, (uint8_t)oneTable->sTableNameLen, oneTable->childTableName};
1,339,294✔
536
    if (tsSmlDot2Underline) {
1,339,037✔
537
      (void)memcpy(superName, oneTable->sTableName, oneTable->sTableNameLen);
1,076,129✔
538
      smlStrReplace(superName, oneTable->sTableNameLen);
1,076,129✔
539
      rName.stbFullName = superName;
1,075,882✔
540
    } else {
541
      rName.stbFullName = oneTable->sTableName;
262,908✔
542
    }
543

544
    SML_CHECK_CODE(buildChildTableName(&rName));
1,338,790✔
545
  }
546

547
END:
4,165✔
548
  taosArrayDestroy(dst);
1,343,616✔
549
  RETURN
1,342,846✔
550
}
551

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

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

584
  meta->tags = taosArrayInit(32, sizeof(SSmlKv));
764,674✔
585
  SML_CHECK_NULL(meta->tags);
764,741✔
586

587
  meta->cols = taosArrayInit(32, sizeof(SSmlKv));
764,741✔
588
  SML_CHECK_NULL(meta->cols);
764,741✔
589
  *sMeta = meta;
764,741✔
590
  return TSDB_CODE_SUCCESS;
764,741✔
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) {
99,651,294✔
599
  const char *pVal = kvVal->value;
99,651,294✔
600
  int32_t     len = kvVal->length;
100,117,491✔
601
  char       *endptr = NULL;
100,033,966✔
602
  double      result = taosStr2Double(pVal, &endptr);
100,222,601✔
603
  if (pVal == endptr) {
99,551,142✔
604
    RETURN_FALSE
702✔
605
  }
606

607
  int32_t left = len - (endptr - pVal);
99,550,440✔
608
  if (left == 0) {
99,550,440✔
609
    SET_DOUBLE
124,944✔
610
  } else if (left == 3) {
99,425,496✔
611
    if (endptr[0] == 'f' || endptr[0] == 'F') {
98,950,269✔
612
      if (endptr[1] == '6' && endptr[2] == '4') {
71,941,910✔
613
        SET_DOUBLE
293,389✔
614
      } else if (endptr[1] == '3' && endptr[2] == '2') {
71,800,408✔
615
        SET_FLOAT
71,967,828✔
616
      } else {
UNCOV
617
        RETURN_FALSE
×
618
      }
619
    } else if (endptr[0] == 'i' || endptr[0] == 'I') {
27,038,942✔
620
      if (endptr[1] == '6' && endptr[2] == '4') {
26,436,534✔
621
        SET_BIGINT
291,543✔
622
      } else if (endptr[1] == '3' && endptr[2] == '2') {
26,183,027✔
623
        SET_INT
25,992,274✔
624
      } else if (endptr[1] == '1' && endptr[2] == '6') {
250,903✔
625
        SET_SMALL_INT
293,502✔
626
      } else {
627
        RETURN_FALSE
×
628
      }
629
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
594,441✔
630
      if (endptr[1] == '6' && endptr[2] == '4') {
594,441✔
631
        SET_UBIGINT
274,158✔
632
      } else if (endptr[1] == '3' && endptr[2] == '2') {
320,283✔
633
        SET_UINT
160,220✔
634
      } else if (endptr[1] == '1' && endptr[2] == '6') {
160,063✔
635
        SET_USMALL_INT
160,310✔
636
      } else {
UNCOV
637
        RETURN_FALSE
×
638
      }
639
    } else {
640
      RETURN_FALSE
×
641
    }
642
  } else if (left == 2) {
475,227✔
643
    if (endptr[0] == 'i' || endptr[0] == 'I') {
458,794✔
644
      if (endptr[1] == '8') {
298,641✔
645
        SET_TINYINT
298,731✔
646
      } else {
647
        RETURN_FALSE
×
648
      }
649
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
160,153✔
650
      if (endptr[1] == '8') {
160,153✔
651
        SET_UTINYINT
160,153✔
652
      } else {
653
        RETURN_FALSE
×
654
      }
655
    } else {
656
      RETURN_FALSE
×
657
    }
658
  } else if (left == 1) {
16,433✔
659
    if (endptr[0] == 'i' || endptr[0] == 'I') {
7,208✔
660
      SET_BIGINT
7,208✔
661
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
×
662
      SET_UBIGINT
×
663
    } else {
664
      RETURN_FALSE
×
665
    }
666
  } else {
667
    RETURN_FALSE;
9,763✔
668
  }
669
  return true;
99,668,578✔
670
}
671

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

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

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

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

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

693
  int64_t id = 0;
731,331✔
694
  do {
695
    id = atomic_add_fetch_64(&linesSmlHandleId, 1);
731,331✔
696
  } while (id == 0);
731,628✔
697

698
  return id;
731,628✔
699
}
700

701
static int32_t smlGenerateSchemaAction(SSchema *colField, SHashObj *colHash, SSmlKv *kv, bool isTag,
11,235,203✔
702
                                       ESchemaAction *action, SSmlHandle *info) {
703
  uint16_t *index = colHash ? (uint16_t *)taosHashGet(colHash, kv->key, kv->keyLen) : NULL;
11,235,203✔
704
  if (index) {
11,235,552✔
705
    if (colField[*index].type != kv->type) {
3,963,481✔
706
      snprintf(info->msgBuf.buf, info->msgBuf.len,
5,592✔
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);
3,728✔
709
      uError("%s", info->msgBuf.buf);
1,864✔
710
      return TSDB_CODE_SML_INVALID_DATA;
1,864✔
711
    }
712

713
    if (((colField[*index].type == TSDB_DATA_TYPE_VARCHAR || colField[*index].type == TSDB_DATA_TYPE_VARBINARY ||
3,961,873✔
714
          colField[*index].type == TSDB_DATA_TYPE_GEOMETRY) &&
2,465,921✔
715
         (colField[*index].bytes - VARSTR_HEADER_SIZE) < kv->length) ||
1,496,271✔
716
        (colField[*index].type == TSDB_DATA_TYPE_NCHAR &&
3,958,695✔
717
         ((colField[*index].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE < kv->length))) {
682,655✔
718
      if (isTag) {
25,166✔
719
        *action = SCHEMA_ACTION_CHANGE_TAG_SIZE;
22,076✔
720
      } else {
721
        *action = SCHEMA_ACTION_CHANGE_COLUMN_SIZE;
3,090✔
722
      }
723
    }
724
  } else {
725
    if (isTag) {
7,272,071✔
726
      *action = SCHEMA_ACTION_ADD_TAG;
3,222,414✔
727
    } else {
728
      *action = SCHEMA_ACTION_ADD_COLUMN;
4,049,657✔
729
    }
730
  }
731
  return TSDB_CODE_SUCCESS;
11,234,200✔
732
}
733

734
#define BOUNDARY 1024
735
static int32_t smlFindNearestPowerOf2(int32_t length, uint8_t type) {
2,233,643✔
736
  int32_t result = 1;
2,233,643✔
737
  if (length >= BOUNDARY) {
2,233,643✔
738
    result = length;
10,152✔
739
  } else {
740
    while (result <= length) {
10,595,442✔
741
      result <<= 1;
8,371,951✔
742
    }
743
  }
744

745
  if ((type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_GEOMETRY) &&
2,233,643✔
746
      result > TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE) {
435,966✔
747
    result = TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE;
×
748
  } else if (type == TSDB_DATA_TYPE_NCHAR && result > (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
2,233,643✔
749
    result = (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
750
  }
751

752
  if (type == TSDB_DATA_TYPE_NCHAR) {
2,233,643✔
753
    result = result * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
1,797,677✔
754
  } else if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_GEOMETRY) {
435,966✔
755
    result = result + VARSTR_HEADER_SIZE;
435,827✔
756
  }
757
  return result;
2,233,643✔
758
}
759

760
static int32_t smlProcessSchemaAction(SSmlHandle *info, SSchema *schemaField, SHashObj *schemaHash, SArray *cols,
307,291✔
761
                                      SHashObj *schemaHashCheck, ESchemaAction *action, bool isTag) {
762
  int32_t code = TSDB_CODE_SUCCESS;
307,291✔
763
  int32_t lino = 0;
307,291✔
764
  for (int j = 0; j < taosArrayGetSize(cols); ++j) {
4,319,000✔
765
    if (j == 0 && !isTag) continue;
4,014,861✔
766
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, j);
3,863,250✔
767
    SML_CHECK_NULL(kv);
3,863,942✔
768
    SML_CHECK_CODE(smlGenerateSchemaAction(schemaField, schemaHash, kv, isTag, action, info));
3,863,942✔
769
    if (taosHashGet(schemaHashCheck, kv->key, kv->keyLen) != NULL) {
3,862,445✔
770
      uError("SML:0x%" PRIx64 ", %s duplicated column %s", info->id, __FUNCTION__, kv->key);
1,143✔
771
      SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
1,143✔
772
    }
773
  }
774

775
END:
304,305✔
776
  RETURN
307,312✔
777
}
778

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

802
END:
296,300✔
803
  taosHashCleanup(hashTmp);
297,078✔
804
  RETURN
297,078✔
805
}
806

807
static int32_t getBytes(uint8_t type, int32_t length) {
7,271,398✔
808
  if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_NCHAR ||
7,271,398✔
809
      type == TSDB_DATA_TYPE_GEOMETRY) {
810
    return smlFindNearestPowerOf2(length, type);
2,233,553✔
811
  } else {
812
    return tDataTypes[type].bytes;
5,037,845✔
813
  }
814
}
815

816
static int32_t smlBuildFieldsList(SSmlHandle *info, SSchema *schemaField, SHashObj *schemaHash, SArray *cols,
737,067✔
817
                                  SArray *results, int32_t numOfCols, bool isTag) {
818
  int32_t code = TSDB_CODE_SUCCESS;
737,067✔
819
  int32_t lino = 0;
737,067✔
820
  for (int j = 0; j < taosArrayGetSize(cols); ++j) {
8,108,307✔
821
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, j);
7,371,240✔
822
    SML_CHECK_NULL(kv);
7,371,420✔
823
    ESchemaAction action = SCHEMA_ACTION_NULL;
7,371,420✔
824
    SML_CHECK_CODE(smlGenerateSchemaAction(schemaField, schemaHash, kv, isTag, &action, info));
7,371,420✔
825
    if (action == SCHEMA_ACTION_ADD_COLUMN || action == SCHEMA_ACTION_ADD_TAG) {
14,630,055✔
826
      SField field = {0};
7,258,676✔
827
      field.type = kv->type;
7,258,676✔
828
      field.bytes = getBytes(kv->type, kv->length);
7,258,676✔
829
      (void)memcpy(field.name, kv->key, TMIN(kv->keyLen, sizeof(field.name) - 1));
7,258,496✔
830
      SML_CHECK_NULL(taosArrayPush(results, &field));
7,258,774✔
831
    } else if (action == SCHEMA_ACTION_CHANGE_COLUMN_SIZE || action == SCHEMA_ACTION_CHANGE_TAG_SIZE) {
112,605✔
832
      uint16_t *index = (uint16_t *)taosHashGet(schemaHash, kv->key, kv->keyLen);
12,722✔
833
      if (index == NULL) {
12,583✔
834
        SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
835
      }
836
      uint16_t newIndex = *index;
12,583✔
837
      if (isTag) newIndex -= numOfCols;
12,583✔
838
      SField *field = (SField *)taosArrayGet(results, newIndex);
12,583✔
839
      SML_CHECK_NULL(field);
12,583✔
840
      field->bytes = getBytes(kv->type, kv->length);
12,583✔
841
    }
842
  }
843

844
  int32_t maxLen = isTag ? TSDB_MAX_TAGS_LEN : TSDB_MAX_BYTES_PER_ROW;
737,206✔
845
  int32_t len = 0;
737,206✔
846
  for (int j = 0; j < taosArrayGetSize(results); ++j) {
8,109,673✔
847
    SField *field = taosArrayGet(results, j);
7,372,377✔
848
    SML_CHECK_NULL(field);
7,372,467✔
849
    len += field->bytes;
7,372,467✔
850
  }
851
  if (len > maxLen) {
737,206✔
852
    return isTag ? TSDB_CODE_PAR_INVALID_TAGS_LENGTH : TSDB_CODE_PAR_INVALID_ROW_LENGTH;
3,997✔
853
  }
854

855
END:
733,209✔
856
  RETURN
733,209✔
857
}
858

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

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

881
  pReq.pColumns = taosArrayInit(pReq.numOfColumns, sizeof(SFieldWithOptions));
372,949✔
882
  SML_CHECK_NULL(pReq.pColumns);
373,088✔
883
  for (int32_t i = 0; i < pReq.numOfColumns; ++i) {
4,478,776✔
884
    SField *pField = taosArrayGet(pColumns, i);
4,105,917✔
885
    SML_CHECK_NULL(pField);
4,106,056✔
886
    SFieldWithOptions fieldWithOption = {0};
4,106,056✔
887
    setFieldWithOptions(&fieldWithOption, pField);
4,106,056✔
888
    setDefaultOptionsForField(&fieldWithOption);
4,106,056✔
889
    SML_CHECK_NULL(taosArrayPush(pReq.pColumns, &fieldWithOption));
8,211,654✔
890
  }
891

892
  if (action == SCHEMA_ACTION_CREATE_STABLE) {
372,859✔
893
    pSql = "sml_create_stable";
359,573✔
894
    smlBuildCreateStbReq(&pReq, 1, 1, 0, TD_REQ_FROM_APP);
895
  } else if (action == SCHEMA_ACTION_ADD_TAG || action == SCHEMA_ACTION_CHANGE_TAG_SIZE) {
13,286✔
896
    pSql = (action == SCHEMA_ACTION_ADD_TAG) ? "sml_add_tag" : "sml_modify_tag_size";
9,852✔
897
    smlBuildCreateStbReq(&pReq, pTableMeta->sversion, pTableMeta->tversion + 1, pTableMeta->uid, TD_REQ_FROM_SML);
9,852✔
898
  } else if (action == SCHEMA_ACTION_ADD_COLUMN || action == SCHEMA_ACTION_CHANGE_COLUMN_SIZE) {
3,434✔
899
    pSql = (action == SCHEMA_ACTION_ADD_COLUMN) ? "sml_add_column" : "sml_modify_column_size";
3,434✔
900
    smlBuildCreateStbReq(&pReq, pTableMeta->sversion + 1, pTableMeta->tversion, pTableMeta->uid, TD_REQ_FROM_SML);
3,434✔
901
  } else {
902
    SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
903
  }
904

905
  SML_CHECK_CODE(buildRequest(info->taos->id, pSql, strlen(pSql), NULL, false, &pRequest, 0));
372,859✔
906

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

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

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

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

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

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

949
  if (pRequest->code == TSDB_CODE_SUCCESS) {
373,088✔
950
    SML_CHECK_CODE(catalogRemoveTableMeta(info->pCatalog, pName));
346,210✔
951
  }
952
  code = pRequest->code;
373,088✔
953

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

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

978
  SML_CHECK_CODE(catalogGetSTableMeta(info->pCatalog, conn, pName, pTableMeta));
332,924✔
979

980
END:
335,871✔
981
  taosArrayDestroy(pColumns);
360,440✔
982
  taosArrayDestroy(pTags);
360,440✔
983
  RETURN
360,440✔
984
}
985

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

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

1024
    SML_CHECK_CODE(smlSendMetaMsg(info, pName, pColumns, &pTags, (*pTableMeta), action));
9,852✔
1025

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

1032
END:
155,368✔
1033
  taosArrayDestroy(pColumns);
155,525✔
1034
  taosArrayDestroy(pTags);
155,682✔
1035
  RETURN
155,435✔
1036
}
1037

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

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

1055
    SML_CHECK_CODE(smlSendMetaMsg(info, pName, pColumns, &pTags, (*pTableMeta), action));
3,434✔
1056

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

1063
END:
151,699✔
1064
  taosArrayDestroy(pColumns);
151,699✔
1065
  taosArrayDestroy(pTags);
151,542✔
1066
  RETURN
151,542✔
1067
}
1068

1069
static int32_t smlBuildTempHash(SHashObj *hashTmp, STableMeta *pTableMeta, uint16_t start, uint16_t end) {
311,540✔
1070
  int32_t code = 0;
311,540✔
1071
  int32_t lino = 0;
311,540✔
1072
  for (uint16_t i = start; i < end; i++) {
4,331,481✔
1073
    SML_CHECK_CODE(
4,020,197✔
1074
        taosHashPut(hashTmp, pTableMeta->schema[i].name, strlen(pTableMeta->schema[i].name), &i, SHORT_BYTES));
1075
  }
1076

1077
END:
311,540✔
1078
  return code;
311,540✔
1079
}
1080

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

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

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

1102
  SSmlSTableMeta **tmp = (SSmlSTableMeta **)taosHashIterate(info->superTables, NULL);
510,626✔
1103
  while (tmp) {
991,700✔
1104
    SSmlSTableMeta *sTableData = *tmp;
516,210✔
1105
    bool            needCheckMeta = false;  // for multi thread
516,210✔
1106

1107
    size_t superTableLen = 0;
516,210✔
1108
    void  *superTable = taosHashGetKey(tmp, &superTableLen);
516,210✔
1109
    char  *measure = taosMemoryMalloc(superTableLen);
516,053✔
1110
    SML_CHECK_NULL(measure);
541,714✔
1111
    (void)memcpy(measure, superTable, superTableLen);
516,210✔
1112
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
516,210✔
1113
      PROCESS_SLASH_IN_MEASUREMENT(measure, superTableLen);
1,715,275✔
1114
    }
1115
    smlStrReplace(measure, superTableLen);
516,210✔
1116
    size_t nameLen = TMIN(superTableLen, TSDB_TABLE_NAME_LEN - 1);
516,053✔
1117
    (void)memcpy(pName.tname, measure, nameLen);
516,053✔
1118
    pName.tname[nameLen] = '\0';
516,053✔
1119
    taosMemoryFree(measure);
516,053✔
1120

1121
    pTableMeta = NULL;
516,053✔
1122
    code = catalogGetSTableMeta(info->pCatalog, &conn, &pName, &pTableMeta);
516,053✔
1123

1124
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_MND_STB_NOT_EXIST) {
516,071✔
1125
      SML_CHECK_CODE(smlCreateTable(info, &conn, sTableData, &pName, &pTableMeta));
360,301✔
1126
    } else if (code == TSDB_CODE_SUCCESS) {
155,770✔
1127
      if (smlIsPKTable(pTableMeta)) {
155,770✔
1128
        SML_CHECK_CODE(TSDB_CODE_SML_NOT_SUPPORT_PK);
×
1129
      }
1130

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

1141
      SML_CHECK_CODE(smlModifyTag(info, colHashTmp, tagHashTmp, &conn, sTableData, &pName, &pTableMeta));
155,770✔
1142
      SML_CHECK_CODE(smlModifyCols(info, tagHashTmp, colHashTmp, &conn, sTableData, &pName, &pTableMeta));
151,364✔
1143

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

1154
    if (needCheckMeta) {
481,852✔
1155
      SML_CHECK_CODE(smlCheckMeta(&(pTableMeta->schema[pTableMeta->tableInfo.numOfColumns]),
148,771✔
1156
                                  pTableMeta->tableInfo.numOfTags, sTableData->tags));
1157
      SML_CHECK_CODE(smlCheckMeta(&(pTableMeta->schema[0]), pTableMeta->tableInfo.numOfColumns, sTableData->cols));
148,150✔
1158
    }
1159

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

1169
  return TSDB_CODE_SUCCESS;
475,490✔
1170

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

1180
  return code;
35,136✔
1181
}
1182

1183
static int32_t smlInsertMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, SHashObj *checkDuplicate) {
980,071✔
1184
  int32_t code = 0;
980,071✔
1185
  int32_t lino = 0;
980,071✔
1186
  terrno = 0;
980,071✔
1187
  for (int16_t i = 0; i < taosArrayGetSize(cols); ++i) {
11,858,261✔
1188
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
10,882,695✔
1189
    SML_CHECK_NULL(kv);
10,882,818✔
1190
    int ret = taosHashPut(metaHash, kv->key, kv->keyLen, &i, SHORT_BYTES);
10,882,818✔
1191
    if (ret == 0) {
10,883,983✔
1192
      SML_CHECK_NULL(taosArrayPush(metaArray, kv));
10,879,936✔
1193
      if (taosHashGet(checkDuplicate, kv->key, kv->keyLen) != NULL) {
10,879,936✔
1194
        uError("%s duplicated column %s", __FUNCTION__, kv->key);
1,462✔
1195
        SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
1,462✔
1196
      }
1197
    } else if (terrno == TSDB_CODE_DUP_KEY) {
3,779✔
1198
      uError("%s duplicated column %s", __FUNCTION__, kv->key);
3,779✔
1199
      return TSDB_CODE_PAR_DUPLICATED_COLUMN;
3,779✔
1200
    }
1201
  }
1202

1203
END:
976,204✔
1204
  RETURN
976,204✔
1205
}
1206

1207
static int32_t smlUpdateMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, bool isTag, SSmlMsgBuf *msg,
54,175,800✔
1208
                             SHashObj *checkDuplicate) {
1209
  int32_t code = 0;
54,175,800✔
1210
  int32_t lino = 0;
54,175,800✔
1211
  for (int i = 0; i < taosArrayGetSize(cols); ++i) {
257,346,193✔
1212
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
202,712,000✔
1213
    SML_CHECK_NULL(kv);
202,716,167✔
1214
    int16_t *index = (int16_t *)taosHashGet(metaHash, kv->key, kv->keyLen);
202,716,167✔
1215
    if (index) {
200,575,878✔
1216
      SSmlKv *value = (SSmlKv *)taosArrayGet(metaArray, *index);
200,566,434✔
1217
      SML_CHECK_NULL(value);
203,420,891✔
1218

1219
      if (isTag) {
203,430,190✔
1220
        if (kv->length > value->length) {
59,089,543✔
1221
          value->length = kv->length;
38,964✔
1222
        }
1223
        continue;
59,105,992✔
1224
      }
1225
      if (kv->type != value->type) {
144,340,647✔
1226
        smlBuildInvalidDataMsg(msg, "the type is not the same like before", kv->key);
×
1227
        SML_CHECK_CODE(TSDB_CODE_SML_NOT_SAME_TYPE);
×
1228
      }
1229

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

1249
END:
53,468,995✔
1250
  RETURN
54,223,541✔
1251
}
1252

1253
void smlDestroyTableInfo(void *para) {
1,343,616✔
1254
  SSmlTableInfo *tag = *(SSmlTableInfo **)para;
1,343,616✔
1255
  for (size_t i = 0; i < taosArrayGetSize(tag->cols); i++) {
28,995,411✔
1256
    SHashObj *kvHash = (SHashObj *)taosArrayGetP(tag->cols, i);
27,647,940✔
1257
    taosHashCleanup(kvHash);
27,601,423✔
1258
  }
1259

1260
  taosArrayDestroy(tag->cols);
1,343,616✔
1261
  taosArrayDestroyEx(tag->tags, freeSSmlKv);
1,343,616✔
1262
  taosMemoryFree(tag);
1,343,359✔
1263
}
1,343,102✔
1264

1265
void freeSSmlKv(void *data) {
170,292,105✔
1266
  SSmlKv *kv = (SSmlKv *)data;
170,292,105✔
1267
  if (kv->keyEscaped) taosMemoryFreeClear(kv->key);
170,292,105✔
1268
  if (kv->valueEscaped) taosMemoryFreeClear(kv->value);
170,312,652✔
1269
#ifdef USE_GEOS
1270
  if (kv->type == TSDB_DATA_TYPE_GEOMETRY) geosFreeBuffer((void *)(kv->value));
170,300,581✔
1271
#endif
1272
  if (kv->type == TSDB_DATA_TYPE_VARBINARY) taosMemoryFreeClear(kv->value);
170,305,714✔
1273
}
170,306,239✔
1274

1275
void smlDestroyInfo(SSmlHandle *info) {
1,463,206✔
1276
  if (info == NULL) return;
1,463,206✔
1277

1278
  taosHashCleanup(info->pVgHash);
731,628✔
1279
  taosHashCleanup(info->childTables);
731,628✔
1280
  taosHashCleanup(info->superTables);
731,628✔
1281
  taosHashCleanup(info->tableUids);
731,628✔
1282

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

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

1295
  taosArrayDestroyEx(info->preLineTagKV, freeSSmlKv);
731,628✔
1296
  taosArrayDestroyP(info->escapedStringList, NULL);
731,628✔
1297

1298
  if (!info->dataFormat) {
731,628✔
1299
    for (int i = 0; i < info->lineNum; i++) {
28,245,202✔
1300
      taosArrayDestroyEx(info->lines[i].colArray, freeSSmlKv);
27,718,649✔
1301
      if (info->lines[i].measureTagsLen != 0 && info->protocol != TSDB_SML_LINE_PROTOCOL) {
27,718,480✔
1302
        taosMemoryFree(info->lines[i].measureTag);
287,048✔
1303
      }
1304
    }
1305
    taosMemoryFree(info->lines);
526,810✔
1306
  }
1307
  if (info->protocol == TSDB_SML_JSON_PROTOCOL) {
731,628✔
1308
    taosMemoryFreeClear(info->preLine.tags);
311,683✔
1309
  }
1310
  cJSON_Delete(info->root);
731,628✔
1311
  taosMemoryFreeClear(info);
731,628✔
1312
}
1313

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

1325
  info->pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
731,578✔
1326
  SML_CHECK_NULL(info->pVgHash);
731,628✔
1327
  info->childTables = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
731,628✔
1328
  SML_CHECK_NULL(info->childTables);
731,628✔
1329
  info->tableUids = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
731,371✔
1330
  SML_CHECK_NULL(info->tableUids);
731,628✔
1331
  info->superTables = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
731,628✔
1332
  SML_CHECK_NULL(info->superTables);
731,628✔
1333
  taosHashSetFreeFp(info->superTables, smlDestroySTableMeta);
731,628✔
1334
  taosHashSetFreeFp(info->childTables, smlDestroyTableInfo);
731,628✔
1335

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

1348
  *handle = info;
731,628✔
1349
  info = NULL;
731,628✔
1350

1351
END:
731,628✔
1352
  smlDestroyInfo(info);
731,628✔
1353
  RETURN
731,578✔
1354
}
1355

1356
static int32_t smlPushCols(SArray *colsArray, SArray *cols) {
27,635,605✔
1357
  int32_t   code = TSDB_CODE_SUCCESS;
27,635,605✔
1358
  int32_t   lino = 0;
27,635,605✔
1359
  SHashObj *kvHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
27,635,605✔
1360
  SML_CHECK_NULL(kvHash);
27,613,378✔
1361
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
178,911,725✔
1362
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
151,104,536✔
1363
    SML_CHECK_NULL(kv);
151,148,289✔
1364
    terrno = 0;
151,148,289✔
1365
    code = taosHashPut(kvHash, kv->key, kv->keyLen, &kv, POINTER_BYTES);
151,338,239✔
1366
    if (terrno == TSDB_CODE_DUP_KEY) {
151,334,676✔
1367
      uError("%s duplicated column %s", __FUNCTION__, kv->key);
1,251✔
1368
      SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
536✔
1369
    }
1370
    SML_CHECK_CODE(code);
151,278,311✔
1371
  }
1372

1373
  SML_CHECK_NULL(taosArrayPush(colsArray, &kvHash));
27,639,009✔
1374
  return code;
27,639,009✔
1375
END:
356✔
1376
  taosHashCleanup(kvHash);
1,251✔
1377
  RETURN
1,251✔
1378
}
1379

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

1386
  for (int32_t i = 0; i < info->lineNum; i++) {
28,082,445✔
1387
    SSmlLineInfo  *elements = info->lines + i;
27,616,391✔
1388
    SSmlTableInfo *tinfo = NULL;
27,613,307✔
1389
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
27,613,307✔
1390
      SSmlTableInfo **tmp =
1391
          (SSmlTableInfo **)taosHashGet(info->childTables, elements->measure, elements->measureTagsLen);
26,984,128✔
1392
      if (tmp) tinfo = *tmp;
27,009,956✔
1393
    } else {
1394
      SSmlTableInfo **tmp = (SSmlTableInfo **)taosHashGet(info->childTables, elements->measureTag,
635,090✔
1395
                                                          elements->measureLen + elements->tagsLen);
635,090✔
1396
      if (tmp) tinfo = *tmp;
633,287✔
1397
    }
1398

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

1405
    if (taosArrayGetSize(tinfo->tags) > TSDB_MAX_TAGS) {
27,642,729✔
1406
      smlBuildInvalidDataMsg(&info->msgBuf, "too many tags than 128", NULL);
2,447✔
1407
      return TSDB_CODE_PAR_INVALID_TAGS_NUM;
2,447✔
1408
    }
1409

1410
    if (taosArrayGetSize(elements->colArray) + taosArrayGetSize(tinfo->tags) > TSDB_MAX_COLUMNS) {
27,632,831✔
1411
      smlBuildInvalidDataMsg(&info->msgBuf, "too many columns than 4096", NULL);
613✔
1412
      return TSDB_CODE_PAR_TOO_MANY_COLUMNS;
613✔
1413
    }
1414

1415
    SML_CHECK_CODE(smlPushCols(tinfo->cols, elements->colArray));
27,636,835✔
1416

1417
    SSmlSTableMeta **tableMeta =
1418
        (SSmlSTableMeta **)taosHashGet(info->superTables, elements->measure, elements->measureLen);
27,292,573✔
1419
    if (tableMeta) {  // update meta
27,642,967✔
1420
      uDebug("SML:0x%" PRIx64 ", %s update meta, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
27,151,042✔
1421
             info->lineNum);
1422
      SML_CHECK_CODE(smlUpdateMeta((*tableMeta)->colHash, (*tableMeta)->cols, elements->colArray, false, &info->msgBuf,
27,151,042✔
1423
                                   (*tableMeta)->tagHash));
1424
      SML_CHECK_CODE(smlUpdateMeta((*tableMeta)->tagHash, (*tableMeta)->tags, tinfo->tags, true, &info->msgBuf,
27,153,009✔
1425
                                   (*tableMeta)->colHash));
1426
    } else {
1427
      uDebug("SML:0x%" PRIx64 ", %s add meta, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
491,925✔
1428
             info->lineNum);
1429
      SSmlSTableMeta *meta = NULL;
491,925✔
1430
      SML_CHECK_CODE(smlBuildSTableMeta(info->dataFormat, &meta));
491,925✔
1431
      code = taosHashPut(info->superTables, elements->measure, elements->measureLen, &meta, POINTER_BYTES);
491,768✔
1432
      if (code != TSDB_CODE_SUCCESS) {
491,925✔
1433
        smlDestroySTableMeta(&meta);
×
1434
        SML_CHECK_CODE(code);
×
1435
      }
1436
      SML_CHECK_CODE(smlInsertMeta(meta->tagHash, meta->tags, tinfo->tags, NULL));
491,925✔
1437
      SML_CHECK_CODE(smlInsertMeta(meta->colHash, meta->cols, elements->colArray, meta->tagHash));
488,146✔
1438
    }
1439
  }
1440
  uDebug("SML:0x%" PRIx64 ", %s end, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat, info->lineNum);
480,960✔
1441

1442
END:
143,963✔
1443
  RETURN
487,452✔
1444
}
1445

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

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

1463
  oneTable = (SSmlTableInfo **)taosHashIterate(info->childTables, NULL);
617,067✔
1464
  while (oneTable) {
1,807,318✔
1465
    SSmlTableInfo *tableData = *oneTable;
1,190,889✔
1466

1467
    int measureLen = tableData->sTableNameLen;
1,190,889✔
1468
    measure = (char *)taosMemoryMalloc(tableData->sTableNameLen);
1,191,660✔
1469
    SML_CHECK_NULL(measure);
1,190,889✔
1470
    (void)memcpy(measure, tableData->sTableName, tableData->sTableNameLen);
1,190,889✔
1471
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
1,191,146✔
1472
      PROCESS_SLASH_IN_MEASUREMENT(measure, measureLen);
7,191,233✔
1473
    }
1474
    smlStrReplace(measure, measureLen);
1,191,917✔
1475
    (void)memcpy(pName.tname, measure, measureLen);
1,191,807✔
1476
    pName.tname[measureLen] = '\0';
1,191,807✔
1477

1478
    if (info->pRequest->tableList == NULL) {
1,191,550✔
1479
      info->pRequest->tableList = taosArrayInit(1, sizeof(SName));
617,067✔
1480
      SML_CHECK_NULL(info->pRequest->tableList);
616,910✔
1481
    }
1482
    SML_CHECK_NULL(taosArrayPush(info->pRequest->tableList, &pName));
2,382,943✔
1483
    tstrncpy(pName.tname, tableData->childTableName, sizeof(pName.tname));
1,191,293✔
1484

1485
    SRequestConnInfo conn = {0};
1,191,293✔
1486
    conn.pTrans = info->taos->pAppInfo->pTransporter;
1,191,807✔
1487
    conn.requestId = info->pRequest->requestId;
1,191,807✔
1488
    conn.requestObjRefId = info->pRequest->self;
1,192,064✔
1489
    conn.mgmtEps = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
1,192,064✔
1490

1491
    SML_CHECK_CODE(smlCheckAuth(info, &conn, pName.tname, AUTH_TYPE_WRITE));
1,192,064✔
1492

1493
    SVgroupInfo vg = {0};
1,190,832✔
1494
    SML_CHECK_CODE(catalogGetTableHashVgroup(info->pCatalog, &conn, &pName, &vg));
1,190,061✔
1495
    SML_CHECK_CODE(taosHashPut(info->pVgHash, (const char *)&vg.vgId, sizeof(vg.vgId), (char *)&vg, sizeof(vg)));
1,190,955✔
1496

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

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

1510
    SML_CHECK_CODE(smlBindData(info->pQuery, info->dataFormat, tableData->tags, (*pMeta)->cols, tableData->cols,
1,191,169✔
1511
                               (*pMeta)->tableMeta, tableData->childTableName, measure, measureLen, info->ttl,
1512
                               info->msgBuf.buf, info->msgBuf.len, info->taos->optionInfo.charsetCxt));
1513
    taosMemoryFreeClear(measure);
1,189,894✔
1514
    oneTable = (SSmlTableInfo **)taosHashIterate(info->childTables, oneTable);
1,189,637✔
1515
  }
1516

1517
  SML_CHECK_CODE(smlBuildOutput(info->pQuery, info->pVgHash));
616,429✔
1518
  info->cost.insertRpcTime = taosGetTimestampUs();
616,272✔
1519

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

1523
  launchQueryImpl(info->pRequest, info->pQuery, true, NULL);  // no need to check return code
616,429✔
1524

1525
  uDebug("SML:0x%" PRIx64 ", %s end, format:%d, code:%d,%s", info->id, __FUNCTION__, info->dataFormat,
616,429✔
1526
         info->pRequest->code, tstrerror(info->pRequest->code));
1527

1528
  return info->pRequest->code;
616,429✔
1529

1530
END:
895✔
1531
  taosMemoryFree(measure);
638✔
1532
  taosHashCancelIterate(info->childTables, oneTable);
638✔
1533
  RETURN
638✔
1534
}
1535

1536
static void smlPrintStatisticInfo(SSmlHandle *info) {
730,603✔
1537
  uDebug(
730,603✔
1538
      "SML:0x%" PRIx64
1539
      " 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 \
1540
        parse cost:%" PRId64 ",schema cost:%" PRId64 ",bind cost:%" PRId64 ",rpc cost:%" PRId64 ",total cost:%" PRId64,
1541
      info->id, info->cost.code, tstrerror(info->cost.code), info->cost.lineNum, info->cost.numOfSTables,
1542
      info->cost.numOfCTables, info->cost.numOfCreateSTables, info->cost.numOfAlterTagSTables,
1543
      info->cost.numOfAlterColSTables, info->cost.schemaTime - info->cost.parseTime,
1544
      info->cost.insertBindTime - info->cost.schemaTime, info->cost.insertRpcTime - info->cost.insertBindTime,
1545
      info->cost.endTime - info->cost.insertRpcTime, info->cost.endTime - info->cost.parseTime);
1546
}
730,603✔
1547

1548
int32_t smlClearForRerun(SSmlHandle *info) {
526,810✔
1549
  int32_t code = 0;
526,810✔
1550
  int32_t lino = 0;
526,810✔
1551
  info->reRun = false;
526,810✔
1552

1553
  taosHashClear(info->childTables);
526,810✔
1554
  taosHashClear(info->superTables);
526,810✔
1555
  taosHashClear(info->tableUids);
526,553✔
1556

1557
  if (!info->dataFormat) {
526,810✔
1558
    info->lines = (SSmlLineInfo *)taosMemoryCalloc(info->lineNum, sizeof(SSmlLineInfo));
526,810✔
1559
    SML_CHECK_NULL(info->lines);
526,810✔
1560
  }
1561

1562
  taosArrayClearP(info->escapedStringList, NULL);
526,810✔
1563
  if (info->protocol == TSDB_SML_JSON_PROTOCOL) {
526,810✔
1564
    taosMemoryFreeClear(info->preLine.tags);
189,601✔
1565
  }
1566
  (void)memset(&info->preLine, 0, sizeof(SSmlLineInfo));
526,810✔
1567
  info->currSTableMeta = NULL;
526,810✔
1568
  info->currTableDataCtx = NULL;
526,810✔
1569

1570
  SVnodeModifyOpStmt *stmt = (SVnodeModifyOpStmt *)(info->pQuery->pRoot);
526,810✔
1571
  stmt->freeHashFunc(stmt->pTableBlockHashObj);
526,810✔
1572
  stmt->pTableBlockHashObj = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
526,810✔
1573
  SML_CHECK_NULL(stmt->pTableBlockHashObj);
526,810✔
1574

1575
END:
526,810✔
1576
  RETURN
526,810✔
1577
}
1578

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

1595
static bool getLine(SSmlHandle *info, char *lines[], char **rawLine, char *rawLineEnd, int numLines, int i, char **tmp,
27,776,822✔
1596
                    int *len) {
1597
  if (lines) {
27,776,822✔
1598
    *tmp = lines[i];
27,917,475✔
1599
    *len = strlen(*tmp);
28,017,448✔
1600
  } else if (*rawLine) {
17,001✔
1601
    *tmp = *rawLine;
23,391✔
1602
    while (*rawLine < rawLineEnd) {
52,867,912✔
1603
      if (*((*rawLine)++) == '\n') {
52,860,268✔
1604
        break;
15,747✔
1605
      }
1606
      (*len)++;
52,844,521✔
1607
    }
1608
    if (IS_COMMENT(info->protocol, (*tmp)[0])) {  // this line is comment
23,391✔
1609
      return false;
319✔
1610
    }
1611
  }
1612

1613
  if (*rawLine != NULL && (uDebugFlag & DEBUG_DEBUG)) {
28,065,114✔
1614
    printRaw(info->id, i, numLines, DEBUG_DEBUG, *tmp, *len);
5,742✔
1615
  } else {
1616
    uDebug("SML:0x%" PRIx64 ", smlParseLine is not raw, line %d/%d :%s", info->id, i, numLines, *tmp);
27,967,880✔
1617
  }
1618
  return true;
27,985,184✔
1619
}
1620

1621
static int32_t smlParseJson(SSmlHandle *info, char *lines[], char *rawLine) {
311,543✔
1622
  int32_t code = TSDB_CODE_SUCCESS;
311,543✔
1623
  if (lines) {
311,543✔
1624
    code = smlParseJSONExt(info, *lines);
310,160✔
1625
  } else if (rawLine) {
1,383✔
1626
    code = smlParseJSONExt(info, rawLine);
1,383✔
1627
  }
1628
  if (code != TSDB_CODE_SUCCESS) {
311,683✔
1629
    uError("%s failed code:%d", __FUNCTION__, code);
41,872✔
1630
  }
1631
  return code;
311,683✔
1632
}
1633

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

1641
  char   *oldRaw = rawLine;
419,374✔
1642
  int32_t i = 0;
419,374✔
1643
  while (i < numLines) {
28,360,369✔
1644
    char *tmp = NULL;
27,995,481✔
1645
    int   len = 0;
27,993,425✔
1646
    if (!getLine(info, lines, &rawLine, rawLineEnd, numLines, i, &tmp, &len)) {
27,994,196✔
1647
      continue;
34,069✔
1648
    }
1649
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
27,988,268✔
1650
      if (info->dataFormat) {
27,200,693✔
1651
        SSmlLineInfo element = {0};
234,847✔
1652
        code = smlParseInfluxString(info, tmp, tmp + len, &element);
234,847✔
1653
      } else {
1654
        code = smlParseInfluxString(info, tmp, tmp + len, info->lines + i);
26,974,841✔
1655
      }
1656
    } else if (info->protocol == TSDB_SML_TELNET_PROTOCOL) {
805,051✔
1657
      if (info->dataFormat) {
804,894✔
1658
        SSmlLineInfo element = {0};
573,443✔
1659
        code = smlParseTelnetString(info, (char *)tmp, (char *)tmp + len, &element);
573,443✔
1660
        if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
573,355✔
1661
      } else {
1662
        code = smlParseTelnetString(info, (char *)tmp, (char *)tmp + len, info->lines + i);
231,451✔
1663
      }
1664
    }
1665
    if (code != TSDB_CODE_SUCCESS) {
27,970,369✔
1666
      if (rawLine != NULL) {
54,879✔
1667
        printRaw(info->id, i, numLines, DEBUG_ERROR, tmp, len);
×
1668
      } else {
1669
        uError("SML:0x%" PRIx64 ", %s failed. line %d :%s", info->id, __FUNCTION__, i, tmp);
54,879✔
1670
      }
1671
      return code;
54,879✔
1672
    }
1673
    if (info->reRun) {
27,915,490✔
1674
      uDebug("SML:0x%" PRIx64 ", %s re run", info->id, __FUNCTION__);
336,952✔
1675
      i = 0;
336,952✔
1676
      rawLine = oldRaw;
336,952✔
1677
      code = smlClearForRerun(info);
336,952✔
1678
      if (code != TSDB_CODE_SUCCESS) {
337,209✔
1679
        return code;
×
1680
      }
1681
      continue;
337,209✔
1682
    }
1683
    i++;
27,581,365✔
1684
  }
1685
  uDebug("SML:0x%" PRIx64 ", %s end", info->id, __FUNCTION__);
364,888✔
1686

1687
  return code;
365,066✔
1688
}
1689

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

1695
  info->cost.parseTime = taosGetTimestampUs();
731,371✔
1696

1697
  SML_CHECK_CODE(smlParseStart(info, lines, rawLine, rawLineEnd, numLines));
731,628✔
1698
  SML_CHECK_CODE(smlParseEnd(info));
634,877✔
1699

1700
  info->cost.lineNum = info->lineNum;
625,325✔
1701
  info->cost.numOfSTables = taosHashGetSize(info->superTables);
625,325✔
1702
  info->cost.numOfCTables = taosHashGetSize(info->childTables);
625,325✔
1703
  info->cost.schemaTime = taosGetTimestampUs();
625,168✔
1704

1705
  do {
1706
    code = smlModifyDBSchemas(info);
652,360✔
1707
    if (code != TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER && code != TSDB_CODE_SDB_OBJ_CREATING && code != TSDB_CODE_SYN_NOT_LEADER &&
651,889✔
1708
        code != TSDB_CODE_MND_TRANS_CONFLICT) {
1709
      break;
625,325✔
1710
    }
1711
    taosMsleep(100);
26,564✔
1712
    uInfo("SML:0x%" PRIx64 ", smlModifyDBSchemas retry code:%s, times:%d", info->id, tstrerror(code), retryNum);
26,878✔
1713
  } while (retryNum++ < taosHashGetSize(info->superTables) * MAX_RETRY_TIMES);
26,878✔
1714

1715
  SML_CHECK_CODE(code);
625,011✔
1716
  info->cost.insertBindTime = taosGetTimestampUs();
616,910✔
1717
  SML_CHECK_CODE(smlInsertData(info));
616,910✔
1718

1719
END:
616,429✔
1720
  RETURN
731,628✔
1721
}
1722

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

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

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

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

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

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

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

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

1784
    smlSetReqSQL(request, lines, rawLine, rawLineEnd);
731,214✔
1785

1786
    if (request->pDb == NULL) {
731,074✔
1787
      request->code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1788
      smlBuildInvalidDataMsg(&msg, "Database not specified", NULL);
×
1789
      goto END;
×
1790
    }
1791

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

1798
    if (protocol == TSDB_SML_LINE_PROTOCOL &&
731,578✔
1799
        (precision < TSDB_SML_TIMESTAMP_NOT_CONFIGURED || precision > TSDB_SML_TIMESTAMP_NANO_SECONDS)) {
235,104✔
1800
      request->code = TSDB_CODE_SML_INVALID_PRECISION_TYPE;
571✔
1801
      smlBuildInvalidDataMsg(&msg, "precision invalidate for line protocol", NULL);
314✔
1802
      goto END;
×
1803
    }
1804

1805
    if (protocol == TSDB_SML_JSON_PROTOCOL) {
731,007✔
1806
      numLines = 1;
311,633✔
1807
    } else if (numLines <= 0) {
419,374✔
1808
      request->code = TSDB_CODE_SML_INVALID_DATA;
×
1809
      smlBuildInvalidDataMsg(&msg, "line num is invalid", NULL);
×
1810
      goto END;
×
1811
    }
1812

1813
    code = smlProcess(info, lines, rawLine, rawLineEnd, numLines);
731,007✔
1814
    request->code = code;
731,628✔
1815
    info->cost.endTime = taosGetTimestampUs();
1,262,631✔
1816
    info->cost.code = code;
731,538✔
1817
    if (NEED_CLIENT_HANDLE_ERROR(code) || code == TSDB_CODE_SDB_OBJ_CREATING || code == TSDB_CODE_PAR_VALUE_TOO_LONG ||
731,538✔
1818
        code == TSDB_CODE_MND_TRANS_CONFLICT || code == TSDB_CODE_SYN_NOT_LEADER) {
730,693✔
1819
      if (cnt++ >= 10) {
935✔
1820
        uInfo("SML:%" PRIx64 " retry:%d/10 end code:%d, msg:%s", info->id, cnt, code, tstrerror(code));
×
1821
        break;
199,690✔
1822
      }
1823
      taosMsleep(100);
935✔
1824
      uInfo("SML:%" PRIx64 " retry:%d/10,ver is old retry or object is creating code:%d, msg:%s", info->id, cnt, code,
935✔
1825
            tstrerror(code));
1826
      code = refreshMeta(request->pTscObj, request);
935✔
1827
      if (code != 0) {
935✔
1828
        uInfo("SML:%" PRIx64 " refresh meta error code:%d, msg:%s", info->id, code, tstrerror(code));
935✔
1829
      }
1830
      smlDestroyInfo(info);
935✔
1831
      info = NULL;
935✔
1832
      taos_free_result(request);
935✔
1833
      request = NULL;
935✔
1834
      continue;
935✔
1835
    }
1836
    smlPrintStatisticInfo(info);
730,603✔
1837
    break;
730,693✔
1838
  }
1839

1840
END:
730,693✔
1841
  smlDestroyInfo(info);
730,693✔
1842
  return (TAOS_RES *)request;
730,693✔
1843
}
1844

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

1864
TAOS_RES *taos_schemaless_insert_ttl_with_reqid_tbname_key(TAOS *taos, char *lines[], int numLines, int protocol,
723,645✔
1865
                                                           int precision, int32_t ttl, int64_t reqid, char *tbnameKey) {
1866
  if (taos == NULL || lines == NULL || numLines < 0) {
723,645✔
1867
    terrno = TSDB_CODE_INVALID_PARA;
×
1868
    return NULL;
×
1869
  }
1870
  for (int i = 0; i < numLines; i++) {
28,975,161✔
1871
    if (lines[i] == NULL) {
28,233,409✔
1872
      terrno = TSDB_CODE_INVALID_PARA;
×
1873
      return NULL;
×
1874
    }
1875
  }
1876
  return taos_schemaless_insert_inner(taos, lines, NULL, NULL, numLines, protocol, precision, ttl, reqid, tbnameKey);
741,752✔
1877
}
1878

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

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

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

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

1899
static int32_t getRawLineLen(char *lines, int len, int protocol) {
6,841✔
1900
  int   numLines = 0;
6,841✔
1901
  char *tmp = lines;
6,841✔
1902
  for (int i = 0; i < len; i++) {
27,517,584✔
1903
    if (lines[i] == '\n' || i == len - 1) {
27,510,743✔
1904
      if (!IS_COMMENT(protocol, tmp[0])) {  // ignore comment
22,088✔
1905
        numLines++;
21,769✔
1906
      }
1907
      tmp = lines + i + 1;
22,088✔
1908
    }
1909
  }
1910
  return numLines;
6,841✔
1911
}
1912

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

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

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

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