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

taosdata / TDengine / #4874

04 Dec 2025 01:55AM UTC coverage: 64.623% (+0.07%) from 64.558%
#4874

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

865 of 2219 new or added lines in 36 files covered. (38.98%)

6317 existing lines in 143 files now uncovered.

159543 of 246882 relevant lines covered (64.62%)

106415537.4 hits per line

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

74.46
/source/client/src/clientRawBlockWrite.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 "cJSON.h"
17
#include "clientInt.h"
18
#include "parser.h"
19
#include "tcol.h"
20
#include "tcompression.h"
21
#include "tdatablock.h"
22
#include "tdef.h"
23
#include "tglobal.h"
24
#include "tmsgtype.h"
25

26
#define RAW_NULL_CHECK(c) \
27
  do {                    \
28
    if (c == NULL) {      \
29
      code = terrno;      \
30
      goto end;           \
31
    }                     \
32
  } while (0)
33

34
#define RAW_FALSE_CHECK(c)           \
35
  do {                               \
36
    if (!c) {                        \
37
      code = TSDB_CODE_INVALID_PARA; \
38
      goto end;                      \
39
    }                                \
40
  } while (0)
41

42
#define RAW_RETURN_CHECK(c) \
43
  do {                      \
44
    code = c;               \
45
    if (code != 0) {        \
46
      goto end;             \
47
    }                       \
48
  } while (0)
49

50
#define LOG_ID_TAG   "connId:0x%" PRIx64 ", QID:0x%" PRIx64
51
#define LOG_ID_VALUE *(int64_t*)taos, pRequest->requestId
52

53
#define TMQ_META_VERSION "1.0"
54

55
static bool tmqAddJsonObjectItem(cJSON* object, const char* string, cJSON* item) {
235,585✔
56
  bool ret = cJSON_AddItemToObject(object, string, item);
235,585✔
57
  if (!ret) {
235,585✔
58
    cJSON_Delete(item);
×
59
  }
60
  return ret;
235,585✔
61
}
62
static bool tmqAddJsonArrayItem(cJSON* array, cJSON* item) {
42,073✔
63
  bool ret = cJSON_AddItemToArray(array, item);
42,073✔
64
  if (!ret) {
42,073✔
65
    cJSON_Delete(item);
×
66
  }
67
  return ret;
42,073✔
68
}
69

70
static int32_t  tmqWriteBatchMetaDataImpl(TAOS* taos, void* meta, uint32_t metaLen);
71
static tb_uid_t processSuid(tb_uid_t suid, char* db) {
3,824✔
72
  if (db == NULL) {
3,824✔
73
    return suid;
×
74
  }
75
  return suid + MurmurHash3_32(db, strlen(db));
3,824✔
76
}
77
static void buildCreateTableJson(SSchemaWrapper* schemaRow, SSchemaWrapper* schemaTag, SExtSchema* pExtSchemas, char* name, int64_t id, int8_t t,
3,271✔
78
                                 SColCmprWrapper* pColCmprRow, cJSON** pJson) {
79
  if (schemaRow == NULL || name == NULL || pColCmprRow == NULL || pJson == NULL) {
3,271✔
80
    uError("invalid parameter, schemaRow:%p, name:%p, pColCmprRow:%p, pJson:%p", schemaRow, name, pColCmprRow, pJson);
×
81
    return;
×
82
  }
83
  int32_t code = TSDB_CODE_SUCCESS;
3,271✔
84
  int8_t  buildDefaultCompress = 0;
3,271✔
85
  if (pColCmprRow->nCols <= 0) {
3,271✔
86
    buildDefaultCompress = 1;
56✔
87
  }
88

89
  char*  string = NULL;
3,271✔
90
  cJSON* json = cJSON_CreateObject();
3,271✔
91
  RAW_NULL_CHECK(json);
3,271✔
92
  cJSON* type = cJSON_CreateString("create");
3,271✔
93
  RAW_NULL_CHECK(type);
3,271✔
94

95
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "type", type));
3,271✔
96
  cJSON* tableType = cJSON_CreateString(t == TSDB_NORMAL_TABLE ? "normal" : "super");
3,271✔
97
  RAW_NULL_CHECK(tableType);
3,271✔
98
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableType", tableType));
3,271✔
99
  cJSON* tableName = cJSON_CreateString(name);
3,271✔
100
  RAW_NULL_CHECK(tableName);
3,271✔
101
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableName", tableName));
3,271✔
102

103
  cJSON* columns = cJSON_CreateArray();
3,271✔
104
  RAW_NULL_CHECK(columns);
3,271✔
105
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "columns", columns));
3,271✔
106

107
  for (int i = 0; i < schemaRow->nCols; i++) {
23,151✔
108
    cJSON* column = cJSON_CreateObject();
19,880✔
109
    RAW_NULL_CHECK(column);
19,880✔
110
    RAW_FALSE_CHECK(tmqAddJsonArrayItem(columns, column));
19,880✔
111
    SSchema* s = schemaRow->pSchema + i;
19,880✔
112
    cJSON*   cname = cJSON_CreateString(s->name);
19,880✔
113
    RAW_NULL_CHECK(cname);
19,880✔
114
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(column, "name", cname));
19,880✔
115
    cJSON* ctype = cJSON_CreateNumber(s->type);
19,880✔
116
    RAW_NULL_CHECK(ctype);
19,880✔
117
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(column, "type", ctype));
19,880✔
118
    if (s->type == TSDB_DATA_TYPE_BINARY || s->type == TSDB_DATA_TYPE_VARBINARY || s->type == TSDB_DATA_TYPE_GEOMETRY) {
21,640✔
119
      int32_t length = s->bytes - VARSTR_HEADER_SIZE;
1,760✔
120
      cJSON*  cbytes = cJSON_CreateNumber(length);
1,760✔
121
      RAW_NULL_CHECK(cbytes);
1,760✔
122
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(column, "length", cbytes));
1,760✔
123
    } else if (s->type == TSDB_DATA_TYPE_NCHAR) {
18,120✔
124
      int32_t length = (s->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
279✔
125
      cJSON*  cbytes = cJSON_CreateNumber(length);
279✔
126
      RAW_NULL_CHECK(cbytes);
279✔
127
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(column, "length", cbytes));
279✔
128
    } else if (IS_STR_DATA_BLOB(s->type)) {
17,841✔
129
      int32_t length = s->bytes - BLOBSTR_HEADER_SIZE;
×
130
      cJSON*  cbytes = cJSON_CreateNumber(length);
×
131
      RAW_NULL_CHECK(cbytes);
×
132
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(column, "length", cbytes));
×
133
    } else if (s->type == TSDB_DATA_TYPE_DECIMAL || s->type == TSDB_DATA_TYPE_DECIMAL64) {
17,841✔
134
      int32_t length = pExtSchemas[i].typeMod;
150✔
135
      cJSON*  cbytes = cJSON_CreateNumber(length);
150✔
136
      RAW_NULL_CHECK(cbytes);
150✔
137
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(column, "length", cbytes));
150✔
138
    }
139
    cJSON* isPk = cJSON_CreateBool(s->flags & COL_IS_KEY);
19,880✔
140
    RAW_NULL_CHECK(isPk);
19,880✔
141
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(column, "isPrimarykey", isPk));
19,880✔
142

143
    if (pColCmprRow == NULL) {
19,880✔
144
      continue;
×
145
    }
146

147
    uint32_t alg = 0;
19,880✔
148
    if (buildDefaultCompress) {
19,880✔
149
      alg = createDefaultColCmprByType(s->type);
112✔
150
    } else {
151
      SColCmpr* pColCmpr = pColCmprRow->pColCmpr + i;
19,768✔
152
      alg = pColCmpr->alg;
19,768✔
153
    }
154
    const char* encode = columnEncodeStr(COMPRESS_L1_TYPE_U32(alg));
19,880✔
155
    RAW_NULL_CHECK(encode);
19,880✔
156
    const char* compress = columnCompressStr(COMPRESS_L2_TYPE_U32(alg));
19,880✔
157
    RAW_NULL_CHECK(compress);
19,880✔
158
    const char* level = columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(alg));
19,880✔
159
    RAW_NULL_CHECK(level);
19,880✔
160

161
    cJSON* encodeJson = cJSON_CreateString(encode);
19,880✔
162
    RAW_NULL_CHECK(encodeJson);
19,880✔
163
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(column, "encode", encodeJson));
19,880✔
164

165
    cJSON* compressJson = cJSON_CreateString(compress);
19,880✔
166
    RAW_NULL_CHECK(compressJson);
19,880✔
167
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(column, "compress", compressJson));
19,880✔
168

169
    cJSON* levelJson = cJSON_CreateString(level);
19,880✔
170
    RAW_NULL_CHECK(levelJson);
19,880✔
171
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(column, "level", levelJson));
19,880✔
172
  }
173

174
  cJSON* tags = cJSON_CreateArray();
3,271✔
175
  RAW_NULL_CHECK(tags);
3,271✔
176
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tags", tags));
3,271✔
177

178
  for (int i = 0; schemaTag && i < schemaTag->nCols; i++) {
9,308✔
179
    cJSON* tag = cJSON_CreateObject();
6,037✔
180
    RAW_NULL_CHECK(tag);
6,037✔
181
    RAW_FALSE_CHECK(tmqAddJsonArrayItem(tags, tag));
6,037✔
182
    SSchema* s = schemaTag->pSchema + i;
6,037✔
183
    cJSON*   tname = cJSON_CreateString(s->name);
6,037✔
184
    RAW_NULL_CHECK(tname);
6,037✔
185
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "name", tname));
6,037✔
186
    cJSON* ttype = cJSON_CreateNumber(s->type);
6,037✔
187
    RAW_NULL_CHECK(ttype);
6,037✔
188
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "type", ttype));
6,037✔
189
    if (s->type == TSDB_DATA_TYPE_BINARY || s->type == TSDB_DATA_TYPE_VARBINARY || s->type == TSDB_DATA_TYPE_GEOMETRY) {
6,473✔
190
      int32_t length = s->bytes - VARSTR_HEADER_SIZE;
436✔
191
      cJSON*  cbytes = cJSON_CreateNumber(length);
436✔
192
      RAW_NULL_CHECK(cbytes);
436✔
193
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "length", cbytes));
436✔
194
    } else if (s->type == TSDB_DATA_TYPE_NCHAR) {
5,601✔
195
      int32_t length = (s->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
1,708✔
196
      cJSON*  cbytes = cJSON_CreateNumber(length);
1,708✔
197
      RAW_NULL_CHECK(cbytes);
1,708✔
198
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "length", cbytes));
1,708✔
199
    } else if (IS_STR_DATA_BLOB(s->type)) {
3,893✔
200
      int32_t length = s->bytes - BLOBSTR_HEADER_SIZE;
×
201
      cJSON*  cbytes = cJSON_CreateNumber(length);
×
202
      RAW_NULL_CHECK(cbytes);
×
203
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "length", cbytes));
×
204
    }
205
  }
206

207
end:
3,271✔
208
  *pJson = json;
3,271✔
209
}
210

211
static int32_t setCompressOption(cJSON* json, uint32_t para) {
×
212
  if (json == NULL) {
×
213
    return TSDB_CODE_INVALID_PARA;
×
214
  }
215
  uint8_t encode = COMPRESS_L1_TYPE_U32(para);
×
216
  int32_t code = 0;
×
217
  if (encode != 0) {
×
218
    const char* encodeStr = columnEncodeStr(encode);
×
219
    RAW_NULL_CHECK(encodeStr);
×
220
    cJSON* encodeJson = cJSON_CreateString(encodeStr);
×
221
    RAW_NULL_CHECK(encodeJson);
×
222
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "encode", encodeJson));
×
223
    return code;
×
224
  }
225
  uint8_t compress = COMPRESS_L2_TYPE_U32(para);
×
226
  if (compress != 0) {
×
227
    const char* compressStr = columnCompressStr(compress);
×
228
    RAW_NULL_CHECK(compressStr);
×
229
    cJSON* compressJson = cJSON_CreateString(compressStr);
×
230
    RAW_NULL_CHECK(compressJson);
×
231
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "compress", compressJson));
×
232
    return code;
×
233
  }
234
  uint8_t level = COMPRESS_L2_TYPE_LEVEL_U32(para);
×
235
  if (level != 0) {
×
236
    const char* levelStr = columnLevelStr(level);
×
237
    RAW_NULL_CHECK(levelStr);
×
238
    cJSON* levelJson = cJSON_CreateString(levelStr);
×
239
    RAW_NULL_CHECK(levelJson);
×
240
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "level", levelJson));
×
241
    return code;
×
242
  }
243

244
end:
×
245
  return code;
×
246
}
247
static void buildAlterSTableJson(void* alterData, int32_t alterDataLen, cJSON** pJson) {
1,717✔
248
  if (alterData == NULL || pJson == NULL) {
1,717✔
NEW
249
    uError("invalid parameter in %s alterData:%p", __func__, alterData);
×
250
    return;
×
251
  }
252
  SMAlterStbReq req = {0};
1,717✔
253
  cJSON*        json = NULL;
1,717✔
254
  char*         string = NULL;
1,717✔
255
  int32_t       code = 0;
1,717✔
256

257
  if (tDeserializeSMAlterStbReq(alterData, alterDataLen, &req) != 0) {
1,717✔
258
    goto end;
×
259
  }
260

261
  json = cJSON_CreateObject();
1,717✔
262
  RAW_NULL_CHECK(json);
1,717✔
263
  cJSON* type = cJSON_CreateString("alter");
1,717✔
264
  RAW_NULL_CHECK(type);
1,717✔
265
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "type", type));
1,717✔
266
  SName name = {0};
1,717✔
267
  RAW_RETURN_CHECK(tNameFromString(&name, req.name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE));
1,717✔
268
  cJSON* tableType = cJSON_CreateString("super");
1,717✔
269
  RAW_NULL_CHECK(tableType);
1,717✔
270
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableType", tableType));
1,717✔
271
  cJSON* tableName = cJSON_CreateString(name.tname);
1,717✔
272
  RAW_NULL_CHECK(tableName);
1,717✔
273
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableName", tableName));
1,717✔
274

275
  cJSON* alterType = cJSON_CreateNumber(req.alterType);
1,717✔
276
  RAW_NULL_CHECK(alterType);
1,717✔
277
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "alterType", alterType));
1,717✔
278
  switch (req.alterType) {
1,717✔
279
    case TSDB_ALTER_TABLE_ADD_TAG:
999✔
280
    case TSDB_ALTER_TABLE_ADD_COLUMN: {
281
      if (taosArrayGetSize(req.pFields) != 1) {
999✔
NEW
282
        uError("invalid field num %" PRIzu " for alter type %d", taosArrayGetSize(req.pFields), req.alterType);
×
NEW
283
        cJSON_Delete(json);
×
NEW
284
        json = NULL;
×
NEW
285
        goto end;
×
286
      }
287
      TAOS_FIELD* field = taosArrayGet(req.pFields, 0);
999✔
288
      RAW_NULL_CHECK(field);
999✔
289
      cJSON* colName = cJSON_CreateString(field->name);
999✔
290
      RAW_NULL_CHECK(colName);
999✔
291
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
999✔
292
      cJSON* colType = cJSON_CreateNumber(field->type);
999✔
293
      RAW_NULL_CHECK(colType);
999✔
294
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colType", colType));
999✔
295

296
      if (field->type == TSDB_DATA_TYPE_BINARY || field->type == TSDB_DATA_TYPE_VARBINARY ||
999✔
297
          field->type == TSDB_DATA_TYPE_GEOMETRY) {
999✔
298
        int32_t length = field->bytes - VARSTR_HEADER_SIZE;
385✔
299
        cJSON*  cbytes = cJSON_CreateNumber(length);
385✔
300
        RAW_NULL_CHECK(cbytes);
385✔
301
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
385✔
302
      } else if (field->type == TSDB_DATA_TYPE_NCHAR) {
614✔
303
        int32_t length = (field->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
52✔
304
        cJSON*  cbytes = cJSON_CreateNumber(length);
52✔
305
        RAW_NULL_CHECK(cbytes);
52✔
306
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
52✔
307
      } else if (IS_STR_DATA_BLOB(field->type)) {
562✔
UNCOV
308
        int32_t length = field->bytes - BLOBSTR_HEADER_SIZE;
×
UNCOV
309
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
UNCOV
310
        RAW_NULL_CHECK(cbytes);
×
UNCOV
311
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
312
      }
313
      break;
999✔
314
    }
315
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION: {
×
316
      SFieldWithOptions* field = taosArrayGet(req.pFields, 0);
×
317
      RAW_NULL_CHECK(field);
×
UNCOV
318
      cJSON* colName = cJSON_CreateString(field->name);
×
UNCOV
319
      RAW_NULL_CHECK(colName);
×
UNCOV
320
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
×
321
      cJSON* colType = cJSON_CreateNumber(field->type);
×
322
      RAW_NULL_CHECK(colType);
×
323
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colType", colType));
×
324

325
      if (field->type == TSDB_DATA_TYPE_BINARY || field->type == TSDB_DATA_TYPE_VARBINARY ||
×
326
          field->type == TSDB_DATA_TYPE_GEOMETRY) {
×
327
        int32_t length = field->bytes - VARSTR_HEADER_SIZE;
×
328
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
329
        RAW_NULL_CHECK(cbytes);
×
UNCOV
330
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
331
      } else if (field->type == TSDB_DATA_TYPE_NCHAR) {
×
332
        int32_t length = (field->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
333
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
334
        RAW_NULL_CHECK(cbytes);
×
335
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
336
      } else if (IS_STR_DATA_BLOB(field->type)) {
×
337
        int32_t length = field->bytes - BLOBSTR_HEADER_SIZE;
×
338
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
339
        RAW_NULL_CHECK(cbytes);
×
340
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
341
      }
342

343
      RAW_RETURN_CHECK(setCompressOption(json, field->compress));
×
344
      break;
×
345
    }
346
    case TSDB_ALTER_TABLE_DROP_TAG:
281✔
347
    case TSDB_ALTER_TABLE_DROP_COLUMN: {
348
      TAOS_FIELD* field = taosArrayGet(req.pFields, 0);
281✔
349
      RAW_NULL_CHECK(field);
281✔
350
      cJSON* colName = cJSON_CreateString(field->name);
281✔
351
      RAW_NULL_CHECK(colName);
281✔
352
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
281✔
353
      break;
281✔
354
    }
355
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
437✔
356
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES: {
357
      if (taosArrayGetSize(req.pFields) != 1) {
437✔
NEW
358
        uError("invalid field num %" PRIzu " for alter type %d", taosArrayGetSize(req.pFields), req.alterType);
×
NEW
359
        cJSON_Delete(json);
×
NEW
360
        json = NULL;
×
NEW
361
        goto end;
×
362
      }
363
      TAOS_FIELD* field = taosArrayGet(req.pFields, 0);
437✔
364
      RAW_NULL_CHECK(field);
437✔
365
      cJSON* colName = cJSON_CreateString(field->name);
437✔
366
      RAW_NULL_CHECK(colName);
437✔
367
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
437✔
368
      cJSON* colType = cJSON_CreateNumber(field->type);
437✔
369
      RAW_NULL_CHECK(colType);
437✔
370
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colType", colType));
437✔
371
      if (field->type == TSDB_DATA_TYPE_BINARY || field->type == TSDB_DATA_TYPE_VARBINARY ||
437✔
372
          field->type == TSDB_DATA_TYPE_GEOMETRY) {
437✔
373
        int32_t length = field->bytes - VARSTR_HEADER_SIZE;
385✔
374
        cJSON*  cbytes = cJSON_CreateNumber(length);
385✔
375
        RAW_NULL_CHECK(cbytes);
385✔
376
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
385✔
377
      } else if (field->type == TSDB_DATA_TYPE_NCHAR) {
52✔
378
        int32_t length = (field->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
52✔
379
        cJSON*  cbytes = cJSON_CreateNumber(length);
52✔
380
        RAW_NULL_CHECK(cbytes);
52✔
381
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
52✔
382
      }
383
      break;
437✔
384
    }
UNCOV
385
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
×
386
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME: {
UNCOV
387
      TAOS_FIELD* oldField = taosArrayGet(req.pFields, 0);
×
UNCOV
388
      RAW_NULL_CHECK(oldField);
×
UNCOV
389
      TAOS_FIELD* newField = taosArrayGet(req.pFields, 1);
×
UNCOV
390
      RAW_NULL_CHECK(newField);
×
UNCOV
391
      cJSON* colName = cJSON_CreateString(oldField->name);
×
UNCOV
392
      RAW_NULL_CHECK(colName);
×
UNCOV
393
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
×
UNCOV
394
      cJSON* colNewName = cJSON_CreateString(newField->name);
×
UNCOV
395
      RAW_NULL_CHECK(colNewName);
×
UNCOV
396
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colNewName", colNewName));
×
397
      break;
×
398
    }
399
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS: {
×
400
      TAOS_FIELD* field = taosArrayGet(req.pFields, 0);
×
401
      RAW_NULL_CHECK(field);
×
402
      cJSON* colName = cJSON_CreateString(field->name);
×
403
      RAW_NULL_CHECK(colName);
×
404
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
×
405
      RAW_RETURN_CHECK(setCompressOption(json, field->bytes));
×
406
      break;
×
407
    }
408
    default:
×
409
      break;
×
410
  }
411

412
end:
1,717✔
413
  tFreeSMAltertbReq(&req);
1,717✔
414
  *pJson = json;
1,717✔
415
}
416

417
static void processCreateStb(SMqMetaRsp* metaRsp, cJSON** pJson) {
2,557✔
418
  if (metaRsp == NULL || pJson == NULL) {
2,557✔
UNCOV
419
    uError("invalid parameter in %s", __func__);
×
420
    return;
×
421
  }
422
  SVCreateStbReq req = {0};
2,557✔
423
  SDecoder       coder = {0};
2,557✔
424

425
  uDebug("create stable data:%p", metaRsp);
2,557✔
426
  // decode and process req
427
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
2,557✔
428
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
2,557✔
429
  tDecoderInit(&coder, data, len);
2,557✔
430

431
  if (tDecodeSVCreateStbReq(&coder, &req) < 0) {
2,557✔
432
    goto end;
×
433
  }
434
  buildCreateTableJson(&req.schemaRow, &req.schemaTag, req.pExtSchemas, req.name, req.suid, TSDB_SUPER_TABLE, &req.colCmpr, pJson);
2,557✔
435

436
end:
2,557✔
437
  uDebug("create stable return");
2,557✔
438
  tDecoderClear(&coder);
2,557✔
439
}
440

441
static void processAlterStb(SMqMetaRsp* metaRsp, cJSON** pJson) {
1,717✔
442
  if (metaRsp == NULL || pJson == NULL) {
1,717✔
UNCOV
443
    uError("invalid parameter in %s", __func__);
×
444
    return;
×
445
  }
446
  SVCreateStbReq req = {0};
1,717✔
447
  SDecoder       coder = {0};
1,717✔
448
  uDebug("alter stable data:%p", metaRsp);
1,717✔
449

450
  // decode and process req
451
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
1,717✔
452
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
1,717✔
453
  tDecoderInit(&coder, data, len);
1,717✔
454

455
  if (tDecodeSVCreateStbReq(&coder, &req) < 0) {
1,717✔
456
    goto end;
×
457
  }
458
  buildAlterSTableJson(req.alterOriData, req.alterOriDataLen, pJson);
1,717✔
459

460
end:
1,717✔
461
  uDebug("alter stable return");
1,717✔
462
  tDecoderClear(&coder);
1,717✔
463
}
464

465
static void buildChildElement(cJSON* json, SVCreateTbReq* pCreateReq) {
5,067✔
466
  if (json == NULL || pCreateReq == NULL) {
5,067✔
UNCOV
467
    uError("invalid parameter in %s", __func__);
×
468
    return;
×
469
  }
470
  STag*   pTag = (STag*)pCreateReq->ctb.pTag;
5,067✔
471
  char*   sname = pCreateReq->ctb.stbName;
5,067✔
472
  char*   name = pCreateReq->name;
5,067✔
473
  SArray* tagName = pCreateReq->ctb.tagName;
5,067✔
474
  int64_t id = pCreateReq->uid;
5,067✔
475
  uint8_t tagNum = pCreateReq->ctb.tagNum;
5,067✔
476
  int32_t code = 0;
5,067✔
477
  SArray* pTagVals = NULL;
5,067✔
478
  char*   pJson = NULL;
5,067✔
479

480
  cJSON* tableName = cJSON_CreateString(name);
5,067✔
481
  RAW_NULL_CHECK(tableName);
5,067✔
482
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableName", tableName));
5,067✔
483
  cJSON* using = cJSON_CreateString(sname);
5,067✔
484
  RAW_NULL_CHECK(using);
5,067✔
485
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "using", using));
5,067✔
486
  cJSON* tagNumJson = cJSON_CreateNumber(tagNum);
5,067✔
487
  RAW_NULL_CHECK(tagNumJson);
5,067✔
488
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tagNum", tagNumJson));
5,067✔
489

490
  cJSON* tags = cJSON_CreateArray();
5,067✔
491
  RAW_NULL_CHECK(tags);
5,067✔
492
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tags", tags));
5,067✔
493
  RAW_RETURN_CHECK(tTagToValArray(pTag, &pTagVals));
5,067✔
494
  if (tTagIsJson(pTag)) {
5,067✔
495
    STag* p = (STag*)pTag;
646✔
496
    if (p->nTag == 0) {
646✔
497
      uError("p->nTag == 0");
323✔
498
      goto end;
323✔
499
    }
500
    parseTagDatatoJson(pTag, &pJson, NULL);
323✔
501
    RAW_NULL_CHECK(pJson);
323✔
502
    cJSON* tag = cJSON_CreateObject();
323✔
503
    RAW_NULL_CHECK(tag);
323✔
504
    RAW_FALSE_CHECK(tmqAddJsonArrayItem(tags, tag));
323✔
505
    STagVal* pTagVal = taosArrayGet(pTagVals, 0);
323✔
506
    RAW_NULL_CHECK(pTagVal);
323✔
507
    char* ptname = taosArrayGet(tagName, 0);
323✔
508
    RAW_NULL_CHECK(ptname);
323✔
509
    cJSON* tname = cJSON_CreateString(ptname);
323✔
510
    RAW_NULL_CHECK(tname);
323✔
511
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "name", tname));
323✔
512
    cJSON* ttype = cJSON_CreateNumber(TSDB_DATA_TYPE_JSON);
323✔
513
    RAW_NULL_CHECK(ttype);
323✔
514
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "type", ttype));
323✔
515
    cJSON* tvalue = cJSON_CreateString(pJson);
323✔
516
    RAW_NULL_CHECK(tvalue);
323✔
517
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "value", tvalue));
323✔
518
    goto end;
323✔
519
  }
520

521
  for (int i = 0; i < taosArrayGetSize(pTagVals); i++) {
14,985✔
522
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, i);
10,564✔
523
    RAW_NULL_CHECK(pTagVal);
10,564✔
524
    cJSON* tag = cJSON_CreateObject();
10,564✔
525
    RAW_NULL_CHECK(tag);
10,564✔
526
    RAW_FALSE_CHECK(tmqAddJsonArrayItem(tags, tag));
10,564✔
527
    char* ptname = taosArrayGet(tagName, i);
10,564✔
528
    RAW_NULL_CHECK(ptname);
10,564✔
529
    cJSON* tname = cJSON_CreateString(ptname);
10,564✔
530
    RAW_NULL_CHECK(tname);
10,564✔
531
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "name", tname));
10,564✔
532
    cJSON* ttype = cJSON_CreateNumber(pTagVal->type);
10,564✔
533
    RAW_NULL_CHECK(ttype);
10,564✔
534
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "type", ttype));
10,564✔
535

536
    cJSON* tvalue = NULL;
10,564✔
537
    if (IS_VAR_DATA_TYPE(pTagVal->type)) {
14,112✔
538
      if (IS_STR_DATA_BLOB(pTagVal->type)) {
3,548✔
UNCOV
539
        goto end;
×
540
      }
541
      int64_t bufSize = 0;
3,548✔
542
      if (pTagVal->type == TSDB_DATA_TYPE_VARBINARY) {
3,548✔
UNCOV
543
        bufSize = pTagVal->nData * 2 + 2 + 3;
×
544
      } else {
545
        bufSize = pTagVal->nData + 3;
3,548✔
546
      }
547
      char* buf = taosMemoryCalloc(bufSize, 1);
3,548✔
548
      RAW_NULL_CHECK(buf);
3,548✔
549
      if (dataConverToStr(buf, bufSize, pTagVal->type, pTagVal->pData, pTagVal->nData, NULL) != TSDB_CODE_SUCCESS) {
3,548✔
UNCOV
550
        taosMemoryFree(buf);
×
551
        goto end;
×
552
      }
553

554
      tvalue = cJSON_CreateString(buf);
3,548✔
555
      taosMemoryFree(buf);
3,548✔
556
      RAW_NULL_CHECK(tvalue);
3,548✔
557
    } else {
558
      double val = 0;
7,016✔
559
      GET_TYPED_DATA(val, double, pTagVal->type, &pTagVal->i64,
7,016✔
560
                     0);  // currently tag type can't be decimal, so pass 0 as typeMod
561
      tvalue = cJSON_CreateNumber(val);
7,016✔
562
      RAW_NULL_CHECK(tvalue);
7,016✔
563
    }
564

565
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "value", tvalue));
10,564✔
566
  }
567

568
end:
5,067✔
569
  taosMemoryFree(pJson);
5,067✔
570
  taosArrayDestroy(pTagVals);
5,067✔
571
}
572

573
static void buildCreateCTableJson(SVCreateTbReq* pCreateReq, int32_t nReqs, cJSON** pJson) {
3,899✔
574
  if (pJson == NULL || pCreateReq == NULL) {
3,899✔
UNCOV
575
    uError("invalid parameter in %s", __func__);
×
UNCOV
576
    return;
×
577
  }
578
  int32_t code = 0;
3,899✔
579
  char*   string = NULL;
3,899✔
580
  cJSON*  json = cJSON_CreateObject();
3,899✔
581
  RAW_NULL_CHECK(json);
3,899✔
582
  cJSON* type = cJSON_CreateString("create");
3,899✔
583
  RAW_NULL_CHECK(type);
3,899✔
584
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "type", type));
3,899✔
585

586
  cJSON* tableType = cJSON_CreateString("child");
3,899✔
587
  RAW_NULL_CHECK(tableType);
3,899✔
588
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableType", tableType));
3,899✔
589

590
  buildChildElement(json, pCreateReq);
3,899✔
591
  cJSON* createList = cJSON_CreateArray();
3,899✔
592
  RAW_NULL_CHECK(createList);
3,899✔
593
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "createList", createList));
3,899✔
594

595
  for (int i = 0; nReqs > 1 && i < nReqs; i++) {
5,067✔
596
    cJSON* create = cJSON_CreateObject();
1,168✔
597
    RAW_NULL_CHECK(create);
1,168✔
598
    buildChildElement(create, pCreateReq + i);
1,168✔
599
    RAW_FALSE_CHECK(tmqAddJsonArrayItem(createList, create));
1,168✔
600
  }
601

602
end:
3,899✔
603
  *pJson = json;
3,899✔
604
}
605

606
static void processCreateTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
4,347✔
607
  if (pJson == NULL || metaRsp == NULL) {
4,347✔
UNCOV
608
    uError("invalid parameter in %s", __func__);
×
UNCOV
609
    return;
×
610
  }
611
  SDecoder           decoder = {0};
4,347✔
612
  SVCreateTbBatchReq req = {0};
4,347✔
613
  SVCreateTbReq*     pCreateReq;
614
  // decode
615
  uDebug("create table data:%p", metaRsp);
4,347✔
616
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
4,347✔
617
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
4,347✔
618
  tDecoderInit(&decoder, data, len);
4,347✔
619
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
4,347✔
620
    goto end;
×
621
  }
622

623
  // loop to create table
624
  if (req.nReqs > 0) {
4,347✔
625
    pCreateReq = req.pReqs;
4,347✔
626
    if (pCreateReq->type == TSDB_CHILD_TABLE) {
4,347✔
627
      buildCreateCTableJson(req.pReqs, req.nReqs, pJson);
3,689✔
628
    } else if (pCreateReq->type == TSDB_NORMAL_TABLE) {
658✔
629
      buildCreateTableJson(&pCreateReq->ntb.schemaRow, NULL, pCreateReq->pExtSchemas, pCreateReq->name, pCreateReq->uid, TSDB_NORMAL_TABLE,
658✔
630
                           &pCreateReq->colCmpr, pJson);
631
    }
632
  }
633

634
end:
4,347✔
635
  uDebug("create table return");
4,347✔
636
  tDeleteSVCreateTbBatchReq(&req);
4,347✔
637
  tDecoderClear(&decoder);
4,347✔
638
}
639

640
static void processAutoCreateTable(SMqDataRsp* rsp, char** string) {
266✔
641
  if (rsp == NULL || string == NULL) {
266✔
UNCOV
642
    uError("invalid parameter in %s", __func__);
×
UNCOV
643
    return;
×
644
  }
645
  SDecoder*      decoder = NULL;
266✔
646
  SVCreateTbReq* pCreateReq = NULL;
266✔
647
  int32_t        code = 0;
266✔
648
  uDebug("auto create table data:%p", rsp);
266✔
649
  if (rsp->createTableNum <= 0) {
266✔
UNCOV
650
    uError("processAutoCreateTable rsp->createTableNum <= 0");
×
UNCOV
651
    goto end;
×
652
  }
653

654
  decoder = taosMemoryCalloc(rsp->createTableNum, sizeof(SDecoder));
266✔
655
  RAW_NULL_CHECK(decoder);
266✔
656
  pCreateReq = taosMemoryCalloc(rsp->createTableNum, sizeof(SVCreateTbReq));
266✔
657
  RAW_NULL_CHECK(pCreateReq);
266✔
658

659
  // loop to create table
660
  for (int32_t iReq = 0; iReq < rsp->createTableNum; iReq++) {
790✔
661
    // decode
662
    void** data = taosArrayGet(rsp->createTableReq, iReq);
524✔
663
    RAW_NULL_CHECK(data);
524✔
664
    int32_t* len = taosArrayGet(rsp->createTableLen, iReq);
524✔
665
    RAW_NULL_CHECK(len);
524✔
666
    tDecoderInit(&decoder[iReq], *data, *len);
524✔
667
    if (tDecodeSVCreateTbReq(&decoder[iReq], pCreateReq + iReq) < 0) {
524✔
UNCOV
668
      goto end;
×
669
    }
670

671
    if (pCreateReq[iReq].type != TSDB_CHILD_TABLE && pCreateReq[iReq].type != TSDB_NORMAL_TABLE) {
524✔
UNCOV
672
      uError("processAutoCreateTable pCreateReq[iReq].type != TSDB_CHILD_TABLE");
×
UNCOV
673
      goto end;
×
674
    }
675
  }
676
  cJSON* pJson = NULL;
266✔
677
  if (pCreateReq->type == TSDB_NORMAL_TABLE) {
266✔
678
    buildCreateTableJson(&pCreateReq->ntb.schemaRow, NULL, pCreateReq->pExtSchemas, pCreateReq->name, pCreateReq->uid, TSDB_NORMAL_TABLE,
56✔
679
                         &pCreateReq->colCmpr, &pJson);
680
  } else if (pCreateReq->type == TSDB_CHILD_TABLE) {
210✔
681
    buildCreateCTableJson(pCreateReq, rsp->createTableNum, &pJson);
210✔
682
  }
683

684
  *string = cJSON_PrintUnformatted(pJson);
266✔
685
  cJSON_Delete(pJson);
266✔
686

687
end:
266✔
688
  uDebug("auto created table return, sql json:%s", *string);
266✔
689
  for (int i = 0; decoder && pCreateReq && i < rsp->createTableNum; i++) {
790✔
690
    tDecoderClear(&decoder[i]);
524✔
691
    taosMemoryFreeClear(pCreateReq[i].comment);
524✔
692
    if (pCreateReq[i].type == TSDB_CHILD_TABLE) {
524✔
693
      taosArrayDestroy(pCreateReq[i].ctb.tagName);
468✔
694
    }
695
  }
696
  taosMemoryFree(decoder);
266✔
697
  taosMemoryFree(pCreateReq);
266✔
698
}
699

700
static void processAlterTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
1,010✔
701
  if (pJson == NULL || metaRsp == NULL) {
1,010✔
UNCOV
702
    uError("invalid parameter in %s", __func__);
×
UNCOV
703
    return;
×
704
  }
705
  SDecoder     decoder = {0};
1,010✔
706
  SVAlterTbReq vAlterTbReq = {0};
1,010✔
707
  char*        string = NULL;
1,010✔
708
  cJSON*       json = NULL;
1,010✔
709
  int32_t      code = 0;
1,010✔
710

711
  uDebug("alter table data:%p", metaRsp);
1,010✔
712
  // decode
713
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
1,010✔
714
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
1,010✔
715
  tDecoderInit(&decoder, data, len);
1,010✔
716
  if (tDecodeSVAlterTbReq(&decoder, &vAlterTbReq) < 0) {
1,010✔
UNCOV
717
    uError("tDecodeSVAlterTbReq error");
×
UNCOV
718
    goto end;
×
719
  }
720

721
  json = cJSON_CreateObject();
1,010✔
722
  RAW_NULL_CHECK(json);
1,010✔
723
  cJSON* type = cJSON_CreateString("alter");
1,010✔
724
  RAW_NULL_CHECK(type);
1,010✔
725
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "type", type));
1,010✔
726
  cJSON* tableType = cJSON_CreateString(vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
1,815✔
727
                                                vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL
805✔
728
                                            ? "child"
729
                                            : "normal");
730
  RAW_NULL_CHECK(tableType);
1,010✔
731
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableType", tableType));
1,010✔
732
  cJSON* tableName = cJSON_CreateString(vAlterTbReq.tbName);
1,010✔
733
  RAW_NULL_CHECK(tableName);
1,010✔
734
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableName", tableName));
1,010✔
735
  cJSON* alterType = cJSON_CreateNumber(vAlterTbReq.action);
1,010✔
736
  RAW_NULL_CHECK(alterType);
1,010✔
737
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "alterType", alterType));
1,010✔
738

739
  switch (vAlterTbReq.action) {
1,010✔
740
    case TSDB_ALTER_TABLE_ADD_COLUMN: {
161✔
741
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
161✔
742
      RAW_NULL_CHECK(colName);
161✔
743
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
161✔
744
      cJSON* colType = cJSON_CreateNumber(vAlterTbReq.type);
161✔
745
      RAW_NULL_CHECK(colType);
161✔
746
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colType", colType));
161✔
747

748
      if (vAlterTbReq.type == TSDB_DATA_TYPE_BINARY || vAlterTbReq.type == TSDB_DATA_TYPE_VARBINARY ||
161✔
749
          vAlterTbReq.type == TSDB_DATA_TYPE_GEOMETRY) {
161✔
UNCOV
750
        int32_t length = vAlterTbReq.bytes - VARSTR_HEADER_SIZE;
×
UNCOV
751
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
UNCOV
752
        RAW_NULL_CHECK(cbytes);
×
UNCOV
753
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
754
      } else if (vAlterTbReq.type == TSDB_DATA_TYPE_NCHAR) {
161✔
UNCOV
755
        int32_t length = (vAlterTbReq.bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
UNCOV
756
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
UNCOV
757
        RAW_NULL_CHECK(cbytes);
×
UNCOV
758
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
759
      }
760
      break;
161✔
761
    }
762
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION: {
×
763
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
×
764
      RAW_NULL_CHECK(colName);
×
765
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
×
UNCOV
766
      cJSON* colType = cJSON_CreateNumber(vAlterTbReq.type);
×
767
      RAW_NULL_CHECK(colType);
×
768
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colType", colType));
×
769

770
      if (vAlterTbReq.type == TSDB_DATA_TYPE_BINARY || vAlterTbReq.type == TSDB_DATA_TYPE_VARBINARY ||
×
UNCOV
771
          vAlterTbReq.type == TSDB_DATA_TYPE_GEOMETRY) {
×
UNCOV
772
        int32_t length = vAlterTbReq.bytes - VARSTR_HEADER_SIZE;
×
UNCOV
773
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
774
        RAW_NULL_CHECK(cbytes);
×
775
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
776
      } else if (vAlterTbReq.type == TSDB_DATA_TYPE_NCHAR) {
×
777
        int32_t length = (vAlterTbReq.bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
778
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
779
        RAW_NULL_CHECK(cbytes);
×
780
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
781
      }
782
      RAW_RETURN_CHECK(setCompressOption(json, vAlterTbReq.compress));
×
783
      break;
×
784
    }
785
    case TSDB_ALTER_TABLE_DROP_COLUMN: {
161✔
786
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
161✔
787
      RAW_NULL_CHECK(colName);
161✔
788
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
161✔
789
      break;
161✔
790
    }
791
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES: {
161✔
792
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
161✔
793
      RAW_NULL_CHECK(colName);
161✔
794
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
161✔
795
      cJSON* colType = cJSON_CreateNumber(vAlterTbReq.colModType);
161✔
796
      RAW_NULL_CHECK(colType);
161✔
797
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colType", colType));
161✔
798
      if (vAlterTbReq.colModType == TSDB_DATA_TYPE_BINARY || vAlterTbReq.colModType == TSDB_DATA_TYPE_VARBINARY ||
161✔
799
          vAlterTbReq.colModType == TSDB_DATA_TYPE_GEOMETRY) {
161✔
UNCOV
800
        int32_t length = vAlterTbReq.colModBytes - VARSTR_HEADER_SIZE;
×
UNCOV
801
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
UNCOV
802
        RAW_NULL_CHECK(cbytes);
×
UNCOV
803
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
804
      } else if (vAlterTbReq.colModType == TSDB_DATA_TYPE_NCHAR) {
161✔
805
        int32_t length = (vAlterTbReq.colModBytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
161✔
806
        cJSON*  cbytes = cJSON_CreateNumber(length);
161✔
807
        RAW_NULL_CHECK(cbytes);
161✔
808
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
161✔
809
      }
810
      break;
161✔
811
    }
812
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME: {
161✔
813
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
161✔
814
      RAW_NULL_CHECK(colName);
161✔
815
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
161✔
816
      cJSON* colNewName = cJSON_CreateString(vAlterTbReq.colNewName);
161✔
817
      RAW_NULL_CHECK(colNewName);
161✔
818
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colNewName", colNewName));
161✔
819
      break;
161✔
820
    }
821
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL: {
205✔
822
      cJSON* tagName = cJSON_CreateString(vAlterTbReq.tagName);
205✔
823
      RAW_NULL_CHECK(tagName);
205✔
824
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", tagName));
205✔
825

826
      bool isNull = vAlterTbReq.isNull;
205✔
827
      if (vAlterTbReq.tagType == TSDB_DATA_TYPE_JSON) {
205✔
UNCOV
828
        STag* jsonTag = (STag*)vAlterTbReq.pTagVal;
×
UNCOV
829
        if (jsonTag->nTag == 0) isNull = true;
×
830
      }
831
      if (!isNull) {
205✔
832
        char* buf = NULL;
205✔
833

834
        if (vAlterTbReq.tagType == TSDB_DATA_TYPE_JSON) {
205✔
UNCOV
835
          if (!tTagIsJson(vAlterTbReq.pTagVal)) {
×
UNCOV
836
            uError("processAlterTable isJson false");
×
UNCOV
837
            goto end;
×
838
          }
UNCOV
839
          parseTagDatatoJson(vAlterTbReq.pTagVal, &buf, NULL);
×
840
          if (buf == NULL) {
×
841
            uError("parseTagDatatoJson failed, buf == NULL");
×
UNCOV
842
            goto end;
×
843
          }
844
        } else {
845
          int64_t bufSize = 0;
205✔
846
          if (vAlterTbReq.tagType == TSDB_DATA_TYPE_VARBINARY) {
205✔
847
            bufSize = vAlterTbReq.nTagVal * 2 + 2 + 3;
×
848
          } else {
849
            bufSize = vAlterTbReq.nTagVal + 32;
205✔
850
          }
851
          buf = taosMemoryCalloc(bufSize, 1);
205✔
852
          RAW_NULL_CHECK(buf);
205✔
853
          if (dataConverToStr(buf, bufSize, vAlterTbReq.tagType, vAlterTbReq.pTagVal, vAlterTbReq.nTagVal, NULL) !=
205✔
854
              TSDB_CODE_SUCCESS) {
UNCOV
855
            taosMemoryFree(buf);
×
UNCOV
856
            goto end;
×
857
          }
858
        }
859

860
        cJSON* colValue = cJSON_CreateString(buf);
205✔
861
        taosMemoryFree(buf);
205✔
862
        RAW_NULL_CHECK(colValue);
205✔
863
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colValue", colValue));
205✔
864
      }
865

866
      cJSON* isNullCJson = cJSON_CreateBool(isNull);
205✔
867
      RAW_NULL_CHECK(isNullCJson);
205✔
868
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colValueNull", isNullCJson));
205✔
869
      break;
205✔
870
    }
UNCOV
871
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL: {
×
UNCOV
872
      int32_t nTags = taosArrayGetSize(vAlterTbReq.pMultiTag);
×
UNCOV
873
      if (nTags <= 0) {
×
UNCOV
874
        uError("processAlterTable parse multi tags error");
×
UNCOV
875
        goto end;
×
876
      }
877

UNCOV
878
      cJSON* tags = cJSON_CreateArray();
×
UNCOV
879
      RAW_NULL_CHECK(tags);
×
UNCOV
880
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tags", tags));
×
881

UNCOV
882
      for (int32_t i = 0; i < nTags; i++) {
×
883
        cJSON* member = cJSON_CreateObject();
×
884
        RAW_NULL_CHECK(member);
×
885
        RAW_FALSE_CHECK(tmqAddJsonArrayItem(tags, member));
×
886

887
        SMultiTagUpateVal* pTagVal = taosArrayGet(vAlterTbReq.pMultiTag, i);
×
UNCOV
888
        cJSON*             tagName = cJSON_CreateString(pTagVal->tagName);
×
UNCOV
889
        RAW_NULL_CHECK(tagName);
×
890
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(member, "colName", tagName));
×
891

892
        if (pTagVal->tagType == TSDB_DATA_TYPE_JSON) {
×
UNCOV
893
          uError("processAlterTable isJson false");
×
894
          goto end;
×
895
        }
896
        bool isNull = pTagVal->isNull;
×
897
        if (!isNull) {
×
UNCOV
898
          int64_t bufSize = 0;
×
899
          if (pTagVal->tagType == TSDB_DATA_TYPE_VARBINARY) {
×
900
            bufSize = pTagVal->nTagVal * 2 + 2 + 3;
×
901
          } else {
902
            bufSize = pTagVal->nTagVal + 3;
×
903
          }
904
          char* buf = taosMemoryCalloc(bufSize, 1);
×
905
          RAW_NULL_CHECK(buf);
×
906
          if (dataConverToStr(buf, bufSize, pTagVal->tagType, pTagVal->pTagVal, pTagVal->nTagVal, NULL) !=
×
907
              TSDB_CODE_SUCCESS) {
908
            taosMemoryFree(buf);
×
909
            goto end;
×
910
          }
911
          cJSON* colValue = cJSON_CreateString(buf);
×
912
          taosMemoryFree(buf);
×
UNCOV
913
          RAW_NULL_CHECK(colValue);
×
914
          RAW_FALSE_CHECK(tmqAddJsonObjectItem(member, "colValue", colValue));
×
915
        }
916
        cJSON* isNullCJson = cJSON_CreateBool(isNull);
×
917
        RAW_NULL_CHECK(isNullCJson);
×
918
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(member, "colValueNull", isNullCJson));
×
919
      }
920
      break;
×
921
    }
922

923
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS: {
×
924
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
×
925
      RAW_NULL_CHECK(colName);
×
926
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
×
UNCOV
927
      RAW_RETURN_CHECK(setCompressOption(json, vAlterTbReq.compress));
×
928
      break;
×
929
    }
930
    default:
161✔
931
      break;
161✔
932
  }
933

934
end:
1,010✔
935
  uDebug("alter table return");
1,010✔
936
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
1,010✔
937
    taosArrayDestroy(vAlterTbReq.pMultiTag);
×
938
  }
939
  tDecoderClear(&decoder);
1,010✔
940
  *pJson = json;
1,010✔
941
}
942

943
static void processDropSTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
266✔
944
  if (pJson == NULL || metaRsp == NULL) {
266✔
UNCOV
945
    uError("invalid parameter in %s", __func__);
×
UNCOV
946
    return;
×
947
  }
948
  SDecoder     decoder = {0};
266✔
949
  SVDropStbReq req = {0};
266✔
950
  cJSON*       json = NULL;
266✔
951
  int32_t      code = 0;
266✔
952

953
  uDebug("processDropSTable data:%p", metaRsp);
266✔
954

955
  // decode
956
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
266✔
957
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
266✔
958
  tDecoderInit(&decoder, data, len);
266✔
959
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
266✔
UNCOV
960
    uError("tDecodeSVDropStbReq failed");
×
UNCOV
961
    goto end;
×
962
  }
963

964
  json = cJSON_CreateObject();
266✔
965
  RAW_NULL_CHECK(json);
266✔
966
  cJSON* type = cJSON_CreateString("drop");
266✔
967
  RAW_NULL_CHECK(type);
266✔
968
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "type", type));
266✔
969
  cJSON* tableType = cJSON_CreateString("super");
266✔
970
  RAW_NULL_CHECK(tableType);
266✔
971
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableType", tableType));
266✔
972
  cJSON* tableName = cJSON_CreateString(req.name);
266✔
973
  RAW_NULL_CHECK(tableName);
266✔
974
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableName", tableName));
266✔
975

976
end:
266✔
977
  uDebug("processDropSTable return");
266✔
978
  tDecoderClear(&decoder);
266✔
979
  *pJson = json;
266✔
980
}
981
static void processDeleteTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
106✔
982
  if (pJson == NULL || metaRsp == NULL) {
106✔
UNCOV
983
    uError("invalid parameter in %s", __func__);
×
UNCOV
984
    return;
×
985
  }
986
  SDeleteRes req = {0};
106✔
987
  SDecoder   coder = {0};
106✔
988
  cJSON*     json = NULL;
106✔
989
  int32_t    code = 0;
106✔
990

991
  uDebug("processDeleteTable data:%p", metaRsp);
106✔
992
  // decode and process req
993
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
106✔
994
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
106✔
995

996
  tDecoderInit(&coder, data, len);
106✔
997
  if (tDecodeDeleteRes(&coder, &req) < 0) {
106✔
UNCOV
998
    uError("tDecodeDeleteRes failed");
×
UNCOV
999
    goto end;
×
1000
  }
1001

1002
  //  getTbName(req.tableFName);
1003
  char sql[256] = {0};
106✔
1004
  (void)snprintf(sql, sizeof(sql), "delete from `%s` where `%s` >= %" PRId64 " and `%s` <= %" PRId64, req.tableFName,
106✔
1005
                 req.tsColName, req.skey, req.tsColName, req.ekey);
1006

1007
  json = cJSON_CreateObject();
106✔
1008
  RAW_NULL_CHECK(json);
106✔
1009
  cJSON* type = cJSON_CreateString("delete");
106✔
1010
  RAW_NULL_CHECK(type);
106✔
1011
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "type", type));
106✔
1012
  cJSON* sqlJson = cJSON_CreateString(sql);
106✔
1013
  RAW_NULL_CHECK(sqlJson);
106✔
1014
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "sql", sqlJson));
106✔
1015

1016
end:
106✔
1017
  uDebug("processDeleteTable return");
106✔
1018
  tDecoderClear(&coder);
106✔
1019
  *pJson = json;
106✔
1020
}
1021

1022
static void processDropTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
188✔
1023
  if (pJson == NULL || metaRsp == NULL) {
188✔
UNCOV
1024
    uError("invalid parameter in %s", __func__);
×
UNCOV
1025
    return;
×
1026
  }
1027
  SDecoder         decoder = {0};
188✔
1028
  SVDropTbBatchReq req = {0};
188✔
1029
  cJSON*           json = NULL;
188✔
1030
  int32_t          code = 0;
188✔
1031

1032
  uDebug("processDropTable data:%p", metaRsp);
188✔
1033
  // decode
1034
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
188✔
1035
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
188✔
1036
  tDecoderInit(&decoder, data, len);
188✔
1037
  if (tDecodeSVDropTbBatchReq(&decoder, &req) < 0) {
188✔
UNCOV
1038
    uError("tDecodeSVDropTbBatchReq failed");
×
UNCOV
1039
    goto end;
×
1040
  }
1041

1042
  json = cJSON_CreateObject();
188✔
1043
  RAW_NULL_CHECK(json);
188✔
1044
  cJSON* type = cJSON_CreateString("drop");
188✔
1045
  RAW_NULL_CHECK(type);
188✔
1046
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "type", type));
188✔
1047
  cJSON* tableNameList = cJSON_CreateArray();
188✔
1048
  RAW_NULL_CHECK(tableNameList);
188✔
1049
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableNameList", tableNameList));
188✔
1050

1051
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
431✔
1052
    SVDropTbReq* pDropTbReq = req.pReqs + iReq;
243✔
1053
    cJSON*       tableName = cJSON_CreateString(pDropTbReq->name);
243✔
1054
    RAW_NULL_CHECK(tableName);
243✔
1055
    RAW_FALSE_CHECK(tmqAddJsonArrayItem(tableNameList, tableName));
243✔
1056
  }
1057

1058
end:
188✔
1059
  uDebug("processDropTable return");
188✔
1060
  tDecoderClear(&decoder);
188✔
1061
  *pJson = json;
188✔
1062
}
1063

1064
static int32_t taosCreateStb(TAOS* taos, void* meta, uint32_t metaLen) {
3,824✔
1065
  if (taos == NULL || meta == NULL) {
3,824✔
UNCOV
1066
    uError("invalid parameter in %s", __func__);
×
UNCOV
1067
    return TSDB_CODE_INVALID_PARA;
×
1068
  }
1069
  SVCreateStbReq req = {0};
3,824✔
1070
  SDecoder       coder = {0};
3,824✔
1071
  SMCreateStbReq pReq = {0};
3,824✔
1072
  int32_t        code = TSDB_CODE_SUCCESS;
3,824✔
1073
  SRequestObj*   pRequest = NULL;
3,824✔
1074

1075
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
3,824✔
1076
  uDebug(LOG_ID_TAG " create stable, meta:%p, metaLen:%d", LOG_ID_VALUE, meta, metaLen);
3,824✔
1077
  pRequest->syncQuery = true;
3,824✔
1078
  if (!pRequest->pDb) {
3,824✔
1079
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
UNCOV
1080
    goto end;
×
1081
  }
1082
  // decode and process req
1083
  void*    data = POINTER_SHIFT(meta, sizeof(SMsgHead));
3,824✔
1084
  uint32_t len = metaLen - sizeof(SMsgHead);
3,824✔
1085
  tDecoderInit(&coder, data, len);
3,824✔
1086
  if (tDecodeSVCreateStbReq(&coder, &req) < 0) {
3,824✔
UNCOV
1087
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1088
    goto end;
×
1089
  }
1090

1091
  int8_t           createDefaultCompress = 0;
3,824✔
1092
  SColCmprWrapper* p = &req.colCmpr;
3,824✔
1093
  if (p->nCols == 0) {
3,824✔
UNCOV
1094
    createDefaultCompress = 1;
×
1095
  }
1096
  // build create stable
1097
  pReq.pColumns = taosArrayInit(req.schemaRow.nCols, sizeof(SFieldWithOptions));
3,824✔
1098
  RAW_NULL_CHECK(pReq.pColumns);
3,824✔
1099
  for (int32_t i = 0; i < req.schemaRow.nCols; i++) {
28,090✔
1100
    SSchema*          pSchema = req.schemaRow.pSchema + i;
24,266✔
1101
    SFieldWithOptions field = {.type = pSchema->type, .flags = pSchema->flags, .bytes = pSchema->bytes};
24,266✔
1102
    tstrncpy(field.name, pSchema->name, TSDB_COL_NAME_LEN);
24,266✔
1103

1104
    if (createDefaultCompress) {
24,266✔
UNCOV
1105
      field.compress = createDefaultColCmprByType(pSchema->type);
×
1106
    } else {
1107
      SColCmpr* pCmp = &req.colCmpr.pColCmpr[i];
24,266✔
1108
      field.compress = pCmp->alg;
24,266✔
1109
    }
1110
    if (req.pExtSchemas) field.typeMod = req.pExtSchemas[i].typeMod;
24,266✔
1111
    RAW_NULL_CHECK(taosArrayPush(pReq.pColumns, &field));
48,532✔
1112
  }
1113
  pReq.pTags = taosArrayInit(req.schemaTag.nCols, sizeof(SField));
3,824✔
1114
  RAW_NULL_CHECK(pReq.pTags);
3,824✔
1115
  for (int32_t i = 0; i < req.schemaTag.nCols; i++) {
14,219✔
1116
    SSchema* pSchema = req.schemaTag.pSchema + i;
10,395✔
1117
    SField   field = {.type = pSchema->type, .flags = pSchema->flags, .bytes = pSchema->bytes};
10,395✔
1118
    tstrncpy(field.name, pSchema->name, TSDB_COL_NAME_LEN);
10,395✔
1119
    RAW_NULL_CHECK(taosArrayPush(pReq.pTags, &field));
20,790✔
1120
  }
1121

1122
  pReq.colVer = req.schemaRow.version;
3,824✔
1123
  pReq.tagVer = req.schemaTag.version;
3,824✔
1124
  pReq.numOfColumns = req.schemaRow.nCols;
3,824✔
1125
  pReq.numOfTags = req.schemaTag.nCols;
3,824✔
1126
  pReq.commentLen = -1;
3,824✔
1127
  pReq.suid = processSuid(req.suid, pRequest->pDb);
3,824✔
1128
  pReq.source = TD_REQ_FROM_TAOX;
3,824✔
1129
  pReq.igExists = true;
3,824✔
1130

1131
  uDebug(LOG_ID_TAG " create stable name:%s suid:%" PRId64 " processSuid:%" PRId64, LOG_ID_VALUE, req.name, req.suid,
3,824✔
1132
         pReq.suid);
1133
  STscObj* pTscObj = pRequest->pTscObj;
3,824✔
1134
  SName    tableName = {0};
3,824✔
1135
  toName(pTscObj->acctId, pRequest->pDb, req.name, &tableName);
3,824✔
1136
  RAW_RETURN_CHECK(tNameExtractFullName(&tableName, pReq.name));
3,824✔
1137
  SCmdMsgInfo pCmdMsg = {0};
3,824✔
1138
  pCmdMsg.epSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
3,824✔
1139
  pCmdMsg.msgType = TDMT_MND_CREATE_STB;
3,824✔
1140
  pCmdMsg.msgLen = tSerializeSMCreateStbReq(NULL, 0, &pReq);
3,824✔
1141
  if (pCmdMsg.msgLen <= 0) {
3,824✔
UNCOV
1142
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1143
    goto end;
×
1144
  }
1145
  pCmdMsg.pMsg = taosMemoryMalloc(pCmdMsg.msgLen);
3,824✔
1146
  RAW_NULL_CHECK(pCmdMsg.pMsg);
3,824✔
1147
  if (tSerializeSMCreateStbReq(pCmdMsg.pMsg, pCmdMsg.msgLen, &pReq) <= 0) {
3,824✔
UNCOV
1148
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1149
    taosMemoryFree(pCmdMsg.pMsg);
×
UNCOV
1150
    goto end;
×
1151
  }
1152

1153
  SQuery pQuery = {0};
3,824✔
1154
  pQuery.execMode = QUERY_EXEC_MODE_RPC;
3,824✔
1155
  pQuery.pCmdMsg = &pCmdMsg;
3,824✔
1156
  pQuery.msgType = pQuery.pCmdMsg->msgType;
3,824✔
1157
  pQuery.stableQuery = true;
3,824✔
1158

1159
  launchQueryImpl(pRequest, &pQuery, true, NULL);  // ignore, because return value is pRequest
3,824✔
1160

1161
  taosMemoryFree(pCmdMsg.pMsg);
3,824✔
1162

1163
  if (pRequest->code == TSDB_CODE_SUCCESS) {
3,824✔
1164
    SCatalog* pCatalog = NULL;
3,824✔
1165
    RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
3,824✔
1166
    RAW_RETURN_CHECK(catalogRemoveTableMeta(pCatalog, &tableName));
3,824✔
1167
  }
1168

1169
  code = pRequest->code;
3,824✔
1170

1171
end:
3,824✔
1172
  uDebug(LOG_ID_TAG " create stable return, msg:%s", LOG_ID_VALUE, tstrerror(code));
3,824✔
1173
  destroyRequest(pRequest);
3,824✔
1174
  tFreeSMCreateStbReq(&pReq);
3,824✔
1175
  tDecoderClear(&coder);
3,824✔
1176
  return code;
3,824✔
1177
}
1178

1179
static int32_t taosDropStb(TAOS* taos, void* meta, uint32_t metaLen) {
266✔
1180
  if (taos == NULL || meta == NULL) {
266✔
UNCOV
1181
    uError("invalid parameter in %s", __func__);
×
UNCOV
1182
    return TSDB_CODE_INVALID_PARA;
×
1183
  }
1184
  SVDropStbReq req = {0};
266✔
1185
  SDecoder     coder = {0};
266✔
1186
  SMDropStbReq pReq = {0};
266✔
1187
  int32_t      code = TSDB_CODE_SUCCESS;
266✔
1188
  SRequestObj* pRequest = NULL;
266✔
1189

1190
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
266✔
1191
  uDebug(LOG_ID_TAG " drop stable, meta:%p, metaLen:%d", LOG_ID_VALUE, meta, metaLen);
266✔
1192
  pRequest->syncQuery = true;
266✔
1193
  if (!pRequest->pDb) {
266✔
1194
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
UNCOV
1195
    goto end;
×
1196
  }
1197
  // decode and process req
1198
  void*    data = POINTER_SHIFT(meta, sizeof(SMsgHead));
266✔
1199
  uint32_t len = metaLen - sizeof(SMsgHead);
266✔
1200
  tDecoderInit(&coder, data, len);
266✔
1201
  if (tDecodeSVDropStbReq(&coder, &req) < 0) {
266✔
UNCOV
1202
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1203
    goto end;
×
1204
  }
1205

1206
  SCatalog* pCatalog = NULL;
266✔
1207
  RAW_RETURN_CHECK(catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog));
266✔
1208
  SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
266✔
1209
                           .requestId = pRequest->requestId,
266✔
1210
                           .requestObjRefId = pRequest->self,
266✔
1211
                           .mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp)};
266✔
1212
  SName            pName = {0};
266✔
1213
  toName(pRequest->pTscObj->acctId, pRequest->pDb, req.name, &pName);
266✔
1214
  STableMeta* pTableMeta = NULL;
266✔
1215
  code = catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta);
266✔
1216
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
266✔
1217
    code = TSDB_CODE_SUCCESS;
52✔
1218
    taosMemoryFreeClear(pTableMeta);
52✔
1219
    goto end;
52✔
1220
  }
1221
  if (code != TSDB_CODE_SUCCESS) {
214✔
UNCOV
1222
    goto end;
×
1223
  }
1224
  pReq.suid = pTableMeta->uid;
214✔
1225
  taosMemoryFreeClear(pTableMeta);
214✔
1226

1227
  // build drop stable
1228
  pReq.igNotExists = true;
214✔
1229
  pReq.source = TD_REQ_FROM_TAOX;
214✔
1230
  //  pReq.suid = processSuid(req.suid, pRequest->pDb);
1231

1232
  uDebug(LOG_ID_TAG " drop stable name:%s suid:%" PRId64 " new suid:%" PRId64, LOG_ID_VALUE, req.name, req.suid,
214✔
1233
         pReq.suid);
1234
  STscObj* pTscObj = pRequest->pTscObj;
214✔
1235
  SName    tableName = {0};
214✔
1236
  toName(pTscObj->acctId, pRequest->pDb, req.name, &tableName);
214✔
1237
  if (tNameExtractFullName(&tableName, pReq.name) != 0) {
214✔
UNCOV
1238
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1239
    goto end;
×
1240
  }
1241

1242
  SCmdMsgInfo pCmdMsg = {0};
214✔
1243
  pCmdMsg.epSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
214✔
1244
  pCmdMsg.msgType = TDMT_MND_DROP_STB;
214✔
1245
  pCmdMsg.msgLen = tSerializeSMDropStbReq(NULL, 0, &pReq);
214✔
1246
  if (pCmdMsg.msgLen <= 0) {
214✔
UNCOV
1247
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1248
    goto end;
×
1249
  }
1250
  pCmdMsg.pMsg = taosMemoryMalloc(pCmdMsg.msgLen);
214✔
1251
  RAW_NULL_CHECK(pCmdMsg.pMsg);
214✔
1252
  if (tSerializeSMDropStbReq(pCmdMsg.pMsg, pCmdMsg.msgLen, &pReq) <= 0) {
214✔
UNCOV
1253
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1254
    taosMemoryFree(pCmdMsg.pMsg);
×
UNCOV
1255
    goto end;
×
1256
  }
1257

1258
  SQuery pQuery = {0};
214✔
1259
  pQuery.execMode = QUERY_EXEC_MODE_RPC;
214✔
1260
  pQuery.pCmdMsg = &pCmdMsg;
214✔
1261
  pQuery.msgType = pQuery.pCmdMsg->msgType;
214✔
1262
  pQuery.stableQuery = true;
214✔
1263

1264
  launchQueryImpl(pRequest, &pQuery, true, NULL);  // ignore, because return value is pRequest
214✔
1265
  taosMemoryFree(pCmdMsg.pMsg);
214✔
1266
  if (pRequest->code == TSDB_CODE_SUCCESS) {
214✔
1267
    // ignore the error code
1268
    RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
214✔
1269
    RAW_RETURN_CHECK(catalogRemoveTableMeta(pCatalog, &tableName));
214✔
1270
  }
1271

1272
  code = pRequest->code;
214✔
1273

1274
end:
266✔
1275
  uDebug(LOG_ID_TAG " drop stable return, msg:%s", LOG_ID_VALUE, tstrerror(code));
266✔
1276
  destroyRequest(pRequest);
266✔
1277
  tDecoderClear(&coder);
266✔
1278
  return code;
266✔
1279
}
1280

1281
typedef struct SVgroupCreateTableBatch {
1282
  SVCreateTbBatchReq req;
1283
  SVgroupInfo        info;
1284
  char               dbName[TSDB_DB_NAME_LEN];
1285
} SVgroupCreateTableBatch;
1286

1287
static void destroyCreateTbReqBatch(void* data) {
4,109✔
1288
  if (data == NULL) {
4,109✔
UNCOV
1289
    uError("invalid parameter in %s", __func__);
×
UNCOV
1290
    return;
×
1291
  }
1292
  SVgroupCreateTableBatch* pTbBatch = (SVgroupCreateTableBatch*)data;
4,109✔
1293
  taosArrayDestroy(pTbBatch->req.pArray);
4,109✔
1294
}
1295

1296
static int32_t taosCreateTable(TAOS* taos, void* meta, uint32_t metaLen) {
4,049✔
1297
  if (taos == NULL || meta == NULL) {
4,049✔
UNCOV
1298
    uError("invalid parameter in %s", __func__);
×
UNCOV
1299
    return TSDB_CODE_INVALID_PARA;
×
1300
  }
1301
  SVCreateTbBatchReq req = {0};
4,049✔
1302
  SDecoder           coder = {0};
4,049✔
1303
  int32_t            code = TSDB_CODE_SUCCESS;
4,049✔
1304
  SRequestObj*       pRequest = NULL;
4,049✔
1305
  SQuery*            pQuery = NULL;
4,049✔
1306
  SHashObj*          pVgroupHashmap = NULL;
4,049✔
1307
  SArray*            pTagList = taosArrayInit(0, POINTER_BYTES);
4,049✔
1308
  RAW_NULL_CHECK(pTagList);
4,049✔
1309
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
4,049✔
1310
  uDebug(LOG_ID_TAG " create table, meta:%p, metaLen:%d", LOG_ID_VALUE, meta, metaLen);
4,049✔
1311

1312
  pRequest->syncQuery = true;
4,049✔
1313
  if (!pRequest->pDb) {
4,049✔
UNCOV
1314
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
UNCOV
1315
    goto end;
×
1316
  }
1317
  // decode and process req
1318
  void*    data = POINTER_SHIFT(meta, sizeof(SMsgHead));
4,049✔
1319
  uint32_t len = metaLen - sizeof(SMsgHead);
4,049✔
1320
  tDecoderInit(&coder, data, len);
4,049✔
1321
  if (tDecodeSVCreateTbBatchReq(&coder, &req) < 0) {
4,049✔
UNCOV
1322
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1323
    goto end;
×
1324
  }
1325

1326
  STscObj* pTscObj = pRequest->pTscObj;
4,049✔
1327

1328
  SVCreateTbReq* pCreateReq = NULL;
4,049✔
1329
  SCatalog*      pCatalog = NULL;
4,049✔
1330
  RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
4,049✔
1331
  pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
4,049✔
1332
  RAW_NULL_CHECK(pVgroupHashmap);
4,049✔
1333
  taosHashSetFreeFp(pVgroupHashmap, destroyCreateTbReqBatch);
4,049✔
1334

1335
  SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
4,049✔
1336
                           .requestId = pRequest->requestId,
4,049✔
1337
                           .requestObjRefId = pRequest->self,
4,049✔
1338
                           .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
4,049✔
1339

1340
  pRequest->tableList = taosArrayInit(req.nReqs, sizeof(SName));
4,049✔
1341
  RAW_NULL_CHECK(pRequest->tableList);
4,049✔
1342
  // loop to create table
1343
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
8,686✔
1344
    pCreateReq = req.pReqs + iReq;
4,637✔
1345

1346
    SVgroupInfo pInfo = {0};
4,637✔
1347
    SName       pName = {0};
4,637✔
1348
    toName(pTscObj->acctId, pRequest->pDb, pCreateReq->name, &pName);
4,637✔
1349
    code = catalogGetTableHashVgroup(pCatalog, &conn, &pName, &pInfo);
4,637✔
1350
    if (code != TSDB_CODE_SUCCESS) {
4,637✔
UNCOV
1351
      goto end;
×
1352
    }
1353

1354
    pCreateReq->flags |= TD_CREATE_IF_NOT_EXISTS;
4,637✔
1355
    // change tag cid to new cid
1356
    if (pCreateReq->type == TSDB_CHILD_TABLE) {
4,637✔
1357
      STableMeta* pTableMeta = NULL;
4,035✔
1358
      SName       sName = {0};
4,035✔
1359
      tb_uid_t    oldSuid = pCreateReq->ctb.suid;
4,035✔
1360
      //      pCreateReq->ctb.suid = processSuid(pCreateReq->ctb.suid, pRequest->pDb);
1361
      toName(pTscObj->acctId, pRequest->pDb, pCreateReq->ctb.stbName, &sName);
4,035✔
1362
      code = catalogGetTableMeta(pCatalog, &conn, &sName, &pTableMeta);
4,035✔
1363
      if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
4,035✔
UNCOV
1364
        code = TSDB_CODE_SUCCESS;
×
UNCOV
1365
        taosMemoryFreeClear(pTableMeta);
×
UNCOV
1366
        continue;
×
1367
      }
1368

1369
      if (code != TSDB_CODE_SUCCESS) {
4,035✔
UNCOV
1370
        goto end;
×
1371
      }
1372
      pCreateReq->ctb.suid = pTableMeta->uid;
4,035✔
1373

1374
      SArray* pTagVals = NULL;
4,035✔
1375
      code = tTagToValArray((STag*)pCreateReq->ctb.pTag, &pTagVals);
4,035✔
1376
      if (code != TSDB_CODE_SUCCESS) {
4,035✔
1377
        taosMemoryFreeClear(pTableMeta);
×
1378
        goto end;
×
1379
      }
1380

1381
      bool rebuildTag = false;
4,035✔
1382
      for (int32_t i = 0; i < taosArrayGetSize(pCreateReq->ctb.tagName); i++) {
12,507✔
1383
        char* tName = taosArrayGet(pCreateReq->ctb.tagName, i);
8,472✔
1384
        if (tName == NULL) {
8,472✔
UNCOV
1385
          continue;
×
1386
        }
1387
        for (int32_t j = pTableMeta->tableInfo.numOfColumns;
8,472✔
1388
             j < pTableMeta->tableInfo.numOfColumns + pTableMeta->tableInfo.numOfTags; j++) {
33,284✔
1389
          SSchema* tag = &pTableMeta->schema[j];
24,812✔
1390
          if (strcmp(tag->name, tName) == 0 && tag->type != TSDB_DATA_TYPE_JSON) {
24,812✔
1391
            STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, i);
7,988✔
1392
            if (pTagVal) {
7,988✔
1393
              if (pTagVal->cid != tag->colId) {
7,988✔
1394
                pTagVal->cid = tag->colId;
649✔
1395
                rebuildTag = true;
649✔
1396
              }
1397
            } else {
UNCOV
1398
              uError("create tb invalid data %s, size:%d index:%d cid:%d", pCreateReq->name,
×
1399
                     (int)taosArrayGetSize(pTagVals), i, tag->colId);
1400
            }
1401
          }
1402
        }
1403
      }
1404
      taosMemoryFreeClear(pTableMeta);
4,035✔
1405
      if (rebuildTag) {
4,035✔
1406
        STag* ppTag = NULL;
413✔
1407
        code = tTagNew(pTagVals, 1, false, &ppTag);
413✔
1408
        taosArrayDestroy(pTagVals);
413✔
1409
        pTagVals = NULL;
413✔
1410
        if (code != TSDB_CODE_SUCCESS) {
413✔
UNCOV
1411
          goto end;
×
1412
        }
1413
        if (NULL == taosArrayPush(pTagList, &ppTag)) {
413✔
UNCOV
1414
          tTagFree(ppTag);
×
UNCOV
1415
          goto end;
×
1416
        }
1417
        pCreateReq->ctb.pTag = (uint8_t*)ppTag;
413✔
1418
      }
1419
      taosArrayDestroy(pTagVals);
4,035✔
1420
    }
1421
    RAW_NULL_CHECK(taosArrayPush(pRequest->tableList, &pName));
9,274✔
1422

1423
    SVgroupCreateTableBatch* pTableBatch = taosHashGet(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId));
4,637✔
1424
    if (pTableBatch == NULL) {
4,637✔
1425
      SVgroupCreateTableBatch tBatch = {0};
4,109✔
1426
      tBatch.info = pInfo;
4,109✔
1427
      tstrncpy(tBatch.dbName, pRequest->pDb, TSDB_DB_NAME_LEN);
4,109✔
1428

1429
      tBatch.req.pArray = taosArrayInit(4, sizeof(struct SVCreateTbReq));
4,109✔
1430
      RAW_NULL_CHECK(tBatch.req.pArray);
4,109✔
1431
      RAW_NULL_CHECK(taosArrayPush(tBatch.req.pArray, pCreateReq));
8,218✔
1432
      tBatch.req.source = TD_REQ_FROM_TAOX;
4,109✔
1433
      RAW_RETURN_CHECK(taosHashPut(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId), &tBatch, sizeof(tBatch)));
4,109✔
1434
    } else {  // add to the correct vgroup
1435
      RAW_NULL_CHECK(taosArrayPush(pTableBatch->req.pArray, pCreateReq));
1,056✔
1436
    }
1437
  }
1438

1439
  if (taosHashGetSize(pVgroupHashmap) == 0) {
4,049✔
UNCOV
1440
    goto end;
×
1441
  }
1442
  SArray* pBufArray = NULL;
4,049✔
1443
  RAW_RETURN_CHECK(serializeVgroupsCreateTableBatch(pVgroupHashmap, &pBufArray));
4,049✔
1444
  pQuery = NULL;
4,049✔
1445
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode**)&pQuery);
4,049✔
1446
  if (TSDB_CODE_SUCCESS != code) goto end;
4,049✔
1447
  pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
4,049✔
1448
  pQuery->msgType = TDMT_VND_CREATE_TABLE;
4,049✔
1449
  pQuery->stableQuery = false;
4,049✔
1450
  code = nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT, &pQuery->pRoot);
4,049✔
1451
  if (TSDB_CODE_SUCCESS != code) goto end;
4,049✔
1452
  RAW_NULL_CHECK(pQuery->pRoot);
4,049✔
1453

1454
  RAW_RETURN_CHECK(rewriteToVnodeModifyOpStmt(pQuery, pBufArray));
4,049✔
1455

1456
  launchQueryImpl(pRequest, pQuery, true, NULL);
4,049✔
1457
  if (pRequest->code == TSDB_CODE_SUCCESS) {
4,049✔
1458
    RAW_RETURN_CHECK(removeMeta(pTscObj, pRequest->tableList, false));
4,049✔
1459
  }
1460

1461
  code = pRequest->code;
4,049✔
1462

1463
end:
4,049✔
1464
  uDebug(LOG_ID_TAG " create table return, msg:%s", LOG_ID_VALUE, tstrerror(code));
4,049✔
1465
  tDeleteSVCreateTbBatchReq(&req);
4,049✔
1466

1467
  taosHashCleanup(pVgroupHashmap);
4,049✔
1468
  destroyRequest(pRequest);
4,049✔
1469
  tDecoderClear(&coder);
4,049✔
1470
  qDestroyQuery(pQuery);
4,049✔
1471
  taosArrayDestroyP(pTagList, NULL);
4,049✔
1472
  return code;
4,049✔
1473
}
1474

1475
typedef struct SVgroupDropTableBatch {
1476
  SVDropTbBatchReq req;
1477
  SVgroupInfo      info;
1478
  char             dbName[TSDB_DB_NAME_LEN];
1479
} SVgroupDropTableBatch;
1480

1481
static void destroyDropTbReqBatch(void* data) {
136✔
1482
  if (data == NULL) {
136✔
UNCOV
1483
    uError("invalid parameter in %s", __func__);
×
UNCOV
1484
    return;
×
1485
  }
1486
  SVgroupDropTableBatch* pTbBatch = (SVgroupDropTableBatch*)data;
136✔
1487
  taosArrayDestroy(pTbBatch->req.pArray);
136✔
1488
}
1489

1490
static int32_t taosDropTable(TAOS* taos, void* meta, uint32_t metaLen) {
188✔
1491
  if (taos == NULL || meta == NULL) {
188✔
UNCOV
1492
    uError("invalid parameter in %s", __func__);
×
UNCOV
1493
    return TSDB_CODE_INVALID_PARA;
×
1494
  }
1495
  SVDropTbBatchReq req = {0};
188✔
1496
  SDecoder         coder = {0};
188✔
1497
  int32_t          code = TSDB_CODE_SUCCESS;
188✔
1498
  SRequestObj*     pRequest = NULL;
188✔
1499
  SQuery*          pQuery = NULL;
188✔
1500
  SHashObj*        pVgroupHashmap = NULL;
188✔
1501

1502
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
188✔
1503
  uDebug(LOG_ID_TAG " drop table, meta:%p, len:%d", LOG_ID_VALUE, meta, metaLen);
188✔
1504

1505
  pRequest->syncQuery = true;
188✔
1506
  if (!pRequest->pDb) {
188✔
UNCOV
1507
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
UNCOV
1508
    goto end;
×
1509
  }
1510
  // decode and process req
1511
  void*    data = POINTER_SHIFT(meta, sizeof(SMsgHead));
188✔
1512
  uint32_t len = metaLen - sizeof(SMsgHead);
188✔
1513
  tDecoderInit(&coder, data, len);
188✔
1514
  if (tDecodeSVDropTbBatchReq(&coder, &req) < 0) {
188✔
UNCOV
1515
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1516
    goto end;
×
1517
  }
1518

1519
  STscObj* pTscObj = pRequest->pTscObj;
188✔
1520

1521
  SVDropTbReq* pDropReq = NULL;
188✔
1522
  SCatalog*    pCatalog = NULL;
188✔
1523
  RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
188✔
1524

1525
  pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
188✔
1526
  RAW_NULL_CHECK(pVgroupHashmap);
188✔
1527
  taosHashSetFreeFp(pVgroupHashmap, destroyDropTbReqBatch);
188✔
1528

1529
  SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
188✔
1530
                           .requestId = pRequest->requestId,
188✔
1531
                           .requestObjRefId = pRequest->self,
188✔
1532
                           .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
188✔
1533
  pRequest->tableList = taosArrayInit(req.nReqs, sizeof(SName));
188✔
1534
  RAW_NULL_CHECK(pRequest->tableList);
188✔
1535
  // loop to create table
1536
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
431✔
1537
    pDropReq = req.pReqs + iReq;
243✔
1538
    pDropReq->igNotExists = true;
243✔
1539
    //    pDropReq->suid = processSuid(pDropReq->suid, pRequest->pDb);
1540

1541
    SVgroupInfo pInfo = {0};
243✔
1542
    SName       pName = {0};
243✔
1543
    toName(pTscObj->acctId, pRequest->pDb, pDropReq->name, &pName);
243✔
1544
    RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &pInfo));
243✔
1545

1546
    STableMeta* pTableMeta = NULL;
243✔
1547
    code = catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta);
243✔
1548
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
243✔
1549
      code = TSDB_CODE_SUCCESS;
52✔
1550
      taosMemoryFreeClear(pTableMeta);
52✔
1551
      continue;
52✔
1552
    }
1553
    if (code != TSDB_CODE_SUCCESS) {
191✔
UNCOV
1554
      goto end;
×
1555
    }
1556
    tb_uid_t oldSuid = pDropReq->suid;
191✔
1557
    pDropReq->suid = pTableMeta->suid;
191✔
1558
    taosMemoryFreeClear(pTableMeta);
191✔
1559
    uDebug(LOG_ID_TAG " drop table name:%s suid:%" PRId64 " new suid:%" PRId64, LOG_ID_VALUE, pDropReq->name, oldSuid,
191✔
1560
           pDropReq->suid);
1561

1562
    RAW_NULL_CHECK(taosArrayPush(pRequest->tableList, &pName));
382✔
1563
    SVgroupDropTableBatch* pTableBatch = taosHashGet(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId));
191✔
1564
    if (pTableBatch == NULL) {
191✔
1565
      SVgroupDropTableBatch tBatch = {0};
136✔
1566
      tBatch.info = pInfo;
136✔
1567
      tBatch.req.pArray = taosArrayInit(TARRAY_MIN_SIZE, sizeof(SVDropTbReq));
136✔
1568
      RAW_NULL_CHECK(tBatch.req.pArray);
136✔
1569
      RAW_NULL_CHECK(taosArrayPush(tBatch.req.pArray, pDropReq));
272✔
1570
      RAW_RETURN_CHECK(taosHashPut(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId), &tBatch, sizeof(tBatch)));
136✔
1571
    } else {  // add to the correct vgroup
1572
      RAW_NULL_CHECK(taosArrayPush(pTableBatch->req.pArray, pDropReq));
110✔
1573
    }
1574
  }
1575

1576
  if (taosHashGetSize(pVgroupHashmap) == 0) {
188✔
1577
    goto end;
52✔
1578
  }
1579
  SArray* pBufArray = NULL;
136✔
1580
  RAW_RETURN_CHECK(serializeVgroupsDropTableBatch(pVgroupHashmap, &pBufArray));
136✔
1581
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode**)&pQuery);
136✔
1582
  if (TSDB_CODE_SUCCESS != code) goto end;
136✔
1583
  pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
136✔
1584
  pQuery->msgType = TDMT_VND_DROP_TABLE;
136✔
1585
  pQuery->stableQuery = false;
136✔
1586
  pQuery->pRoot = NULL;
136✔
1587
  code = nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT, &pQuery->pRoot);
136✔
1588
  if (TSDB_CODE_SUCCESS != code) goto end;
136✔
1589
  RAW_RETURN_CHECK(rewriteToVnodeModifyOpStmt(pQuery, pBufArray));
136✔
1590

1591
  launchQueryImpl(pRequest, pQuery, true, NULL);
136✔
1592
  if (pRequest->code == TSDB_CODE_SUCCESS) {
136✔
1593
    RAW_RETURN_CHECK(removeMeta(pTscObj, pRequest->tableList, false));
136✔
1594
  }
1595
  code = pRequest->code;
136✔
1596

1597
end:
188✔
1598
  uDebug(LOG_ID_TAG " drop table return, msg:%s", LOG_ID_VALUE, tstrerror(code));
188✔
1599
  taosHashCleanup(pVgroupHashmap);
188✔
1600
  destroyRequest(pRequest);
188✔
1601
  tDecoderClear(&coder);
188✔
1602
  qDestroyQuery(pQuery);
188✔
1603
  return code;
188✔
1604
}
1605

1606
static int32_t taosDeleteData(TAOS* taos, void* meta, uint32_t metaLen) {
106✔
1607
  if (taos == NULL || meta == NULL) {
106✔
UNCOV
1608
    uError("invalid parameter in %s", __func__);
×
UNCOV
1609
    return TSDB_CODE_INVALID_PARA;
×
1610
  }
1611
  SDeleteRes req = {0};
106✔
1612
  SDecoder   coder = {0};
106✔
1613
  char       sql[256] = {0};
106✔
1614
  int32_t    code = TSDB_CODE_SUCCESS;
106✔
1615

1616
  uDebug("connId:0x%" PRIx64 " delete data, meta:%p, len:%d", *(int64_t*)taos, meta, metaLen);
106✔
1617

1618
  // decode and process req
1619
  void*    data = POINTER_SHIFT(meta, sizeof(SMsgHead));
106✔
1620
  uint32_t len = metaLen - sizeof(SMsgHead);
106✔
1621
  tDecoderInit(&coder, data, len);
106✔
1622
  if (tDecodeDeleteRes(&coder, &req) < 0) {
106✔
UNCOV
1623
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1624
    goto end;
×
1625
  }
1626

1627
  (void)snprintf(sql, sizeof(sql), "delete from `%s` where `%s` >= %" PRId64 " and `%s` <= %" PRId64, req.tableFName,
106✔
1628
                 req.tsColName, req.skey, req.tsColName, req.ekey);
1629

1630
  TAOS_RES* res = taosQueryImpl(taos, sql, false, TD_REQ_FROM_TAOX);
106✔
1631
  RAW_NULL_CHECK(res);
106✔
1632
  SRequestObj* pRequest = (SRequestObj*)res;
106✔
1633
  code = pRequest->code;
106✔
1634
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_PAR_GET_META_ERROR) {
106✔
1635
    code = TSDB_CODE_SUCCESS;
26✔
1636
  }
1637
  taos_free_result(res);
106✔
1638

1639
end:
106✔
1640
  uDebug("connId:0x%" PRIx64 " delete data sql:%s, code:%s", *(int64_t*)taos, sql, tstrerror(code));
106✔
1641
  tDecoderClear(&coder);
106✔
1642
  return code;
106✔
1643
}
1644

1645
static int32_t taosAlterTable(TAOS* taos, void* meta, uint32_t metaLen) {
966✔
1646
  if (taos == NULL || meta == NULL) {
966✔
UNCOV
1647
    uError("invalid parameter in %s", __func__);
×
UNCOV
1648
    return TSDB_CODE_INVALID_PARA;
×
1649
  }
1650
  SVAlterTbReq   req = {0};
966✔
1651
  SDecoder       dcoder = {0};
966✔
1652
  int32_t        code = TSDB_CODE_SUCCESS;
966✔
1653
  SRequestObj*   pRequest = NULL;
966✔
1654
  SQuery*        pQuery = NULL;
966✔
1655
  SArray*        pArray = NULL;
966✔
1656
  SVgDataBlocks* pVgData = NULL;
966✔
1657

1658
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
966✔
1659
  uDebug(LOG_ID_TAG " alter table, meta:%p, len:%d", LOG_ID_VALUE, meta, metaLen);
966✔
1660
  pRequest->syncQuery = true;
966✔
1661
  if (!pRequest->pDb) {
966✔
UNCOV
1662
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
UNCOV
1663
    goto end;
×
1664
  }
1665
  // decode and process req
1666
  void*    data = POINTER_SHIFT(meta, sizeof(SMsgHead));
966✔
1667
  uint32_t len = metaLen - sizeof(SMsgHead);
966✔
1668
  tDecoderInit(&dcoder, data, len);
966✔
1669
  if (tDecodeSVAlterTbReq(&dcoder, &req) < 0) {
966✔
UNCOV
1670
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1671
    goto end;
×
1672
  }
1673

1674
  // do not deal TSDB_ALTER_TABLE_UPDATE_OPTIONS
1675
  if (req.action == TSDB_ALTER_TABLE_UPDATE_OPTIONS) {
966✔
1676
    goto end;
161✔
1677
  }
1678

1679
  STscObj*  pTscObj = pRequest->pTscObj;
805✔
1680
  SCatalog* pCatalog = NULL;
805✔
1681
  RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
805✔
1682
  SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
805✔
1683
                           .requestId = pRequest->requestId,
805✔
1684
                           .requestObjRefId = pRequest->self,
805✔
1685
                           .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
805✔
1686

1687
  SVgroupInfo pInfo = {0};
805✔
1688
  SName       pName = {0};
805✔
1689
  toName(pTscObj->acctId, pRequest->pDb, req.tbName, &pName);
805✔
1690
  RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &pInfo));
805✔
1691
  pArray = taosArrayInit(1, sizeof(void*));
805✔
1692
  RAW_NULL_CHECK(pArray);
805✔
1693

1694
  pVgData = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
805✔
1695
  RAW_NULL_CHECK(pVgData);
805✔
1696
  pVgData->vg = pInfo;
805✔
1697

1698
  int tlen = 0;
805✔
1699
  req.source = TD_REQ_FROM_TAOX;
805✔
1700
  tEncodeSize(tEncodeSVAlterTbReq, &req, tlen, code);
805✔
1701
  if (code != 0) {
805✔
UNCOV
1702
    code = terrno;
×
UNCOV
1703
    goto end;
×
1704
  }
1705
  tlen += sizeof(SMsgHead);
805✔
1706
  void* pMsg = taosMemoryMalloc(tlen);
805✔
1707
  RAW_NULL_CHECK(pMsg);
805✔
1708
  ((SMsgHead*)pMsg)->vgId = htonl(pInfo.vgId);
805✔
1709
  ((SMsgHead*)pMsg)->contLen = htonl(tlen);
805✔
1710
  void*    pBuf = POINTER_SHIFT(pMsg, sizeof(SMsgHead));
805✔
1711
  SEncoder coder = {0};
805✔
1712
  tEncoderInit(&coder, pBuf, tlen - sizeof(SMsgHead));
805✔
1713
  code = tEncodeSVAlterTbReq(&coder, &req);
805✔
1714
  if (code != 0) {
805✔
1715
    tEncoderClear(&coder);
×
UNCOV
1716
    code = terrno;
×
UNCOV
1717
    goto end;
×
1718
  }
1719
  tEncoderClear(&coder);
805✔
1720

1721
  pVgData->pData = pMsg;
805✔
1722
  pVgData->size = tlen;
805✔
1723

1724
  pVgData->numOfTables = 1;
805✔
1725
  RAW_NULL_CHECK(taosArrayPush(pArray, &pVgData));
805✔
1726

1727
  pQuery = NULL;
805✔
1728
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode**)&pQuery);
805✔
1729
  if (NULL == pQuery) goto end;
805✔
1730
  pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
805✔
1731
  pQuery->msgType = TDMT_VND_ALTER_TABLE;
805✔
1732
  pQuery->stableQuery = false;
805✔
1733
  pQuery->pRoot = NULL;
805✔
1734
  code = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, &pQuery->pRoot);
805✔
1735
  if (TSDB_CODE_SUCCESS != code) goto end;
805✔
1736
  RAW_RETURN_CHECK(rewriteToVnodeModifyOpStmt(pQuery, pArray));
805✔
1737

1738
  launchQueryImpl(pRequest, pQuery, true, NULL);
805✔
1739

1740
  pVgData = NULL;
805✔
1741
  pArray = NULL;
805✔
1742
  code = pRequest->code;
805✔
1743
  if (code == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
805✔
1744
    code = TSDB_CODE_SUCCESS;
26✔
1745
  }
1746

1747
  if (pRequest->code == TSDB_CODE_SUCCESS) {
805✔
1748
    SExecResult* pRes = &pRequest->body.resInfo.execRes;
779✔
1749
    if (pRes->res != NULL) {
779✔
1750
      code = handleAlterTbExecRes(pRes->res, pCatalog);
644✔
1751
    }
1752
  }
1753
end:
966✔
1754
  uDebug(LOG_ID_TAG " alter table return, meta:%p, len:%d, msg:%s", LOG_ID_VALUE, meta, metaLen, tstrerror(code));
966✔
1755
  taosArrayDestroy(pArray);
966✔
1756
  if (pVgData) taosMemoryFreeClear(pVgData->pData);
966✔
1757
  taosMemoryFreeClear(pVgData);
966✔
1758
  destroyRequest(pRequest);
966✔
1759
  tDecoderClear(&dcoder);
966✔
1760
  qDestroyQuery(pQuery);
966✔
1761
  taosArrayDestroy(req.pMultiTag);
966✔
1762
  return code;
966✔
1763
}
1764

1765
int taos_write_raw_block_with_fields(TAOS* taos, int rows, char* pData, const char* tbname, TAOS_FIELD* fields,
34✔
1766
                                     int numFields) {
1767
  return taos_write_raw_block_with_fields_with_reqid(taos, rows, pData, tbname, fields, numFields, 0);
34✔
1768
}
1769

1770
int taos_write_raw_block_with_fields_with_reqid(TAOS* taos, int rows, char* pData, const char* tbname,
34✔
1771
                                                TAOS_FIELD* fields, int numFields, int64_t reqid) {
1772
  if (taos == NULL || pData == NULL || tbname == NULL) {
34✔
UNCOV
1773
    uError("invalid parameter in %s", __func__);
×
UNCOV
1774
    return TSDB_CODE_INVALID_PARA;
×
1775
  }
1776
  int32_t     code = TSDB_CODE_SUCCESS;
34✔
1777
  STableMeta* pTableMeta = NULL;
34✔
1778
  SQuery*     pQuery = NULL;
34✔
1779
  SHashObj*   pVgHash = NULL;
34✔
1780

1781
  SRequestObj* pRequest = NULL;
34✔
1782
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, reqid));
34✔
1783

1784
  uDebug(LOG_ID_TAG " write raw block with field, rows:%d, pData:%p, tbname:%s, fields:%p, numFields:%d", LOG_ID_VALUE,
34✔
1785
         rows, pData, tbname, fields, numFields);
1786

1787
  pRequest->syncQuery = true;
34✔
1788
  if (!pRequest->pDb) {
34✔
UNCOV
1789
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
UNCOV
1790
    goto end;
×
1791
  }
1792

1793
  SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}};
34✔
1794
  tstrncpy(pName.dbname, pRequest->pDb, sizeof(pName.dbname));
34✔
1795
  tstrncpy(pName.tname, tbname, sizeof(pName.tname));
34✔
1796

1797
  struct SCatalog* pCatalog = NULL;
34✔
1798
  RAW_RETURN_CHECK(catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog));
34✔
1799

1800
  SRequestConnInfo conn = {0};
34✔
1801
  conn.pTrans = pRequest->pTscObj->pAppInfo->pTransporter;
34✔
1802
  conn.requestId = pRequest->requestId;
34✔
1803
  conn.requestObjRefId = pRequest->self;
34✔
1804
  conn.mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp);
34✔
1805

1806
  SVgroupInfo vgData = {0};
34✔
1807
  RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &vgData));
34✔
1808
  RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta));
34✔
1809
  RAW_RETURN_CHECK(smlInitHandle(&pQuery));
34✔
1810
  pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
34✔
1811
  RAW_NULL_CHECK(pVgHash);
34✔
1812
  RAW_RETURN_CHECK(
34✔
1813
      taosHashPut(pVgHash, (const char*)&vgData.vgId, sizeof(vgData.vgId), (char*)&vgData, sizeof(vgData)));
1814
  RAW_RETURN_CHECK(rawBlockBindData(pQuery, pTableMeta, pData, NULL, fields, numFields, false, NULL, 0, false));
34✔
1815
  RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash));
34✔
1816

1817
  launchQueryImpl(pRequest, pQuery, true, NULL);
34✔
1818
  code = pRequest->code;
34✔
1819

1820
end:
34✔
1821
  uDebug(LOG_ID_TAG " write raw block with field return, msg:%s", LOG_ID_VALUE, tstrerror(code));
34✔
1822
  taosMemoryFreeClear(pTableMeta);
34✔
1823
  qDestroyQuery(pQuery);
34✔
1824
  destroyRequest(pRequest);
34✔
1825
  taosHashCleanup(pVgHash);
34✔
1826
  return code;
34✔
1827
}
1828

1829
int taos_write_raw_block(TAOS* taos, int rows, char* pData, const char* tbname) {
238✔
1830
  return taos_write_raw_block_with_reqid(taos, rows, pData, tbname, 0);
238✔
1831
}
1832

1833
int taos_write_raw_block_with_reqid(TAOS* taos, int rows, char* pData, const char* tbname, int64_t reqid) {
238✔
1834
  if (taos == NULL || pData == NULL || tbname == NULL) {
238✔
UNCOV
1835
    return TSDB_CODE_INVALID_PARA;
×
1836
  }
1837
  int32_t     code = TSDB_CODE_SUCCESS;
238✔
1838
  STableMeta* pTableMeta = NULL;
238✔
1839
  SQuery*     pQuery = NULL;
238✔
1840
  SHashObj*   pVgHash = NULL;
238✔
1841

1842
  SRequestObj* pRequest = NULL;
238✔
1843
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, reqid));
238✔
1844

1845
  uDebug(LOG_ID_TAG " write raw block, rows:%d, pData:%p, tbname:%s", LOG_ID_VALUE, rows, pData, tbname);
238✔
1846

1847
  pRequest->syncQuery = true;
238✔
1848
  if (!pRequest->pDb) {
238✔
UNCOV
1849
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
UNCOV
1850
    goto end;
×
1851
  }
1852

1853
  SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}};
238✔
1854
  tstrncpy(pName.dbname, pRequest->pDb, sizeof(pName.dbname));
238✔
1855
  tstrncpy(pName.tname, tbname, sizeof(pName.tname));
238✔
1856

1857
  struct SCatalog* pCatalog = NULL;
238✔
1858
  RAW_RETURN_CHECK(catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog));
238✔
1859

1860
  SRequestConnInfo conn = {0};
238✔
1861
  conn.pTrans = pRequest->pTscObj->pAppInfo->pTransporter;
238✔
1862
  conn.requestId = pRequest->requestId;
238✔
1863
  conn.requestObjRefId = pRequest->self;
238✔
1864
  conn.mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp);
238✔
1865

1866
  SVgroupInfo vgData = {0};
238✔
1867
  RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &vgData));
238✔
1868
  RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta));
238✔
1869
  RAW_RETURN_CHECK(smlInitHandle(&pQuery));
204✔
1870
  pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
204✔
1871
  RAW_NULL_CHECK(pVgHash);
204✔
1872
  RAW_RETURN_CHECK(
204✔
1873
      taosHashPut(pVgHash, (const char*)&vgData.vgId, sizeof(vgData.vgId), (char*)&vgData, sizeof(vgData)));
1874
  RAW_RETURN_CHECK(rawBlockBindData(pQuery, pTableMeta, pData, NULL, NULL, 0, false, NULL, 0, false));
204✔
1875
  RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash));
136✔
1876

1877
  launchQueryImpl(pRequest, pQuery, true, NULL);
136✔
1878
  code = pRequest->code;
136✔
1879

1880
end:
238✔
1881
  uDebug(LOG_ID_TAG " write raw block return, msg:%s", LOG_ID_VALUE, tstrerror(code));
238✔
1882
  taosMemoryFreeClear(pTableMeta);
238✔
1883
  qDestroyQuery(pQuery);
238✔
1884
  destroyRequest(pRequest);
238✔
1885
  taosHashCleanup(pVgHash);
238✔
1886
  return code;
238✔
1887
}
1888

1889
static void* getRawDataFromRes(void* pRetrieve) {
3,927✔
1890
  if (pRetrieve == NULL) {
3,927✔
UNCOV
1891
    uError("invalid parameter in %s", __func__);
×
UNCOV
1892
    return NULL;
×
1893
  }
1894
  void* rawData = NULL;
3,927✔
1895
  // deal with compatibility
1896
  if (*(int64_t*)pRetrieve == RETRIEVE_TABLE_RSP_VERSION) {
3,927✔
UNCOV
1897
    rawData = ((SRetrieveTableRsp*)pRetrieve)->data;
×
1898
  } else if (*(int64_t*)pRetrieve == RETRIEVE_TABLE_RSP_TMQ_VERSION ||
3,927✔
UNCOV
1899
             *(int64_t*)pRetrieve == RETRIEVE_TABLE_RSP_TMQ_RAW_VERSION) {
×
1900
    rawData = ((SRetrieveTableRspForTmq*)pRetrieve)->data;
3,927✔
1901
  }
1902
  return rawData;
3,927✔
1903
}
1904

1905
static int32_t buildCreateTbMap(SMqDataRsp* rsp, SHashObj* pHashObj) {
166✔
1906
  if (rsp == NULL || pHashObj == NULL) {
166✔
UNCOV
1907
    uError("invalid parameter in %s", __func__);
×
UNCOV
1908
    return TSDB_CODE_INVALID_PARA;
×
1909
  }
1910
  // find schema data info
1911
  int32_t       code = 0;
166✔
1912
  SVCreateTbReq pCreateReq = {0};
166✔
1913
  SDecoder      decoderTmp = {0};
166✔
1914

1915
  for (int j = 0; j < rsp->createTableNum; j++) {
590✔
1916
    void** dataTmp = taosArrayGet(rsp->createTableReq, j);
424✔
1917
    RAW_NULL_CHECK(dataTmp);
424✔
1918
    int32_t* lenTmp = taosArrayGet(rsp->createTableLen, j);
424✔
1919
    RAW_NULL_CHECK(lenTmp);
424✔
1920

1921
    tDecoderInit(&decoderTmp, *dataTmp, *lenTmp);
424✔
1922
    RAW_RETURN_CHECK(tDecodeSVCreateTbReq(&decoderTmp, &pCreateReq));
424✔
1923

1924
    if (pCreateReq.type != TSDB_CHILD_TABLE) {
424✔
UNCOV
1925
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1926
      goto end;
×
1927
    }
1928
    if (taosHashGet(pHashObj, pCreateReq.name, strlen(pCreateReq.name)) == NULL) {
424✔
1929
      RAW_RETURN_CHECK(
424✔
1930
          taosHashPut(pHashObj, pCreateReq.name, strlen(pCreateReq.name), &pCreateReq, sizeof(SVCreateTbReq)));
1931
    } else {
UNCOV
1932
      tDestroySVCreateTbReq(&pCreateReq, TSDB_MSG_FLG_DECODE);
×
UNCOV
1933
      pCreateReq = (SVCreateTbReq){0};
×
1934
    }
1935

1936
    tDecoderClear(&decoderTmp);
424✔
1937
  }
1938
  return 0;
166✔
1939

UNCOV
1940
end:
×
UNCOV
1941
  tDecoderClear(&decoderTmp);
×
UNCOV
1942
  tDestroySVCreateTbReq(&pCreateReq, TSDB_MSG_FLG_DECODE);
×
UNCOV
1943
  return code;
×
1944
}
1945

1946
typedef enum {
1947
  WRITE_RAW_INIT_START = 0,
1948
  WRITE_RAW_INIT_OK,
1949
  WRITE_RAW_INIT_FAIL,
1950
} WRITE_RAW_INIT_STATUS;
1951

1952
static SHashObj* writeRawCache = NULL;
1953
static int8_t    initFlag = 0;
1954
static int8_t    initedFlag = WRITE_RAW_INIT_START;
1955

1956
typedef struct {
1957
  SHashObj* pVgHash;
1958
  SHashObj* pNameHash;
1959
  SHashObj* pMetaHash;
1960
} rawCacheInfo;
1961

1962
typedef struct {
1963
  SVgroupInfo vgInfo;
1964
  int64_t     uid;
1965
  int64_t     suid;
1966
} tbInfo;
1967

1968
static void tmqFreeMeta(void* data) {
1,182✔
1969
  if (data == NULL) {
1,182✔
UNCOV
1970
    uError("invalid parameter in %s", __func__);
×
UNCOV
1971
    return;
×
1972
  }
1973
  STableMeta* pTableMeta = *(STableMeta**)data;
1,182✔
1974
  taosMemoryFree(pTableMeta);
1,182✔
1975
}
1976

UNCOV
1977
static void freeRawCache(void* data) {
×
UNCOV
1978
  if (data == NULL) {
×
UNCOV
1979
    uError("invalid parameter in %s", __func__);
×
UNCOV
1980
    return;
×
1981
  }
1982
  rawCacheInfo* pRawCache = (rawCacheInfo*)data;
×
1983
  taosHashCleanup(pRawCache->pMetaHash);
×
UNCOV
1984
  taosHashCleanup(pRawCache->pNameHash);
×
UNCOV
1985
  taosHashCleanup(pRawCache->pVgHash);
×
1986
}
1987

1988
static int32_t initRawCacheHash() {
436✔
1989
  if (writeRawCache == NULL) {
436✔
1990
    writeRawCache = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
436✔
1991
    if (writeRawCache == NULL) {
436✔
1992
      return terrno;
×
1993
    }
1994
    taosHashSetFreeFp(writeRawCache, freeRawCache);
436✔
1995
  }
1996
  return 0;
436✔
1997
}
1998

1999
static bool needRefreshMeta(void* rawData, STableMeta* pTableMeta, SSchemaWrapper* pSW) {
466✔
2000
  if (rawData == NULL || pSW == NULL) {
466✔
UNCOV
2001
    return false;
×
2002
  }
2003
  if (pTableMeta == NULL) {
466✔
2004
    uError("invalid parameter in %s", __func__);
×
UNCOV
2005
    return false;
×
2006
  }
2007
  char* p = (char*)rawData;
466✔
2008
  // | version | total length | total rows | blankFill | total columns | flag seg| block group id | column schema | each
2009
  // column length |
2010
  p += sizeof(int32_t);
466✔
2011
  p += sizeof(int32_t);
466✔
2012
  p += sizeof(int32_t);
466✔
2013
  p += sizeof(int32_t);
466✔
2014
  p += sizeof(int32_t);
466✔
2015
  p += sizeof(uint64_t);
466✔
2016
  int8_t* fields = p;
466✔
2017

2018
  if (pSW->nCols != pTableMeta->tableInfo.numOfColumns) {
466✔
2019
    return true;
140✔
2020
  }
2021

2022
  for (int i = 0; i < pSW->nCols; i++) {
1,710✔
2023
    int j = 0;
1,384✔
2024
    for (; j < pTableMeta->tableInfo.numOfColumns; j++) {
3,660✔
2025
      SSchema*    pColSchema = &pTableMeta->schema[j];
3,660✔
2026
      SSchemaExt* pColExtSchema = &pTableMeta->schemaExt[j];
3,660✔
2027
      char*       fieldName = pSW->pSchema[i].name;
3,660✔
2028

2029
      if (strcmp(pColSchema->name, fieldName) == 0) {
3,660✔
2030
        if (checkSchema(pColSchema, pColExtSchema, fields, NULL, 0) != 0) {
1,384✔
UNCOV
2031
          return true;
×
2032
        }
2033
        break;
1,384✔
2034
      }
2035
    }
2036
    fields += sizeof(int8_t) + sizeof(int32_t);
1,384✔
2037

2038
    if (j == pTableMeta->tableInfo.numOfColumns) return true;
1,384✔
2039
  }
2040
  return false;
326✔
2041
}
2042

2043
static int32_t getRawCache(SHashObj** pVgHash, SHashObj** pNameHash, SHashObj** pMetaHash, void* key) {
1,447✔
2044
  if (pVgHash == NULL || pNameHash == NULL || pMetaHash == NULL || key == NULL) {
1,447✔
UNCOV
2045
    uError("invalid parameter in %s", __func__);
×
UNCOV
2046
    return TSDB_CODE_INVALID_PARA;
×
2047
  }
2048
  int32_t code = 0;
1,447✔
2049
  void*   cacheInfo = taosHashGet(writeRawCache, &key, POINTER_BYTES);
1,447✔
2050
  if (cacheInfo == NULL) {
1,447✔
2051
    *pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
1,447✔
2052
    RAW_NULL_CHECK(*pVgHash);
1,447✔
2053
    *pNameHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,447✔
2054
    RAW_NULL_CHECK(*pNameHash);
1,447✔
2055
    *pMetaHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
1,447✔
2056
    RAW_NULL_CHECK(*pMetaHash);
1,447✔
2057
    taosHashSetFreeFp(*pMetaHash, tmqFreeMeta);
1,447✔
2058
    rawCacheInfo info = {*pVgHash, *pNameHash, *pMetaHash};
1,447✔
2059
    RAW_RETURN_CHECK(taosHashPut(writeRawCache, &key, POINTER_BYTES, &info, sizeof(rawCacheInfo)));
1,447✔
2060
  } else {
UNCOV
2061
    rawCacheInfo* info = (rawCacheInfo*)cacheInfo;
×
UNCOV
2062
    *pVgHash = info->pVgHash;
×
UNCOV
2063
    *pNameHash = info->pNameHash;
×
UNCOV
2064
    *pMetaHash = info->pMetaHash;
×
2065
  }
2066

2067
  return 0;
1,447✔
UNCOV
2068
end:
×
UNCOV
2069
  taosHashCleanup(*pMetaHash);
×
UNCOV
2070
  taosHashCleanup(*pNameHash);
×
UNCOV
2071
  taosHashCleanup(*pVgHash);
×
UNCOV
2072
  return code;
×
2073
}
2074

2075
static int32_t buildRawRequest(TAOS* taos, SRequestObj** pRequest, SCatalog** pCatalog, SRequestConnInfo* conn) {
1,447✔
2076
  if (taos == NULL || pRequest == NULL || pCatalog == NULL || conn == NULL) {
1,447✔
UNCOV
2077
    uError("invalid parameter in %s", __func__);
×
UNCOV
2078
    return TSDB_CODE_INVALID_PARA;
×
2079
  }
2080
  int32_t code = 0;
1,447✔
2081
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, pRequest, 0));
1,447✔
2082
  (*pRequest)->syncQuery = true;
1,447✔
2083
  if (!(*pRequest)->pDb) {
1,447✔
2084
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
UNCOV
2085
    goto end;
×
2086
  }
2087

2088
  RAW_RETURN_CHECK(catalogGetHandle((*pRequest)->pTscObj->pAppInfo->clusterId, pCatalog));
1,447✔
2089
  conn->pTrans = (*pRequest)->pTscObj->pAppInfo->pTransporter;
1,447✔
2090
  conn->requestId = (*pRequest)->requestId;
1,447✔
2091
  conn->requestObjRefId = (*pRequest)->self;
1,447✔
2092
  conn->mgmtEps = getEpSet_s(&(*pRequest)->pTscObj->pAppInfo->mgmtEp);
1,447✔
2093

2094
end:
1,447✔
2095
  return code;
1,447✔
2096
}
2097

2098
typedef int32_t _raw_decode_func_(SDecoder* pDecoder, SMqDataRsp* pRsp);
2099
static int32_t  decodeRawData(SDecoder* decoder, void* data, uint32_t dataLen, _raw_decode_func_ func,
1,447✔
2100
                              SMqRspObj* rspObj) {
2101
  if (decoder == NULL || data == NULL || func == NULL || rspObj == NULL) {
1,447✔
UNCOV
2102
    uError("invalid parameter in %s", __func__);
×
UNCOV
2103
    return TSDB_CODE_INVALID_PARA;
×
2104
  }
2105
  int8_t dataVersion = *(int8_t*)data;
1,447✔
2106
  if (dataVersion >= MQ_DATA_RSP_VERSION) {
1,447✔
2107
    data = POINTER_SHIFT(data, sizeof(int8_t) + sizeof(int32_t));
1,447✔
2108
    if (dataLen < sizeof(int8_t) + sizeof(int32_t)) {
1,447✔
UNCOV
2109
      return TSDB_CODE_INVALID_PARA;
×
2110
    }
2111
    dataLen -= sizeof(int8_t) + sizeof(int32_t);
1,447✔
2112
  }
2113

2114
  rspObj->resIter = -1;
1,447✔
2115
  tDecoderInit(decoder, data, dataLen);
1,447✔
2116
  int32_t code = func(decoder, &rspObj->dataRsp);
1,447✔
2117
  if (code != 0) {
1,447✔
UNCOV
2118
    SET_ERROR_MSG("decode mq taosx data rsp failed");
×
2119
  }
2120
  return code;
1,447✔
2121
}
2122

2123
static int32_t processCacheMeta(SHashObj* pVgHash, SHashObj* pNameHash, SHashObj* pMetaHash,
3,927✔
2124
                                SVCreateTbReq* pCreateReqDst, SCatalog* pCatalog, SRequestConnInfo* conn, SName* pName,
2125
                                STableMeta** pMeta, SSchemaWrapper* pSW, void* rawData, int32_t retry) {
2126
  if (pVgHash == NULL || pNameHash == NULL || pMetaHash == NULL || pCatalog == NULL || conn == NULL || pName == NULL ||
3,927✔
2127
      pMeta == NULL) {
UNCOV
2128
    uError("invalid parameter in %s", __func__);
×
UNCOV
2129
    return TSDB_CODE_INVALID_PARA;
×
2130
  }
2131
  int32_t     code = 0;
3,927✔
2132
  STableMeta* pTableMeta = NULL;
3,927✔
2133
  tbInfo*     tmpInfo = (tbInfo*)taosHashGet(pNameHash, pName->tname, strlen(pName->tname));
3,927✔
2134
  if (tmpInfo == NULL || retry > 0) {
3,927✔
2135
    tbInfo info = {0};
3,461✔
2136

2137
    RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, conn, pName, &info.vgInfo));
3,461✔
2138
    if (pCreateReqDst && tmpInfo == NULL) {  // change stable name to get meta
3,461✔
2139
      tstrncpy(pName->tname, pCreateReqDst->ctb.stbName, TSDB_TABLE_NAME_LEN);
424✔
2140
    }
2141
    RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, conn, pName, &pTableMeta));
3,461✔
2142
    info.uid = pTableMeta->uid;
3,461✔
2143
    if (pTableMeta->tableType == TSDB_CHILD_TABLE) {
3,461✔
2144
      info.suid = pTableMeta->suid;
2,571✔
2145
    } else {
2146
      info.suid = pTableMeta->uid;
890✔
2147
    }
2148
    code = taosHashPut(pMetaHash, &info.suid, LONG_BYTES, &pTableMeta, POINTER_BYTES);
3,461✔
2149
    if (code != 0) {
3,461✔
UNCOV
2150
      taosMemoryFree(pTableMeta);
×
UNCOV
2151
      goto end;
×
2152
    }
2153
    uDebug("put table meta to hash1, suid:%" PRId64 ", metaHashSIze:%d, nameHashSize:%d, vgHashSize:%d", info.suid, taosHashGetSize(pMetaHash),
3,461✔
2154
           taosHashGetSize(pNameHash), taosHashGetSize(pVgHash));
2155
    if (pCreateReqDst) {
3,461✔
2156
      pTableMeta->vgId = info.vgInfo.vgId;
424✔
2157
      pTableMeta->uid = pCreateReqDst->uid;
424✔
2158
      pCreateReqDst->ctb.suid = pTableMeta->suid;
424✔
2159
    }
2160

2161
    RAW_RETURN_CHECK(taosHashPut(pNameHash, pName->tname, strlen(pName->tname), &info, sizeof(tbInfo)));
3,461✔
2162
    tmpInfo = (tbInfo*)taosHashGet(pNameHash, pName->tname, strlen(pName->tname));
3,461✔
2163
    RAW_RETURN_CHECK(
3,461✔
2164
        taosHashPut(pVgHash, &info.vgInfo.vgId, sizeof(info.vgInfo.vgId), &info.vgInfo, sizeof(SVgroupInfo)));
2165
  }
2166

2167
  if (pTableMeta == NULL || retry > 0) {
3,927✔
2168
    STableMeta** pTableMetaTmp = (STableMeta**)taosHashGet(pMetaHash, &tmpInfo->suid, LONG_BYTES);
466✔
2169
    if (pTableMetaTmp == NULL || retry > 0 || needRefreshMeta(rawData, *pTableMetaTmp, pSW)) {
466✔
2170
      RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, conn, pName, &pTableMeta));
140✔
2171
      code = taosHashPut(pMetaHash, &tmpInfo->suid, LONG_BYTES, &pTableMeta, POINTER_BYTES);
140✔
2172
      if (code != 0) {
140✔
UNCOV
2173
        taosMemoryFree(pTableMeta);
×
UNCOV
2174
        goto end;
×
2175
      }
2176
      uDebug("put table meta to hash2, suid:%" PRId64 ", metaHashSIze:%d, nameHashSize:%d, vgHashSize:%d", tmpInfo->suid, taosHashGetSize(pMetaHash),
140✔
2177
      taosHashGetSize(pNameHash), taosHashGetSize(pVgHash));
2178
    } else {
2179
      pTableMeta = *pTableMetaTmp;
326✔
2180
      pTableMeta->uid = tmpInfo->uid;
326✔
2181
      pTableMeta->vgId = tmpInfo->vgInfo.vgId;
326✔
2182
    }
2183
  }
2184
  *pMeta = pTableMeta;
3,927✔
2185

2186
end:
3,927✔
2187
  return code;
3,927✔
2188
}
2189

2190
static int32_t tmqWriteRawDataImpl(TAOS* taos, void* data, uint32_t dataLen) {
1,281✔
2191
  if (taos == NULL || data == NULL) {
1,281✔
UNCOV
2192
    uError("invalid parameter in %s", __func__);
×
UNCOV
2193
    return TSDB_CODE_INVALID_PARA;
×
2194
  }
2195
  int32_t   code = TSDB_CODE_SUCCESS;
1,281✔
2196
  SQuery*   pQuery = NULL;
1,281✔
2197
  SMqRspObj rspObj = {0};
1,281✔
2198
  SDecoder  decoder = {0};
1,281✔
2199

2200
  SRequestObj*     pRequest = NULL;
1,281✔
2201
  SCatalog*        pCatalog = NULL;
1,281✔
2202
  SRequestConnInfo conn = {0};
1,281✔
2203
  RAW_RETURN_CHECK(buildRawRequest(taos, &pRequest, &pCatalog, &conn));
1,281✔
2204
  uDebug(LOG_ID_TAG " write raw data, data:%p, dataLen:%d", LOG_ID_VALUE, data, dataLen);
1,281✔
2205
  RAW_RETURN_CHECK(decodeRawData(&decoder, data, dataLen, tDecodeMqDataRsp, &rspObj));
1,281✔
2206

2207
  SHashObj* pVgHash = NULL;
1,281✔
2208
  SHashObj* pNameHash = NULL;
1,281✔
2209
  SHashObj* pMetaHash = NULL;
1,281✔
2210
  RAW_RETURN_CHECK(getRawCache(&pVgHash, &pNameHash, &pMetaHash, taos));
1,281✔
2211
  int retry = 0;
1,281✔
2212
  while (1) {
2213
    RAW_RETURN_CHECK(smlInitHandle(&pQuery));
1,281✔
2214
    uDebug(LOG_ID_TAG " write raw meta data block num:%d", LOG_ID_VALUE, rspObj.dataRsp.blockNum);
1,281✔
2215
    while (++rspObj.resIter < rspObj.dataRsp.blockNum) {
4,466✔
2216
      if (!rspObj.dataRsp.withSchema) {
3,185✔
UNCOV
2217
        goto end;
×
2218
      }
2219

2220
      const char* tbName = (const char*)taosArrayGetP(rspObj.dataRsp.blockTbName, rspObj.resIter);
3,185✔
2221
      RAW_NULL_CHECK(tbName);
3,185✔
2222
      SSchemaWrapper* pSW = (SSchemaWrapper*)taosArrayGetP(rspObj.dataRsp.blockSchema, rspObj.resIter);
3,185✔
2223
      RAW_NULL_CHECK(pSW);
3,185✔
2224
      void* pRetrieve = taosArrayGetP(rspObj.dataRsp.blockData, rspObj.resIter);
3,185✔
2225
      RAW_NULL_CHECK(pRetrieve);
3,185✔
2226
      void* rawData = getRawDataFromRes(pRetrieve);
3,185✔
2227
      RAW_NULL_CHECK(rawData);
3,185✔
2228

2229
      uTrace(LOG_ID_TAG " write raw data block tbname:%s", LOG_ID_VALUE, tbName);
3,185✔
2230
      SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}};
3,185✔
2231
      tstrncpy(pName.dbname, pRequest->pDb, TSDB_DB_NAME_LEN);
3,185✔
2232
      tstrncpy(pName.tname, tbName, TSDB_TABLE_NAME_LEN);
3,185✔
2233

2234
      STableMeta* pTableMeta = NULL;
3,185✔
2235
      RAW_RETURN_CHECK(processCacheMeta(pVgHash, pNameHash, pMetaHash, NULL, pCatalog, &conn, &pName, &pTableMeta, pSW,
3,185✔
2236
                                        rawData, retry));
2237
      char err[ERR_MSG_LEN] = {0};
3,185✔
2238
      code = rawBlockBindData(pQuery, pTableMeta, rawData, NULL, pSW, pSW->nCols, true, err, ERR_MSG_LEN, true);
3,185✔
2239
      if (code != TSDB_CODE_SUCCESS) {
3,185✔
UNCOV
2240
        SET_ERROR_MSG("table:%s, err:%s", pName.tname, err);
×
UNCOV
2241
        goto end;
×
2242
      }
2243
    }
2244
    RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash));
1,281✔
2245
    launchQueryImpl(pRequest, pQuery, true, NULL);
1,281✔
2246
    code = pRequest->code;
1,281✔
2247

2248
    if (NEED_CLIENT_HANDLE_ERROR(code) && retry++ < 3) {
1,281✔
UNCOV
2249
      uInfo("write raw retry:%d/3 end code:%d, msg:%s", retry, code, tstrerror(code));
×
UNCOV
2250
      qDestroyQuery(pQuery);
×
UNCOV
2251
      pQuery = NULL;
×
2252
      rspObj.resIter = -1;
×
2253
      continue;
×
2254
    }
2255
    break;
1,281✔
2256
  }
2257

2258
end:
1,281✔
2259
  uDebug(LOG_ID_TAG " write raw data return, msg:%s", LOG_ID_VALUE, tstrerror(code));
1,281✔
2260
  tDeleteMqDataRsp(&rspObj.dataRsp);
1,281✔
2261
  tDecoderClear(&decoder);
1,281✔
2262
  qDestroyQuery(pQuery);
1,281✔
2263
  destroyRequest(pRequest);
1,281✔
2264
  return code;
1,281✔
2265
}
2266

2267
static int32_t tmqWriteRawMetaDataImpl(TAOS* taos, void* data, uint32_t dataLen) {
166✔
2268
  if (taos == NULL || data == NULL) {
166✔
UNCOV
2269
    uError("invalid parameter in %s", __func__);
×
UNCOV
2270
    return TSDB_CODE_INVALID_PARA;
×
2271
  }
2272
  int32_t   code = TSDB_CODE_SUCCESS;
166✔
2273
  SQuery*   pQuery = NULL;
166✔
2274
  SMqRspObj rspObj = {0};
166✔
2275
  SDecoder  decoder = {0};
166✔
2276
  SHashObj* pCreateTbHash = NULL;
166✔
2277

2278
  SRequestObj*     pRequest = NULL;
166✔
2279
  SCatalog*        pCatalog = NULL;
166✔
2280
  SRequestConnInfo conn = {0};
166✔
2281

2282
  RAW_RETURN_CHECK(buildRawRequest(taos, &pRequest, &pCatalog, &conn));
166✔
2283
  uDebug(LOG_ID_TAG " write raw metadata, data:%p, dataLen:%d", LOG_ID_VALUE, data, dataLen);
166✔
2284
  RAW_RETURN_CHECK(decodeRawData(&decoder, data, dataLen, tDecodeSTaosxRsp, &rspObj));
166✔
2285

2286
  pCreateTbHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
166✔
2287
  RAW_NULL_CHECK(pCreateTbHash);
166✔
2288
  RAW_RETURN_CHECK(buildCreateTbMap(&rspObj.dataRsp, pCreateTbHash));
166✔
2289

2290
  SHashObj* pVgHash = NULL;
166✔
2291
  SHashObj* pNameHash = NULL;
166✔
2292
  SHashObj* pMetaHash = NULL;
166✔
2293
  RAW_RETURN_CHECK(getRawCache(&pVgHash, &pNameHash, &pMetaHash, taos));
166✔
2294
  int retry = 0;
166✔
2295
  while (1) {
2296
    RAW_RETURN_CHECK(smlInitHandle(&pQuery));
166✔
2297
    uDebug(LOG_ID_TAG " write raw meta data block num:%d", LOG_ID_VALUE, rspObj.dataRsp.blockNum);
166✔
2298
    while (++rspObj.resIter < rspObj.dataRsp.blockNum) {
908✔
2299
      if (!rspObj.dataRsp.withSchema) {
742✔
UNCOV
2300
        goto end;
×
2301
      }
2302

2303
      const char* tbName = (const char*)taosArrayGetP(rspObj.dataRsp.blockTbName, rspObj.resIter);
742✔
2304
      RAW_NULL_CHECK(tbName);
742✔
2305
      SSchemaWrapper* pSW = (SSchemaWrapper*)taosArrayGetP(rspObj.dataRsp.blockSchema, rspObj.resIter);
742✔
2306
      RAW_NULL_CHECK(pSW);
742✔
2307
      void* pRetrieve = taosArrayGetP(rspObj.dataRsp.blockData, rspObj.resIter);
742✔
2308
      RAW_NULL_CHECK(pRetrieve);
742✔
2309
      void* rawData = getRawDataFromRes(pRetrieve);
742✔
2310
      RAW_NULL_CHECK(rawData);
742✔
2311

2312
      uTrace(LOG_ID_TAG " write raw data block tbname:%s", LOG_ID_VALUE, tbName);
742✔
2313
      SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}};
742✔
2314
      tstrncpy(pName.dbname, pRequest->pDb, TSDB_DB_NAME_LEN);
742✔
2315
      tstrncpy(pName.tname, tbName, TSDB_TABLE_NAME_LEN);
742✔
2316

2317
      // find schema data info
2318
      SVCreateTbReq* pCreateReqDst = (SVCreateTbReq*)taosHashGet(pCreateTbHash, pName.tname, strlen(pName.tname));
742✔
2319
      STableMeta*    pTableMeta = NULL;
742✔
2320
      RAW_RETURN_CHECK(processCacheMeta(pVgHash, pNameHash, pMetaHash, pCreateReqDst, pCatalog, &conn, &pName,
742✔
2321
                                        &pTableMeta, pSW, rawData, retry));
2322
      char err[ERR_MSG_LEN] = {0};
742✔
2323
      code =
2324
          rawBlockBindData(pQuery, pTableMeta, rawData, pCreateReqDst, pSW, pSW->nCols, true, err, ERR_MSG_LEN, true);
742✔
2325
      if (code != TSDB_CODE_SUCCESS) {
742✔
UNCOV
2326
        SET_ERROR_MSG("table:%s, err:%s", pName.tname, err);
×
UNCOV
2327
        goto end;
×
2328
      }
2329
    }
2330
    RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash));
166✔
2331
    launchQueryImpl(pRequest, pQuery, true, NULL);
166✔
2332
    code = pRequest->code;
166✔
2333

2334
    if (NEED_CLIENT_HANDLE_ERROR(code) && retry++ < 3) {
166✔
UNCOV
2335
      uInfo("write raw retry:%d/3 end code:%d, msg:%s", retry, code, tstrerror(code));
×
UNCOV
2336
      qDestroyQuery(pQuery);
×
UNCOV
2337
      pQuery = NULL;
×
2338
      rspObj.resIter = -1;
×
2339
      continue;
×
2340
    }
2341
    break;
166✔
2342
  }
2343

2344
end:
166✔
2345
  uDebug(LOG_ID_TAG " write raw metadata return, msg:%s", LOG_ID_VALUE, tstrerror(code));
166✔
2346
  tDeleteSTaosxRsp(&rspObj.dataRsp);
166✔
2347
  void* pIter = taosHashIterate(pCreateTbHash, NULL);
166✔
2348
  while (pIter) {
590✔
2349
    tDestroySVCreateTbReq(pIter, TSDB_MSG_FLG_DECODE);
424✔
2350
    pIter = taosHashIterate(pCreateTbHash, pIter);
424✔
2351
  }
2352
  taosHashCleanup(pCreateTbHash);
166✔
2353
  tDecoderClear(&decoder);
166✔
2354
  qDestroyQuery(pQuery);
166✔
2355
  destroyRequest(pRequest);
166✔
2356
  return code;
166✔
2357
}
2358

UNCOV
2359
static int32_t tmqWriteRawRawDataImpl(TAOS* taos, void* data, uint32_t dataLen) {
×
UNCOV
2360
  if (taos == NULL || data == NULL) {
×
UNCOV
2361
    uError("invalid parameter in %s", __func__);
×
UNCOV
2362
    return TSDB_CODE_INVALID_PARA;
×
2363
  }
UNCOV
2364
  int32_t   code = TSDB_CODE_SUCCESS;
×
UNCOV
2365
  SQuery*   pQuery = NULL;
×
UNCOV
2366
  SHashObj* pVgroupHash = NULL;
×
UNCOV
2367
  SMqRspObj rspObj = {0};
×
UNCOV
2368
  SDecoder  decoder = {0};
×
2369

UNCOV
2370
  SRequestObj*     pRequest = NULL;
×
2371
  SCatalog*        pCatalog = NULL;
×
2372
  SRequestConnInfo conn = {0};
×
2373

2374
  RAW_RETURN_CHECK(buildRawRequest(taos, &pRequest, &pCatalog, &conn));
×
UNCOV
2375
  uDebug(LOG_ID_TAG " write raw rawdata, data:%p, dataLen:%d", LOG_ID_VALUE, data, dataLen);
×
2376
  RAW_RETURN_CHECK(decodeRawData(&decoder, data, dataLen, tDecodeMqDataRsp, &rspObj));
×
2377

2378
  SHashObj* pVgHash = NULL;
×
2379
  SHashObj* pNameHash = NULL;
×
2380
  SHashObj* pMetaHash = NULL;
×
UNCOV
2381
  RAW_RETURN_CHECK(getRawCache(&pVgHash, &pNameHash, &pMetaHash, taos));
×
2382
  int retry = 0;
×
2383
  while (1) {
×
2384
    RAW_RETURN_CHECK(smlInitHandle(&pQuery));
×
UNCOV
2385
    uDebug(LOG_ID_TAG " write raw rawdata block num:%d", LOG_ID_VALUE, rspObj.dataRsp.blockNum);
×
2386
    SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)(pQuery)->pRoot;
×
2387
    pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
×
2388
    RAW_NULL_CHECK(pVgroupHash);
×
UNCOV
2389
    pStmt->pVgDataBlocks = taosArrayInit(8, POINTER_BYTES);
×
2390
    RAW_NULL_CHECK(pStmt->pVgDataBlocks);
×
2391

2392
    while (++rspObj.resIter < rspObj.dataRsp.blockNum) {
×
2393
      const char* tbName = (const char*)taosArrayGetP(rspObj.dataRsp.blockTbName, rspObj.resIter);
×
2394
      RAW_NULL_CHECK(tbName);
×
2395
      void* pRetrieve = taosArrayGetP(rspObj.dataRsp.blockData, rspObj.resIter);
×
2396
      RAW_NULL_CHECK(pRetrieve);
×
2397
      void* rawData = getRawDataFromRes(pRetrieve);
×
2398
      RAW_NULL_CHECK(rawData);
×
2399

2400
      uTrace(LOG_ID_TAG " write raw data block tbname:%s", LOG_ID_VALUE, tbName);
×
2401
      SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}};
×
2402
      tstrncpy(pName.dbname, pRequest->pDb, TSDB_DB_NAME_LEN);
×
UNCOV
2403
      tstrncpy(pName.tname, tbName, TSDB_TABLE_NAME_LEN);
×
2404

2405
      // find schema data info
2406
      STableMeta* pTableMeta = NULL;
×
2407
      RAW_RETURN_CHECK(processCacheMeta(pVgHash, pNameHash, pMetaHash, NULL, pCatalog, &conn, &pName, &pTableMeta, NULL,
×
2408
                                        NULL, retry));
2409
      char err[ERR_MSG_LEN] = {0};
×
2410
      code = rawBlockBindRawData(pVgroupHash, pStmt->pVgDataBlocks, pTableMeta, rawData);
×
UNCOV
2411
      if (code != TSDB_CODE_SUCCESS) {
×
2412
        SET_ERROR_MSG("table:%s, err:%s", pName.tname, err);
×
2413
        goto end;
×
2414
      }
2415
    }
UNCOV
2416
    taosHashCleanup(pVgroupHash);
×
UNCOV
2417
    pVgroupHash = NULL;
×
2418

2419
    RAW_RETURN_CHECK(smlBuildOutputRaw(pQuery, pVgHash));
×
UNCOV
2420
    launchQueryImpl(pRequest, pQuery, true, NULL);
×
2421
    code = pRequest->code;
×
2422

2423
    if (NEED_CLIENT_HANDLE_ERROR(code) && retry++ < 3) {
×
2424
      uInfo("write raw retry:%d/3 end code:%d, msg:%s", retry, code, tstrerror(code));
×
2425
      qDestroyQuery(pQuery);
×
UNCOV
2426
      pQuery = NULL;
×
UNCOV
2427
      rspObj.resIter = -1;
×
2428
      continue;
×
2429
    }
UNCOV
2430
    break;
×
2431
  }
2432

2433
end:
×
UNCOV
2434
  uDebug(LOG_ID_TAG " write raw rawdata return, msg:%s", LOG_ID_VALUE, tstrerror(code));
×
2435
  tDeleteMqDataRsp(&rspObj.dataRsp);
×
2436
  tDecoderClear(&decoder);
×
2437
  qDestroyQuery(pQuery);
×
2438
  taosHashCleanup(pVgroupHash);
×
2439
  destroyRequest(pRequest);
×
2440
  return code;
×
2441
}
2442

2443
static void processSimpleMeta(SMqMetaRsp* pMetaRsp, cJSON** meta) {
10,191✔
2444
  if (pMetaRsp == NULL || meta == NULL) {
10,191✔
2445
    uError("invalid parameter in %s", __func__);
×
2446
    return;
×
2447
  }
2448
  if (pMetaRsp->resMsgType == TDMT_VND_CREATE_STB) {
10,191✔
2449
    processCreateStb(pMetaRsp, meta);
2,557✔
2450
  } else if (pMetaRsp->resMsgType == TDMT_VND_ALTER_STB) {
7,634✔
2451
    processAlterStb(pMetaRsp, meta);
1,717✔
2452
  } else if (pMetaRsp->resMsgType == TDMT_VND_DROP_STB) {
5,917✔
2453
    processDropSTable(pMetaRsp, meta);
266✔
2454
  } else if (pMetaRsp->resMsgType == TDMT_VND_CREATE_TABLE) {
5,651✔
2455
    processCreateTable(pMetaRsp, meta);
4,347✔
2456
  } else if (pMetaRsp->resMsgType == TDMT_VND_ALTER_TABLE) {
1,304✔
2457
    processAlterTable(pMetaRsp, meta);
1,010✔
2458
  } else if (pMetaRsp->resMsgType == TDMT_VND_DROP_TABLE) {
294✔
2459
    processDropTable(pMetaRsp, meta);
188✔
2460
  } else if (pMetaRsp->resMsgType == TDMT_VND_DELETE) {
106✔
2461
    processDeleteTable(pMetaRsp, meta);
106✔
2462
  }
2463
}
2464

2465
static void processBatchMetaToJson(SMqBatchMetaRsp* pMsgRsp, char** string) {
693✔
2466
  if (pMsgRsp == NULL || string == NULL) {
693✔
UNCOV
2467
    uError("invalid parameter in %s", __func__);
×
UNCOV
2468
    return;
×
2469
  }
2470
  SDecoder        coder = {0};
693✔
2471
  SMqBatchMetaRsp rsp = {0};
693✔
2472
  int32_t         code = 0;
693✔
2473
  cJSON*          pJson = NULL;
693✔
2474
  tDecoderInit(&coder, pMsgRsp->pMetaBuff, pMsgRsp->metaBuffLen);
693✔
2475
  if (tDecodeMqBatchMetaRsp(&coder, &rsp) < 0) {
693✔
UNCOV
2476
    goto end;
×
2477
  }
2478

2479
  pJson = cJSON_CreateObject();
693✔
2480
  RAW_NULL_CHECK(pJson);
693✔
2481
  RAW_FALSE_CHECK(cJSON_AddStringToObject(pJson, "tmq_meta_version", TMQ_META_VERSION));
693✔
2482
  cJSON* pMetaArr = cJSON_CreateArray();
693✔
2483
  RAW_NULL_CHECK(pMetaArr);
693✔
2484
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(pJson, "metas", pMetaArr));
693✔
2485

2486
  int32_t num = taosArrayGetSize(rsp.batchMetaReq);
693✔
2487
  for (int32_t i = 0; i < num; i++) {
4,551✔
2488
    int32_t* len = taosArrayGet(rsp.batchMetaLen, i);
3,858✔
2489
    RAW_NULL_CHECK(len);
3,858✔
2490
    void* tmpBuf = taosArrayGetP(rsp.batchMetaReq, i);
3,858✔
2491
    RAW_NULL_CHECK(tmpBuf);
3,858✔
2492
    SDecoder   metaCoder = {0};
3,858✔
2493
    SMqMetaRsp metaRsp = {0};
3,858✔
2494
    tDecoderInit(&metaCoder, POINTER_SHIFT(tmpBuf, sizeof(SMqRspHead)), *len - sizeof(SMqRspHead));
3,858✔
2495
    if (tDecodeMqMetaRsp(&metaCoder, &metaRsp) < 0) {
3,858✔
UNCOV
2496
      goto end;
×
2497
    }
2498
    cJSON* pItem = NULL;
3,858✔
2499
    processSimpleMeta(&metaRsp, &pItem);
3,858✔
2500
    tDeleteMqMetaRsp(&metaRsp);
3,858✔
2501
    if (pItem != NULL) RAW_FALSE_CHECK(tmqAddJsonArrayItem(pMetaArr, pItem));
3,858✔
2502
  }
2503

2504
  tDeleteMqBatchMetaRsp(&rsp);
693✔
2505
  char* fullStr = cJSON_PrintUnformatted(pJson);
693✔
2506
  cJSON_Delete(pJson);
693✔
2507
  *string = fullStr;
693✔
2508
  return;
693✔
2509

UNCOV
2510
end:
×
UNCOV
2511
  cJSON_Delete(pJson);
×
UNCOV
2512
  tDeleteMqBatchMetaRsp(&rsp);
×
2513
}
2514

2515
char* tmq_get_json_meta(TAOS_RES* res) {
7,292✔
2516
  if (res == NULL) {
7,292✔
UNCOV
2517
    uError("invalid parameter in %s", __func__);
×
UNCOV
2518
    return NULL;
×
2519
  }
2520
  uDebug("tmq_get_json_meta res:%p", res);
7,292✔
2521
  if (!TD_RES_TMQ_META(res) && !TD_RES_TMQ_METADATA(res) && !TD_RES_TMQ_BATCH_META(res)) {
7,292✔
2522
    return NULL;
×
2523
  }
2524

2525
  char*      string = NULL;
7,292✔
2526
  SMqRspObj* rspObj = (SMqRspObj*)res;
7,292✔
2527
  if (TD_RES_TMQ_METADATA(res)) {
7,292✔
2528
    processAutoCreateTable(&rspObj->dataRsp, &string);
266✔
2529
  } else if (TD_RES_TMQ_BATCH_META(res)) {
7,026✔
2530
    processBatchMetaToJson(&rspObj->batchMetaRsp, &string);
693✔
2531
  } else if (TD_RES_TMQ_META(res)) {
6,333✔
2532
    cJSON* pJson = NULL;
6,333✔
2533
    processSimpleMeta(&rspObj->metaRsp, &pJson);
6,333✔
2534
    string = cJSON_PrintUnformatted(pJson);
6,333✔
2535
    cJSON_Delete(pJson);
6,333✔
2536
  } else {
UNCOV
2537
    uError("tmq_get_json_meta res:%d, invalid type", *(int8_t*)res);
×
2538
  }
2539

2540
  uDebug("tmq_get_json_meta string:%s", string);
7,292✔
2541
  return string;
7,292✔
2542
}
2543

2544
void tmq_free_json_meta(char* jsonMeta) { taosMemoryFreeClear(jsonMeta); }
8,707✔
2545

2546
static int32_t getOffSetLen(const SMqDataRsp* pRsp) {
2,725✔
2547
  if (pRsp == NULL) {
2,725✔
UNCOV
2548
    uError("invalid parameter in %s", __func__);
×
2549
    return TSDB_CODE_INVALID_PARA;
×
2550
  }
2551
  SEncoder coder = {0};
2,725✔
2552
  tEncoderInit(&coder, NULL, 0);
2,725✔
2553
  if (tEncodeSTqOffsetVal(&coder, &pRsp->reqOffset) < 0) return -1;
2,725✔
2554
  if (tEncodeSTqOffsetVal(&coder, &pRsp->rspOffset) < 0) return -1;
2,725✔
2555
  int32_t pos = coder.pos;
2,725✔
2556
  tEncoderClear(&coder);
2,725✔
2557
  return pos;
2,725✔
2558
}
2559

2560
typedef int32_t __encode_func__(SEncoder* pEncoder, const SMqDataRsp* pRsp);
2561
static int32_t  encodeMqDataRsp(__encode_func__* encodeFunc, SMqDataRsp* rspObj, tmq_raw_data* raw) {
2,725✔
2562
  if (raw == NULL || encodeFunc == NULL || rspObj == NULL) {
2,725✔
UNCOV
2563
    uError("invalid parameter in %s", __func__);
×
UNCOV
2564
    return TSDB_CODE_INVALID_PARA;
×
2565
  }
2566
  uint32_t len = 0;
2,725✔
2567
  int32_t  code = 0;
2,725✔
2568
  SEncoder encoder = {0};
2,725✔
2569
  void*    buf = NULL;
2,725✔
2570
  tEncodeSize(encodeFunc, rspObj, len, code);
2,725✔
2571
  if (code < 0) {
2,725✔
UNCOV
2572
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2573
    goto FAILED;
×
2574
  }
2575
  len += sizeof(int8_t) + sizeof(int32_t);
2,725✔
2576
  buf = taosMemoryCalloc(1, len);
2,725✔
2577
  if (buf == NULL) {
2,725✔
UNCOV
2578
    code = terrno;
×
UNCOV
2579
    goto FAILED;
×
2580
  }
2581
  tEncoderInit(&encoder, buf, len);
2,725✔
2582
  if (tEncodeI8(&encoder, MQ_DATA_RSP_VERSION) < 0) {
2,725✔
UNCOV
2583
    code = TSDB_CODE_INVALID_MSG;
×
2584
    goto FAILED;
×
2585
  }
2586
  int32_t offsetLen = getOffSetLen(rspObj);
2,725✔
2587
  if (offsetLen <= 0) {
2,725✔
UNCOV
2588
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2589
    goto FAILED;
×
2590
  }
2591
  if (tEncodeI32(&encoder, offsetLen) < 0) {
2,725✔
UNCOV
2592
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2593
    goto FAILED;
×
2594
  }
2595
  if (encodeFunc(&encoder, rspObj) < 0) {
2,725✔
2596
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2597
    goto FAILED;
×
2598
  }
2599
  tEncoderClear(&encoder);
2,725✔
2600

2601
  raw->raw = buf;
2,725✔
2602
  raw->raw_len = len;
2,725✔
2603
  return code;
2,725✔
2604
FAILED:
×
2605
  tEncoderClear(&encoder);
×
UNCOV
2606
  taosMemoryFree(buf);
×
UNCOV
2607
  return code;
×
2608
}
2609

2610
int32_t tmq_get_raw(TAOS_RES* res, tmq_raw_data* raw) {
9,725✔
2611
  if (raw == NULL || res == NULL) {
9,725✔
UNCOV
2612
    uError("invalid parameter in %s", __func__);
×
UNCOV
2613
    return TSDB_CODE_INVALID_PARA;
×
2614
  }
2615
  *raw = (tmq_raw_data){0};
9,725✔
2616
  SMqRspObj* rspObj = ((SMqRspObj*)res);
9,725✔
2617
  if (TD_RES_TMQ_META(res)) {
9,725✔
2618
    raw->raw = rspObj->metaRsp.metaRsp;
6,463✔
2619
    raw->raw_len = rspObj->metaRsp.metaRspLen >= 0 ? rspObj->metaRsp.metaRspLen : 0;
6,463✔
2620
    raw->raw_type = rspObj->metaRsp.resMsgType;
6,463✔
2621
    uDebug("tmq get raw type meta:%p", raw);
6,463✔
2622
  } else if (TD_RES_TMQ(res)) {
3,262✔
2623
    int32_t code = encodeMqDataRsp(tEncodeMqDataRsp, &rspObj->dataRsp, raw);
2,559✔
2624
    if (code != 0) {
2,559✔
2625
      uError("tmq get raw type error:%d", terrno);
×
UNCOV
2626
      return code;
×
2627
    }
2628
    raw->raw_type = RES_TYPE__TMQ;
2,559✔
2629
    uDebug("tmq get raw type data:%p", raw);
2,559✔
2630
  } else if (TD_RES_TMQ_METADATA(res)) {
703✔
2631
    int32_t code = encodeMqDataRsp(tEncodeSTaosxRsp, &rspObj->dataRsp, raw);
166✔
2632
    if (code != 0) {
166✔
UNCOV
2633
      uError("tmq get raw type error:%d", terrno);
×
UNCOV
2634
      return code;
×
2635
    }
2636
    raw->raw_type = RES_TYPE__TMQ_METADATA;
166✔
2637
    uDebug("tmq get raw type metadata:%p", raw);
166✔
2638
  } else if (TD_RES_TMQ_BATCH_META(res)) {
537✔
2639
    raw->raw = rspObj->batchMetaRsp.pMetaBuff;
537✔
2640
    raw->raw_len = rspObj->batchMetaRsp.metaBuffLen;
537✔
2641
    raw->raw_type = rspObj->resType;
537✔
2642
    uDebug("tmq get raw batch meta:%p", raw);
537✔
UNCOV
2643
  } else if (TD_RES_TMQ_RAW(res)) {
×
UNCOV
2644
    raw->raw = rspObj->dataRsp.rawData;
×
2645
    rspObj->dataRsp.rawData = NULL;
×
2646
    raw->raw_len = rspObj->dataRsp.len;
×
UNCOV
2647
    raw->raw_type = rspObj->resType;
×
UNCOV
2648
    uDebug("tmq get raw raw:%p", raw);
×
2649
  } else {
UNCOV
2650
    uError("tmq get raw error type:%d", *(int8_t*)res);
×
UNCOV
2651
    return TSDB_CODE_TMQ_INVALID_MSG;
×
2652
  }
2653
  return TSDB_CODE_SUCCESS;
9,725✔
2654
}
2655

2656
void tmq_free_raw(tmq_raw_data raw) {
8,447✔
2657
  uDebug("tmq free raw data type:%d", raw.raw_type);
8,447✔
2658
  if (raw.raw_type == RES_TYPE__TMQ || raw.raw_type == RES_TYPE__TMQ_METADATA) {
8,447✔
2659
    taosMemoryFree(raw.raw);
1,447✔
2660
  } else if (raw.raw_type == RES_TYPE__TMQ_RAWDATA && raw.raw != NULL) {
7,000✔
UNCOV
2661
    taosMemoryFree(POINTER_SHIFT(raw.raw, -sizeof(SMqRspHead)));
×
2662
  }
2663
  (void)memset(terrMsg, 0, ERR_MSG_LEN);
8,447✔
2664
}
8,447✔
2665

2666
static int32_t writeRawInit() {
11,383✔
2667
  while (atomic_load_8(&initedFlag) == WRITE_RAW_INIT_START) {
11,819✔
2668
    int8_t old = atomic_val_compare_exchange_8(&initFlag, 0, 1);
436✔
2669
    if (old == 0) {
436✔
2670
      int32_t code = initRawCacheHash();
436✔
2671
      if (code != 0) {
436✔
UNCOV
2672
        uError("tmq writeRawImpl init error:%d", code);
×
2673
        atomic_store_8(&initedFlag, WRITE_RAW_INIT_FAIL);
×
UNCOV
2674
        return code;
×
2675
      }
2676
      atomic_store_8(&initedFlag, WRITE_RAW_INIT_OK);
436✔
2677
    }
2678
  }
2679

2680
  if (atomic_load_8(&initedFlag) == WRITE_RAW_INIT_FAIL) {
11,383✔
UNCOV
2681
    return TSDB_CODE_INTERNAL_ERROR;
×
2682
  }
2683
  return 0;
11,383✔
2684
}
2685

2686
static int32_t writeRawImpl(TAOS* taos, void* buf, uint32_t len, uint16_t type) {
11,383✔
2687
  if (taos == NULL || buf == NULL) {
11,383✔
UNCOV
2688
    uError("invalid parameter in %s", __func__);
×
UNCOV
2689
    return TSDB_CODE_INVALID_PARA;
×
2690
  }
2691
  if (writeRawInit() != 0) {
11,383✔
UNCOV
2692
    return TSDB_CODE_INTERNAL_ERROR;
×
2693
  }
2694

2695
  if (type == TDMT_VND_CREATE_STB) {
11,383✔
2696
    return taosCreateStb(taos, buf, len);
2,419✔
2697
  } else if (type == TDMT_VND_ALTER_STB) {
8,964✔
2698
    return taosCreateStb(taos, buf, len);
1,405✔
2699
  } else if (type == TDMT_VND_DROP_STB) {
7,559✔
2700
    return taosDropStb(taos, buf, len);
266✔
2701
  } else if (type == TDMT_VND_CREATE_TABLE) {
7,293✔
2702
    return taosCreateTable(taos, buf, len);
4,049✔
2703
  } else if (type == TDMT_VND_ALTER_TABLE) {
3,244✔
2704
    return taosAlterTable(taos, buf, len);
966✔
2705
  } else if (type == TDMT_VND_DROP_TABLE) {
2,278✔
2706
    return taosDropTable(taos, buf, len);
188✔
2707
  } else if (type == TDMT_VND_DELETE) {
2,090✔
2708
    return taosDeleteData(taos, buf, len);
106✔
2709
  } else if (type == RES_TYPE__TMQ_METADATA) {
1,984✔
2710
    return tmqWriteRawMetaDataImpl(taos, buf, len);
166✔
2711
  } else if (type == RES_TYPE__TMQ_RAWDATA) {
1,818✔
UNCOV
2712
    return tmqWriteRawRawDataImpl(taos, buf, len);
×
2713
  } else if (type == RES_TYPE__TMQ) {
1,818✔
2714
    return tmqWriteRawDataImpl(taos, buf, len);
1,281✔
2715
  } else if (type == RES_TYPE__TMQ_BATCH_META) {
537✔
2716
    return tmqWriteBatchMetaDataImpl(taos, buf, len);
537✔
2717
  }
UNCOV
2718
  return TSDB_CODE_INVALID_PARA;
×
2719
}
2720

2721
int32_t tmq_write_raw(TAOS* taos, tmq_raw_data raw) {
8,105✔
2722
  if (taos == NULL || raw.raw == NULL || raw.raw_len <= 0) {
8,105✔
2723
    SET_ERROR_MSG("taos:%p or data:%p is NULL or raw_len <= 0", taos, raw.raw);
424✔
2724
    return TSDB_CODE_INVALID_PARA;
424✔
2725
  }
2726
  taosClearErrMsg();  // clear global error message
7,681✔
2727

2728
  return writeRawImpl(taos, raw.raw, raw.raw_len, raw.raw_type);
7,681✔
2729
}
2730

2731
static int32_t tmqWriteBatchMetaDataImpl(TAOS* taos, void* meta, uint32_t metaLen) {
537✔
2732
  if (taos == NULL || meta == NULL) {
537✔
UNCOV
2733
    uError("invalid parameter in %s", __func__);
×
UNCOV
2734
    return TSDB_CODE_INVALID_PARA;
×
2735
  }
2736
  SMqBatchMetaRsp rsp = {0};
537✔
2737
  SDecoder        coder = {0};
537✔
2738
  int32_t         code = TSDB_CODE_SUCCESS;
537✔
2739

2740
  // decode and process req
2741
  tDecoderInit(&coder, meta, metaLen);
537✔
2742
  if (tDecodeMqBatchMetaRsp(&coder, &rsp) < 0) {
537✔
UNCOV
2743
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
2744
    goto end;
×
2745
  }
2746
  int32_t num = taosArrayGetSize(rsp.batchMetaReq);
537✔
2747
  for (int32_t i = 0; i < num; i++) {
4,239✔
2748
    int32_t* len = taosArrayGet(rsp.batchMetaLen, i);
3,702✔
2749
    RAW_NULL_CHECK(len);
3,702✔
2750
    void* tmpBuf = taosArrayGetP(rsp.batchMetaReq, i);
3,702✔
2751
    RAW_NULL_CHECK(tmpBuf);
3,702✔
2752
    SDecoder   metaCoder = {0};
3,702✔
2753
    SMqMetaRsp metaRsp = {0};
3,702✔
2754
    tDecoderInit(&metaCoder, POINTER_SHIFT(tmpBuf, sizeof(SMqRspHead)), *len - sizeof(SMqRspHead));
3,702✔
2755
    if (tDecodeMqMetaRsp(&metaCoder, &metaRsp) < 0) {
3,702✔
2756
      code = TSDB_CODE_INVALID_PARA;
×
UNCOV
2757
      goto end;
×
2758
    }
2759
    code = writeRawImpl(taos, metaRsp.metaRsp, metaRsp.metaRspLen, metaRsp.resMsgType);
3,702✔
2760
    tDeleteMqMetaRsp(&metaRsp);
3,702✔
2761
    if (code != TSDB_CODE_SUCCESS) {
3,702✔
UNCOV
2762
      goto end;
×
2763
    }
2764
  }
2765

2766
end:
537✔
2767
  tDeleteMqBatchMetaRsp(&rsp);
537✔
2768
  return code;
537✔
2769
}
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