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

taosdata / TDengine / #3630

06 Mar 2025 11:35AM UTC coverage: 63.629% (-0.06%) from 63.692%
#3630

push

travis-ci

web-flow
Merge pull request #30042 from taosdata/doc/internal

docs: format

149060 of 300532 branches covered (49.6%)

Branch coverage included in aggregate %.

233739 of 301077 relevant lines covered (77.63%)

17473135.72 hits per line

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

72.07
/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
  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
  errno = 0;                                                                                    \
67
  uint64_t tmp = taosStr2UInt64(pVal, &endptr, 10);                                             \
68
  if (errno == ERANGE || result < 0) {                                                          \
69
    smlBuildInvalidDataMsg(msg, "unsigned big int out of range[0,18446744073709551615]", pVal); \
70
    return false;                                                                               \
71
  }                                                                                             \
72
  kvVal->type = TSDB_DATA_TYPE_UBIGINT;                                                         \
73
  kvVal->u = tmp;
74

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

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

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

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

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

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

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

126
  int32_t      code = TSDB_CODE_SUCCESS;
2,510✔
127
  SUserAuthRes authRes = {0};
2,510✔
128

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

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

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

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

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

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

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

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

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

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

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

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

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

220
  int   measureLen = currElement->measureLen;
1,203✔
221
  char *measure = (char *)taosMemoryMalloc(measureLen);
1,203!
222
  SML_CHECK_NULL(measure);
1,208!
223
  (void)memcpy(measure, currElement->measure, measureLen);
1,208✔
224
  if (currElement->measureEscaped) {
1,208✔
225
    PROCESS_SLASH_IN_MEASUREMENT(measure, measureLen);
108!
226
  }
227
  smlStrReplace(measure, measureLen);
1,208✔
228
  code = smlGetMeta(info, measure, measureLen, &pTableMeta);
1,205✔
229
  taosMemoryFree(measure);
1,211!
230
  if (code != TSDB_CODE_SUCCESS) {
1,210✔
231
    info->dataFormat = false;
459✔
232
    info->reRun = true;
459✔
233
    goto END;
459✔
234
  }
235
  SML_CHECK_CODE(smlBuildSTableMeta(info->dataFormat, sMeta));
751!
236
  for (int i = 0; i < pTableMeta->tableInfo.numOfTags + pTableMeta->tableInfo.numOfColumns; i++) {
8,637✔
237
    SSchema *col = pTableMeta->schema + i;
7,885✔
238
    SSmlKv   kv = {.key = col->name, .keyLen = strlen(col->name), .type = col->type};
7,885✔
239
    if (col->type == TSDB_DATA_TYPE_NCHAR) {
7,885✔
240
      kv.length = (col->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
2,359✔
241
    } else if (col->type == TSDB_DATA_TYPE_BINARY || col->type == TSDB_DATA_TYPE_GEOMETRY ||
5,526✔
242
               col->type == TSDB_DATA_TYPE_VARBINARY) {
4,506✔
243
      kv.length = col->bytes - VARSTR_HEADER_SIZE;
1,044✔
244
    } else {
245
      kv.length = col->bytes;
4,482✔
246
    }
247

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

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

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

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

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

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

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

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

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

344
  return false;
1,066✔
345
}
346

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

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

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

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

384
    tinfo->tags = taosArrayDup(info->preLineTagKV, NULL);
2,202✔
385
    SML_CHECK_NULL(tinfo->tags);
2,202✔
386
    for (size_t i = 0; i < taosArrayGetSize(info->preLineTagKV); i++) {
17,480✔
387
      SSmlKv *kv = (SSmlKv *)taosArrayGet(info->preLineTagKV, i);
15,277✔
388
      SML_CHECK_NULL(kv);
15,277!
389
      if (kv->keyEscaped) kv->key = NULL;
15,279✔
390
      if (kv->valueEscaped) kv->value = NULL;
15,279✔
391
    }
392

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

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

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

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

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

434
END:
1,805✔
435
  RETURN
1,805!
436
}
437

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

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

458
  return TSDB_CODE_SUCCESS;
97,081✔
459
}
460

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

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

512
END:
1,184✔
513
  RETURN
1,254!
514
}
515

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

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

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

541
END:
70✔
542
  taosArrayDestroy(dst);
2,202✔
543
  RETURN
2,201!
544
}
545

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

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

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

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

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

592
int32_t smlParseNumber(SSmlKv *kvVal, SSmlMsgBuf *msg) {
30,230,650✔
593
  const char *pVal = kvVal->value;
30,230,650✔
594
  int32_t     len = kvVal->length;
30,230,650✔
595
  char       *endptr = NULL;
30,230,650✔
596
  double      result = taosStr2Double(pVal, &endptr);
30,230,650✔
597
  if (pVal == endptr) {
30,292,694✔
598
    RETURN_FALSE
3,268✔
599
  }
600

601
  int32_t left = len - (endptr - pVal);
30,289,426✔
602
  if (left == 0) {
30,289,426✔
603
    SET_DOUBLE
1,658✔
604
  } else if (left == 3) {
30,287,768✔
605
    if (endptr[0] == 'f' || endptr[0] == 'F') {
20,286,055!
606
      if (endptr[1] == '6' && endptr[2] == '4') {
10,181,133!
607
        SET_DOUBLE
10,001,339✔
608
      } else if (endptr[1] == '3' && endptr[2] == '2') {
179,794!
609
        SET_FLOAT
189,203!
610
      } else {
611
        RETURN_FALSE
1✔
612
      }
613
    } else if (endptr[0] == 'i' || endptr[0] == 'I') {
10,104,922!
614
      if (endptr[1] == '6' && endptr[2] == '4') {
102,327!
615
        SET_BIGINT
1,370✔
616
      } else if (endptr[1] == '3' && endptr[2] == '2') {
100,957!
617
        SET_INT
99,778!
618
      } else if (endptr[1] == '1' && endptr[2] == '6') {
1,179!
619
        SET_SMALL_INT
1,300!
620
      } else {
621
        RETURN_FALSE
1✔
622
      }
623
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
10,002,595!
624
      if (endptr[1] == '6' && endptr[2] == '4') {
10,002,594!
625
        SET_UBIGINT
1,300✔
626
      } else if (endptr[1] == '3' && endptr[2] == '2') {
10,001,294!
627
        SET_UINT
10,000,647✔
628
      } else if (endptr[1] == '1' && endptr[2] == '6') {
647!
629
        SET_USMALL_INT
647✔
630
      } else {
631
        RETURN_FALSE
×
632
      }
633
    } else {
634
      RETURN_FALSE
1✔
635
    }
636
  } else if (left == 2) {
10,001,713✔
637
    if (endptr[0] == 'i' || endptr[0] == 'I') {
10,001,993!
638
      if (endptr[1] == '8') {
1,341!
639
        SET_TINYINT
1,341!
640
      } else {
641
        RETURN_FALSE
×
642
      }
643
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
10,000,652!
644
      if (endptr[1] == '8') {
10,000,647!
645
        SET_UTINYINT
10,000,647✔
646
      } else {
647
        RETURN_FALSE
×
648
      }
649
    } else {
650
      RETURN_FALSE
5✔
651
    }
652
  } else if (left == 1) {
21✔
653
    if (endptr[0] == 'i' || endptr[0] == 'I') {
33!
654
      SET_BIGINT
32✔
655
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
1!
656
      SET_UBIGINT
×
657
    } else {
658
      RETURN_FALSE
1✔
659
    }
660
  } else {
661
    RETURN_FALSE;
18✔
662
  }
663
  return true;
30,299,706✔
664
}
665

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

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

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

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

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

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

692
  return id;
1,216✔
693
}
694

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

706
    if (((colField[*index].type == TSDB_DATA_TYPE_VARCHAR || colField[*index].type == TSDB_DATA_TYPE_VARBINARY ||
3,837!
707
          colField[*index].type == TSDB_DATA_TYPE_GEOMETRY) &&
3,479✔
708
         (colField[*index].bytes - VARSTR_HEADER_SIZE) < kv->length) ||
364✔
709
        (colField[*index].type == TSDB_DATA_TYPE_NCHAR &&
3,825✔
710
         ((colField[*index].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE < kv->length))) {
1,870✔
711
      if (isTag) {
94✔
712
        *action = SCHEMA_ACTION_CHANGE_TAG_SIZE;
82✔
713
      } else {
714
        *action = SCHEMA_ACTION_CHANGE_COLUMN_SIZE;
12✔
715
      }
716
    }
717
  } else {
718
    if (isTag) {
4,414✔
719
      *action = SCHEMA_ACTION_ADD_TAG;
2,397✔
720
    } else {
721
      *action = SCHEMA_ACTION_ADD_COLUMN;
2,017✔
722
    }
723
  }
724
  return TSDB_CODE_SUCCESS;
8,251✔
725
}
726

727
#define BOUNDARY 1024
728
static int32_t smlFindNearestPowerOf2(int32_t length, uint8_t type) {
1,893✔
729
  int32_t result = 1;
1,893✔
730
  if (length >= BOUNDARY) {
1,893✔
731
    result = length;
18✔
732
  } else {
733
    while (result <= length) {
8,805✔
734
      result <<= 1;
6,930✔
735
    }
736
  }
737

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

745
  if (type == TSDB_DATA_TYPE_NCHAR) {
1,893✔
746
    result = result * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
1,367✔
747
  } else if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_GEOMETRY) {
526!
748
    result = result + VARSTR_HEADER_SIZE;
527✔
749
  }
750
  return result;
1,893✔
751
}
752

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

767
END:
636✔
768
  RETURN
660!
769
}
770

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

793
END:
629✔
794
  taosHashCleanup(hashTmp);
629✔
795
  RETURN
630!
796
}
797

798
static int32_t getBytes(uint8_t type, int32_t length) {
4,367✔
799
  if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_NCHAR ||
4,367✔
800
      type == TSDB_DATA_TYPE_GEOMETRY) {
801
    return smlFindNearestPowerOf2(length, type);
1,888✔
802
  } else {
803
    return tDataTypes[type].bytes;
2,479✔
804
  }
805
}
806

807
static int32_t smlBuildFieldsList(SSmlHandle *info, SSchema *schemaField, SHashObj *schemaHash, SArray *cols,
970✔
808
                                  SArray *results, int32_t numOfCols, bool isTag) {
809
  int32_t code = TSDB_CODE_SUCCESS;
970✔
810
  int32_t lino = 0;
970✔
811
  for (int j = 0; j < taosArrayGetSize(cols); ++j) {
5,862✔
812
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, j);
4,889✔
813
    SML_CHECK_NULL(kv);
4,889!
814
    ESchemaAction action = SCHEMA_ACTION_NULL;
4,889✔
815
    SML_CHECK_CODE(smlGenerateSchemaAction(schemaField, schemaHash, kv, isTag, &action, info));
4,889!
816
    if (action == SCHEMA_ACTION_ADD_COLUMN || action == SCHEMA_ACTION_ADD_TAG) {
9,215✔
817
      SField field = {0};
4,321✔
818
      field.type = kv->type;
4,321✔
819
      field.bytes = getBytes(kv->type, kv->length);
4,321✔
820
      (void)memcpy(field.name, kv->key, TMIN(kv->keyLen, sizeof(field.name) - 1));
4,322✔
821
      SML_CHECK_NULL(taosArrayPush(results, &field));
4,329!
822
    } else if (action == SCHEMA_ACTION_CHANGE_COLUMN_SIZE || action == SCHEMA_ACTION_CHANGE_TAG_SIZE) {
565✔
823
      uint16_t *index = (uint16_t *)taosHashGet(schemaHash, kv->key, kv->keyLen);
49✔
824
      if (index == NULL) {
47!
825
        SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
826
      }
827
      uint16_t newIndex = *index;
47✔
828
      if (isTag) newIndex -= numOfCols;
47✔
829
      SField *field = (SField *)taosArrayGet(results, newIndex);
47✔
830
      SML_CHECK_NULL(field);
47!
831
      field->bytes = getBytes(kv->type, kv->length);
47✔
832
    }
833
  }
834

835
  int32_t maxLen = isTag ? TSDB_MAX_TAGS_LEN : TSDB_MAX_BYTES_PER_ROW;
971✔
836
  int32_t len = 0;
971✔
837
  for (int j = 0; j < taosArrayGetSize(results); ++j) {
5,883✔
838
    SField *field = taosArrayGet(results, j);
4,909✔
839
    SML_CHECK_NULL(field);
4,911!
840
    len += field->bytes;
4,912✔
841
  }
842
  if (len > maxLen) {
972✔
843
    return isTag ? TSDB_CODE_PAR_INVALID_TAGS_LENGTH : TSDB_CODE_PAR_INVALID_ROW_LENGTH;
6!
844
  }
845

846
END:
966✔
847
  RETURN
966!
848
}
849

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

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

871
  pReq.pColumns = taosArrayInit(pReq.numOfColumns, sizeof(SFieldWithOptions));
521✔
872
  SML_CHECK_NULL(pReq.pColumns);
520!
873
  for (int32_t i = 0; i < pReq.numOfColumns; ++i) {
2,906✔
874
    SField *pField = taosArrayGet(pColumns, i);
2,386✔
875
    SML_CHECK_NULL(pField);
2,386!
876
    SFieldWithOptions fieldWithOption = {0};
2,386✔
877
    setFieldWithOptions(&fieldWithOption, pField);
2,386✔
878
    setDefaultOptionsForField(&fieldWithOption);
2,388✔
879
    SML_CHECK_NULL(taosArrayPush(pReq.pColumns, &fieldWithOption));
4,762!
880
  }
881

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

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

897
  pRequest->syncQuery = true;
521✔
898
  if (!pRequest->pDb) {
521!
899
    SML_CHECK_CODE(TSDB_CODE_PAR_DB_NOT_SPECIFIED);
×
900
  }
901

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

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

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

931
  SQuery pQuery = {0};
520✔
932
  pQuery.execMode = QUERY_EXEC_MODE_RPC;
520✔
933
  pQuery.pCmdMsg = &pCmdMsg;
520✔
934
  pQuery.msgType = pQuery.pCmdMsg->msgType;
520✔
935
  pQuery.stableQuery = true;
520✔
936

937
  launchQueryImpl(pRequest, &pQuery, true, NULL);  // no need to check return value
520✔
938

939
  if (pRequest->code == TSDB_CODE_SUCCESS) {
521✔
940
    SML_CHECK_CODE(catalogRemoveTableMeta(info->pCatalog, pName));
386!
941
  }
942
  code = pRequest->code;
521✔
943

944
END:
521✔
945
  destroyRequest(pRequest);
521✔
946
  tFreeSMCreateStbReq(&pReq);
521✔
947
  RETURN
521!
948
}
949

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

968
  SML_CHECK_CODE(catalogGetSTableMeta(info->pCatalog, conn, pName, pTableMeta));
304!
969

970
END:
304✔
971
  taosArrayDestroy(pColumns);
451✔
972
  taosArrayDestroy(pTags);
451✔
973
  RETURN
451!
974
}
975

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

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

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

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

1022
END:
321✔
1023
  taosArrayDestroy(pColumns);
339✔
1024
  taosArrayDestroy(pTags);
339✔
1025
  RETURN
339!
1026
}
1027

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

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

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

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

1053
END:
315✔
1054
  taosArrayDestroy(pColumns);
321✔
1055
  taosArrayDestroy(pTags);
321✔
1056
  RETURN
321!
1057
}
1058

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

1066
END:
678✔
1067
  return code;
678✔
1068
}
1069

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

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

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

1091
  SSmlSTableMeta **tmp = (SSmlSTableMeta **)taosHashIterate(info->superTables, NULL);
736✔
1092
  while (tmp) {
1,355✔
1093
    SSmlSTableMeta *sTableData = *tmp;
790✔
1094
    bool            needCheckMeta = false;  // for multi thread
790✔
1095

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

1110
    code = catalogGetSTableMeta(info->pCatalog, &conn, &pName, &pTableMeta);
790✔
1111

1112
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_MND_STB_NOT_EXIST) {
790!
1113
      SML_CHECK_CODE(smlCreateTable(info, &conn, sTableData, &pName, &pTableMeta));
451✔
1114
    } else if (code == TSDB_CODE_SUCCESS) {
339!
1115
      if (smlIsPKTable(pTableMeta)) {
339!
1116
        SML_CHECK_CODE(TSDB_CODE_SML_NOT_SUPPORT_PK);
×
1117
      }
1118

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

1126
      SML_CHECK_CODE(smlModifyTag(info, colHashTmp, tagHashTmp, &conn, sTableData, &pName, &pTableMeta));
339✔
1127
      SML_CHECK_CODE(smlModifyCols(info, tagHashTmp, colHashTmp, &conn, sTableData, &pName, &pTableMeta));
321✔
1128

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

1139
    if (needCheckMeta) {
619✔
1140
      SML_CHECK_CODE(smlCheckMeta(&(pTableMeta->schema[pTableMeta->tableInfo.numOfColumns]), pTableMeta->tableInfo.numOfTags, sTableData->tags));
315!
1141
      SML_CHECK_CODE(smlCheckMeta(&(pTableMeta->schema[0]), pTableMeta->tableInfo.numOfColumns, sTableData->cols));
315!
1142
    }
1143

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

1153
  return TSDB_CODE_SUCCESS;
565✔
1154

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

1164
  return code;
171✔
1165
}
1166

1167
static int32_t smlInsertMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, SHashObj *checkDuplicate) {
1,324✔
1168
  int32_t code = 0;
1,324✔
1169
  int32_t lino = 0;
1,324✔
1170
  terrno = 0;
1,324✔
1171
  for (int16_t i = 0; i < taosArrayGetSize(cols); ++i) {
7,597✔
1172
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
6,296✔
1173
    SML_CHECK_NULL(kv);
6,314!
1174
    int ret = taosHashPut(metaHash, kv->key, kv->keyLen, &i, SHORT_BYTES);
6,296✔
1175
    if (ret == 0) {
6,297✔
1176
      SML_CHECK_NULL(taosArrayPush(metaArray, kv));
6,291!
1177
      if (taosHashGet(checkDuplicate, kv->key, kv->keyLen) != NULL) {
6,291✔
1178
        SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
18!
1179
      }
1180
    } else if (terrno == TSDB_CODE_DUP_KEY) {
6!
1181
      return TSDB_CODE_PAR_DUPLICATED_COLUMN;
6✔
1182
    }
1183
  }
1184

1185
END:
1,318✔
1186
  RETURN
1,318!
1187
}
1188

1189
static int32_t smlUpdateMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, bool isTag, SSmlMsgBuf *msg,
180,070✔
1190
                             SHashObj *checkDuplicate) {
1191
  int32_t code = 0;
180,070✔
1192
  int32_t lino = 0;
180,070✔
1193
  for (int i = 0; i < taosArrayGetSize(cols); ++i) {
721,091✔
1194
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
469,314✔
1195
    SML_CHECK_NULL(kv);
455,844!
1196
    int16_t *index = (int16_t *)taosHashGet(metaHash, kv->key, kv->keyLen);
455,844✔
1197
    if (index) {
506,563!
1198
      SSmlKv *value = (SSmlKv *)taosArrayGet(metaArray, *index);
506,619✔
1199
      SML_CHECK_NULL(value);
495,484!
1200

1201
      if (isTag) {
540,936✔
1202
        if (kv->length > value->length) {
203,568✔
1203
          value->length = kv->length;
79✔
1204
        }
1205
        continue;
203,568✔
1206
      }
1207
      if (kv->type != value->type) {
337,368!
1208
        smlBuildInvalidDataMsg(msg, "the type is not the same like before", kv->key);
×
1209
        SML_CHECK_CODE(TSDB_CODE_SML_NOT_SAME_TYPE);
×
1210
      }
1211

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

1230
END:
130,912✔
1231
  RETURN
130,912!
1232
}
1233

1234
void smlDestroyTableInfo(void *para) {
2,202✔
1235
  SSmlTableInfo *tag = *(SSmlTableInfo **)para;
2,202✔
1236
  for (size_t i = 0; i < taosArrayGetSize(tag->cols); i++) {
103,640✔
1237
    SHashObj *kvHash = (SHashObj *)taosArrayGetP(tag->cols, i);
101,001✔
1238
    taosHashCleanup(kvHash);
99,828✔
1239
  }
1240

1241
  taosArrayDestroy(tag->cols);
2,238✔
1242
  taosArrayDestroyEx(tag->tags, freeSSmlKv);
2,202✔
1243
  taosMemoryFree(tag);
2,202!
1244
}
2,202✔
1245

1246
void freeSSmlKv(void *data) {
385,493✔
1247
  SSmlKv *kv = (SSmlKv *)data;
385,493✔
1248
  if (kv->keyEscaped) taosMemoryFreeClear(kv->key);
385,493!
1249
  if (kv->valueEscaped) taosMemoryFreeClear(kv->value);
385,493!
1250
  if (kv->type == TSDB_DATA_TYPE_GEOMETRY) geosFreeBuffer((void *)(kv->value));
385,493✔
1251
  if (kv->type == TSDB_DATA_TYPE_VARBINARY) taosMemoryFreeClear(kv->value);
385,493!
1252
}
385,493✔
1253

1254
void smlDestroyInfo(SSmlHandle *info) {
2,432✔
1255
  if (info == NULL) return;
2,432✔
1256

1257
  taosHashCleanup(info->pVgHash);
1,216✔
1258
  taosHashCleanup(info->childTables);
1,216✔
1259
  taosHashCleanup(info->superTables);
1,216✔
1260
  taosHashCleanup(info->tableUids);
1,216✔
1261

1262
  for (int i = 0; i < taosArrayGetSize(info->tagJsonArray); i++) {
1,216!
1263
    cJSON *tags = (cJSON *)taosArrayGetP(info->tagJsonArray, i);
×
1264
    cJSON_Delete(tags);
×
1265
  }
1266
  taosArrayDestroy(info->tagJsonArray);
1,216✔
1267

1268
  for (int i = 0; i < taosArrayGetSize(info->valueJsonArray); i++) {
1,216!
1269
    cJSON *value = (cJSON *)taosArrayGetP(info->valueJsonArray, i);
×
1270
    cJSON_Delete(value);
×
1271
  }
1272
  taosArrayDestroy(info->valueJsonArray);
1,216✔
1273

1274
  taosArrayDestroyEx(info->preLineTagKV, freeSSmlKv);
1,216✔
1275
  taosArrayDestroyP(info->escapedStringList, NULL);
1,216✔
1276

1277
  if (!info->dataFormat) {
1,216✔
1278
    for (int i = 0; i < info->lineNum; i++) {
102,140✔
1279
      taosArrayDestroyEx(info->lines[i].colArray, freeSSmlKv);
101,491✔
1280
      if (info->lines[i].measureTagsLen != 0 && info->protocol != TSDB_SML_LINE_PROTOCOL) {
101,508✔
1281
        taosMemoryFree(info->lines[i].measureTag);
300!
1282
      }
1283
    }
1284
    taosMemoryFree(info->lines);
649!
1285
  }
1286
  if(info->protocol == TSDB_SML_JSON_PROTOCOL)  {
1,216✔
1287
    taosMemoryFreeClear(info->preLine.tags);
542!
1288
  }
1289
  cJSON_Delete(info->root);
1,216✔
1290
  taosMemoryFreeClear(info);
1,215!
1291
}
1292

1293
int32_t smlBuildSmlInfo(TAOS *taos, SSmlHandle **handle) {
1,212✔
1294
  int32_t     code = TSDB_CODE_SUCCESS;
1,212✔
1295
  int32_t     lino = 0;
1,212✔
1296
  SSmlHandle *info = (SSmlHandle *)taosMemoryCalloc(1, sizeof(SSmlHandle));
1,212!
1297
  SML_CHECK_NULL(info);
1,216!
1298
  if (taos != NULL){
1,216✔
1299
    info->taos = acquireTscObj(*(int64_t *)taos);
1,211✔
1300
    SML_CHECK_NULL(info->taos);
1,211!
1301
    SML_CHECK_CODE(catalogGetHandle(info->taos->pAppInfo->clusterId, &info->pCatalog));
1,211!
1302
  }
1303

1304
  info->pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
1,216✔
1305
  SML_CHECK_NULL(info->pVgHash);
1,214!
1306
  info->childTables = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,214✔
1307
  SML_CHECK_NULL(info->childTables);
1,215!
1308
  info->tableUids = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,215✔
1309
  SML_CHECK_NULL(info->tableUids);
1,216!
1310
  info->superTables = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,216✔
1311
  SML_CHECK_NULL(info->superTables);
1,215!
1312
  taosHashSetFreeFp(info->superTables, smlDestroySTableMeta);
1,215✔
1313
  taosHashSetFreeFp(info->childTables, smlDestroyTableInfo);
1,214✔
1314

1315
  info->id = smlGenId();
1,214✔
1316
  SML_CHECK_CODE(smlInitHandle(&info->pQuery));
1,216!
1317
  info->dataFormat = true;
1,215✔
1318
  info->tagJsonArray = taosArrayInit(8, POINTER_BYTES);
1,215✔
1319
  SML_CHECK_NULL(info->tagJsonArray);
1,216!
1320
  info->valueJsonArray = taosArrayInit(8, POINTER_BYTES);
1,216✔
1321
  SML_CHECK_NULL(info->valueJsonArray);
1,216!
1322
  info->preLineTagKV = taosArrayInit(8, sizeof(SSmlKv));
1,216✔
1323
  SML_CHECK_NULL(info->preLineTagKV);
1,216!
1324
  info->escapedStringList = taosArrayInit(8, POINTER_BYTES);
1,216✔
1325
  SML_CHECK_NULL(info->escapedStringList);
1,216✔
1326

1327
  *handle = info;
1,215✔
1328
  info = NULL;
1,215✔
1329

1330
END:
1,215✔
1331
  smlDestroyInfo(info);
1,215✔
1332
  RETURN
1,216!
1333
}
1334

1335
static int32_t smlPushCols(SArray *colsArray, SArray *cols) {
96,072✔
1336
  int32_t code = TSDB_CODE_SUCCESS;
96,072✔
1337
  int32_t lino = 0;
96,072✔
1338
  SHashObj *kvHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
96,072✔
1339
  SML_CHECK_NULL(kvHash);
99,100!
1340
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
467,186✔
1341
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
349,901✔
1342
    SML_CHECK_NULL(kv);
344,406!
1343
    terrno = 0;
344,394✔
1344
    code = taosHashPut(kvHash, kv->key, kv->keyLen, &kv, POINTER_BYTES);
332,663✔
1345
    if (terrno == TSDB_CODE_DUP_KEY) {
385,353✔
1346
      SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
12!
1347
    }
1348
    SML_CHECK_CODE(code);
368,086!
1349
  }
1350

1351
  SML_CHECK_NULL(taosArrayPush(colsArray, &kvHash));
97,454!
1352
  return code;
97,454✔
1353
END:
12✔
1354
  taosHashCleanup(kvHash);
12✔
1355
  RETURN
12!
1356
}
1357

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

1365
  for (int32_t i = 0; i < info->lineNum; i++) {
97,986✔
1366
    SSmlLineInfo  *elements = info->lines + i;
97,317✔
1367
    SSmlTableInfo *tinfo = NULL;
97,317✔
1368
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
97,317✔
1369
      SSmlTableInfo **tmp =
1370
          (SSmlTableInfo **)taosHashGet(info->childTables, elements->measure, elements->measureTagsLen);
95,510✔
1371
      if (tmp) tinfo = *tmp;
97,845!
1372
    } else {
1373
      SSmlTableInfo **tmp = (SSmlTableInfo **)taosHashGet(info->childTables, elements->measureTag,
1,807✔
1374
                                                          elements->measureLen + elements->tagsLen);
1,807✔
1375
      if (tmp) tinfo = *tmp;
1,809!
1376
    }
1377

1378
    if (tinfo == NULL) {
99,654!
1379
      uError("SML:0x%" PRIx64 ", get oneTable failed, line num:%d", info->id, i);
×
1380
      smlBuildInvalidDataMsg(&info->msgBuf, "get oneTable failed", elements->measure);
×
1381
      return TSDB_CODE_SML_INVALID_DATA;
×
1382
    }
1383

1384
    if (taosArrayGetSize(tinfo->tags) > TSDB_MAX_TAGS) {
99,654!
1385
      smlBuildInvalidDataMsg(&info->msgBuf, "too many tags than 128", NULL);
×
1386
      return TSDB_CODE_PAR_INVALID_TAGS_NUM;
×
1387
    }
1388

1389
    if (taosArrayGetSize(elements->colArray) + taosArrayGetSize(tinfo->tags) > TSDB_MAX_COLUMNS) {
98,794!
1390
      smlBuildInvalidDataMsg(&info->msgBuf, "too many columns than 4096", NULL);
×
1391
      return TSDB_CODE_PAR_TOO_MANY_COLUMNS;
×
1392
    }
1393

1394
    SML_CHECK_CODE(smlPushCols(tinfo->cols, elements->colArray));
96,722✔
1395

1396
    SSmlSTableMeta **tableMeta =
1397
        (SSmlSTableMeta **)taosHashGet(info->superTables, elements->measure, elements->measureLen);
97,206✔
1398
    if (tableMeta) {  // update meta
96,922✔
1399
      uDebug("SML:0x%" PRIx64 ", %s update meta, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
96,263!
1400
             info->lineNum);
1401
      SML_CHECK_CODE(smlUpdateMeta((*tableMeta)->colHash, (*tableMeta)->cols, elements->colArray, false, &info->msgBuf,
96,263!
1402
                          (*tableMeta)->tagHash));
1403
      SML_CHECK_CODE(smlUpdateMeta((*tableMeta)->tagHash, (*tableMeta)->tags, tinfo->tags, true, &info->msgBuf,
95,534!
1404
                          (*tableMeta)->colHash));
1405
    } else {
1406
      uDebug("SML:0x%" PRIx64 ", %s add meta, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
659!
1407
             info->lineNum);
1408
      SSmlSTableMeta *meta = NULL;
659✔
1409
      SML_CHECK_CODE(smlBuildSTableMeta(info->dataFormat, &meta));
683!
1410
      code = taosHashPut(info->superTables, elements->measure, elements->measureLen, &meta, POINTER_BYTES);
665✔
1411
      if (code != TSDB_CODE_SUCCESS) {
665!
1412
        smlDestroySTableMeta(&meta);
×
1413
        SML_CHECK_CODE(code);
×
1414
      }
1415
      SML_CHECK_CODE(smlInsertMeta(meta->tagHash, meta->tags, tinfo->tags, NULL));
665✔
1416
      SML_CHECK_CODE(smlInsertMeta(meta->colHash, meta->cols, elements->colArray, meta->tagHash));
659✔
1417
    }
1418
  }
1419
  uDebug("SML:0x%" PRIx64 ", %s end, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat, info->lineNum);
669!
1420

1421
END:
669✔
1422
  RETURN
705!
1423
}
1424

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

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

1442
  oneTable = (SSmlTableInfo **)taosHashIterate(info->childTables, NULL);
1,090✔
1443
  while (oneTable) {
3,057✔
1444
    SSmlTableInfo *tableData = *oneTable;
1,978✔
1445

1446
    int   measureLen = tableData->sTableNameLen;
1,978✔
1447
    measure = (char *)taosMemoryMalloc(tableData->sTableNameLen);
1,978!
1448
    SML_CHECK_NULL(measure);
1,990!
1449
    (void)memcpy(measure, tableData->sTableName, tableData->sTableNameLen);
1,978✔
1450
    if (info->protocol == TSDB_SML_LINE_PROTOCOL){
1,978✔
1451
      PROCESS_SLASH_IN_MEASUREMENT(measure, measureLen);
7,852✔
1452
    }
1453
    smlStrReplace(measure, measureLen);
1,978✔
1454
    (void)memcpy(pName.tname, measure, measureLen);
1,978✔
1455
    pName.tname[measureLen] = '\0';
1,978✔
1456

1457
    if (info->pRequest->tableList == NULL) {
1,978✔
1458
      info->pRequest->tableList = taosArrayInit(1, sizeof(SName));
1,091✔
1459
      SML_CHECK_NULL(info->pRequest->tableList);
1,090!
1460
    }
1461
    SML_CHECK_NULL(taosArrayPush(info->pRequest->tableList, &pName));
3,954!
1462
    tstrncpy(pName.tname, tableData->childTableName, sizeof(pName.tname));
1,977✔
1463

1464
    SRequestConnInfo conn = {0};
1,977✔
1465
    conn.pTrans = info->taos->pAppInfo->pTransporter;
1,977✔
1466
    conn.requestId = info->pRequest->requestId;
1,977✔
1467
    conn.requestObjRefId = info->pRequest->self;
1,977✔
1468
    conn.mgmtEps = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
1,977✔
1469

1470
    SML_CHECK_CODE(smlCheckAuth(info, &conn, pName.tname, AUTH_TYPE_WRITE));
1,978✔
1471

1472
    SVgroupInfo vg = {0};
1,965✔
1473
    SML_CHECK_CODE(catalogGetTableHashVgroup(info->pCatalog, &conn, &pName, &vg));
1,965!
1474
    SML_CHECK_CODE(taosHashPut(info->pVgHash, (const char *)&vg.vgId, sizeof(vg.vgId), (char *)&vg, sizeof(vg)));
1,966!
1475

1476
    SSmlSTableMeta **pMeta =
1477
        (SSmlSTableMeta **)taosHashGet(info->superTables, tableData->sTableName, tableData->sTableNameLen);
1,966✔
1478
    if (unlikely(NULL == pMeta || NULL == *pMeta || NULL == (*pMeta)->tableMeta)) {
1,966!
1479
      uError("SML:0x%" PRIx64 ", %s NULL == pMeta. table name:%s", info->id, __FUNCTION__, tableData->childTableName);
×
1480
      SML_CHECK_CODE(TSDB_CODE_SML_INTERNAL_ERROR);
×
1481
    }
1482

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

1489
    SML_CHECK_CODE(smlBindData(info->pQuery, info->dataFormat, tableData->tags, (*pMeta)->cols, tableData->cols,
1,966!
1490
                       (*pMeta)->tableMeta, tableData->childTableName, measure, measureLen, info->ttl, info->msgBuf.buf,
1491
                       info->msgBuf.len, info->taos->optionInfo.charsetCxt));
1492
    taosMemoryFreeClear(measure);
1,964!
1493
    oneTable = (SSmlTableInfo **)taosHashIterate(info->childTables, oneTable);
1,963✔
1494
  }
1495

1496
  SML_CHECK_CODE(smlBuildOutput(info->pQuery, info->pVgHash));
1,079!
1497
  info->cost.insertRpcTime = taosGetTimestampUs();
1,079✔
1498

1499
  SAppClusterSummary *pActivity = &info->taos->pAppInfo->summary;
1,079✔
1500
  (void)atomic_add_fetch_64((int64_t *)&pActivity->numOfInsertsReq, 1);  // no need to check return code
1,079✔
1501

1502
  launchQueryImpl(info->pRequest, info->pQuery, true, NULL);  // no need to check return code
1,079✔
1503

1504
  uDebug("SML:0x%" PRIx64 ", %s end, format:%d, code:%d,%s", info->id, __FUNCTION__, info->dataFormat, info->pRequest->code,
1,079!
1505
         tstrerror(info->pRequest->code));
1506

1507
  return info->pRequest->code;
1,079✔
1508

1509
END:
12✔
1510
  taosMemoryFree(measure);
12!
1511
  taosHashCancelIterate(info->childTables, oneTable);
12✔
1512
  RETURN
12!
1513
}
1514

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

1527
int32_t smlClearForRerun(SSmlHandle *info) {
648✔
1528
  int32_t code = 0;
648✔
1529
  int32_t lino = 0;
648✔
1530
  info->reRun = false;
648✔
1531

1532
  taosHashClear(info->childTables);
648✔
1533
  taosHashClear(info->superTables);
648✔
1534
  taosHashClear(info->tableUids);
648✔
1535

1536
  if (!info->dataFormat) {
648!
1537
    info->lines = (SSmlLineInfo *)taosMemoryCalloc(info->lineNum, sizeof(SSmlLineInfo));
648!
1538
    SML_CHECK_NULL(info->lines);
648!
1539
  }
1540

1541
  taosArrayClearP(info->escapedStringList, NULL);
648✔
1542
  if(info->protocol == TSDB_SML_JSON_PROTOCOL)  {
648✔
1543
    taosMemoryFreeClear(info->preLine.tags);
170!
1544
  }
1545
  (void)memset(&info->preLine, 0, sizeof(SSmlLineInfo));
648✔
1546
  info->currSTableMeta = NULL;
648✔
1547
  info->currTableDataCtx = NULL;
648✔
1548

1549
  SVnodeModifyOpStmt *stmt = (SVnodeModifyOpStmt *)(info->pQuery->pRoot);
648✔
1550
  stmt->freeHashFunc(stmt->pTableBlockHashObj);
648✔
1551
  stmt->pTableBlockHashObj = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
648✔
1552
  SML_CHECK_NULL(stmt->pTableBlockHashObj);
648!
1553

1554
END:
648✔
1555
  RETURN
648!
1556
}
1557

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

1574
static bool getLine(SSmlHandle *info, char *lines[], char **rawLine, char *rawLineEnd, int numLines, int i, char **tmp,
99,144✔
1575
                    int *len) {
1576
  if (lines) {
99,144✔
1577
    *tmp = lines[i];
99,070✔
1578
    *len = strlen(*tmp);
99,070✔
1579
  } else if (*rawLine) {
74!
1580
    *tmp = *rawLine;
96✔
1581
    while (*rawLine < rawLineEnd) {
969,582✔
1582
      if (*((*rawLine)++) == '\n') {
969,522✔
1583
        break;
36✔
1584
      }
1585
      (*len)++;
969,486✔
1586
    }
1587
    if (IS_COMMENT(info->protocol,(*tmp)[0])) {  // this line is comment
96!
1588
      return false;
6✔
1589
    }
1590
  }
1591

1592
  if (*rawLine != NULL && (uDebugFlag & DEBUG_DEBUG)) {
99,138!
1593
    printRaw(info->id, i, numLines, DEBUG_DEBUG, *tmp, *len);
×
1594
  } else {
1595
    uDebug("SML:0x%" PRIx64 ", smlParseLine is not raw, line %d/%d :%s", info->id, i, numLines, *tmp);
99,138!
1596
  }
1597
  return true;
100,419✔
1598
}
1599

1600

1601
static int32_t smlParseJson(SSmlHandle *info, char *lines[], char *rawLine) {
541✔
1602
  int32_t code = TSDB_CODE_SUCCESS;
541✔
1603
  if (lines) {
541!
1604
    code = smlParseJSONExt(info, *lines);
541✔
1605
  } else if (rawLine) {
×
1606
    code = smlParseJSONExt(info, rawLine);
×
1607
  }
1608
  if (code != TSDB_CODE_SUCCESS) {
542✔
1609
    uError("%s failed code:%d", __FUNCTION__ , code);
6!
1610
  }
1611
  return code;
542✔
1612
}
1613

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

1621
  char   *oldRaw = rawLine;
664✔
1622
  int32_t i = 0;
664✔
1623
  while (i < numLines) {
99,768✔
1624
    char *tmp = NULL;
99,115✔
1625
    int   len = 0;
99,115✔
1626
    if (!getLine(info, lines, &rawLine, rawLineEnd, numLines, i, &tmp, &len)) {
99,115✔
1627
      continue;
483✔
1628
    }
1629
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
100,595✔
1630
      if (info->dataFormat) {
98,243✔
1631
        SSmlLineInfo element = {0};
413✔
1632
        code = smlParseInfluxString(info, tmp, tmp + len, &element);
413✔
1633
      } else {
1634
        code = smlParseInfluxString(info, tmp, tmp + len, info->lines + i);
97,830✔
1635
      }
1636
    } else if (info->protocol == TSDB_SML_TELNET_PROTOCOL) {
2,352!
1637
      if (info->dataFormat) {
2,372✔
1638
        SSmlLineInfo element = {0};
1,765✔
1639
        code = smlParseTelnetString(info, (char *)tmp, (char *)tmp + len, &element);
1,765✔
1640
        if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
1,767!
1641
      } else {
1642
        code = smlParseTelnetString(info, (char *)tmp, (char *)tmp + len, info->lines + i);
607✔
1643
      }
1644
    }
1645
    if (code != TSDB_CODE_SUCCESS) {
99,140✔
1646
      if (rawLine != NULL) {
42!
1647
        printRaw(info->id, i, numLines, DEBUG_ERROR, tmp, len);
×
1648
      } else {
1649
        uError("SML:0x%" PRIx64 ", %s failed. line %d :%s", info->id, __FUNCTION__, i, tmp);
42!
1650
      }
1651
      return code;
42✔
1652
    }
1653
    if (info->reRun) {
99,098✔
1654
      uDebug("SML:0x%" PRIx64 ", %s re run", info->id, __FUNCTION__);
477!
1655
      i = 0;
477✔
1656
      rawLine = oldRaw;
477✔
1657
      code = smlClearForRerun(info);
477✔
1658
      if (code != TSDB_CODE_SUCCESS) {
477!
1659
        return code;
×
1660
      }
1661
      continue;
477✔
1662
    }
1663
    i++;
98,621✔
1664
  }
1665
  uDebug("SML:0x%" PRIx64 ", %s end", info->id, __FUNCTION__);
653!
1666

1667
  return code;
626✔
1668
}
1669

1670
static int smlProcess(SSmlHandle *info, char *lines[], char *rawLine, char *rawLineEnd, int numLines) {
1,207✔
1671
  int32_t code = TSDB_CODE_SUCCESS;
1,207✔
1672
  int32_t lino = 0;
1,207✔
1673
  int32_t retryNum = 0;
1,207✔
1674

1675
  info->cost.parseTime = taosGetTimestampUs();
1,208✔
1676

1677
  SML_CHECK_CODE(smlParseStart(info, lines, rawLine, rawLineEnd, numLines));
1,208✔
1678
  SML_CHECK_CODE(smlParseEnd(info));
1,162✔
1679

1680
  info->cost.lineNum = info->lineNum;
1,126✔
1681
  info->cost.numOfSTables = taosHashGetSize(info->superTables);
1,126✔
1682
  info->cost.numOfCTables = taosHashGetSize(info->childTables);
1,127✔
1683
  info->cost.schemaTime = taosGetTimestampUs();
1,127✔
1684

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

1695
  SML_CHECK_CODE(code);
1,127✔
1696
  info->cost.insertBindTime = taosGetTimestampUs();
1,091✔
1697
  SML_CHECK_CODE(smlInsertData(info));
1,091✔
1698

1699
END:
1,079✔
1700
  RETURN
1,211!
1701
}
1702

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

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

1721
    if (NULL == p) {
×
1722
      return;
×
1723
    }
1724

1725
    rlen = TMIN(len, TSDB_MAX_ALLOWED_SQL_LEN);
×
1726
    rlen = TMAX(rlen, 0);
×
1727

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

1736
    request->sqlstr = sql;
×
1737
    request->sqlLen = rlen;
×
1738
  }
1739
}
1740

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

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

1764
    smlSetReqSQL(request, lines, rawLine, rawLineEnd);
1,211✔
1765

1766
    if (request->pDb == NULL) {
1,207!
1767
      request->code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1768
      smlBuildInvalidDataMsg(&msg, "Database not specified", NULL);
×
1769
      goto END;
×
1770
    }
1771

1772
    if (protocol < TSDB_SML_LINE_PROTOCOL || protocol > TSDB_SML_JSON_PROTOCOL) {
1,207!
1773
      request->code = TSDB_CODE_SML_INVALID_PROTOCOL_TYPE;
×
1774
      smlBuildInvalidDataMsg(&msg, "protocol invalidate", NULL);
×
1775
      goto END;
×
1776
    }
1777

1778
    if (protocol == TSDB_SML_LINE_PROTOCOL &&
1,210!
1779
        (precision < TSDB_SML_TIMESTAMP_NOT_CONFIGURED || precision > TSDB_SML_TIMESTAMP_NANO_SECONDS)) {
412!
1780
      request->code = TSDB_CODE_SML_INVALID_PRECISION_TYPE;
×
1781
      smlBuildInvalidDataMsg(&msg, "precision invalidate for line protocol", NULL);
×
1782
      goto END;
×
1783
    }
1784

1785
    if (protocol == TSDB_SML_JSON_PROTOCOL) {
1,211✔
1786
      numLines = 1;
542✔
1787
    } else if (numLines <= 0) {
669!
1788
      request->code = TSDB_CODE_SML_INVALID_DATA;
×
1789
      smlBuildInvalidDataMsg(&msg, "line num is invalid", NULL);
×
1790
      goto END;
×
1791
    }
1792

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

1820
END:
1,211✔
1821
  smlDestroyInfo(info);
1,211✔
1822
  return (TAOS_RES *)request;
1,209✔
1823
}
1824

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

1844
TAOS_RES *taos_schemaless_insert_ttl_with_reqid_tbname_key(TAOS *taos, char *lines[], int numLines, int protocol,
1,173✔
1845
                                                           int precision, int32_t ttl, int64_t reqid, char *tbnameKey) {
1846
  if (taos == NULL || lines == NULL || numLines < 0) {
1,173!
1847
    terrno = TSDB_CODE_INVALID_PARA;
×
1848
    return NULL;
×
1849
  }
1850
  for (int i = 0; i < numLines; i++){
41,710✔
1851
    if (lines[i] == NULL){
40,536!
1852
      terrno = TSDB_CODE_INVALID_PARA;
×
1853
      return NULL;
×
1854
    }
1855
  }
1856
  return taos_schemaless_insert_inner(taos, lines, NULL, NULL, numLines, protocol, precision, ttl, reqid, tbnameKey);
1,174✔
1857
}
1858

1859
TAOS_RES *taos_schemaless_insert_ttl_with_reqid(TAOS *taos, char *lines[], int numLines, int protocol, int precision,
1,173✔
1860
                                                int32_t ttl, int64_t reqid) {
1861
  return taos_schemaless_insert_ttl_with_reqid_tbname_key(taos, lines, numLines, protocol, precision, ttl, reqid, NULL);
1,173✔
1862
}
1863

1864
TAOS_RES *taos_schemaless_insert(TAOS *taos, char *lines[], int numLines, int protocol, int precision) {
1,149✔
1865
  return taos_schemaless_insert_ttl_with_reqid(taos, lines, numLines, protocol, precision, TSDB_DEFAULT_TABLE_TTL, 0);
1,149✔
1866
}
1867

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

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

1879
static int32_t getRawLineLen(char *lines, int len, int protocol) {
36✔
1880
  int numLines = 0;
36✔
1881
  char *tmp = lines;
36✔
1882
  for (int i = 0; i < len; i++) {
485,532✔
1883
    if (lines[i] == '\n' || i == len - 1) {
485,496✔
1884
      if (!IS_COMMENT(protocol, tmp[0])) {  // ignore comment
60!
1885
        numLines++;
54✔
1886
      }
1887
      tmp = lines + i + 1;
60✔
1888
    }
1889
  }
1890
  return numLines;
36✔
1891
}
1892

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

1908
TAOS_RES *taos_schemaless_insert_raw_ttl_with_reqid(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
36✔
1909
                                                    int precision, int32_t ttl, int64_t reqid) {
1910
  return taos_schemaless_insert_raw_ttl_with_reqid_tbname_key(taos, lines, len, totalRows, protocol, precision, ttl,
36✔
1911
                                                              reqid, NULL);
1912
}
1913

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

1924
TAOS_RES *taos_schemaless_insert_raw(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
36✔
1925
                                     int precision) {
1926
  return taos_schemaless_insert_raw_ttl_with_reqid(taos, lines, len, totalRows, protocol, precision,
36✔
1927
                                                   TSDB_DEFAULT_TABLE_TTL, 0);
1928
}
1929

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