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

taosdata / TDengine / #3543

29 Nov 2024 02:58AM UTC coverage: 60.842% (+0.02%) from 60.819%
#3543

push

travis-ci

web-flow
Merge pull request #28973 from taosdata/merge/mainto3.0

merge: from main to 3.0

120460 of 253224 branches covered (47.57%)

Branch coverage included in aggregate %.

706 of 908 new or added lines in 18 files covered. (77.75%)

2401 existing lines in 137 files now uncovered.

201633 of 276172 relevant lines covered (73.01%)

19045673.23 hits per line

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

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

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

21
#include "clientSml.h"
22

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

168
  return convertTimePrecision(tsInt64, fromPrecision, toPrecision);
2,187✔
169
}
170

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

344
  return false;
563✔
345
}
346

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

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

369
  if (smlIsPKTable(sMeta->tableMeta)) {
361✔
370
    return TSDB_CODE_SML_NOT_SUPPORT_PK;
27✔
371
  }
372
  return 0;
335✔
373
}
374

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

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

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

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

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

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

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

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

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

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

458
  return TSDB_CODE_SUCCESS;
1,864✔
459
}
460

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

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

512
END:
1,160✔
513
  RETURN
1,230!
514
}
515

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

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

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

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

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

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

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

581
  meta->cols = taosArrayInit(32, sizeof(SSmlKv));
1,373✔
582
  SML_CHECK_NULL(meta->cols);
1,374✔
583
  *sMeta = meta;
1,373✔
584
  return TSDB_CODE_SUCCESS;
1,373✔
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) {
12,451✔
593
  const char *pVal = kvVal->value;
12,451✔
594
  int32_t     len = kvVal->length;
12,451✔
595
  char       *endptr = NULL;
12,451✔
596
  double      result = taosStr2Double(pVal, &endptr);
12,451✔
597
  if (pVal == endptr) {
12,464✔
598
    RETURN_FALSE
3✔
599
  }
600

601
  int32_t left = len - (endptr - pVal);
12,461✔
602
  if (left == 0) {
12,461✔
603
    SET_DOUBLE
1,672✔
604
  } else if (left == 3) {
10,789✔
605
    if (endptr[0] == 'f' || endptr[0] == 'F') {
8,942✔
606
      if (endptr[1] == '6' && endptr[2] == '4') {
2,592!
607
        SET_DOUBLE
1,317✔
608
      } else if (endptr[1] == '3' && endptr[2] == '2') {
1,275!
609
        SET_FLOAT
1,281✔
610
      } else {
611
        RETURN_FALSE
×
612
      }
613
    } else if (endptr[0] == 'i' || endptr[0] == 'I') {
6,350✔
614
      if (endptr[1] == '6' && endptr[2] == '4') {
4,127!
615
        SET_BIGINT
1,336✔
616
      } else if (endptr[1] == '3' && endptr[2] == '2') {
2,791!
617
        SET_INT
1,510✔
618
      } else if (endptr[1] == '1' && endptr[2] == '6') {
1,281!
619
        SET_SMALL_INT
1,283✔
620
      } else {
621
        RETURN_FALSE
×
622
      }
623
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
2,223!
624
      if (endptr[1] == '6' && endptr[2] == '4') {
2,223!
625
        SET_UBIGINT
1,263!
626
      } else if (endptr[1] == '3' && endptr[2] == '2') {
960!
627
        SET_UINT
480!
628
      } else if (endptr[1] == '1' && endptr[2] == '6') {
480!
629
        SET_USMALL_INT
480!
630
      } else {
631
        RETURN_FALSE
×
632
      }
633
    } else {
634
      RETURN_FALSE
×
635
    }
636
  } else if (left == 2) {
1,847✔
637
    if (endptr[0] == 'i' || endptr[0] == 'I') {
1,807✔
638
      if (endptr[1] == '8') {
1,327!
639
        SET_TINYINT
1,327✔
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) {
40✔
653
    if (endptr[0] == 'i' || endptr[0] == 'I') {
32!
654
      SET_BIGINT
32!
655
    } else if (endptr[0] == 'u' || endptr[0] == 'U') {
×
656
      SET_UBIGINT
×
657
    } else {
658
      RETURN_FALSE
×
659
    }
660
  } else {
661
    RETURN_FALSE;
8✔
662
  }
663
  return true;
12,441✔
664
}
665

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

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

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

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

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

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

692
  return id;
1,302✔
693
}
694

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

706
    if (((colField[*index].type == TSDB_DATA_TYPE_VARCHAR || colField[*index].type == TSDB_DATA_TYPE_VARBINARY ||
2,533✔
707
          colField[*index].type == TSDB_DATA_TYPE_GEOMETRY) &&
2,257!
708
         (colField[*index].bytes - VARSTR_HEADER_SIZE) < kv->length) ||
276✔
709
        (colField[*index].type == TSDB_DATA_TYPE_NCHAR &&
2,517✔
710
         ((colField[*index].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE < kv->length))) {
1,168✔
711
      if (isTag) {
42✔
712
        *action = SCHEMA_ACTION_CHANGE_TAG_SIZE;
26✔
713
      } else {
714
        *action = SCHEMA_ACTION_CHANGE_COLUMN_SIZE;
16✔
715
      }
716
    }
717
  } else {
718
    if (isTag) {
13,535✔
719
      *action = SCHEMA_ACTION_ADD_TAG;
5,764✔
720
    } else {
721
      *action = SCHEMA_ACTION_ADD_COLUMN;
7,771✔
722
    }
723
  }
724
  return TSDB_CODE_SUCCESS;
16,068✔
725
}
726

727
#define BOUNDARY 1024
728
static int32_t smlFindNearestPowerOf2(int32_t length, uint8_t type) {
4,267✔
729
  int32_t result = 1;
4,267✔
730
  if (length >= BOUNDARY) {
4,267✔
731
    result = length;
33✔
732
  } else {
733
    while (result <= length) {
19,497✔
734
      result <<= 1;
15,263✔
735
    }
736
  }
737

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

745
  if (type == TSDB_DATA_TYPE_NCHAR) {
4,267✔
746
    result = result * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
3,418✔
747
  } else if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_GEOMETRY) {
849✔
748
    result = result + VARSTR_HEADER_SIZE;
848✔
749
  }
750
  return result;
4,267✔
751
}
752

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

764
  for (int j = 0; j < taosArrayGetSize(checkDumplicateCols); ++j) {
2,901✔
765
    SSmlKv *kv = (SSmlKv *)taosArrayGet(checkDumplicateCols, j);
2,491✔
766
    SML_CHECK_NULL(kv);
2,492!
767
    if (taosHashGet(schemaHash, kv->key, kv->keyLen) != NULL) {
2,492✔
768
      SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
13!
769
    }
770
  }
771
END:
409✔
772
  RETURN
436!
773
}
774

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

797
END:
392✔
798
  taosHashCleanup(hashTmp);
392✔
799
  RETURN
392!
800
}
801

802
static int32_t getBytes(uint8_t type, int32_t length) {
13,457✔
803
  if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY || type == TSDB_DATA_TYPE_NCHAR ||
13,457✔
804
      type == TSDB_DATA_TYPE_GEOMETRY) {
805
    return smlFindNearestPowerOf2(length, type);
4,267✔
806
  } else {
807
    return tDataTypes[type].bytes;
9,190✔
808
  }
809
}
810

811
static int32_t smlBuildFieldsList(SSmlHandle *info, SSchema *schemaField, SHashObj *schemaHash, SArray *cols,
1,632✔
812
                                  SArray *results, int32_t numOfCols, bool isTag) {
813
  int32_t code = TSDB_CODE_SUCCESS;
1,632✔
814
  int32_t lino = 0;
1,632✔
815
  for (int j = 0; j < taosArrayGetSize(cols); ++j) {
15,428✔
816
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, j);
13,795✔
817
    SML_CHECK_NULL(kv);
13,794!
818
    ESchemaAction action = SCHEMA_ACTION_NULL;
13,794✔
819
    SML_CHECK_CODE(smlGenerateSchemaAction(schemaField, schemaHash, kv, isTag, &action, info));
13,794!
820
    if (action == SCHEMA_ACTION_ADD_COLUMN || action == SCHEMA_ACTION_ADD_TAG) {
27,232✔
821
      SField field = {0};
13,436✔
822
      field.type = kv->type;
13,436✔
823
      field.bytes = getBytes(kv->type, kv->length);
13,436✔
824
      (void)memcpy(field.name, kv->key, TMIN(kv->keyLen, sizeof(field.name) - 1));
13,436✔
825
      SML_CHECK_NULL(taosArrayPush(results, &field));
13,437!
826
    } else if (action == SCHEMA_ACTION_CHANGE_COLUMN_SIZE || action == SCHEMA_ACTION_CHANGE_TAG_SIZE) {
359✔
827
      uint16_t *index = (uint16_t *)taosHashGet(schemaHash, kv->key, kv->keyLen);
21✔
828
      if (index == NULL) {
21!
UNCOV
829
        SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
830
      }
831
      uint16_t newIndex = *index;
21✔
832
      if (isTag) newIndex -= numOfCols;
21✔
833
      SField *field = (SField *)taosArrayGet(results, newIndex);
21✔
834
      SML_CHECK_NULL(field);
21!
835
      field->bytes = getBytes(kv->type, kv->length);
21✔
836
    }
837
  }
838

839
  int32_t maxLen = isTag ? TSDB_MAX_TAGS_LEN : TSDB_MAX_BYTES_PER_ROW;
1,633✔
840
  int32_t len = 0;
1,633✔
841
  for (int j = 0; j < taosArrayGetSize(results); ++j) {
15,448✔
842
    SField *field = taosArrayGet(results, j);
13,815✔
843
    SML_CHECK_NULL(field);
13,816✔
844
    len += field->bytes;
13,815✔
845
  }
846
  if (len > maxLen) {
1,633✔
847
    return isTag ? TSDB_CODE_PAR_INVALID_TAGS_LENGTH : TSDB_CODE_PAR_INVALID_ROW_LENGTH;
12✔
848
  }
849

850
END:
1,621✔
851
  RETURN
1,621!
852
}
853

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

869
  // put front for free
870
  pReq.numOfColumns = taosArrayGetSize(pColumns);
839✔
871
  pReq.pTags = *pTags;
839✔
872
  pReq.numOfTags = taosArrayGetSize(*pTags);
839✔
873
  *pTags = NULL;
839✔
874

875
  pReq.pColumns = taosArrayInit(pReq.numOfColumns, sizeof(SFieldWithOptions));
839✔
876
  SML_CHECK_NULL(pReq.pColumns);
839!
877
  for (int32_t i = 0; i < pReq.numOfColumns; ++i) {
8,918✔
878
    SField *pField = taosArrayGet(pColumns, i);
8,080✔
879
    SML_CHECK_NULL(pField);
8,080!
880
    SFieldWithOptions fieldWithOption = {0};
8,080✔
881
    setFieldWithOptions(&fieldWithOption, pField);
8,080✔
882
    setDefaultOptionsForField(&fieldWithOption);
8,080✔
883
    SML_CHECK_NULL(taosArrayPush(pReq.pColumns, &fieldWithOption));
16,158!
884
  }
885

886
  if (action == SCHEMA_ACTION_CREATE_STABLE) {
838✔
887
    pSql = "sml_create_stable";
775✔
888
    smlBuildCreateStbReq(&pReq, 1, 1, 0, TD_REQ_FROM_APP);
889
  } else if (action == SCHEMA_ACTION_ADD_TAG || action == SCHEMA_ACTION_CHANGE_TAG_SIZE) {
63✔
890
    pSql = (action == SCHEMA_ACTION_ADD_TAG) ? "sml_add_tag" : "sml_modify_tag_size";
30✔
891
    smlBuildCreateStbReq(&pReq, pTableMeta->sversion, pTableMeta->tversion + 1, pTableMeta->uid, TD_REQ_FROM_TAOX);
30✔
892
  } else if (action == SCHEMA_ACTION_ADD_COLUMN || action == SCHEMA_ACTION_CHANGE_COLUMN_SIZE) {
33!
893
    pSql = (action == SCHEMA_ACTION_ADD_COLUMN) ? "sml_add_column" : "sml_modify_column_size";
33✔
894
    smlBuildCreateStbReq(&pReq, pTableMeta->sversion + 1, pTableMeta->tversion, pTableMeta->uid, TD_REQ_FROM_TAOX);
33✔
895
  } else {
UNCOV
896
    SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
897
  }
898

899
  SML_CHECK_CODE(buildRequest(info->taos->id, pSql, strlen(pSql), NULL, false, &pRequest, 0));
838!
900

901
  pRequest->syncQuery = true;
839✔
902
  if (!pRequest->pDb) {
839!
UNCOV
903
    SML_CHECK_CODE(TSDB_CODE_PAR_DB_NOT_SPECIFIED);
×
904
  }
905

906
  if (pReq.numOfTags == 0) {
839✔
907
    pReq.numOfTags = 1;
6✔
908
    SField field = {0};
6✔
909
    field.type = TSDB_DATA_TYPE_NCHAR;
6✔
910
    field.bytes = TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
6✔
911
    tstrncpy(field.name, tsSmlTagName, sizeof(field.name));
6✔
912
    SML_CHECK_NULL(taosArrayPush(pReq.pTags, &field));
12!
913
  }
914

915
  pReq.commentLen = -1;
839✔
916
  pReq.igExists = true;
839✔
917
  SML_CHECK_CODE(tNameExtractFullName(pName, pReq.name));
839!
918

919
  pCmdMsg.epSet = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
839✔
920
  pCmdMsg.msgType = TDMT_MND_CREATE_STB;
839✔
921
  pCmdMsg.msgLen = tSerializeSMCreateStbReq(NULL, 0, &pReq);
839✔
922
  if (pCmdMsg.msgLen < 0) {
837!
UNCOV
923
    uError("failed to serialize create stable request1, code:%d, terrno:%d", pCmdMsg.msgLen, terrno);
×
UNCOV
924
    SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
925
  }
926
  pCmdMsg.pMsg = taosMemoryMalloc(pCmdMsg.msgLen);
837✔
927
  SML_CHECK_NULL(pCmdMsg.pMsg);
838!
928
  code = tSerializeSMCreateStbReq(pCmdMsg.pMsg, pCmdMsg.msgLen, &pReq);
838✔
929
  if (code < 0) {
838!
UNCOV
930
    taosMemoryFree(pCmdMsg.pMsg);
×
UNCOV
931
    uError("failed to serialize create stable request2, code:%d, terrno:%d", code, terrno);
×
UNCOV
932
    SML_CHECK_CODE(TSDB_CODE_SML_INVALID_DATA);
×
933
  }
934

935
  SQuery pQuery = {0};
838✔
936
  pQuery.execMode = QUERY_EXEC_MODE_RPC;
838✔
937
  pQuery.pCmdMsg = &pCmdMsg;
838✔
938
  pQuery.msgType = pQuery.pCmdMsg->msgType;
838✔
939
  pQuery.stableQuery = true;
838✔
940

941
  launchQueryImpl(pRequest, &pQuery, true, NULL);  // no need to check return value
838✔
942

943
  if (pRequest->code == TSDB_CODE_SUCCESS) {
839✔
944
    SML_CHECK_CODE(catalogRemoveTableMeta(info->pCatalog, pName));
803!
945
  }
946
  code = pRequest->code;
839✔
947

948
END:
839✔
949
  destroyRequest(pRequest);
839✔
950
  tFreeSMCreateStbReq(&pReq);
839✔
951
  RETURN
839!
952
}
953

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

972
  SML_CHECK_CODE(catalogGetSTableMeta(info->pCatalog, conn, pName, pTableMeta));
740!
973

974
END:
740✔
975
  taosArrayDestroy(pColumns);
788✔
976
  taosArrayDestroy(pTags);
788✔
977
  RETURN
788!
978
}
979

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

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

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

1020
    info->cost.numOfAlterTagSTables++;
30✔
1021
    taosMemoryFreeClear(*pTableMeta);
30!
1022
    SML_CHECK_CODE(catalogRefreshTableMeta(info->pCatalog, conn, pName, -1));
30!
1023
    SML_CHECK_CODE(catalogGetSTableMeta(info->pCatalog, conn, pName, pTableMeta));
30!
1024
  }
1025

1026
END:
207✔
1027
  taosArrayDestroy(pColumns);
229✔
1028
  taosArrayDestroy(pTags);
229✔
1029
  RETURN
229!
1030
}
1031

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

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

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

1051
    info->cost.numOfAlterColSTables++;
33✔
1052
    taosMemoryFreeClear(*pTableMeta);
33!
1053
    SML_CHECK_CODE(catalogRefreshTableMeta(info->pCatalog, conn, pName, -1));
33!
1054
    SML_CHECK_CODE(catalogGetSTableMeta(info->pCatalog, conn, pName, pTableMeta));
33!
1055
  }
1056

1057
END:
196✔
1058
  taosArrayDestroy(pColumns);
207✔
1059
  taosArrayDestroy(pTags);
207✔
1060
  RETURN
207!
1061
}
1062

1063
static int32_t smlBuildTempHash(SHashObj *hashTmp, STableMeta *pTableMeta, uint16_t start, uint16_t end){
436✔
1064
  int32_t code = 0;
436✔
1065
  int32_t lino = 0;
436✔
1066
  for (uint16_t i = start; i < end; i++) {
2,918✔
1067
    SML_CHECK_CODE(taosHashPut(hashTmp, pTableMeta->schema[i].name, strlen(pTableMeta->schema[i].name), &i, SHORT_BYTES));
2,482!
1068
  }
1069

1070
END:
436✔
1071
  return code;
436✔
1072
}
1073

1074
static int32_t smlModifyDBSchemas(SSmlHandle *info) {
1,089✔
1075
  uDebug("SML:0x%" PRIx64 " %s start, format:%d, needModifySchema:%d", info->id, __FUNCTION__, info->dataFormat,
1,089!
1076
         info->needModifySchema);
1077
  if (info->dataFormat && !info->needModifySchema) {
1,089!
1078
    return TSDB_CODE_SUCCESS;
128✔
1079
  }
1080
  int32_t     code = 0;
961✔
1081
  int32_t     lino = 0;
961✔
1082
  SHashObj   *hashTmp = NULL;
961✔
1083
  STableMeta *pTableMeta = NULL;
961✔
1084

1085
  SName pName = {TSDB_TABLE_NAME_T, info->taos->acctId, {0}, {0}};
961✔
1086
  tstrncpy(pName.dbname, info->pRequest->pDb, sizeof(pName.dbname));
961✔
1087

1088
  SRequestConnInfo conn = {0};
961✔
1089
  conn.pTrans = info->taos->pAppInfo->pTransporter;
961✔
1090
  conn.requestId = info->pRequest->requestId;
961✔
1091
  conn.requestObjRefId = info->pRequest->self;
961✔
1092
  conn.mgmtEps = getEpSet_s(&info->taos->pAppInfo->mgmtEp);
961✔
1093

1094
  SSmlSTableMeta **tmp = (SSmlSTableMeta **)taosHashIterate(info->superTables, NULL);
961✔
1095
  while (tmp) {
1,897✔
1096
    SSmlSTableMeta *sTableData = *tmp;
1,017✔
1097
    bool            needCheckMeta = false;  // for multi thread
1,017✔
1098

1099
    size_t superTableLen = 0;
1,017✔
1100
    void  *superTable = taosHashGetKey(tmp, &superTableLen);
1,017✔
1101
    char  *measure = taosMemoryMalloc(superTableLen);
1,017✔
1102
    SML_CHECK_NULL(measure);
1,098!
1103
    (void)memcpy(measure, superTable, superTableLen);
1,017✔
1104
    if (info->protocol == TSDB_SML_LINE_PROTOCOL){
1,017✔
1105
      PROCESS_SLASH_IN_MEASUREMENT(measure, superTableLen);
3,328✔
1106
    }
1107
    smlStrReplace(measure, superTableLen);
1,017✔
1108
    size_t nameLen = TMIN(superTableLen, TSDB_TABLE_NAME_LEN - 1);
1,017✔
1109
    (void)memcpy(pName.tname, measure, nameLen);
1,017✔
1110
    pName.tname[nameLen] = '\0';
1,017✔
1111
    taosMemoryFree(measure);
1,017✔
1112

1113
    code = catalogGetSTableMeta(info->pCatalog, &conn, &pName, &pTableMeta);
1,017✔
1114

1115
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_MND_STB_NOT_EXIST) {
1,017!
1116
      SML_CHECK_CODE(smlCreateTable(info, &conn, sTableData, &pName, &pTableMeta));
788✔
1117
    } else if (code == TSDB_CODE_SUCCESS) {
229!
1118
      if (smlIsPKTable(pTableMeta)) {
229!
UNCOV
1119
        SML_CHECK_CODE(TSDB_CODE_SML_NOT_SUPPORT_PK);
×
1120
      }
1121

1122
      hashTmp = taosHashInit(pTableMeta->tableInfo.numOfTags, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
229✔
1123
      SML_CHECK_NULL(hashTmp);
229!
1124
      SML_CHECK_CODE(smlBuildTempHash(hashTmp, pTableMeta, pTableMeta->tableInfo.numOfColumns, pTableMeta->tableInfo.numOfColumns + pTableMeta->tableInfo.numOfTags));
229!
1125
      SML_CHECK_CODE(smlModifyTag(info, hashTmp, &conn, sTableData, &pName, &pTableMeta));
229✔
1126
      taosHashClear(hashTmp);
207✔
1127
      SML_CHECK_CODE(smlBuildTempHash(hashTmp, pTableMeta, 0, pTableMeta->tableInfo.numOfColumns));
207!
1128
      SML_CHECK_CODE(smlModifyCols(info, hashTmp, &conn, sTableData, &pName, &pTableMeta));
207✔
1129

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

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

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

1152
  return TSDB_CODE_SUCCESS;
880✔
1153

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

1162
  return code;
81✔
1163
}
1164

1165
static int32_t smlInsertMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, SHashObj *checkDuplicate) {
2,012✔
1166
  int32_t code = 0;
2,012✔
1167
  int32_t lino = 0;
2,012✔
1168
  terrno = 0;
2,012✔
1169
  for (int16_t i = 0; i < taosArrayGetSize(cols); ++i) {
17,407✔
1170
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
15,422✔
1171
    SML_CHECK_NULL(kv);
15,444!
1172
    int ret = taosHashPut(metaHash, kv->key, kv->keyLen, &i, SHORT_BYTES);
15,425✔
1173
    if (ret == 0) {
15,431✔
1174
      SML_CHECK_NULL(taosArrayPush(metaArray, kv));
15,417!
1175
      if (taosHashGet(checkDuplicate, kv->key, kv->keyLen) != NULL) {
15,417✔
1176
        SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
19!
1177
      }
1178
    } else if (terrno == TSDB_CODE_DUP_KEY) {
12!
1179
      return TSDB_CODE_PAR_DUPLICATED_COLUMN;
12✔
1180
    }
1181
  }
1182

1183
END:
2,000✔
1184
  RETURN
2,000!
1185
}
1186

1187
static int32_t smlUpdateMeta(SHashObj *metaHash, SArray *metaArray, SArray *cols, bool isTag, SSmlMsgBuf *msg,
3,362✔
1188
                             SHashObj *checkDuplicate) {
1189
  int32_t code = 0;
3,362✔
1190
  int32_t lino = 0;
3,362✔
1191
  for (int i = 0; i < taosArrayGetSize(cols); ++i) {
33,943✔
1192
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
30,535✔
1193
    SML_CHECK_NULL(kv);
30,529!
1194
    int16_t *index = (int16_t *)taosHashGet(metaHash, kv->key, kv->keyLen);
30,529✔
1195
    if (index) {
30,563✔
1196
      SSmlKv *value = (SSmlKv *)taosArrayGet(metaArray, *index);
30,470✔
1197
      SML_CHECK_NULL(value);
30,464!
1198

1199
      if (isTag) {
30,488✔
1200
        if (kv->length > value->length) {
14,647✔
1201
          value->length = kv->length;
79✔
1202
        }
1203
        continue;
14,647✔
1204
      }
1205
      if (kv->type != value->type) {
15,841!
UNCOV
1206
        smlBuildInvalidDataMsg(msg, "the type is not the same like before", kv->key);
×
UNCOV
1207
        SML_CHECK_CODE(TSDB_CODE_SML_NOT_SAME_TYPE);
×
1208
      }
1209

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

1228
END:
3,345✔
1229
  RETURN
3,345!
1230
}
1231

1232
void smlDestroyTableInfo(void *para) {
2,104✔
1233
  SSmlTableInfo *tag = *(SSmlTableInfo **)para;
2,104✔
1234
  for (size_t i = 0; i < taosArrayGetSize(tag->cols); i++) {
4,799✔
1235
    SHashObj *kvHash = (SHashObj *)taosArrayGetP(tag->cols, i);
2,695✔
1236
    taosHashCleanup(kvHash);
2,695✔
1237
  }
1238

1239
  taosArrayDestroy(tag->cols);
2,104✔
1240
  taosArrayDestroyEx(tag->tags, freeSSmlKv);
2,104✔
1241
  taosMemoryFree(tag);
2,104✔
1242
}
2,104✔
1243

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

1252
void smlDestroyInfo(SSmlHandle *info) {
2,604✔
1253
  if (info == NULL) return;
2,604✔
1254

1255
  taosHashCleanup(info->pVgHash);
1,302✔
1256
  taosHashCleanup(info->childTables);
1,302✔
1257
  taosHashCleanup(info->superTables);
1,302✔
1258
  taosHashCleanup(info->tableUids);
1,302✔
1259

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

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

1272
  taosArrayDestroyEx(info->preLineTagKV, freeSSmlKv);
1,302✔
1273
  taosArrayDestroyP(info->escapedStringList, taosMemoryFree);
1,302✔
1274

1275
  if (!info->dataFormat) {
1,302✔
1276
    for (int i = 0; i < info->lineNum; i++) {
3,857✔
1277
      taosArrayDestroyEx(info->lines[i].colArray, freeSSmlKv);
2,802✔
1278
      if (info->lines[i].measureTagsLen != 0 && info->protocol != TSDB_SML_LINE_PROTOCOL) {
2,802✔
1279
        taosMemoryFree(info->lines[i].measureTag);
524✔
1280
      }
1281
    }
1282
    taosMemoryFree(info->lines);
1,055✔
1283
  }
1284
  if(info->protocol == TSDB_SML_JSON_PROTOCOL)  {
1,302✔
1285
    taosMemoryFreeClear(info->preLine.tags);
513✔
1286
  }
1287
  cJSON_Delete(info->root);
1,302✔
1288
  taosMemoryFreeClear(info);
1,302!
1289
}
1290

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

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

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

1325
  *handle = info;
1,302✔
1326
  info = NULL;
1,302✔
1327

1328
END:
1,302✔
1329
  smlDestroyInfo(info);
1,302✔
1330
  RETURN
1,300!
1331
}
1332

1333
static int32_t smlPushCols(SArray *colsArray, SArray *cols) {
2,708✔
1334
  int32_t code = TSDB_CODE_SUCCESS;
2,708✔
1335
  int32_t lino = 0;
2,708✔
1336
  SHashObj *kvHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
2,708✔
1337
  SML_CHECK_NULL(kvHash);
2,705!
1338
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
27,599✔
1339
    SSmlKv *kv = (SSmlKv *)taosArrayGet(cols, i);
24,885✔
1340
    SML_CHECK_NULL(kv);
24,891!
1341
    terrno = 0;
24,878✔
1342
    code = taosHashPut(kvHash, kv->key, kv->keyLen, &kv, POINTER_BYTES);
24,872✔
1343
    if (terrno == TSDB_CODE_DUP_KEY) {
24,911✔
1344
      SML_CHECK_CODE(TSDB_CODE_PAR_DUPLICATED_COLUMN);
13!
1345
    }
1346
    SML_CHECK_CODE(code);
24,894!
1347
  }
1348

1349
  SML_CHECK_NULL(taosArrayPush(colsArray, &kvHash));
2,694!
1350
  return code;
2,694✔
1351
END:
13✔
1352
  taosHashCleanup(kvHash);
13✔
1353
  RETURN
13!
1354
}
1355

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

1363
  for (int32_t i = 0; i < info->lineNum; i++) {
3,636✔
1364
    SSmlLineInfo  *elements = info->lines + i;
2,711✔
1365
    SSmlTableInfo *tinfo = NULL;
2,711✔
1366
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
2,711✔
1367
      SSmlTableInfo **tmp =
1368
          (SSmlTableInfo **)taosHashGet(info->childTables, elements->measure, elements->measureTagsLen);
1,862✔
1369
      if (tmp) tinfo = *tmp;
1,864!
1370
    } else {
1371
      SSmlTableInfo **tmp = (SSmlTableInfo **)taosHashGet(info->childTables, elements->measureTag,
849✔
1372
                                                          elements->measureLen + elements->tagsLen);
849✔
1373
      if (tmp) tinfo = *tmp;
849!
1374
    }
1375

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

1382
    if (taosArrayGetSize(tinfo->tags) > TSDB_MAX_TAGS) {
2,713✔
1383
      smlBuildInvalidDataMsg(&info->msgBuf, "too many tags than 128", NULL);
4✔
1384
      return TSDB_CODE_PAR_INVALID_TAGS_NUM;
4✔
1385
    }
1386

1387
    if (taosArrayGetSize(elements->colArray) + taosArrayGetSize(tinfo->tags) > TSDB_MAX_COLUMNS) {
2,709✔
1388
      smlBuildInvalidDataMsg(&info->msgBuf, "too many columns than 4096", NULL);
1✔
1389
      return TSDB_CODE_PAR_TOO_MANY_COLUMNS;
1✔
1390
    }
1391

1392
    SML_CHECK_CODE(smlPushCols(tinfo->cols, elements->colArray));
2,708✔
1393

1394
    SSmlSTableMeta **tableMeta =
1395
        (SSmlSTableMeta **)taosHashGet(info->superTables, elements->measure, elements->measureLen);
2,694✔
1396
    if (tableMeta) {  // update meta
2,694✔
1397
      uDebug("SML:0x%" PRIx64 " %s update meta, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
1,682!
1398
             info->lineNum);
1399
      SML_CHECK_CODE(smlUpdateMeta((*tableMeta)->colHash, (*tableMeta)->cols, elements->colArray, false, &info->msgBuf,
1,682!
1400
                          (*tableMeta)->tagHash));
1401
      SML_CHECK_CODE(smlUpdateMeta((*tableMeta)->tagHash, (*tableMeta)->tags, tinfo->tags, true, &info->msgBuf,
1,681!
1402
                          (*tableMeta)->colHash));
1403
    } else {
1404
      uDebug("SML:0x%" PRIx64 " %s add meta, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat,
1,012!
1405
             info->lineNum);
1406
      SSmlSTableMeta *meta = NULL;
1,012✔
1407
      SML_CHECK_CODE(smlBuildSTableMeta(info->dataFormat, &meta));
1,043!
1408
      code = taosHashPut(info->superTables, elements->measure, elements->measureLen, &meta, POINTER_BYTES);
1,012✔
1409
      if (code != TSDB_CODE_SUCCESS) {
1,012!
UNCOV
1410
        smlDestroySTableMeta(&meta);
×
UNCOV
1411
        SML_CHECK_CODE(code);
×
1412
      }
1413
      SML_CHECK_CODE(smlInsertMeta(meta->tagHash, meta->tags, tinfo->tags, NULL));
1,012✔
1414
      SML_CHECK_CODE(smlInsertMeta(meta->colHash, meta->cols, elements->colArray, meta->tagHash));
1,000✔
1415
    }
1416
  }
1417
  uDebug("SML:0x%" PRIx64 " %s end, format:%d, linenum:%d", info->id, __FUNCTION__, info->dataFormat, info->lineNum);
925!
1418

1419
END:
925✔
1420
  RETURN
969!
1421
}
1422

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

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

1440
  oneTable = (SSmlTableInfo **)taosHashIterate(info->childTables, NULL);
1,009✔
1441
  while (oneTable) {
2,827✔
1442
    SSmlTableInfo *tableData = *oneTable;
1,830✔
1443

1444
    int   measureLen = tableData->sTableNameLen;
1,830✔
1445
    measure = (char *)taosMemoryMalloc(tableData->sTableNameLen);
1,830✔
1446
    SML_CHECK_NULL(measure);
1,842!
1447
    (void)memcpy(measure, tableData->sTableName, tableData->sTableNameLen);
1,830✔
1448
    if (info->protocol == TSDB_SML_LINE_PROTOCOL){
1,830✔
1449
      PROCESS_SLASH_IN_MEASUREMENT(measure, measureLen);
8,652✔
1450
    }
1451
    smlStrReplace(measure, measureLen);
1,830✔
1452
    (void)memcpy(pName.tname, measure, measureLen);
1,830✔
1453
    pName.tname[measureLen] = '\0';
1,830✔
1454

1455
    if (info->pRequest->tableList == NULL) {
1,830✔
1456
      info->pRequest->tableList = taosArrayInit(1, sizeof(SName));
1,009✔
1457
      SML_CHECK_NULL(info->pRequest->tableList);
1,009!
1458
    }
1459
    SML_CHECK_NULL(taosArrayPush(info->pRequest->tableList, &pName));
3,660!
1460
    tstrncpy(pName.tname, tableData->childTableName, sizeof(pName.tname));
1,830✔
1461

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

1468
    SML_CHECK_CODE(smlCheckAuth(info, &conn, pName.tname, AUTH_TYPE_WRITE));
1,830✔
1469

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

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

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

1487
    SML_CHECK_CODE(smlBindData(info->pQuery, info->dataFormat, tableData->tags, (*pMeta)->cols, tableData->cols,
1,818!
1488
                       (*pMeta)->tableMeta, tableData->childTableName, measure, measureLen, info->ttl, info->msgBuf.buf,
1489
                       info->msgBuf.len));
1490
    taosMemoryFreeClear(measure);
1,818!
1491
    oneTable = (SSmlTableInfo **)taosHashIterate(info->childTables, oneTable);
1,818✔
1492
  }
1493

1494
  SML_CHECK_CODE(smlBuildOutput(info->pQuery, info->pVgHash));
997!
1495
  info->cost.insertRpcTime = taosGetTimestampUs();
996✔
1496

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

1500
  launchQueryImpl(info->pRequest, info->pQuery, true, NULL);  // no need to check return code
997✔
1501

1502
  uDebug("SML:0x%" PRIx64 " %s end, format:%d, code:%d,%s", info->id, __FUNCTION__, info->dataFormat, info->pRequest->code,
997!
1503
         tstrerror(info->pRequest->code));
1504

1505
  return info->pRequest->code;
997✔
1506

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

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

1525
int32_t smlClearForRerun(SSmlHandle *info) {
1,055✔
1526
  int32_t code = 0;
1,055✔
1527
  int32_t lino = 0;
1,055✔
1528
  info->reRun = false;
1,055✔
1529

1530
  taosHashClear(info->childTables);
1,055✔
1531
  taosHashClear(info->superTables);
1,055✔
1532
  taosHashClear(info->tableUids);
1,055✔
1533

1534
  if (!info->dataFormat) {
1,055!
1535
    info->lines = (SSmlLineInfo *)taosMemoryCalloc(info->lineNum, sizeof(SSmlLineInfo));
1,055✔
1536
    SML_CHECK_NULL(info->lines);
1,055!
1537
  }
1538

1539
  taosArrayClearP(info->escapedStringList, taosMemoryFree);
1,055✔
1540
  if(info->protocol == TSDB_SML_JSON_PROTOCOL)  {
1,055✔
1541
    taosMemoryFreeClear(info->preLine.tags);
348!
1542
  }
1543
  (void)memset(&info->preLine, 0, sizeof(SSmlLineInfo));
1,055✔
1544
  info->currSTableMeta = NULL;
1,055✔
1545
  info->currTableDataCtx = NULL;
1,055✔
1546

1547
  SVnodeModifyOpStmt *stmt = (SVnodeModifyOpStmt *)(info->pQuery->pRoot);
1,055✔
1548
  stmt->freeHashFunc(stmt->pTableBlockHashObj);
1,055✔
1549
  stmt->pTableBlockHashObj = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
1,055✔
1550
  SML_CHECK_NULL(stmt->pTableBlockHashObj);
1,055!
1551

1552
END:
1,055✔
1553
  RETURN
1,055!
1554
}
1555

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

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

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

1598

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

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

1619
  char   *oldRaw = rawLine;
787✔
1620
  int32_t i = 0;
787✔
1621
  while (i < numLines) {
3,549✔
1622
    char *tmp = NULL;
2,888✔
1623
    int   len = 0;
2,888✔
1624
    if (!getLine(info, lines, &rawLine, rawLineEnd, numLines, i, &tmp, &len)) {
2,888✔
1625
      continue;
713✔
1626
    }
1627
    if (info->protocol == TSDB_SML_LINE_PROTOCOL) {
2,882✔
1628
      if (info->dataFormat) {
2,476✔
1629
        SSmlLineInfo element = {0};
560✔
1630
        code = smlParseInfluxString(info, tmp, tmp + len, &element);
560✔
1631
      } else {
1632
        code = smlParseInfluxString(info, tmp, tmp + len, info->lines + i);
1,916✔
1633
      }
1634
    } else if (info->protocol == TSDB_SML_TELNET_PROTOCOL) {
406!
1635
      if (info->dataFormat) {
406✔
1636
        SSmlLineInfo element = {0};
228✔
1637
        code = smlParseTelnetString(info, (char *)tmp, (char *)tmp + len, &element);
228✔
1638
        if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
228✔
1639
      } else {
1640
        code = smlParseTelnetString(info, (char *)tmp, (char *)tmp + len, info->lines + i);
178✔
1641
      }
1642
    }
1643
    if (code != TSDB_CODE_SUCCESS) {
2,882✔
1644
      if (rawLine != NULL) {
126!
UNCOV
1645
        printRaw(info->id, i, numLines, DEBUG_ERROR, tmp, len);
×
1646
      } else {
1647
        uError("SML:0x%" PRIx64 " %s failed. line %d : %s", info->id, __FUNCTION__, i, tmp);
126!
1648
      }
1649
      return code;
126✔
1650
    }
1651
    if (info->reRun) {
2,756✔
1652
      uDebug("SML:0x%" PRIx64 " %s re run", info->id, __FUNCTION__);
707!
1653
      i = 0;
707✔
1654
      rawLine = oldRaw;
707✔
1655
      code = smlClearForRerun(info);
707✔
1656
      if (code != TSDB_CODE_SUCCESS) {
707!
UNCOV
1657
        return code;
×
1658
      }
1659
      continue;
707✔
1660
    }
1661
    i++;
2,049✔
1662
  }
1663
  uDebug("SML:0x%" PRIx64 " %s end", info->id, __FUNCTION__);
661!
1664

1665
  return code;
663✔
1666
}
1667

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

1673
  info->cost.parseTime = taosGetTimestampUs();
1,302✔
1674

1675
  SML_CHECK_CODE(smlParseStart(info, lines, rawLine, rawLineEnd, numLines));
1,302✔
1676
  SML_CHECK_CODE(smlParseEnd(info));
1,102✔
1677

1678
  info->cost.lineNum = info->lineNum;
1,053✔
1679
  info->cost.numOfSTables = taosHashGetSize(info->superTables);
1,053✔
1680
  info->cost.numOfCTables = taosHashGetSize(info->childTables);
1,053✔
1681
  info->cost.schemaTime = taosGetTimestampUs();
1,054✔
1682

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

1693
  SML_CHECK_CODE(code);
1,053✔
1694
  info->cost.insertBindTime = taosGetTimestampUs();
1,009✔
1695
  SML_CHECK_CODE(smlInsertData(info));
1,009✔
1696

1697
END:
997✔
1698
  RETURN
1,302!
1699
}
1700

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

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

1719
    if (NULL == p) {
×
1720
      return;
×
1721
    }
1722

1723
    rlen = TMIN(len, TSDB_MAX_ALLOWED_SQL_LEN);
×
1724
    rlen = TMAX(rlen, 0);
×
1725

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

UNCOV
1734
    request->sqlstr = sql;
×
UNCOV
1735
    request->sqlLen = rlen;
×
1736
  }
1737
}
1738

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

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

1762
    smlSetReqSQL(request, lines, rawLine, rawLineEnd);
1,301✔
1763

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

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

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

1783
    if (protocol == TSDB_SML_JSON_PROTOCOL) {
1,300✔
1784
      numLines = 1;
513✔
1785
    } else if (numLines <= 0) {
787!
UNCOV
1786
      request->code = TSDB_CODE_SML_INVALID_DATA;
×
UNCOV
1787
      smlBuildInvalidDataMsg(&msg, "line num is invalid", NULL);
×
UNCOV
1788
      goto END;
×
1789
    }
1790

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

1818
END:
1,302✔
1819
  smlDestroyInfo(info);
1,302✔
1820
  return (TAOS_RES *)request;
1,302✔
1821
}
1822

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

1842
TAOS_RES *taos_schemaless_insert_ttl_with_reqid_tbname_key(TAOS *taos, char *lines[], int numLines, int protocol,
1,265✔
1843
                                                           int precision, int32_t ttl, int64_t reqid, char *tbnameKey) {
1844
  if (taos == NULL || lines == NULL || numLines < 0) {
1,265!
UNCOV
1845
    terrno = TSDB_CODE_INVALID_PARA;
×
1846
    return NULL;
×
1847
  }
1848
  for (int i = 0; i < numLines; i++){
5,254✔
1849
    if (lines[i] == NULL){
3,989!
UNCOV
1850
      terrno = TSDB_CODE_INVALID_PARA;
×
UNCOV
1851
      return NULL;
×
1852
    }
1853
  }
1854
  return taos_schemaless_insert_inner(taos, lines, NULL, NULL, numLines, protocol, precision, ttl, reqid, tbnameKey);
1,265✔
1855
}
1856

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

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

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

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

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

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

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

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

1922
TAOS_RES *taos_schemaless_insert_raw(TAOS *taos, char *lines, int len, int32_t *totalRows, int protocol,
36✔
1923
                                     int precision) {
1924
  return taos_schemaless_insert_raw_ttl_with_reqid(taos, lines, len, totalRows, protocol, precision,
36✔
1925
                                                   TSDB_DEFAULT_TABLE_TTL, 0);
1926
}
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