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

taosdata / TDengine / #4720

08 Sep 2025 08:43AM UTC coverage: 58.139% (-0.6%) from 58.762%
#4720

push

travis-ci

web-flow
Merge pull request #32881 from taosdata/enh/add-new-windows-ci

fix(ci): update workflow reference to use new Windows CI YAML

133181 of 292179 branches covered (45.58%)

Branch coverage included in aggregate %.

201691 of 283811 relevant lines covered (71.07%)

5442780.71 hits per line

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

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

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

21
#include "clientSml.h"
22

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

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

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

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

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

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

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

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

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

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

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

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

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

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

125
  int32_t      code = TSDB_CODE_SUCCESS;
4,376✔
126
  SUserAuthRes authRes = {0};
4,376✔
127

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

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

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

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

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

167
  return convertTimePrecision(tsInt64, fromPrecision, toPrecision);
7,799✔
168
}
169

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

176
  tag->sTableName = measure;
4,383✔
177
  tag->sTableNameLen = measureLen;
4,383✔
178

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

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

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

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

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

219
  int   measureLen = currElement->measureLen;
1,264✔
220
  char *measure = (char *)taosMemoryMalloc(measureLen);
1,264!
221
  SML_CHECK_NULL(measure);
1,265!
222
  (void)memcpy(measure, currElement->measure, measureLen);
1,265✔
223
  if (currElement->measureEscaped) {
1,265✔
224
    PROCESS_SLASH_IN_MEASUREMENT(measure, measureLen);
108!
225
  }
226
  smlStrReplace(measure, measureLen);
1,265✔
227
  code = smlGetMeta(info, measure, measureLen, &pTableMeta);
1,265✔
228
  taosMemoryFree(measure);
1,266!
229
  if (code != TSDB_CODE_SUCCESS) {
1,266✔
230
    info->dataFormat = false;
418✔
231
    info->reRun = true;
418✔
232
    goto END;
418✔
233
  }
234
  SML_CHECK_CODE(smlBuildSTableMeta(info->dataFormat, sMeta));
848!
235
  for (int i = 0; i < pTableMeta->tableInfo.numOfTags + pTableMeta->tableInfo.numOfColumns; i++) {
18,314✔
236
    SSchema *col = pTableMeta->schema + i;
17,466✔
237
    SSmlKv   kv = {.key = col->name, .keyLen = strlen(col->name), .type = col->type};
17,466✔
238
    if (col->type == TSDB_DATA_TYPE_NCHAR) {
17,466✔
239
      kv.length = (col->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
2,902✔
240
    } else if (col->type == TSDB_DATA_TYPE_BINARY || col->type == TSDB_DATA_TYPE_GEOMETRY ||
14,564✔
241
               col->type == TSDB_DATA_TYPE_VARBINARY) {
8,603✔
242
      kv.length = col->bytes - VARSTR_HEADER_SIZE;
5,984✔
243
    } else {
244
      kv.length = col->bytes;
8,580✔
245
    }
246

247
    if (i < pTableMeta->tableInfo.numOfColumns) {
17,466✔
248
      SML_CHECK_NULL(taosArrayPush((*sMeta)->cols, &kv));
25,186!
249
    } else {
250
      SML_CHECK_NULL(taosArrayPush((*sMeta)->tags, &kv));
9,746!
251
    }
252
  }
253
  SML_CHECK_CODE(taosHashPut(info->superTables, currElement->measure, currElement->measureLen, sMeta, POINTER_BYTES));
848!
254
  (*sMeta)->tableMeta = pTableMeta;
848✔
255
  return code;
848✔
256

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

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

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

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

314
  if (unlikely(kv->length > maxKV->length)) {
4,720✔
315
    maxKV->length = kv->length;
13✔
316
    info->needModifySchema = true;
13✔
317
  }
318
  return true;
4,720✔
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) {
653✔
327
  elements->measureTag = (char *)taosMemoryMalloc(elements->measureLen + elements->tagsLen);
653!
328
  if (elements->measureTag == NULL) {
653!
329
    return terrno;
×
330
  }
331
  (void)memcpy(elements->measureTag, elements->measure, elements->measureLen);
653✔
332
  (void)memcpy(elements->measureTag + elements->measureLen, elements->tags, elements->tagsLen);
653✔
333
  elements->measureTagsLen = elements->measureLen + elements->tagsLen;
653✔
334
  return TSDB_CODE_SUCCESS;
653✔
335
}
336

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

344
  return false;
1,372✔
345
}
346

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

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

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

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

386
    tinfo->tags = taosArrayDup(info->preLineTagKV, NULL);
4,386✔
387
    SML_CHECK_NULL(tinfo->tags);
4,386!
388
    for (size_t i = 0; i < taosArrayGetSize(info->preLineTagKV); i++) {
24,302✔
389
      SSmlKv *kv = (SSmlKv *)taosArrayGet(info->preLineTagKV, i);
19,916✔
390
      SML_CHECK_NULL(kv);
19,915!
391
      if (kv->keyEscaped) kv->key = NULL;
19,916✔
392
      if (kv->valueEscaped) kv->value = NULL;
19,916✔
393
    }
394

395
    SML_CHECK_CODE(smlSetCTableName(tinfo, info->tbnameKey));
4,382!
396
    SML_CHECK_CODE(getTableUid(info, elements, tinfo));
4,385!
397
    if (info->dataFormat) {
4,386✔
398
      info->currSTableMeta->uid = tinfo->uid;
764✔
399
      SML_CHECK_CODE(smlInitTableDataCtx(info->pQuery, info->currSTableMeta, &tinfo->tableDataCtx));
764!
400
    }
401
  } else {
402
    tinfo = *oneTable;
192✔
403
  }
404
  if (info->dataFormat) info->currTableDataCtx = tinfo->tableDataCtx;
4,578✔
405
  return TSDB_CODE_SUCCESS;
4,578✔
406

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

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

422
END:
3,756✔
423
  clearColValArraySml(info->currTableDataCtx->pValues);
3,756✔
424
  RETURN
3,753!
425
}
426

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

438
END:
1,400✔
439
  RETURN
1,400!
440
}
441

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

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

463
  return TSDB_CODE_SUCCESS;
5,576✔
464
}
465

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

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

518
END:
1,200✔
519
  RETURN
1,266!
520
}
521

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

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

544
    SML_CHECK_CODE(buildChildTableName(&rName));
4,319!
545
  }
546

547
END:
66✔
548
  taosArrayDestroy(dst);
4,385✔
549
  RETURN
4,386!
550
}
551

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

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

584
  meta->tags = taosArrayInit(32, sizeof(SSmlKv));
1,728✔
585
  SML_CHECK_NULL(meta->tags);
1,728!
586

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

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

598
int32_t smlParseNumber(SSmlKv *kvVal, SSmlMsgBuf *msg) {
90,091,665✔
599
  const char *pVal = kvVal->value;
90,091,665✔
600
  int32_t     len = kvVal->length;
90,091,665✔
601
  char       *endptr = NULL;
90,091,665✔
602
  double      result = taosStr2Double(pVal, &endptr);
90,091,665✔
603
  if (pVal == endptr) {
90,091,780✔
604
    RETURN_FALSE
33✔
605
  }
606

607
  int32_t left = len - (endptr - pVal);
90,091,750✔
608
  if (left == 0) {
90,091,750✔
609
    SET_DOUBLE
1,698✔
610
  } else if (left == 3) {
90,090,052✔
611
    if (endptr[0] == 'f' || endptr[0] == 'F') {
60,088,267!
612
      if (endptr[1] == '6' && endptr[2] == '4') {
30,082,350!
613
        SET_DOUBLE
30,001,183✔
614
      } else if (endptr[1] == '3' && endptr[2] == '2') {
81,167!
615
        SET_FLOAT
81,172!
616
      } else {
617
        RETURN_FALSE
3✔
618
      }
619
    } else if (endptr[0] == 'i' || endptr[0] == 'I') {
30,005,917!
620
      if (endptr[1] == '6' && endptr[2] == '4') {
3,780!
621
        SET_BIGINT
1,219✔
622
      } else if (endptr[1] == '3' && endptr[2] == '2') {
2,561!
623
        SET_INT
1,407!
624
      } else if (endptr[1] == '1' && endptr[2] == '6') {
1,154!
625
        SET_SMALL_INT
1,154!
626
      } else {
627
        RETURN_FALSE
3✔
628
      }
629
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
30,002,137!
630
      if (endptr[1] == '6' && endptr[2] == '4') {
30,002,134!
631
        SET_UBIGINT
1,150✔
632
      } else if (endptr[1] == '3' && endptr[2] == '2') {
30,000,984!
633
        SET_UINT
30,000,493✔
634
      } else if (endptr[1] == '1' && endptr[2] == '6') {
491!
635
        SET_USMALL_INT
493✔
636
      } else {
637
        RETURN_FALSE
×
638
      }
639
    } else {
640
      RETURN_FALSE
3✔
641
    }
642
  } else if (left == 2) {
30,001,785✔
643
    if (endptr[0] == 'i' || endptr[0] == 'I') {
30,001,694!
644
      if (endptr[1] == '8') {
1,186!
645
        SET_TINYINT
1,186!
646
      } else {
647
        RETURN_FALSE
×
648
      }
649
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
30,000,508!
650
      if (endptr[1] == '8') {
30,000,493!
651
        SET_UTINYINT
30,000,493✔
652
      } else {
653
        RETURN_FALSE
×
654
      }
655
    } else {
656
      RETURN_FALSE
15✔
657
    }
658
  } else if (left == 1) {
91✔
659
    if (endptr[0] == 'i' || endptr[0] == 'I') {
39!
660
      SET_BIGINT
36✔
661
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
3!
662
      SET_UBIGINT
×
663
    } else {
664
      RETURN_FALSE
3✔
665
    }
666
  } else {
667
    RETURN_FALSE;
54✔
668
  }
669
  return true;
90,091,639✔
670
}
671

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

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

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

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

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

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

698
  return id;
1,281✔
699
}
700

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

713
    if (((colField[*index].type == TSDB_DATA_TYPE_VARCHAR || colField[*index].type == TSDB_DATA_TYPE_VARBINARY ||
14,770!
714
          colField[*index].type == TSDB_DATA_TYPE_GEOMETRY) &&
8,978✔
715
         (colField[*index].bytes - VARSTR_HEADER_SIZE) < kv->length) ||
5,798✔
716
        (colField[*index].type == TSDB_DATA_TYPE_NCHAR &&
14,758✔
717
         ((colField[*index].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE < kv->length))) {
2,299✔
718
      if (isTag) {
80✔
719
        *action = SCHEMA_ACTION_CHANGE_TAG_SIZE;
68✔
720
      } else {
721
        *action = SCHEMA_ACTION_CHANGE_COLUMN_SIZE;
12✔
722
      }
723
    }
724
  } else {
725
    if (isTag) {
4,461✔
726
      *action = SCHEMA_ACTION_ADD_TAG;
1,981✔
727
    } else {
728
      *action = SCHEMA_ACTION_ADD_COLUMN;
2,480✔
729
    }
730
  }
731
  return TSDB_CODE_SUCCESS;
19,231✔
732
}
733

734
#define BOUNDARY 1024
735
static int32_t smlFindNearestPowerOf2(int32_t length, uint8_t type) {
2,047✔
736
  int32_t result = 1;
2,047✔
737
  if (length >= BOUNDARY) {
2,047✔
738
    result = length;
18✔
739
  } else {
740
    while (result <= length) {
9,847✔
741
      result <<= 1;
7,818✔
742
    }
743
  }
744

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

752
  if (type == TSDB_DATA_TYPE_NCHAR) {
2,047✔
753
    result = result * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
1,327✔
754
  } else if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_GEOMETRY) {
720!
755
    result = result + VARSTR_HEADER_SIZE;
729✔
756
  }
757
  return result;
2,047✔
758
}
759

760
static int32_t smlProcessSchemaAction(SSmlHandle *info, SSchema *schemaField, SHashObj *schemaHash, SArray *cols,
1,080✔
761
                                      SHashObj *schemaHashCheck, ESchemaAction *action, bool isTag) {
762
  int32_t code = TSDB_CODE_SUCCESS;
1,080✔
763
  int32_t lino = 0;
1,080✔
764
  for (int j = 0; j < taosArrayGetSize(cols); ++j) {
15,982✔
765
    if (j == 0 && !isTag) continue;
14,926✔
766
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, j);
14,395✔
767
    SML_CHECK_NULL(kv);
14,395!
768
    SML_CHECK_CODE(smlGenerateSchemaAction(schemaField, schemaHash, kv, isTag, action, info));
14,395✔
769
    if (taosHashGet(schemaHashCheck, kv->key, kv->keyLen) != NULL) {
14,383✔
770
      uError("SML:0x%" PRIx64 ", %s duplicated column %s", info->id, __FUNCTION__, kv->key);
12!
771
      SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
12!
772
    }
773
  }
774

775
END:
1,055✔
776
  RETURN
1,079!
777
}
778

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

802
END:
1,046✔
803
  taosHashCleanup(hashTmp);
1,048✔
804
  RETURN
1,048!
805
}
806

807
static int32_t getBytes(uint8_t type, int32_t length) {
4,405✔
808
  if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_NCHAR ||
4,405✔
809
      type == TSDB_DATA_TYPE_GEOMETRY) {
810
    return smlFindNearestPowerOf2(length, type);
2,038✔
811
  } else {
812
    return tDataTypes[type].bytes;
2,367✔
813
  }
814
}
815

816
static int32_t smlBuildFieldsList(SSmlHandle *info, SSchema *schemaField, SHashObj *schemaHash, SArray *cols,
902✔
817
                                  SArray *results, int32_t numOfCols, bool isTag) {
818
  int32_t code = TSDB_CODE_SUCCESS;
902✔
819
  int32_t lino = 0;
902✔
820
  for (int j = 0; j < taosArrayGetSize(cols); ++j) {
5,780✔
821
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, j);
4,851✔
822
    SML_CHECK_NULL(kv);
4,852!
823
    ESchemaAction action = SCHEMA_ACTION_NULL;
4,852✔
824
    SML_CHECK_CODE(smlGenerateSchemaAction(schemaField, schemaHash, kv, isTag, &action, info));
4,852!
825
    if (action == SCHEMA_ACTION_ADD_COLUMN || action == SCHEMA_ACTION_ADD_TAG) {
9,249✔
826
      SField field = {0};
4,367✔
827
      field.type = kv->type;
4,367✔
828
      field.bytes = getBytes(kv->type, kv->length);
4,367✔
829
      (void)memcpy(field.name, kv->key, TMIN(kv->keyLen, sizeof(field.name) - 1));
4,371✔
830
      SML_CHECK_NULL(taosArrayPush(results, &field));
4,405!
831
    } else if (action == SCHEMA_ACTION_CHANGE_COLUMN_SIZE || action == SCHEMA_ACTION_CHANGE_TAG_SIZE) {
477✔
832
      uint16_t *index = (uint16_t *)taosHashGet(schemaHash, kv->key, kv->keyLen);
44✔
833
      if (index == NULL) {
40!
834
        SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
835
      }
836
      uint16_t newIndex = *index;
40✔
837
      if (isTag) newIndex -= numOfCols;
40✔
838
      SField *field = (SField *)taosArrayGet(results, newIndex);
40✔
839
      SML_CHECK_NULL(field);
40!
840
      field->bytes = getBytes(kv->type, kv->length);
40✔
841
    }
842
  }
843

844
  int32_t maxLen = isTag ? TSDB_MAX_TAGS_LEN : TSDB_MAX_BYTES_PER_ROW;
903✔
845
  int32_t len = 0;
903✔
846
  for (int j = 0; j < taosArrayGetSize(results); ++j) {
5,629✔
847
    SField *field = taosArrayGet(results, j);
4,715✔
848
    SML_CHECK_NULL(field);
4,726!
849
    len += field->bytes;
4,726✔
850
  }
851
  if (len > maxLen) {
897✔
852
    return isTag ? TSDB_CODE_PAR_INVALID_TAGS_LENGTH : TSDB_CODE_PAR_INVALID_ROW_LENGTH;
6!
853
  }
854

855
END:
891✔
856
  RETURN
891!
857
}
858

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

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

881
  pReq.pColumns = taosArrayInit(pReq.numOfColumns, sizeof(SFieldWithOptions));
482✔
882
  SML_CHECK_NULL(pReq.pColumns);
483!
883
  for (int32_t i = 0; i < pReq.numOfColumns; ++i) {
3,271✔
884
    SField *pField = taosArrayGet(pColumns, i);
2,792✔
885
    SML_CHECK_NULL(pField);
2,787!
886
    SFieldWithOptions fieldWithOption = {0};
2,787✔
887
    setFieldWithOptions(&fieldWithOption, pField);
2,787✔
888
    setDefaultOptionsForField(&fieldWithOption);
2,793✔
889
    SML_CHECK_NULL(taosArrayPush(pReq.pColumns, &fieldWithOption));
5,525!
890
  }
891

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

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

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

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

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

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

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

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

949
  if (pRequest->code == TSDB_CODE_SUCCESS) {
483✔
950
    SML_CHECK_CODE(catalogRemoveTableMeta(info->pCatalog, pName));
375!
951
  }
952
  code = pRequest->code;
483✔
953

954
END:
483✔
955
  destroyRequest(pRequest);
483✔
956
  tFreeSMCreateStbReq(&pReq);
483✔
957
  RETURN
483!
958
}
959

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

978
  SML_CHECK_CODE(catalogGetSTableMeta(info->pCatalog, conn, pName, pTableMeta));
302!
979

980
END:
302✔
981
  taosArrayDestroy(pColumns);
422✔
982
  taosArrayDestroy(pTags);
422✔
983
  RETURN
422!
984
}
985

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

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

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

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

1032
END:
531✔
1033
  taosArrayDestroy(pColumns);
549✔
1034
  taosArrayDestroy(pTags);
549✔
1035
  RETURN
549!
1036
}
1037

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

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

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

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

1063
END:
525✔
1064
  taosArrayDestroy(pColumns);
531✔
1065
  taosArrayDestroy(pTags);
531✔
1066
  RETURN
531!
1067
}
1068

1069
static int32_t smlBuildTempHash(SHashObj *hashTmp, STableMeta *pTableMeta, uint16_t start, uint16_t end) {
1,098✔
1070
  int32_t code = 0;
1,098✔
1071
  int32_t lino = 0;
1,098✔
1072
  for (uint16_t i = start; i < end; i++) {
16,072✔
1073
    SML_CHECK_CODE(
14,973!
1074
        taosHashPut(hashTmp, pTableMeta->schema[i].name, strlen(pTableMeta->schema[i].name), &i, SHORT_BYTES));
1075
  }
1076

1077
END:
1,099✔
1078
  return code;
1,099✔
1079
}
1080

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

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

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

1102
  SSmlSTableMeta **tmp = (SSmlSTableMeta **)taosHashIterate(info->superTables, NULL);
911✔
1103
  while (tmp) {
1,734✔
1104
    SSmlSTableMeta *sTableData = *tmp;
970✔
1105
    bool            needCheckMeta = false;  // for multi thread
970✔
1106

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

1121
    code = catalogGetSTableMeta(info->pCatalog, &conn, &pName, &pTableMeta);
971✔
1122

1123
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_MND_STB_NOT_EXIST) {
971!
1124
      SML_CHECK_CODE(smlCreateTable(info, &conn, sTableData, &pName, &pTableMeta));
422✔
1125
    } else if (code == TSDB_CODE_SUCCESS) {
549!
1126
      if (smlIsPKTable(pTableMeta)) {
549!
1127
        SML_CHECK_CODE(TSDB_CODE_SML_NOT_SUPPORT_PK);
×
1128
      }
1129

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

1140
      SML_CHECK_CODE(smlModifyTag(info, colHashTmp, tagHashTmp, &conn, sTableData, &pName, &pTableMeta));
549✔
1141
      SML_CHECK_CODE(smlModifyCols(info, tagHashTmp, colHashTmp, &conn, sTableData, &pName, &pTableMeta));
531✔
1142

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

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

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

1168
  return TSDB_CODE_SUCCESS;
764✔
1169

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

1179
  return code;
146✔
1180
}
1181

1182
static int32_t smlInsertMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, SHashObj *checkDuplicate) {
1,754✔
1183
  int32_t code = 0;
1,754✔
1184
  int32_t lino = 0;
1,754✔
1185
  terrno = 0;
1,754✔
1186
  for (int16_t i = 0; i < taosArrayGetSize(cols); ++i) {
19,368✔
1187
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
17,637✔
1188
    SML_CHECK_NULL(kv);
17,657!
1189
    int ret = taosHashPut(metaHash, kv->key, kv->keyLen, &i, SHORT_BYTES);
17,639✔
1190
    if (ret == 0) {
17,647✔
1191
      SML_CHECK_NULL(taosArrayPush(metaArray, kv));
17,638!
1192
      if (taosHashGet(checkDuplicate, kv->key, kv->keyLen) != NULL) {
17,638✔
1193
        uError("%s duplicated column %s", __FUNCTION__, kv->key);
18!
1194
        SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
18!
1195
      }
1196
    } else if (terrno == TSDB_CODE_DUP_KEY) {
6!
1197
      uError("%s duplicated column %s", __FUNCTION__, kv->key);
6!
1198
      return TSDB_CODE_PAR_DUPLICATED_COLUMN;
6✔
1199
    }
1200
  }
1201

1202
END:
1,748✔
1203
  RETURN
1,748!
1204
}
1205

1206
static int32_t smlUpdateMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, bool isTag, SSmlMsgBuf *msg,
12,122✔
1207
                             SHashObj *checkDuplicate) {
1208
  int32_t code = 0;
12,122✔
1209
  int32_t lino = 0;
12,122✔
1210
  for (int i = 0; i < taosArrayGetSize(cols); ++i) {
210,475✔
1211
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
198,069✔
1212
    SML_CHECK_NULL(kv);
197,911!
1213
    int16_t *index = (int16_t *)taosHashGet(metaHash, kv->key, kv->keyLen);
197,911✔
1214
    if (index) {
198,272✔
1215
      SSmlKv *value = (SSmlKv *)taosArrayGet(metaArray, *index);
198,198✔
1216
      SML_CHECK_NULL(value);
198,047!
1217

1218
      if (isTag) {
198,269✔
1219
        if (kv->length > value->length) {
31,079✔
1220
          value->length = kv->length;
150✔
1221
        }
1222
        continue;
31,079✔
1223
      }
1224
      if (kv->type != value->type) {
167,190!
1225
        smlBuildInvalidDataMsg(msg, "the type is not the same like before", kv->key);
×
1226
        SML_CHECK_CODE(TSDB_CODE_SML_NOT_SAME_TYPE);
×
1227
      }
1228

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

1248
END:
12,021✔
1249
  RETURN
12,021!
1250
}
1251

1252
void smlDestroyTableInfo(void *para) {
4,385✔
1253
  SSmlTableInfo *tag = *(SSmlTableInfo **)para;
4,385✔
1254
  for (size_t i = 0; i < taosArrayGetSize(tag->cols); i++) {
11,333✔
1255
    SHashObj *kvHash = (SHashObj *)taosArrayGetP(tag->cols, i);
6,947✔
1256
    taosHashCleanup(kvHash);
6,947✔
1257
  }
1258

1259
  taosArrayDestroy(tag->cols);
4,385✔
1260
  taosArrayDestroyEx(tag->tags, freeSSmlKv);
4,386✔
1261
  taosMemoryFree(tag);
4,386!
1262
}
4,386✔
1263

1264
void freeSSmlKv(void *data) {
222,298✔
1265
  SSmlKv *kv = (SSmlKv *)data;
222,298✔
1266
  if (kv->keyEscaped) taosMemoryFreeClear(kv->key);
222,298!
1267
  if (kv->valueEscaped) taosMemoryFreeClear(kv->value);
222,298!
1268
#ifdef USE_GEOS
1269
  if (kv->type == TSDB_DATA_TYPE_GEOMETRY) geosFreeBuffer((void *)(kv->value));
222,298✔
1270
#endif
1271
  if (kv->type == TSDB_DATA_TYPE_VARBINARY) taosMemoryFreeClear(kv->value);
222,298!
1272
}
222,298✔
1273

1274
void smlDestroyInfo(SSmlHandle *info) {
2,560✔
1275
  if (info == NULL) return;
2,560✔
1276

1277
  taosHashCleanup(info->pVgHash);
1,281✔
1278
  taosHashCleanup(info->childTables);
1,281✔
1279
  taosHashCleanup(info->superTables);
1,281✔
1280
  taosHashCleanup(info->tableUids);
1,281✔
1281

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

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

1294
  taosArrayDestroyEx(info->preLineTagKV, freeSSmlKv);
1,281✔
1295
  taosArrayDestroyP(info->escapedStringList, NULL);
1,281✔
1296

1297
  if (!info->dataFormat) {
1,281✔
1298
    for (int i = 0; i < info->lineNum; i++) {
7,861✔
1299
      taosArrayDestroyEx(info->lines[i].colArray, freeSSmlKv);
6,990✔
1300
      if (info->lines[i].measureTagsLen != 0 && info->protocol != TSDB_SML_LINE_PROTOCOL) {
6,990✔
1301
        taosMemoryFree(info->lines[i].measureTag);
258!
1302
      }
1303
    }
1304
    taosMemoryFree(info->lines);
871!
1305
  }
1306
  if (info->protocol == TSDB_SML_JSON_PROTOCOL) {
1,281✔
1307
    taosMemoryFreeClear(info->preLine.tags);
355!
1308
  }
1309
  cJSON_Delete(info->root);
1,281✔
1310
  taosMemoryFreeClear(info);
1,281!
1311
}
1312

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

1324
  info->pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
1,281✔
1325
  SML_CHECK_NULL(info->pVgHash);
1,281!
1326
  info->childTables = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,281✔
1327
  SML_CHECK_NULL(info->childTables);
1,281!
1328
  info->tableUids = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,281✔
1329
  SML_CHECK_NULL(info->tableUids);
1,281!
1330
  info->superTables = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,281✔
1331
  SML_CHECK_NULL(info->superTables);
1,281!
1332
  taosHashSetFreeFp(info->superTables, smlDestroySTableMeta);
1,281✔
1333
  taosHashSetFreeFp(info->childTables, smlDestroyTableInfo);
1,281✔
1334

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

1347
  *handle = info;
1,273✔
1348
  info = NULL;
1,273✔
1349

1350
END:
1,273✔
1351
  smlDestroyInfo(info);
1,273✔
1352
  RETURN
1,278!
1353
}
1354

1355
static int32_t smlPushCols(SArray *colsArray, SArray *cols) {
6,957✔
1356
  int32_t   code = TSDB_CODE_SUCCESS;
6,957✔
1357
  int32_t   lino = 0;
6,957✔
1358
  SHashObj *kvHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
6,957✔
1359
  SML_CHECK_NULL(kvHash);
6,957!
1360
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
188,787✔
1361
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
181,635✔
1362
    SML_CHECK_NULL(kv);
181,664!
1363
    terrno = 0;
181,652✔
1364
    code = taosHashPut(kvHash, kv->key, kv->keyLen, &kv, POINTER_BYTES);
181,506✔
1365
    if (terrno == TSDB_CODE_DUP_KEY) {
181,948✔
1366
      uError("%s duplicated column %s", __FUNCTION__, kv->key);
12!
1367
      SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
52✔
1368
    }
1369
    SML_CHECK_CODE(code);
181,830!
1370
  }
1371

1372
  SML_CHECK_NULL(taosArrayPush(colsArray, &kvHash));
6,946!
1373
  return code;
6,946✔
1374
END:
12✔
1375
  taosHashCleanup(kvHash);
12✔
1376
  RETURN
12!
1377
}
1378

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

1385
  for (int32_t i = 0; i < info->lineNum; i++) {
7,752✔
1386
    SSmlLineInfo  *elements = info->lines + i;
6,956✔
1387
    SSmlTableInfo *tinfo = NULL;
6,956✔
1388
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
6,956✔
1389
      SSmlTableInfo **tmp =
1390
          (SSmlTableInfo **)taosHashGet(info->childTables, elements->measure, elements->measureTagsLen);
5,560✔
1391
      if (tmp) tinfo = *tmp;
5,558!
1392
    } else {
1393
      SSmlTableInfo **tmp = (SSmlTableInfo **)taosHashGet(info->childTables, elements->measureTag,
1,396✔
1394
                                                          elements->measureLen + elements->tagsLen);
1,396✔
1395
      if (tmp) tinfo = *tmp;
1,396!
1396
    }
1397

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

1404
    if (taosArrayGetSize(tinfo->tags) > TSDB_MAX_TAGS) {
6,954!
1405
      smlBuildInvalidDataMsg(&info->msgBuf, "too many tags than 128", NULL);
×
1406
      return TSDB_CODE_PAR_INVALID_TAGS_NUM;
×
1407
    }
1408

1409
    if (taosArrayGetSize(elements->colArray) + taosArrayGetSize(tinfo->tags) > TSDB_MAX_COLUMNS) {
6,956!
1410
      smlBuildInvalidDataMsg(&info->msgBuf, "too many columns than 4096", NULL);
×
1411
      return TSDB_CODE_PAR_TOO_MANY_COLUMNS;
×
1412
    }
1413

1414
    SML_CHECK_CODE(smlPushCols(tinfo->cols, elements->colArray));
6,957✔
1415

1416
    SSmlSTableMeta **tableMeta =
1417
        (SSmlSTableMeta **)taosHashGet(info->superTables, elements->measure, elements->measureLen);
6,946✔
1418
    if (tableMeta) {  // update meta
6,946✔
1419
      uDebug("SML:0x%" PRIx64 ", %s update meta, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
6,066✔
1420
             info->lineNum);
1421
      SML_CHECK_CODE(smlUpdateMeta((*tableMeta)->colHash, (*tableMeta)->cols, elements->colArray, false, &info->msgBuf,
6,066!
1422
                                   (*tableMeta)->tagHash));
1423
      SML_CHECK_CODE(smlUpdateMeta((*tableMeta)->tagHash, (*tableMeta)->tags, tinfo->tags, true, &info->msgBuf,
6,063!
1424
                                   (*tableMeta)->colHash));
1425
    } else {
1426
      uDebug("SML:0x%" PRIx64 ", %s add meta, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
880✔
1427
             info->lineNum);
1428
      SSmlSTableMeta *meta = NULL;
880✔
1429
      SML_CHECK_CODE(smlBuildSTableMeta(info->dataFormat, &meta));
904!
1430
      code = taosHashPut(info->superTables, elements->measure, elements->measureLen, &meta, POINTER_BYTES);
880✔
1431
      if (code != TSDB_CODE_SUCCESS) {
880!
1432
        smlDestroySTableMeta(&meta);
×
1433
        SML_CHECK_CODE(code);
×
1434
      }
1435
      SML_CHECK_CODE(smlInsertMeta(meta->tagHash, meta->tags, tinfo->tags, NULL));
880✔
1436
      SML_CHECK_CODE(smlInsertMeta(meta->colHash, meta->cols, elements->colArray, meta->tagHash));
874✔
1437
    }
1438
  }
1439
  uDebug("SML:0x%" PRIx64 ", %s end, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat, info->lineNum);
796✔
1440

1441
END:
460✔
1442
  RETURN
832!
1443
}
1444

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

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

1462
  oneTable = (SSmlTableInfo **)taosHashIterate(info->childTables, NULL);
1,144✔
1463
  while (oneTable) {
5,015✔
1464
    SSmlTableInfo *tableData = *oneTable;
3,883✔
1465

1466
    int measureLen = tableData->sTableNameLen;
3,883✔
1467
    measure = (char *)taosMemoryMalloc(tableData->sTableNameLen);
3,883!
1468
    SML_CHECK_NULL(measure);
3,894!
1469
    (void)memcpy(measure, tableData->sTableName, tableData->sTableNameLen);
3,882✔
1470
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
3,882✔
1471
      PROCESS_SLASH_IN_MEASUREMENT(measure, measureLen);
30,811✔
1472
    }
1473
    smlStrReplace(measure, measureLen);
3,882✔
1474
    (void)memcpy(pName.tname, measure, measureLen);
3,886✔
1475
    pName.tname[measureLen] = '\0';
3,886✔
1476

1477
    if (info->pRequest->tableList == NULL) {
3,886✔
1478
      info->pRequest->tableList = taosArrayInit(1, sizeof(SName));
1,144✔
1479
      SML_CHECK_NULL(info->pRequest->tableList);
1,144!
1480
    }
1481
    SML_CHECK_NULL(taosArrayPush(info->pRequest->tableList, &pName));
7,771!
1482
    tstrncpy(pName.tname, tableData->childTableName, sizeof(pName.tname));
3,885✔
1483

1484
    SRequestConnInfo conn = {0};
3,885✔
1485
    conn.pTrans = info->taos->pAppInfo->pTransporter;
3,885✔
1486
    conn.requestId = info->pRequest->requestId;
3,885✔
1487
    conn.requestObjRefId = info->pRequest->self;
3,885✔
1488
    conn.mgmtEps = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
3,885✔
1489

1490
    SML_CHECK_CODE(smlCheckAuth(info, &conn, pName.tname, AUTH_TYPE_WRITE));
3,886✔
1491

1492
    SVgroupInfo vg = {0};
3,869✔
1493
    SML_CHECK_CODE(catalogGetTableHashVgroup(info->pCatalog, &conn, &pName, &vg));
3,869!
1494
    SML_CHECK_CODE(taosHashPut(info->pVgHash, (const char *)&vg.vgId, sizeof(vg.vgId), (char *)&vg, sizeof(vg)));
3,874!
1495

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

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

1509
    SML_CHECK_CODE(smlBindData(info->pQuery, info->dataFormat, tableData->tags, (*pMeta)->cols, tableData->cols,
3,874!
1510
                               (*pMeta)->tableMeta, tableData->childTableName, measure, measureLen, info->ttl,
1511
                               info->msgBuf.buf, info->msgBuf.len, info->taos->optionInfo.charsetCxt));
1512
    taosMemoryFreeClear(measure);
3,871!
1513
    oneTable = (SSmlTableInfo **)taosHashIterate(info->childTables, oneTable);
3,870✔
1514
  }
1515

1516
  SML_CHECK_CODE(smlBuildOutput(info->pQuery, info->pVgHash));
1,132!
1517
  info->cost.insertRpcTime = taosGetTimestampUs();
1,132✔
1518

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

1522
  launchQueryImpl(info->pRequest, info->pQuery, true, NULL);  // no need to check return code
1,132✔
1523

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

1527
  return info->pRequest->code;
1,132✔
1528

1529
END:
12✔
1530
  taosMemoryFree(measure);
12!
1531
  taosHashCancelIterate(info->childTables, oneTable);
12✔
1532
  RETURN
12!
1533
}
1534

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

1547
int32_t smlClearForRerun(SSmlHandle *info) {
859✔
1548
  int32_t code = 0;
859✔
1549
  int32_t lino = 0;
859✔
1550
  info->reRun = false;
859✔
1551

1552
  taosHashClear(info->childTables);
859✔
1553
  taosHashClear(info->superTables);
859✔
1554
  taosHashClear(info->tableUids);
859✔
1555

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

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

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

1574
END:
859✔
1575
  RETURN
859!
1576
}
1577

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

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

1612
  if (*rawLine != NULL && (uDebugFlag & DEBUG_DEBUG)) {
8,499!
1613
    printRaw(info->id, i, numLines, DEBUG_DEBUG, *tmp, *len);
108✔
1614
  } else {
1615
    uDebug("SML:0x%" PRIx64 ", smlParseLine is not raw, line %d/%d :%s", info->id, i, numLines, *tmp);
8,391✔
1616
  }
1617
  return true;
8,500✔
1618
}
1619

1620
static int32_t smlParseJson(SSmlHandle *info, char *lines[], char *rawLine) {
353✔
1621
  int32_t code = TSDB_CODE_SUCCESS;
353✔
1622
  if (lines) {
353!
1623
    code = smlParseJSONExt(info, *lines);
353✔
1624
  } else if (rawLine) {
×
1625
    code = smlParseJSONExt(info, rawLine);
×
1626
  }
1627
  if (code != TSDB_CODE_SUCCESS) {
355✔
1628
    uError("%s failed code:%d", __FUNCTION__, code);
6!
1629
  }
1630
  return code;
355✔
1631
}
1632

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

1640
  char   *oldRaw = rawLine;
909✔
1641
  int32_t i = 0;
909✔
1642
  while (i < numLines) {
9,373✔
1643
    char *tmp = NULL;
8,505✔
1644
    int   len = 0;
8,505✔
1645
    if (!getLine(info, lines, &rawLine, rawLineEnd, numLines, i, &tmp, &len)) {
8,505✔
1646
      continue;
735✔
1647
    }
1648
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
8,500✔
1649
      if (info->dataFormat) {
6,248✔
1650
        SSmlLineInfo element = {0};
664✔
1651
        code = smlParseInfluxString(info, tmp, tmp + len, &element);
664✔
1652
      } else {
1653
        code = smlParseInfluxString(info, tmp, tmp + len, info->lines + i);
5,584✔
1654
      }
1655
    } else if (info->protocol == TSDB_SML_TELNET_PROTOCOL) {
2,252!
1656
      if (info->dataFormat) {
2,252✔
1657
        SSmlLineInfo element = {0};
1,669✔
1658
        code = smlParseTelnetString(info, (char *)tmp, (char *)tmp + len, &element);
1,669✔
1659
        if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
1,668!
1660
      } else {
1661
        code = smlParseTelnetString(info, (char *)tmp, (char *)tmp + len, info->lines + i);
583✔
1662
      }
1663
    }
1664
    if (code != TSDB_CODE_SUCCESS) {
8,500✔
1665
      if (rawLine != NULL) {
42!
1666
        printRaw(info->id, i, numLines, DEBUG_ERROR, tmp, len);
×
1667
      } else {
1668
        uError("SML:0x%" PRIx64 ", %s failed. line %d :%s", info->id, __FUNCTION__, i, tmp);
42!
1669
      }
1670
      return code;
42✔
1671
    }
1672
    if (info->reRun) {
8,458✔
1673
      uDebug("SML:0x%" PRIx64 ", %s re run", info->id, __FUNCTION__);
729✔
1674
      i = 0;
729✔
1675
      rawLine = oldRaw;
729✔
1676
      code = smlClearForRerun(info);
729✔
1677
      if (code != TSDB_CODE_SUCCESS) {
729!
1678
        return code;
×
1679
      }
1680
      continue;
729✔
1681
    }
1682
    i++;
7,729✔
1683
  }
1684
  uDebug("SML:0x%" PRIx64 ", %s end", info->id, __FUNCTION__);
868✔
1685

1686
  return code;
869✔
1687
}
1688

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

1694
  info->cost.parseTime = taosGetTimestampUs();
1,265✔
1695

1696
  SML_CHECK_CODE(smlParseStart(info, lines, rawLine, rawLineEnd, numLines));
1,265✔
1697
  SML_CHECK_CODE(smlParseEnd(info));
1,218✔
1698

1699
  info->cost.lineNum = info->lineNum;
1,182✔
1700
  info->cost.numOfSTables = taosHashGetSize(info->superTables);
1,182✔
1701
  info->cost.numOfCTables = taosHashGetSize(info->childTables);
1,182✔
1702
  info->cost.schemaTime = taosGetTimestampUs();
1,182✔
1703

1704
  do {
1705
    code = smlModifyDBSchemas(info);
1,290✔
1706
    if (code != TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER && code != TSDB_CODE_SDB_OBJ_CREATING && code != TSDB_CODE_SYN_NOT_LEADER &&
1,289!
1707
        code != TSDB_CODE_MND_TRANS_CONFLICT) {
1708
      break;
1,181✔
1709
    }
1710
    taosMsleep(100);
108✔
1711
    uInfo("SML:0x%" PRIx64 ", smlModifyDBSchemas retry code:%s, times:%d", info->id, tstrerror(code), retryNum);
108!
1712
  } while (retryNum++ < taosHashGetSize(info->superTables) * MAX_RETRY_TIMES);
108!
1713

1714
  SML_CHECK_CODE(code);
1,181✔
1715
  info->cost.insertBindTime = taosGetTimestampUs();
1,144✔
1716
  SML_CHECK_CODE(smlInsertData(info));
1,144✔
1717

1718
END:
1,132✔
1719
  RETURN
1,266!
1720
}
1721

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

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

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

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

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

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

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

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

1783
    smlSetReqSQL(request, lines, rawLine, rawLineEnd);
1,264✔
1784

1785
    if (request->pDb == NULL) {
1,264!
1786
      request->code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1787
      smlBuildInvalidDataMsg(&msg, "Database not specified", NULL);
×
1788
      goto END;
×
1789
    }
1790

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

1797
    if (protocol == TSDB_SML_LINE_PROTOCOL &&
1,264!
1798
        (precision < TSDB_SML_TIMESTAMP_NOT_CONFIGURED || precision > TSDB_SML_TIMESTAMP_NANO_SECONDS)) {
664!
1799
      request->code = TSDB_CODE_SML_INVALID_PRECISION_TYPE;
×
1800
      smlBuildInvalidDataMsg(&msg, "precision invalidate for line protocol", NULL);
×
1801
      goto END;
×
1802
    }
1803

1804
    if (protocol == TSDB_SML_JSON_PROTOCOL) {
1,264✔
1805
      numLines = 1;
354✔
1806
    } else if (numLines <= 0) {
910!
1807
      request->code = TSDB_CODE_SML_INVALID_DATA;
×
1808
      smlBuildInvalidDataMsg(&msg, "line num is invalid", NULL);
×
1809
      goto END;
×
1810
    }
1811

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

1839
END:
1,264✔
1840
  smlDestroyInfo(info);
1,264✔
1841
  return (TAOS_RES *)request;
1,264✔
1842
}
1843

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

1863
TAOS_RES *taos_schemaless_insert_ttl_with_reqid_tbname_key(TAOS *taos, char *lines[], int numLines, int protocol,
1,222✔
1864
                                                           int precision, int32_t ttl, int64_t reqid, char *tbnameKey) {
1865
  if (taos == NULL || lines == NULL || numLines < 0) {
1,222!
1866
    terrno = TSDB_CODE_INVALID_PARA;
×
1867
    return NULL;
×
1868
  }
1869
  for (int i = 0; i < numLines; i++) {
9,000✔
1870
    if (lines[i] == NULL) {
7,778!
1871
      terrno = TSDB_CODE_INVALID_PARA;
×
1872
      return NULL;
×
1873
    }
1874
  }
1875
  return taos_schemaless_insert_inner(taos, lines, NULL, NULL, numLines, protocol, precision, ttl, reqid, tbnameKey);
1,222✔
1876
}
1877

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

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

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

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

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

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

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

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

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

© 2025 Coveralls, Inc