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

taosdata / TDengine / #3533

20 Nov 2024 07:11AM UTC coverage: 58.848% (-1.9%) from 60.78%
#3533

push

travis-ci

web-flow
Merge pull request #28823 from taosdata/fix/3.0/TD-32587

fix:[TD-32587]fix stmt segmentation fault

115578 of 252434 branches covered (45.79%)

Branch coverage included in aggregate %.

1 of 4 new or added lines in 1 file covered. (25.0%)

8038 existing lines in 233 files now uncovered.

194926 of 275199 relevant lines covered (70.83%)

1494459.59 hits per line

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

73.26
/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) {
975✔
115
  SUserAuthInfo pAuth = {0};
975✔
116
  (void)snprintf(pAuth.user, sizeof(pAuth.user), "%s", info->taos->user);
975✔
117
  if (NULL == pTabName) {
975✔
118
    if (tNameSetDbName(&pAuth.tbName, info->taos->acctId, info->pRequest->pDb, strlen(info->pRequest->pDb)) != 0) {
190!
119
      return TSDB_CODE_SML_INVALID_DATA;
×
120
    }
121
  } else {
122
    toName(info->taos->acctId, info->pRequest->pDb, pTabName, &pAuth.tbName);
785✔
123
  }
124
  pAuth.type = type;
975✔
125

126
  int32_t      code = TSDB_CODE_SUCCESS;
975✔
127
  SUserAuthRes authRes = {0};
975✔
128

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

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

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

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

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

168
  return convertTimePrecision(tsInt64, fromPrecision, toPrecision);
1,106✔
169
}
170

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

177
  tag->sTableName = measure;
829✔
178
  tag->sTableNameLen = measureLen;
829✔
179

180
  tag->cols = taosArrayInit(numRows, POINTER_BYTES);
829✔
181
  SML_CHECK_NULL(tag->cols)
829!
182
  *tInfo = tag;
829✔
183
  return code;
829✔
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) {
2,604✔
192
  kv->key = tsSmlTsDefaultName;
2,604✔
193
  kv->keyLen = strlen(tsSmlTsDefaultName);
2,604✔
194
  kv->type = TSDB_DATA_TYPE_TIMESTAMP;
2,604✔
195
  kv->i = ts;
2,604✔
196
  kv->length = (size_t)tDataTypes[TSDB_DATA_TYPE_TIMESTAMP].bytes;
2,604✔
197
}
2,604✔
198

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

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

220
  int   measureLen = currElement->measureLen;
418✔
221
  char *measure = (char *)taosMemoryMalloc(measureLen);
418✔
222
  SML_CHECK_NULL(measure);
418!
223
  (void)memcpy(measure, currElement->measure, measureLen);
418✔
224
  if (currElement->measureEscaped) {
418✔
225
    PROCESS_SLASH_IN_MEASUREMENT(measure, measureLen);
54!
226
  }
227
  smlStrReplace(measure, measureLen);
418✔
228
  code = smlGetMeta(info, measure, measureLen, &pTableMeta);
418✔
229
  taosMemoryFree(measure);
418✔
230
  if (code != TSDB_CODE_SUCCESS) {
418✔
231
    info->dataFormat = false;
193✔
232
    info->reRun = true;
193✔
233
    goto END;
193✔
234
  }
235
  SML_CHECK_CODE(smlBuildSTableMeta(info->dataFormat, sMeta));
225!
236
  for (int i = 1; i < pTableMeta->tableInfo.numOfTags + pTableMeta->tableInfo.numOfColumns; i++) {
2,665✔
237
    SSchema *col = pTableMeta->schema + i;
2,440✔
238
    SSmlKv   kv = {.key = col->name, .keyLen = strlen(col->name), .type = col->type};
2,440✔
239
    if (col->type == TSDB_DATA_TYPE_NCHAR) {
2,440✔
240
      kv.length = (col->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
656✔
241
    } else if (col->type == TSDB_DATA_TYPE_BINARY || col->type == TSDB_DATA_TYPE_GEOMETRY ||
1,784!
242
               col->type == TSDB_DATA_TYPE_VARBINARY) {
1,428✔
243
      kv.length = col->bytes - VARSTR_HEADER_SIZE;
368✔
244
    } else {
245
      kv.length = col->bytes;
1,416✔
246
    }
247

248
    if (i < pTableMeta->tableInfo.numOfColumns) {
2,440✔
249
      SML_CHECK_NULL(taosArrayPush((*sMeta)->cols, &kv));
1,554!
250
    } else {
251
      SML_CHECK_NULL(taosArrayPush((*sMeta)->tags, &kv));
3,326!
252
    }
253
  }
254
  SML_CHECK_CODE(taosHashPut(info->superTables, currElement->measure, currElement->measureLen, sMeta, POINTER_BYTES));
225!
255
  (*sMeta)->tableMeta = pTableMeta;
225✔
256
  return code;
225✔
257

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

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

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

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

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

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

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

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

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

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

344
  return false;
296✔
345
}
346

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

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

369
  if (smlIsPKTable(sMeta->tableMeta)) {
225✔
370
    return TSDB_CODE_SML_NOT_SUPPORT_PK;
12✔
371
  }
372
  return 0;
213✔
373
}
374

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

384
    tinfo->tags = taosArrayDup(info->preLineTagKV, NULL);
829✔
385
    SML_CHECK_NULL(tinfo->tags);
829!
386
    for (size_t i = 0; i < taosArrayGetSize(info->preLineTagKV); i++) {
6,570✔
387
      SSmlKv *kv = (SSmlKv *)taosArrayGet(info->preLineTagKV, i);
5,741✔
388
      SML_CHECK_NULL(kv);
5,741!
389
      if (kv->keyEscaped) kv->key = NULL;
5,741✔
390
      if (kv->valueEscaped) kv->value = NULL;
5,741✔
391
    }
392

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

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

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

418
END:
1,117✔
419
  clearColValArraySml(info->currTableDataCtx->pValues);
1,117✔
420
  RETURN
1,116!
421
}
422

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

434
END:
412✔
435
  RETURN
412!
436
}
437

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

446
    clearColValArraySml(info->currTableDataCtx->pValues);
372✔
447
    taosArrayClearP(info->escapedStringList, taosMemoryFree);
371✔
448
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
371!
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);
704!
454
    taosArraySet(elements->colArray, 0, kvTs);
704✔
455
  }
456
  info->preLine = *elements;
1,075✔
457

458
  return TSDB_CODE_SUCCESS;
1,075✔
459
}
460

461
static int32_t smlParseTableName(SArray *tags, char *childTableName, char *tbnameKey) {
829✔
462
  int32_t code = 0;
829✔
463
  int32_t lino = 0;
829✔
464
  bool    autoChildName = false;
829✔
465
  size_t  delimiter = strlen(tsSmlAutoChildTableNameDelimiter);
829✔
466
  if (delimiter > 0 && tbnameKey == NULL) {
829!
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) {
829✔
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) {
825!
492
      tbnameKey = tsSmlChildTableName;
825✔
493
    }
494
    size_t childTableNameLen = strlen(tbnameKey);
825✔
495
    if (childTableNameLen <= 0) return TSDB_CODE_SUCCESS;
825✔
496

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

512
END:
557✔
513
  RETURN
594!
514
}
515

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

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

538
    SML_CHECK_CODE(buildChildTableName(&rName));
792!
539
  }
540

541
END:
37✔
542
  taosArrayDestroy(dst);
829✔
543
  RETURN
829!
544
}
545

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

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

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

581
  meta->cols = taosArrayInit(32, sizeof(SSmlKv));
475✔
582
  SML_CHECK_NULL(meta->cols);
475!
583
  *sMeta = meta;
475✔
584
  return TSDB_CODE_SUCCESS;
475✔
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) {
8,087✔
593
  const char *pVal = kvVal->value;
8,087✔
594
  int32_t     len = kvVal->length;
8,087✔
595
  char       *endptr = NULL;
8,087✔
596
  double      result = taosStr2Double(pVal, &endptr);
8,087✔
597
  if (pVal == endptr) {
8,105!
UNCOV
598
    RETURN_FALSE
×
599
  }
600

601
  int32_t left = len - (endptr - pVal);
8,105✔
602
  if (left == 0) {
8,105✔
603
    SET_DOUBLE
845✔
604
  } else if (left == 3) {
7,260✔
605
    if (endptr[0] == 'f' || endptr[0] == 'F') {
5,935!
606
      if (endptr[1] == '6' && endptr[2] == '4') {
1,627!
607
        SET_DOUBLE
818✔
608
      } else if (endptr[1] == '3' && endptr[2] == '2') {
809!
609
        SET_FLOAT
811!
610
      } else {
611
        RETURN_FALSE
×
612
      }
613
    } else if (endptr[0] == 'i' || endptr[0] == 'I') {
4,308!
614
      if (endptr[1] == '6' && endptr[2] == '4') {
2,543!
615
        SET_BIGINT
830!
616
      } else if (endptr[1] == '3' && endptr[2] == '2') {
1,713!
617
        SET_INT
906!
618
      } else if (endptr[1] == '1' && endptr[2] == '6') {
807!
619
        SET_SMALL_INT
809!
620
      } else {
621
        RETURN_FALSE
×
622
      }
623
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
1,765!
624
      if (endptr[1] == '6' && endptr[2] == '4') {
1,765!
625
        SET_UBIGINT
809!
626
      } else if (endptr[1] == '3' && endptr[2] == '2') {
956!
627
        SET_UINT
479!
628
      } else if (endptr[1] == '1' && endptr[2] == '6') {
477!
629
        SET_USMALL_INT
479!
630
      } else {
631
        RETURN_FALSE
×
632
      }
633
    } else {
634
      RETURN_FALSE
×
635
    }
636
  } else if (left == 2) {
1,325✔
637
    if (endptr[0] == 'i' || endptr[0] == 'I') {
1,313!
638
      if (endptr[1] == '8') {
833!
639
        SET_TINYINT
833!
640
      } else {
641
        RETURN_FALSE
×
642
      }
643
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
480!
644
      if (endptr[1] == '8') {
480!
645
        SET_UTINYINT
480!
646
      } else {
647
        RETURN_FALSE
×
648
      }
649
    } else {
650
      RETURN_FALSE
×
651
    }
652
  } else if (left == 1) {
12!
653
    if (endptr[0] == 'i' || endptr[0] == 'I') {
15!
654
      SET_BIGINT
15!
655
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
×
656
      SET_UBIGINT
×
657
    } else {
658
      RETURN_FALSE
×
659
    }
660
  } else {
UNCOV
661
    RETURN_FALSE;
×
662
  }
663
  return true;
8,114✔
664
}
665

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

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

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

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

684
static int64_t smlGenId() {
418✔
685
  static volatile int64_t linesSmlHandleId = 0;
686

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

692
  return id;
418✔
693
}
694

695
static int32_t smlGenerateSchemaAction(SSchema *colField, SHashObj *colHash, SSmlKv *kv, bool isTag,
2,863✔
696
                                       ESchemaAction *action, SSmlHandle *info) {
697
  uint16_t *index = colHash ? (uint16_t *)taosHashGet(colHash, kv->key, kv->keyLen) : NULL;
2,863✔
698
  if (index) {
2,863✔
699
    if (colField[*index].type != kv->type) {
907✔
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",
6✔
701
               info->id, __FUNCTION__, tDataTypes[colField[*index].type].name, tDataTypes[kv->type].name, kv->key);
6✔
702
      uError("%s", info->msgBuf.buf);
6!
703
      return TSDB_CODE_SML_INVALID_DATA;
6✔
704
    }
705

706
    if (((colField[*index].type == TSDB_DATA_TYPE_VARCHAR || colField[*index].type == TSDB_DATA_TYPE_VARBINARY ||
901✔
707
          colField[*index].type == TSDB_DATA_TYPE_GEOMETRY) &&
825!
708
         (colField[*index].bytes - VARSTR_HEADER_SIZE) < kv->length) ||
76!
709
        (colField[*index].type == TSDB_DATA_TYPE_NCHAR &&
901✔
710
         ((colField[*index].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE < kv->length))) {
386✔
711
      if (isTag) {
18!
712
        *action = SCHEMA_ACTION_CHANGE_TAG_SIZE;
18✔
713
      } else {
UNCOV
714
        *action = SCHEMA_ACTION_CHANGE_COLUMN_SIZE;
×
715
      }
716
    }
717
  } else {
718
    if (isTag) {
1,956✔
719
      *action = SCHEMA_ACTION_ADD_TAG;
980✔
720
    } else {
721
      *action = SCHEMA_ACTION_ADD_COLUMN;
976✔
722
    }
723
  }
724
  return TSDB_CODE_SUCCESS;
2,857✔
725
}
726

727
#define BOUNDARY 1024
728
static int32_t smlFindNearestPowerOf2(int32_t length, uint8_t type) {
814✔
729
  int32_t result = 1;
814✔
730
  if (length >= BOUNDARY) {
814✔
731
    result = length;
9✔
732
  } else {
733
    while (result <= length) {
3,772✔
734
      result <<= 1;
2,967✔
735
    }
736
  }
737

738
  if ((type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_GEOMETRY) &&
814✔
739
      result > TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE) {
204!
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) {
814!
742
    result = (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
743
  }
744

745
  if (type == TSDB_DATA_TYPE_NCHAR) {
814✔
746
    result = result * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
610✔
747
  } else if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_GEOMETRY) {
204!
748
    result = result + VARSTR_HEADER_SIZE;
205✔
749
  }
750
  return result;
814✔
751
}
752

753
static int32_t smlProcessSchemaAction(SSmlHandle *info, SSchema *schemaField, SHashObj *schemaHash, SArray *cols,
157✔
754
                                      SArray *checkDumplicateCols, ESchemaAction *action, bool isTag) {
755
  int32_t code = TSDB_CODE_SUCCESS;
157✔
756
  int32_t lino = 0;
157✔
757
  for (int j = 0; j < taosArrayGetSize(cols); ++j) {
999✔
758
    if (j == 0 && !isTag) continue;
848✔
759
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, j);
774✔
760
    SML_CHECK_NULL(kv);
774!
761
    SML_CHECK_CODE(smlGenerateSchemaAction(schemaField, schemaHash, kv, isTag, action, info));
774✔
762
  }
763

764
  for (int j = 0; j < taosArrayGetSize(checkDumplicateCols); ++j) {
989✔
765
    SSmlKv *kv = (SSmlKv *)taosArrayGet(checkDumplicateCols, j);
845✔
766
    SML_CHECK_NULL(kv);
845!
767
    if (taosHashGet(schemaHash, kv->key, kv->keyLen) != NULL) {
845✔
768
      SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
7!
769
    }
770
  }
771
END:
144✔
772
  RETURN
157!
773
}
774

775
static int32_t smlCheckMeta(SSchema *schema, int32_t length, SArray *cols, bool isTag) {
140✔
776
  int32_t   code = TSDB_CODE_SUCCESS;
140✔
777
  int32_t   lino = 0;
140✔
778
  SHashObj *hashTmp = taosHashInit(length, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
140✔
779
  SML_CHECK_NULL(hashTmp);
140!
780
  int32_t i = 0;
140✔
781
  for (; i < length; i++) {
975✔
782
    SML_CHECK_CODE(taosHashPut(hashTmp, schema[i].name, strlen(schema[i].name), &i, SHORT_BYTES));
835!
783
  }
784
  i = isTag ? 0 : 1;
140✔
785
  for (; i < taosArrayGetSize(cols); i++) {
888✔
786
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
748✔
787
    SML_CHECK_NULL(kv);
748!
788
    if (taosHashGet(hashTmp, kv->key, kv->keyLen) == NULL) {
748!
789
      SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
790
    }
791
  }
792

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

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

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

835
  int32_t maxLen = isTag ? TSDB_MAX_TAGS_LEN : TSDB_MAX_BYTES_PER_ROW;
397✔
836
  int32_t len = 0;
397✔
837
  for (int j = 0; j < taosArrayGetSize(results); ++j) {
2,476✔
838
    SField *field = taosArrayGet(results, j);
2,077✔
839
    SML_CHECK_NULL(field);
2,079!
840
    len += field->bytes;
2,079✔
841
  }
842
  if (len > maxLen) {
397✔
843
    return isTag ? TSDB_CODE_PAR_INVALID_TAGS_LENGTH : TSDB_CODE_PAR_INVALID_ROW_LENGTH;
3!
844
  }
845

846
END:
394✔
847
  RETURN
394!
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;
207✔
852
  pReq->tagVer = tagVer;
207✔
853
  pReq->suid = suid;
207✔
854
  pReq->source = source;
207✔
855
}
207✔
856
static int32_t smlSendMetaMsg(SSmlHandle *info, SName *pName, SArray *pColumns, SArray **pTags, STableMeta *pTableMeta,
207✔
857
                              ESchemaAction action) {
858
  SRequestObj   *pRequest = NULL;
207✔
859
  SMCreateStbReq pReq = {0};
207✔
860
  int32_t        code = TSDB_CODE_SUCCESS;
207✔
861
  int32_t        lino = 0;
207✔
862
  SCmdMsgInfo    pCmdMsg = {0};
207✔
863
  char          *pSql = NULL;
207✔
864

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

871
  pReq.pColumns = taosArrayInit(pReq.numOfColumns, sizeof(SFieldWithOptions));
207✔
872
  SML_CHECK_NULL(pReq.pColumns);
207!
873
  for (int32_t i = 0; i < pReq.numOfColumns; ++i) {
1,338✔
874
    SField *pField = taosArrayGet(pColumns, i);
1,131✔
875
    SML_CHECK_NULL(pField);
1,131!
876
    SFieldWithOptions fieldWithOption = {0};
1,131✔
877
    setFieldWithOptions(&fieldWithOption, pField);
1,131✔
878
    setDefaultOptionsForField(&fieldWithOption);
1,131✔
879
    SML_CHECK_NULL(taosArrayPush(pReq.pColumns, &fieldWithOption));
2,262!
880
  }
881

882
  if (action == SCHEMA_ACTION_CREATE_STABLE) {
207✔
883
    pSql = "sml_create_stable";
184✔
884
    smlBuildCreateStbReq(&pReq, 1, 1, 0, TD_REQ_FROM_APP);
885
  } else if (action == SCHEMA_ACTION_ADD_TAG || action == SCHEMA_ACTION_CHANGE_TAG_SIZE) {
23✔
886
    pSql = (action == SCHEMA_ACTION_ADD_TAG) ? "sml_add_tag" : "sml_modify_tag_size";
14✔
887
    smlBuildCreateStbReq(&pReq, pTableMeta->sversion, pTableMeta->tversion + 1, pTableMeta->uid, TD_REQ_FROM_TAOX);
14✔
888
  } else if (action == SCHEMA_ACTION_ADD_COLUMN || action == SCHEMA_ACTION_CHANGE_COLUMN_SIZE) {
9!
889
    pSql = (action == SCHEMA_ACTION_ADD_COLUMN) ? "sml_add_column" : "sml_modify_column_size";
9!
890
    smlBuildCreateStbReq(&pReq, pTableMeta->sversion + 1, pTableMeta->tversion, pTableMeta->uid, TD_REQ_FROM_TAOX);
9✔
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));
207!
896

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

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

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

915
  pCmdMsg.epSet = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
207✔
916
  pCmdMsg.msgType = TDMT_MND_CREATE_STB;
207✔
917
  pCmdMsg.msgLen = tSerializeSMCreateStbReq(NULL, 0, &pReq);
207✔
918
  if (pCmdMsg.msgLen < 0) {
207!
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);
207✔
923
  SML_CHECK_NULL(pCmdMsg.pMsg);
207!
924
  code = tSerializeSMCreateStbReq(pCmdMsg.pMsg, pCmdMsg.msgLen, &pReq);
207✔
925
  if (code < 0) {
207!
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};
207✔
932
  pQuery.execMode = QUERY_EXEC_MODE_RPC;
207✔
933
  pQuery.pCmdMsg = &pCmdMsg;
207✔
934
  pQuery.msgType = pQuery.pCmdMsg->msgType;
207✔
935
  pQuery.stableQuery = true;
207✔
936

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1090
  SSmlSTableMeta **tmp = (SSmlSTableMeta **)taosHashIterate(info->superTables, NULL);
249✔
1091
  while (tmp) {
470✔
1092
    SSmlSTableMeta *sTableData = *tmp;
273✔
1093
    bool            needCheckMeta = false;  // for multi thread
273✔
1094

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

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

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

1118
      hashTmp = taosHashInit(pTableMeta->tableInfo.numOfTags, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
83✔
1119
      SML_CHECK_NULL(hashTmp);
83!
1120
      SML_CHECK_CODE(smlBuildTempHash(hashTmp, pTableMeta, pTableMeta->tableInfo.numOfColumns, pTableMeta->tableInfo.numOfColumns + pTableMeta->tableInfo.numOfTags));
83!
1121
      SML_CHECK_CODE(smlModifyTag(info, hashTmp, &conn, sTableData, &pName, &pTableMeta));
83✔
1122
      taosHashClear(hashTmp);
74✔
1123
      SML_CHECK_CODE(smlBuildTempHash(hashTmp, pTableMeta, 0, pTableMeta->tableInfo.numOfColumns));
74!
1124
      SML_CHECK_CODE(smlModifyCols(info, hashTmp, &conn, sTableData, &pName, &pTableMeta));
74✔
1125

1126
      needCheckMeta = true;
70✔
1127
      taosHashCleanup(hashTmp);
70✔
1128
      hashTmp = NULL;
70✔
1129
    } else {
1130
      uError("SML:0x%" PRIx64 " %s load table meta error: %s", info->id, __FUNCTION__, tstrerror(code));
×
1131
      goto END;
×
1132
    }
1133

1134
    if (needCheckMeta) {
221✔
1135
      SML_CHECK_CODE(smlCheckMeta(&(pTableMeta->schema[pTableMeta->tableInfo.numOfColumns]), pTableMeta->tableInfo.numOfTags, sTableData->tags, true));
70!
1136
      SML_CHECK_CODE(smlCheckMeta(&(pTableMeta->schema[0]), pTableMeta->tableInfo.numOfColumns, sTableData->cols, false));
70!
1137
    }
1138

1139
    taosMemoryFreeClear(sTableData->tableMeta);
221✔
1140
    sTableData->tableMeta = pTableMeta;
221✔
1141
    uDebug("SML:0x%" PRIx64 " %s modify schema uid:%" PRIu64 ", sversion:%d, tversion:%d", info->id, __FUNCTION__, pTableMeta->uid,
221!
1142
           pTableMeta->sversion, pTableMeta->tversion);
1143
    tmp = (SSmlSTableMeta **)taosHashIterate(info->superTables, tmp);
221✔
1144
  }
1145
  uDebug("SML:0x%" PRIx64 " %s end success, format:%d, needModifySchema:%d", info->id, __FUNCTION__, info->dataFormat,
197!
1146
         info->needModifySchema);
1147

1148
  return TSDB_CODE_SUCCESS;
197✔
1149

1150
END:
52✔
1151
  taosHashCancelIterate(info->superTables, tmp);
52✔
1152
  taosHashCleanup(hashTmp);
52✔
1153
  taosMemoryFreeClear(pTableMeta);
52✔
1154
  (void)catalogRefreshTableMeta(info->pCatalog, &conn, &pName, 1);  // ignore refresh meta code if there is an error
52✔
1155
  uError("SML:0x%" PRIx64 " %s end failed:%d:%s, format:%d, needModifySchema:%d", info->id, __FUNCTION__, code,
52!
1156
         tstrerror(code), info->dataFormat, info->needModifySchema);
1157

1158
  return code;
52✔
1159
}
1160

1161
static int32_t smlInsertMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, SHashObj *checkDuplicate) {
495✔
1162
  int32_t code = 0;
495✔
1163
  int32_t lino = 0;
495✔
1164
  terrno = 0;
495✔
1165
  for (int16_t i = 0; i < taosArrayGetSize(cols); ++i) {
2,673✔
1166
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
2,193✔
1167
    SML_CHECK_NULL(kv);
2,203!
1168
    int ret = taosHashPut(metaHash, kv->key, kv->keyLen, &i, SHORT_BYTES);
2,193✔
1169
    if (ret == 0) {
2,193✔
1170
      SML_CHECK_NULL(taosArrayPush(metaArray, kv));
2,188!
1171
      if (taosHashGet(checkDuplicate, kv->key, kv->keyLen) != NULL) {
2,188✔
1172
        SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
10!
1173
      }
1174
    } else if (terrno == TSDB_CODE_DUP_KEY) {
5!
1175
      return TSDB_CODE_PAR_DUPLICATED_COLUMN;
5✔
1176
    }
1177
  }
1178

1179
END:
490✔
1180
  RETURN
490!
1181
}
1182

1183
static int32_t smlUpdateMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, bool isTag, SSmlMsgBuf *msg,
1,720✔
1184
                             SHashObj *checkDuplicate) {
1185
  int32_t code = 0;
1,720✔
1186
  int32_t lino = 0;
1,720✔
1187
  for (int i = 0; i < taosArrayGetSize(cols); ++i) {
15,477✔
1188
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
13,714✔
1189
    SML_CHECK_NULL(kv);
13,714!
1190
    int16_t *index = (int16_t *)taosHashGet(metaHash, kv->key, kv->keyLen);
13,714✔
1191
    if (index) {
13,736✔
1192
      SSmlKv *value = (SSmlKv *)taosArrayGet(metaArray, *index);
13,699✔
1193
      SML_CHECK_NULL(value);
13,691!
1194

1195
      if (isTag) {
13,720✔
1196
        if (kv->length > value->length) {
7,102✔
1197
          value->length = kv->length;
24✔
1198
        }
1199
        continue;
7,102✔
1200
      }
1201
      if (kv->type != value->type) {
6,618!
1202
        smlBuildInvalidDataMsg(msg, "the type is not the same like before", kv->key);
×
1203
        SML_CHECK_CODE(TSDB_CODE_SML_NOT_SAME_TYPE);
×
1204
      }
1205

1206
      if (IS_VAR_DATA_TYPE(kv->type) && (kv->length > value->length)) {  // update string len, if bigger
6,618!
1207
        value->length = kv->length;
23✔
1208
      }
1209
    } else {
1210
      size_t tmp = taosArrayGetSize(metaArray);
37✔
1211
      if (tmp > INT16_MAX) {
37!
1212
        smlBuildInvalidDataMsg(msg, "too many cols or tags", kv->key);
×
1213
        SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
1214
      }
1215
      int16_t size = tmp;
37✔
1216
      SML_CHECK_CODE(taosHashPut(metaHash, kv->key, kv->keyLen, &size, SHORT_BYTES));
37!
1217
      SML_CHECK_NULL(taosArrayPush(metaArray, kv));
37!
1218
      if (taosHashGet(checkDuplicate, kv->key, kv->keyLen) != NULL) {
37!
1219
        SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
×
1220
      }
1221
    }
1222
  }
1223

1224
END:
1,710✔
1225
  RETURN
1,710!
1226
}
1227

1228
void smlDestroyTableInfo(void *para) {
829✔
1229
  SSmlTableInfo *tag = *(SSmlTableInfo **)para;
829✔
1230
  for (size_t i = 0; i < taosArrayGetSize(tag->cols); i++) {
1,939✔
1231
    SHashObj *kvHash = (SHashObj *)taosArrayGetP(tag->cols, i);
1,110✔
1232
    taosHashCleanup(kvHash);
1,110✔
1233
  }
1234

1235
  taosArrayDestroy(tag->cols);
829✔
1236
  taosArrayDestroyEx(tag->tags, freeSSmlKv);
829✔
1237
  taosMemoryFree(tag);
829✔
1238
}
829✔
1239

1240
void freeSSmlKv(void *data) {
24,112✔
1241
  SSmlKv *kv = (SSmlKv *)data;
24,112✔
1242
  if (kv->keyEscaped) taosMemoryFreeClear(kv->key);
24,112✔
1243
  if (kv->valueEscaped) taosMemoryFreeClear(kv->value);
24,112✔
1244
  if (kv->type == TSDB_DATA_TYPE_GEOMETRY) geosFreeBuffer((void *)(kv->value));
24,112✔
1245
  if (kv->type == TSDB_DATA_TYPE_VARBINARY) taosMemoryFreeClear(kv->value);
24,112!
1246
}
24,112✔
1247

1248
void smlDestroyInfo(SSmlHandle *info) {
836✔
1249
  if (info == NULL) return;
836✔
1250

1251
  taosHashCleanup(info->pVgHash);
418✔
1252
  taosHashCleanup(info->childTables);
418✔
1253
  taosHashCleanup(info->superTables);
418✔
1254
  taosHashCleanup(info->tableUids);
418✔
1255

1256
  for (int i = 0; i < taosArrayGetSize(info->tagJsonArray); i++) {
418!
1257
    cJSON *tags = (cJSON *)taosArrayGetP(info->tagJsonArray, i);
×
1258
    cJSON_Delete(tags);
×
1259
  }
1260
  taosArrayDestroy(info->tagJsonArray);
418✔
1261

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

1268
  taosArrayDestroyEx(info->preLineTagKV, freeSSmlKv);
418✔
1269
  taosArrayDestroyP(info->escapedStringList, taosMemoryFree);
418✔
1270

1271
  if (!info->dataFormat) {
418✔
1272
    for (int i = 0; i < info->lineNum; i++) {
1,375✔
1273
      taosArrayDestroyEx(info->lines[i].colArray, freeSSmlKv);
1,131✔
1274
      if (info->lines[i].measureTagsLen != 0 && info->protocol != TSDB_SML_LINE_PROTOCOL) {
1,131✔
1275
        taosMemoryFree(info->lines[i].measureTag);
112✔
1276
      }
1277
    }
1278
    taosMemoryFree(info->lines);
244✔
1279
  }
1280
  if(info->protocol == TSDB_SML_JSON_PROTOCOL)  {
418✔
1281
    taosMemoryFreeClear(info->preLine.tags);
187✔
1282
  }
1283
  cJSON_Delete(info->root);
418✔
1284
  taosMemoryFreeClear(info);
418!
1285
}
1286

1287
int32_t smlBuildSmlInfo(TAOS *taos, SSmlHandle **handle) {
418✔
1288
  int32_t     code = TSDB_CODE_SUCCESS;
418✔
1289
  int32_t     lino = 0;
418✔
1290
  SSmlHandle *info = (SSmlHandle *)taosMemoryCalloc(1, sizeof(SSmlHandle));
418✔
1291
  SML_CHECK_NULL(info);
418!
1292
  if (taos != NULL){
418!
1293
    info->taos = acquireTscObj(*(int64_t *)taos);
418✔
1294
    SML_CHECK_NULL(info->taos);
418!
1295
    SML_CHECK_CODE(catalogGetHandle(info->taos->pAppInfo->clusterId, &info->pCatalog));
418!
1296
  }
1297

1298
  info->pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
418✔
1299
  SML_CHECK_NULL(info->pVgHash);
418!
1300
  info->childTables = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
418✔
1301
  SML_CHECK_NULL(info->childTables);
418!
1302
  info->tableUids = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
418✔
1303
  SML_CHECK_NULL(info->tableUids);
418!
1304
  info->superTables = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
418✔
1305
  SML_CHECK_NULL(info->superTables);
418!
1306
  taosHashSetFreeFp(info->superTables, smlDestroySTableMeta);
418✔
1307
  taosHashSetFreeFp(info->childTables, smlDestroyTableInfo);
418✔
1308

1309
  info->id = smlGenId();
418✔
1310
  SML_CHECK_CODE(smlInitHandle(&info->pQuery));
418!
1311
  info->dataFormat = true;
418✔
1312
  info->tagJsonArray = taosArrayInit(8, POINTER_BYTES);
418✔
1313
  SML_CHECK_NULL(info->tagJsonArray);
418!
1314
  info->valueJsonArray = taosArrayInit(8, POINTER_BYTES);
418✔
1315
  SML_CHECK_NULL(info->valueJsonArray);
418!
1316
  info->preLineTagKV = taosArrayInit(8, sizeof(SSmlKv));
418✔
1317
  SML_CHECK_NULL(info->preLineTagKV);
418!
1318
  info->escapedStringList = taosArrayInit(8, POINTER_BYTES);
418✔
1319
  SML_CHECK_NULL(info->escapedStringList);
418!
1320

1321
  *handle = info;
418✔
1322
  info = NULL;
418✔
1323

1324
END:
418✔
1325
  smlDestroyInfo(info);
418✔
1326
  RETURN
418!
1327
}
1328

1329
static int32_t smlPushCols(SArray *colsArray, SArray *cols) {
1,116✔
1330
  int32_t code = TSDB_CODE_SUCCESS;
1,116✔
1331
  int32_t lino = 0;
1,116✔
1332
  SHashObj *kvHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
1,116✔
1333
  SML_CHECK_NULL(kvHash);
1,116!
1334
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
8,933✔
1335
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
7,814✔
1336
    SML_CHECK_NULL(kv);
7,819!
1337
    terrno = 0;
7,813✔
1338
    code = taosHashPut(kvHash, kv->key, kv->keyLen, &kv, POINTER_BYTES);
7,808✔
1339
    if (terrno == TSDB_CODE_DUP_KEY) {
7,824✔
1340
      SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
6!
1341
    }
1342
    SML_CHECK_CODE(code);
7,817!
1343
  }
1344

1345
  SML_CHECK_NULL(taosArrayPush(colsArray, &kvHash));
1,109!
1346
  return code;
1,109✔
1347
END:
6✔
1348
  taosHashCleanup(kvHash);
6✔
1349
  RETURN
6!
1350
}
1351

1352
static int32_t smlParseEnd(SSmlHandle *info) {
394✔
1353
  uDebug("SML:0x%" PRIx64 " %s start, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
394!
1354
         info->lineNum);
1355
  int32_t code = 0;
394✔
1356
  int32_t lino = 0;
394✔
1357
  if (info->dataFormat) return TSDB_CODE_SUCCESS;
394✔
1358

1359
  for (int32_t i = 0; i < info->lineNum; i++) {
1,326✔
1360
    SSmlLineInfo  *elements = info->lines + i;
1,115✔
1361
    SSmlTableInfo *tinfo = NULL;
1,115✔
1362
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
1,115✔
1363
      SSmlTableInfo **tmp =
1364
          (SSmlTableInfo **)taosHashGet(info->childTables, elements->measure, elements->measureTagsLen);
704✔
1365
      if (tmp) tinfo = *tmp;
704!
1366
    } else {
1367
      SSmlTableInfo **tmp = (SSmlTableInfo **)taosHashGet(info->childTables, elements->measureTag,
411✔
1368
                                                          elements->measureLen + elements->tagsLen);
411✔
1369
      if (tmp) tinfo = *tmp;
412!
1370
    }
1371

1372
    if (tinfo == NULL) {
1,116!
1373
      uError("SML:0x%" PRIx64 "get oneTable failed, line num:%d", info->id, i);
×
1374
      smlBuildInvalidDataMsg(&info->msgBuf, "get oneTable failed", elements->measure);
×
1375
      return TSDB_CODE_SML_INVALID_DATA;
×
1376
    }
1377

1378
    if (taosArrayGetSize(tinfo->tags) > TSDB_MAX_TAGS) {
1,116!
UNCOV
1379
      smlBuildInvalidDataMsg(&info->msgBuf, "too many tags than 128", NULL);
×
UNCOV
1380
      return TSDB_CODE_PAR_INVALID_TAGS_NUM;
×
1381
    }
1382

1383
    if (taosArrayGetSize(elements->colArray) + taosArrayGetSize(tinfo->tags) > TSDB_MAX_COLUMNS) {
1,116!
UNCOV
1384
      smlBuildInvalidDataMsg(&info->msgBuf, "too many columns than 4096", NULL);
×
UNCOV
1385
      return TSDB_CODE_PAR_TOO_MANY_COLUMNS;
×
1386
    }
1387

1388
    SML_CHECK_CODE(smlPushCols(tinfo->cols, elements->colArray));
1,116✔
1389

1390
    SSmlSTableMeta **tableMeta =
1391
        (SSmlSTableMeta **)taosHashGet(info->superTables, elements->measure, elements->measureLen);
1,109✔
1392
    if (tableMeta) {  // update meta
1,110✔
1393
      uDebug("SML:0x%" PRIx64 " %s update meta, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
860!
1394
             info->lineNum);
1395
      SML_CHECK_CODE(smlUpdateMeta((*tableMeta)->colHash, (*tableMeta)->cols, elements->colArray, false, &info->msgBuf,
860!
1396
                          (*tableMeta)->tagHash));
1397
      SML_CHECK_CODE(smlUpdateMeta((*tableMeta)->tagHash, (*tableMeta)->tags, tinfo->tags, true, &info->msgBuf,
860!
1398
                          (*tableMeta)->colHash));
1399
    } else {
1400
      uDebug("SML:0x%" PRIx64 " %s add meta, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
250!
1401
             info->lineNum);
1402
      SSmlSTableMeta *meta = NULL;
250✔
1403
      SML_CHECK_CODE(smlBuildSTableMeta(info->dataFormat, &meta));
265!
1404
      code = taosHashPut(info->superTables, elements->measure, elements->measureLen, &meta, POINTER_BYTES);
250✔
1405
      if (code != TSDB_CODE_SUCCESS) {
250!
1406
        smlDestroySTableMeta(&meta);
×
1407
        SML_CHECK_CODE(code);
×
1408
      }
1409
      SML_CHECK_CODE(smlInsertMeta(meta->tagHash, meta->tags, tinfo->tags, NULL));
250✔
1410
      SML_CHECK_CODE(smlInsertMeta(meta->colHash, meta->cols, elements->colArray, meta->tagHash));
245✔
1411
    }
1412
  }
1413
  uDebug("SML:0x%" PRIx64 " %s end, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat, info->lineNum);
211!
1414

1415
END:
211✔
1416
  RETURN
232!
1417
}
1418

1419
static int32_t smlInsertData(SSmlHandle *info) {
354✔
1420
  int32_t         code      = TSDB_CODE_SUCCESS;
354✔
1421
  int32_t         lino      = 0;
354✔
1422
  char           *measure   = NULL;
354✔
1423
  SSmlTableInfo **oneTable  = NULL;
354✔
1424
  uDebug("SML:0x%" PRIx64 " %s start, format:%d", info->id, __FUNCTION__, info->dataFormat);
354!
1425

1426
  if (info->pRequest->dbList == NULL) {
354!
1427
    info->pRequest->dbList = taosArrayInit(1, TSDB_DB_FNAME_LEN);
354✔
1428
    SML_CHECK_NULL(info->pRequest->dbList);
354!
1429
  }
1430
  char *data = (char *)taosArrayReserve(info->pRequest->dbList, 1);
354✔
1431
  SML_CHECK_NULL(data);
354!
1432
  SName pName = {TSDB_TABLE_NAME_T, info->taos->acctId, {0}, {0}};
354✔
1433
  tstrncpy(pName.dbname, info->pRequest->pDb, sizeof(pName.dbname));
354✔
1434
  (void)tNameGetFullDbName(&pName, data);  // ignore
354✔
1435

1436
  oneTable = (SSmlTableInfo **)taosHashIterate(info->childTables, NULL);
354✔
1437
  while (oneTable) {
1,110✔
1438
    SSmlTableInfo *tableData = *oneTable;
762✔
1439

1440
    int   measureLen = tableData->sTableNameLen;
762✔
1441
    measure = (char *)taosMemoryMalloc(tableData->sTableNameLen);
762✔
1442
    SML_CHECK_NULL(measure);
768!
1443
    (void)memcpy(measure, tableData->sTableName, tableData->sTableNameLen);
762✔
1444
    if (info->protocol == TSDB_SML_LINE_PROTOCOL){
762✔
1445
      PROCESS_SLASH_IN_MEASUREMENT(measure, measureLen);
3,799✔
1446
    }
1447
    smlStrReplace(measure, measureLen);
762✔
1448
    (void)memcpy(pName.tname, measure, measureLen);
762✔
1449
    pName.tname[measureLen] = '\0';
762✔
1450

1451
    if (info->pRequest->tableList == NULL) {
762✔
1452
      info->pRequest->tableList = taosArrayInit(1, sizeof(SName));
354✔
1453
      SML_CHECK_NULL(info->pRequest->tableList);
354!
1454
    }
1455
    SML_CHECK_NULL(taosArrayPush(info->pRequest->tableList, &pName));
1,524!
1456
    tstrncpy(pName.tname, tableData->childTableName, sizeof(pName.tname));
762✔
1457

1458
    SRequestConnInfo conn = {0};
762✔
1459
    conn.pTrans = info->taos->pAppInfo->pTransporter;
762✔
1460
    conn.requestId = info->pRequest->requestId;
762✔
1461
    conn.requestObjRefId = info->pRequest->self;
762✔
1462
    conn.mgmtEps = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
762✔
1463

1464
    SML_CHECK_CODE(smlCheckAuth(info, &conn, pName.tname, AUTH_TYPE_WRITE));
762✔
1465

1466
    SVgroupInfo vg = {0};
756✔
1467
    SML_CHECK_CODE(catalogGetTableHashVgroup(info->pCatalog, &conn, &pName, &vg));
756!
1468
    SML_CHECK_CODE(taosHashPut(info->pVgHash, (const char *)&vg.vgId, sizeof(vg.vgId), (char *)&vg, sizeof(vg)));
756!
1469

1470
    SSmlSTableMeta **pMeta =
1471
        (SSmlSTableMeta **)taosHashGet(info->superTables, tableData->sTableName, tableData->sTableNameLen);
756✔
1472
    if (unlikely(NULL == pMeta || NULL == *pMeta || NULL == (*pMeta)->tableMeta)) {
756!
1473
      uError("SML:0x%" PRIx64 " %s NULL == pMeta. table name: %s", info->id, __FUNCTION__, tableData->childTableName);
×
1474
      SML_CHECK_CODE(TSDB_CODE_SML_INTERNAL_ERROR);
×
1475
    }
1476

1477
    // use tablemeta of stable to save vgid and uid of child table
1478
    (*pMeta)->tableMeta->vgId = vg.vgId;
756✔
1479
    (*pMeta)->tableMeta->uid = tableData->uid;  // one table merge data block together according uid
756✔
1480
    uDebug("SML:0x%" PRIx64 " %s table:%s, uid:%" PRIu64 ", format:%d", info->id, __FUNCTION__, pName.tname,
756!
1481
           tableData->uid, info->dataFormat);
1482

1483
    SML_CHECK_CODE(smlBindData(info->pQuery, info->dataFormat, tableData->tags, (*pMeta)->cols, tableData->cols,
756!
1484
                       (*pMeta)->tableMeta, tableData->childTableName, measure, measureLen, info->ttl, info->msgBuf.buf,
1485
                       info->msgBuf.len));
1486
    taosMemoryFreeClear(measure);
756!
1487
    oneTable = (SSmlTableInfo **)taosHashIterate(info->childTables, oneTable);
756✔
1488
  }
1489

1490
  SML_CHECK_CODE(smlBuildOutput(info->pQuery, info->pVgHash));
348!
1491
  info->cost.insertRpcTime = taosGetTimestampUs();
348✔
1492

1493
  SAppClusterSummary *pActivity = &info->taos->pAppInfo->summary;
348✔
1494
  (void)atomic_add_fetch_64((int64_t *)&pActivity->numOfInsertsReq, 1);  // no need to check return code
348✔
1495

1496
  launchQueryImpl(info->pRequest, info->pQuery, true, NULL);  // no need to check return code
348✔
1497

1498
  uDebug("SML:0x%" PRIx64 " %s end, format:%d, code:%d,%s", info->id, __FUNCTION__, info->dataFormat, info->pRequest->code,
348!
1499
         tstrerror(info->pRequest->code));
1500

1501
  return info->pRequest->code;
348✔
1502

1503
END:
6✔
1504
  taosMemoryFree(measure);
6✔
1505
  taosHashCancelIterate(info->childTables, oneTable);
6✔
1506
  RETURN
6!
1507
}
1508

1509
static void smlPrintStatisticInfo(SSmlHandle *info) {
418✔
1510
  uDebug(
418!
1511
      "SML:0x%" PRIx64
1512
      " 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 \
1513
        parse cost:%" PRId64 ",schema cost:%" PRId64 ",bind cost:%" PRId64 ",rpc cost:%" PRId64 ",total cost:%" PRId64,
1514
      info->id, info->cost.code, tstrerror(info->cost.code), info->cost.lineNum, info->cost.numOfSTables,
1515
      info->cost.numOfCTables, info->cost.numOfCreateSTables, info->cost.numOfAlterTagSTables,
1516
      info->cost.numOfAlterColSTables, info->cost.schemaTime - info->cost.parseTime,
1517
      info->cost.insertBindTime - info->cost.schemaTime, info->cost.insertRpcTime - info->cost.insertBindTime,
1518
      info->cost.endTime - info->cost.insertRpcTime, info->cost.endTime - info->cost.parseTime);
1519
}
418✔
1520

1521
int32_t smlClearForRerun(SSmlHandle *info) {
244✔
1522
  int32_t code = 0;
244✔
1523
  int32_t lino = 0;
244✔
1524
  info->reRun = false;
244✔
1525

1526
  taosHashClear(info->childTables);
244✔
1527
  taosHashClear(info->superTables);
244✔
1528
  taosHashClear(info->tableUids);
244✔
1529

1530
  if (!info->dataFormat) {
244!
1531
    info->lines = (SSmlLineInfo *)taosMemoryCalloc(info->lineNum, sizeof(SSmlLineInfo));
244✔
1532
    SML_CHECK_NULL(info->lines);
244!
1533
  }
1534

1535
  taosArrayClearP(info->escapedStringList, taosMemoryFree);
244✔
1536
  if(info->protocol == TSDB_SML_JSON_PROTOCOL)  {
244✔
1537
    taosMemoryFreeClear(info->preLine.tags);
70!
1538
  }
1539
  (void)memset(&info->preLine, 0, sizeof(SSmlLineInfo));
244✔
1540
  info->currSTableMeta = NULL;
244✔
1541
  info->currTableDataCtx = NULL;
244✔
1542

1543
  SVnodeModifyOpStmt *stmt = (SVnodeModifyOpStmt *)(info->pQuery->pRoot);
244✔
1544
  stmt->freeHashFunc(stmt->pTableBlockHashObj);
244✔
1545
  stmt->pTableBlockHashObj = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
244✔
1546
  SML_CHECK_NULL(stmt->pTableBlockHashObj);
244!
1547

1548
END:
244✔
1549
  RETURN
244!
1550
}
1551

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

1568
static bool getLine(SSmlHandle *info, char *lines[], char **rawLine, char *rawLineEnd, int numLines, int i, char **tmp,
1,306✔
1569
                    int *len) {
1570
  if (lines) {
1,306✔
1571
    *tmp = lines[i];
1,258✔
1572
    *len = strlen(*tmp);
1,258✔
1573
  } else if (*rawLine) {
48!
1574
    *tmp = *rawLine;
48✔
1575
    while (*rawLine < rawLineEnd) {
483,075✔
1576
      if (*((*rawLine)++) == '\n') {
483,045✔
1577
        break;
18✔
1578
      }
1579
      (*len)++;
483,027✔
1580
    }
1581
    if (IS_COMMENT(info->protocol,(*tmp)[0])) {  // this line is comment
48!
1582
      return false;
3✔
1583
    }
1584
  }
1585

1586
  if (*rawLine != NULL && (uDebugFlag & DEBUG_DEBUG)) {
1,303!
1587
    printRaw(info->id, i, numLines, DEBUG_DEBUG, *tmp, *len);
×
1588
  } else {
1589
    uDebug("SML:0x%" PRIx64 " smlParseLine is not raw, line %d/%d : %s", info->id, i, numLines, *tmp);
1,303!
1590
  }
1591
  return true;
1,302✔
1592
}
1593

1594

1595
static int32_t smlParseJson(SSmlHandle *info, char *lines[], char *rawLine) {
187✔
1596
  int32_t code = TSDB_CODE_SUCCESS;
187✔
1597
  if (lines) {
187!
1598
    code = smlParseJSONExt(info, *lines);
187✔
1599
  } else if (rawLine) {
×
1600
    code = smlParseJSONExt(info, rawLine);
×
1601
  }
1602
  if (code != TSDB_CODE_SUCCESS) {
187✔
1603
    uError("%s failed code:%d", __FUNCTION__ , code);
3!
1604
  }
1605
  return code;
187✔
1606
}
1607

1608
static int32_t smlParseStart(SSmlHandle *info, char *lines[], char *rawLine, char *rawLineEnd, int numLines) {
418✔
1609
  uDebug("SML:0x%" PRIx64 " %s start", info->id, __FUNCTION__);
418!
1610
  int32_t code = TSDB_CODE_SUCCESS;
418✔
1611
  if (info->protocol == TSDB_SML_JSON_PROTOCOL) {
418✔
1612
    return smlParseJson(info, lines, rawLine);
187✔
1613
  }
1614

1615
  char   *oldRaw = rawLine;
231✔
1616
  int32_t i = 0;
231✔
1617
  while (i < numLines) {
1,516✔
1618
    char *tmp = NULL;
1,306✔
1619
    int   len = 0;
1,306✔
1620
    if (!getLine(info, lines, &rawLine, rawLineEnd, numLines, i, &tmp, &len)) {
1,306✔
1621
      continue;
177✔
1622
    }
1623
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
1,302✔
1624
      if (info->dataFormat) {
1,245✔
1625
        SSmlLineInfo element = {0};
530✔
1626
        code = smlParseInfluxString(info, tmp, tmp + len, &element);
530✔
1627
      } else {
1628
        code = smlParseInfluxString(info, tmp, tmp + len, info->lines + i);
715✔
1629
      }
1630
    } else if (info->protocol == TSDB_SML_TELNET_PROTOCOL) {
57!
1631
      if (info->dataFormat) {
57✔
1632
        SSmlLineInfo element = {0};
27✔
1633
        code = smlParseTelnetString(info, (char *)tmp, (char *)tmp + len, &element);
27✔
1634
        if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
27✔
1635
      } else {
1636
        code = smlParseTelnetString(info, (char *)tmp, (char *)tmp + len, info->lines + i);
30✔
1637
      }
1638
    }
1639
    if (code != TSDB_CODE_SUCCESS) {
1,303✔
1640
      if (rawLine != NULL) {
21!
1641
        printRaw(info->id, i, numLines, DEBUG_ERROR, tmp, len);
×
1642
      } else {
1643
        uError("SML:0x%" PRIx64 " %s failed. line %d : %s", info->id, __FUNCTION__, i, tmp);
21!
1644
      }
1645
      return code;
21✔
1646
    }
1647
    if (info->reRun) {
1,282✔
1648
      uDebug("SML:0x%" PRIx64 " %s re run", info->id, __FUNCTION__);
174!
1649
      i = 0;
174✔
1650
      rawLine = oldRaw;
174✔
1651
      code = smlClearForRerun(info);
174✔
1652
      if (code != TSDB_CODE_SUCCESS) {
174!
1653
        return code;
×
1654
      }
1655
      continue;
174✔
1656
    }
1657
    i++;
1,108✔
1658
  }
1659
  uDebug("SML:0x%" PRIx64 " %s end", info->id, __FUNCTION__);
210!
1660

1661
  return code;
210✔
1662
}
1663

1664
static int smlProcess(SSmlHandle *info, char *lines[], char *rawLine, char *rawLineEnd, int numLines) {
418✔
1665
  int32_t code = TSDB_CODE_SUCCESS;
418✔
1666
  int32_t lino = 0;
418✔
1667
  int32_t retryNum = 0;
418✔
1668

1669
  info->cost.parseTime = taosGetTimestampUs();
418✔
1670

1671
  SML_CHECK_CODE(smlParseStart(info, lines, rawLine, rawLineEnd, numLines));
418✔
1672
  SML_CHECK_CODE(smlParseEnd(info));
394✔
1673

1674
  info->cost.lineNum = info->lineNum;
373✔
1675
  info->cost.numOfSTables = taosHashGetSize(info->superTables);
373✔
1676
  info->cost.numOfCTables = taosHashGetSize(info->childTables);
373✔
1677
  info->cost.schemaTime = taosGetTimestampUs();
373✔
1678

1679
  do {
1680
    code = smlModifyDBSchemas(info);
406✔
1681
    if (code != TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER && code != TSDB_CODE_SDB_OBJ_CREATING &&
406!
1682
        code != TSDB_CODE_MND_TRANS_CONFLICT) {
1683
      break;
373✔
1684
    }
1685
    taosMsleep(100);
33✔
1686
    uInfo("SML:0x%" PRIx64 " smlModifyDBSchemas retry code:%s, times:%d", info->id, tstrerror(code), retryNum);
33!
1687
  } while (retryNum++ < taosHashGetSize(info->superTables) * MAX_RETRY_TIMES);
33!
1688

1689
  SML_CHECK_CODE(code);
373✔
1690
  info->cost.insertBindTime = taosGetTimestampUs();
354✔
1691
  SML_CHECK_CODE(smlInsertData(info));
354✔
1692

1693
END:
348✔
1694
  RETURN
418!
1695
}
1696

1697
void smlSetReqSQL(SRequestObj *request, char *lines[], char *rawLine, char *rawLineEnd) {
418✔
1698
  if (request->pTscObj->pAppInfo->serverCfg.monitorParas.tsSlowLogScope & SLOW_LOG_TYPE_INSERT) {
418!
1699
    int32_t len = 0;
×
1700
    int32_t rlen = 0;
×
1701
    char   *p = NULL;
×
1702

1703
    if (lines && lines[0]) {
×
1704
      len = strlen(lines[0]);
×
1705
      p = lines[0];
×
1706
    } else if (rawLine) {
×
1707
      if (rawLineEnd) {
×
1708
        len = rawLineEnd - rawLine;
×
1709
      } else {
1710
        len = strlen(rawLine);
×
1711
      }
1712
      p = rawLine;
×
1713
    }
1714

1715
    if (NULL == p) {
×
1716
      return;
×
1717
    }
1718

1719
    rlen = TMIN(len, TSDB_MAX_ALLOWED_SQL_LEN);
×
1720
    rlen = TMAX(rlen, 0);
×
1721

1722
    char *sql = taosMemoryMalloc(rlen + 1);
×
1723
    if (NULL == sql) {
×
1724
      uError("malloc %d for sml sql failed", rlen + 1);
×
1725
      return;
×
1726
    }
1727
    (void)memcpy(sql, p, rlen);
×
1728
    sql[rlen] = 0;
×
1729

1730
    request->sqlstr = sql;
×
1731
    request->sqlLen = rlen;
×
1732
  }
1733
}
1734

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

1748
    info->pRequest = request;
418✔
1749
    info->pRequest->pQuery = info->pQuery;
418✔
1750
    info->ttl = ttl;
418✔
1751
    info->precision = precision;
418✔
1752
    info->protocol = (TSDB_SML_PROTOCOL_TYPE)protocol;
418✔
1753
    info->msgBuf.buf = info->pRequest->msgBuf;
418✔
1754
    info->msgBuf.len = ERROR_MSG_BUF_DEFAULT_SIZE;
418✔
1755
    info->lineNum = numLines;
418✔
1756
    info->tbnameKey = tbnameKey;
418✔
1757

1758
    smlSetReqSQL(request, lines, rawLine, rawLineEnd);
418✔
1759

1760
    if (request->pDb == NULL) {
418!
1761
      request->code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1762
      smlBuildInvalidDataMsg(&msg, "Database not specified", NULL);
×
1763
      goto END;
×
1764
    }
1765

1766
    if (protocol < TSDB_SML_LINE_PROTOCOL || protocol > TSDB_SML_JSON_PROTOCOL) {
418!
1767
      request->code = TSDB_CODE_SML_INVALID_PROTOCOL_TYPE;
×
1768
      smlBuildInvalidDataMsg(&msg, "protocol invalidate", NULL);
×
1769
      goto END;
×
1770
    }
1771

1772
    if (protocol == TSDB_SML_LINE_PROTOCOL &&
418!
1773
        (precision < TSDB_SML_TIMESTAMP_NOT_CONFIGURED || precision > TSDB_SML_TIMESTAMP_NANO_SECONDS)) {
204!
1774
      request->code = TSDB_CODE_SML_INVALID_PRECISION_TYPE;
×
1775
      smlBuildInvalidDataMsg(&msg, "precision invalidate for line protocol", NULL);
×
1776
      goto END;
×
1777
    }
1778

1779
    if (protocol == TSDB_SML_JSON_PROTOCOL) {
418✔
1780
      numLines = 1;
187✔
1781
    } else if (numLines <= 0) {
231!
1782
      request->code = TSDB_CODE_SML_INVALID_DATA;
×
1783
      smlBuildInvalidDataMsg(&msg, "line num is invalid", NULL);
×
1784
      goto END;
×
1785
    }
1786

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

1814
END:
418✔
1815
  smlDestroyInfo(info);
418✔
1816
  return (TAOS_RES *)request;
418✔
1817
}
1818

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

1838
TAOS_RES *taos_schemaless_insert_ttl_with_reqid_tbname_key(TAOS *taos, char *lines[], int numLines, int protocol,
400✔
1839
                                                           int precision, int32_t ttl, int64_t reqid, char *tbnameKey) {
1840
  if (taos == NULL || lines == NULL || numLines < 0) {
400!
1841
    terrno = TSDB_CODE_INVALID_PARA;
×
1842
    return NULL;
×
1843
  }
1844
  for (int i = 0; i < numLines; i++){
1,555✔
1845
    if (lines[i] == NULL){
1,155!
1846
      terrno = TSDB_CODE_INVALID_PARA;
×
1847
      return NULL;
×
1848
    }
1849
  }
1850
  return taos_schemaless_insert_inner(taos, lines, NULL, NULL, numLines, protocol, precision, ttl, reqid, tbnameKey);
400✔
1851
}
1852

1853
TAOS_RES *taos_schemaless_insert_ttl_with_reqid(TAOS *taos, char *lines[], int numLines, int protocol, int precision,
400✔
1854
                                                int32_t ttl, int64_t reqid) {
1855
  return taos_schemaless_insert_ttl_with_reqid_tbname_key(taos, lines, numLines, protocol, precision, ttl, reqid, NULL);
400✔
1856
}
1857

1858
TAOS_RES *taos_schemaless_insert(TAOS *taos, char *lines[], int numLines, int protocol, int precision) {
388✔
1859
  return taos_schemaless_insert_ttl_with_reqid(taos, lines, numLines, protocol, precision, TSDB_DEFAULT_TABLE_TTL, 0);
388✔
1860
}
1861

1862
TAOS_RES *taos_schemaless_insert_ttl(TAOS *taos, char *lines[], int numLines, int protocol, int precision,
12✔
1863
                                     int32_t ttl) {
1864
  return taos_schemaless_insert_ttl_with_reqid(taos, lines, numLines, protocol, precision, ttl, 0);
12✔
1865
}
1866

1867
TAOS_RES *taos_schemaless_insert_with_reqid(TAOS *taos, char *lines[], int numLines, int protocol, int precision,
×
1868
                                            int64_t reqid) {
1869
  return taos_schemaless_insert_ttl_with_reqid(taos, lines, numLines, protocol, precision, TSDB_DEFAULT_TABLE_TTL,
×
1870
                                               reqid);
1871
}
1872

1873
static int32_t getRawLineLen(char *lines, int len, int protocol) {
18✔
1874
  int numLines = 0;
18✔
1875
  char *tmp = lines;
18✔
1876
  for (int i = 0; i < len; i++) {
242,766✔
1877
    if (lines[i] == '\n' || i == len - 1) {
242,748✔
1878
      if (!IS_COMMENT(protocol, tmp[0])) {  // ignore comment
30!
1879
        numLines++;
27✔
1880
      }
1881
      tmp = lines + i + 1;
30✔
1882
    }
1883
  }
1884
  return numLines;
18✔
1885
}
1886

1887
TAOS_RES *taos_schemaless_insert_raw_ttl_with_reqid_tbname_key(TAOS *taos, char *lines, int len, int32_t *totalRows,
18✔
1888
                                                               int protocol, int precision, int32_t ttl, int64_t reqid,
1889
                                                               char *tbnameKey) {
1890
  if (taos == NULL || lines == NULL || len < 0) {
18!
1891
    terrno = TSDB_CODE_INVALID_PARA;
×
1892
    return NULL;
×
1893
  }
1894
  int numLines = getRawLineLen(lines, len, protocol);
18✔
1895
  if (totalRows != NULL){
18!
1896
    *totalRows = numLines;
18✔
1897
  }
1898
  return taos_schemaless_insert_inner(taos, NULL, lines, lines + len, numLines, protocol, precision, ttl, reqid,
18✔
1899
                                      tbnameKey);
1900
}
1901

1902
TAOS_RES *taos_schemaless_insert_raw_ttl_with_reqid(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
18✔
1903
                                                    int precision, int32_t ttl, int64_t reqid) {
1904
  return taos_schemaless_insert_raw_ttl_with_reqid_tbname_key(taos, lines, len, totalRows, protocol, precision, ttl,
18✔
1905
                                                              reqid, NULL);
1906
}
1907

1908
TAOS_RES *taos_schemaless_insert_raw_with_reqid(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
×
1909
                                                int precision, int64_t reqid) {
1910
  return taos_schemaless_insert_raw_ttl_with_reqid(taos, lines, len, totalRows, protocol, precision,
×
1911
                                                   TSDB_DEFAULT_TABLE_TTL, reqid);
1912
}
1913
TAOS_RES *taos_schemaless_insert_raw_ttl(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
×
1914
                                         int precision, int32_t ttl) {
1915
  return taos_schemaless_insert_raw_ttl_with_reqid(taos, lines, len, totalRows, protocol, precision, ttl, 0);
×
1916
}
1917

1918
TAOS_RES *taos_schemaless_insert_raw(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
18✔
1919
                                     int precision) {
1920
  return taos_schemaless_insert_raw_ttl_with_reqid(taos, lines, len, totalRows, protocol, precision,
18✔
1921
                                                   TSDB_DEFAULT_TABLE_TTL, 0);
1922
}
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