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

taosdata / TDengine / #3561

19 Dec 2024 03:15AM UTC coverage: 58.812% (-1.3%) from 60.124%
#3561

push

travis-ci

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

merge: from main to 3.0 branch

130770 of 287658 branches covered (45.46%)

Branch coverage included in aggregate %.

32 of 78 new or added lines in 4 files covered. (41.03%)

7347 existing lines in 166 files now uncovered.

205356 of 283866 relevant lines covered (72.34%)

7187865.64 hits per line

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

59.49
/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){
6,377✔
56
  bool ret = cJSON_AddItemToObject(object, string, item);
6,377✔
57
  if (!ret){
6,377!
58
    cJSON_Delete(item);
×
59
  }
60
  return ret;
6,377✔
61
}
62
static bool  tmqAddJsonArrayItem(cJSON *array, cJSON *item){
1,189✔
63
  bool ret = cJSON_AddItemToArray(array, item);
1,189✔
64
  if (!ret){
1,189!
65
    cJSON_Delete(item);
×
66
  }
67
  return ret;
1,189✔
68
}
69

70

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

90
  char*  string = NULL;
96✔
91
  cJSON* json = cJSON_CreateObject();
96✔
92
  RAW_NULL_CHECK(json);
96!
93
  cJSON* type = cJSON_CreateString("create");
96✔
94
  RAW_NULL_CHECK(type);
96!
95

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

104
  cJSON* columns = cJSON_CreateArray();
96✔
105
  RAW_NULL_CHECK(columns);
96!
106
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "columns", columns));
96!
107

108
  for (int i = 0; i < schemaRow->nCols; i++) {
584✔
109
    cJSON* column = cJSON_CreateObject();
488✔
110
    RAW_NULL_CHECK(column);
488!
111
    RAW_FALSE_CHECK(tmqAddJsonArrayItem(columns, column));
488!
112
    SSchema* s = schemaRow->pSchema + i;
488✔
113
    cJSON*   cname = cJSON_CreateString(s->name);
488✔
114
    RAW_NULL_CHECK(cname);
488!
115
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(column, "name", cname));
488!
116
    cJSON* ctype = cJSON_CreateNumber(s->type);
488✔
117
    RAW_NULL_CHECK(ctype);
488!
118
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(column, "type", ctype));
488!
119
    if (s->type == TSDB_DATA_TYPE_BINARY || s->type == TSDB_DATA_TYPE_VARBINARY || s->type == TSDB_DATA_TYPE_GEOMETRY) {
546!
120
      int32_t length = s->bytes - VARSTR_HEADER_SIZE;
58✔
121
      cJSON*  cbytes = cJSON_CreateNumber(length);
58✔
122
      RAW_NULL_CHECK(cbytes);
58!
123
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(column, "length", cbytes));
58!
124
    } else if (s->type == TSDB_DATA_TYPE_NCHAR) {
430✔
125
      int32_t length = (s->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
8✔
126
      cJSON*  cbytes = cJSON_CreateNumber(length);
8✔
127
      RAW_NULL_CHECK(cbytes);
8!
128
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(column, "length", cbytes));
8!
129
    }
130
    cJSON* isPk = cJSON_CreateBool(s->flags & COL_IS_KEY);
488✔
131
    RAW_NULL_CHECK(isPk);
488!
132
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(column, "isPrimarykey", isPk));
488!
133

134
    if (pColCmprRow == NULL) {
488!
UNCOV
135
      continue;
×
136
    }
137

138
    uint32_t alg = 0;
488✔
139
    if (buildDefaultCompress) {
488!
UNCOV
140
      alg = createDefaultColCmprByType(s->type);
×
141
    } else {
142
      SColCmpr* pColCmpr = pColCmprRow->pColCmpr + i;
488✔
143
      alg = pColCmpr->alg;
488✔
144
    }
145
    const char* encode = columnEncodeStr(COMPRESS_L1_TYPE_U32(alg));
488✔
146
    RAW_NULL_CHECK(encode);
488!
147
    const char* compress = columnCompressStr(COMPRESS_L2_TYPE_U32(alg));
488✔
148
    RAW_NULL_CHECK(compress);
488!
149
    const char* level = columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(alg));
488✔
150
    RAW_NULL_CHECK(level);
488!
151

152
    cJSON* encodeJson = cJSON_CreateString(encode);
488✔
153
    RAW_NULL_CHECK(encodeJson);
488!
154
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(column, "encode", encodeJson));
488!
155

156
    cJSON* compressJson = cJSON_CreateString(compress);
488✔
157
    RAW_NULL_CHECK(compressJson);
488!
158
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(column, "compress", compressJson));
488!
159

160
    cJSON* levelJson = cJSON_CreateString(level);
488✔
161
    RAW_NULL_CHECK(levelJson);
488!
162
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(column, "level", levelJson));
488!
163
  }
164

165
  cJSON* tags = cJSON_CreateArray();
96✔
166
  RAW_NULL_CHECK(tags);
96!
167
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tags", tags));
96!
168

169
  for (int i = 0; schemaTag && i < schemaTag->nCols; i++) {
290✔
170
    cJSON* tag = cJSON_CreateObject();
194✔
171
    RAW_NULL_CHECK(tag);
194!
172
    RAW_FALSE_CHECK(tmqAddJsonArrayItem(tags, tag));
194!
173
    SSchema* s = schemaTag->pSchema + i;
194✔
174
    cJSON*   tname = cJSON_CreateString(s->name);
194✔
175
    RAW_NULL_CHECK(tname);
194!
176
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "name", tname));
194!
177
    cJSON* ttype = cJSON_CreateNumber(s->type);
194✔
178
    RAW_NULL_CHECK(ttype);
194!
179
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "type", ttype));
194!
180
    if (s->type == TSDB_DATA_TYPE_BINARY || s->type == TSDB_DATA_TYPE_VARBINARY || s->type == TSDB_DATA_TYPE_GEOMETRY) {
202!
181
      int32_t length = s->bytes - VARSTR_HEADER_SIZE;
8✔
182
      cJSON*  cbytes = cJSON_CreateNumber(length);
8✔
183
      RAW_NULL_CHECK(cbytes);
8!
184
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "length", cbytes));
8!
185
    } else if (s->type == TSDB_DATA_TYPE_NCHAR) {
186✔
186
      int32_t length = (s->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
55✔
187
      cJSON*  cbytes = cJSON_CreateNumber(length);
55✔
188
      RAW_NULL_CHECK(cbytes);
55!
189
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "length", cbytes));
55!
190
    }
191
  }
192

193
end:
96✔
194
  *pJson = json;
96✔
195
}
196

197
static int32_t setCompressOption(cJSON* json, uint32_t para) {
×
UNCOV
198
  if (json == NULL) {
×
199
    return TSDB_CODE_INVALID_PARA;
×
200
  }
201
  uint8_t encode = COMPRESS_L1_TYPE_U32(para);
×
202
  int32_t code = 0;
×
203
  if (encode != 0) {
×
204
    const char* encodeStr = columnEncodeStr(encode);
×
205
    RAW_NULL_CHECK(encodeStr);
×
206
    cJSON* encodeJson = cJSON_CreateString(encodeStr);
×
UNCOV
207
    RAW_NULL_CHECK(encodeJson);
×
208
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "encode", encodeJson));
×
209
    return code;
×
210
  }
211
  uint8_t compress = COMPRESS_L2_TYPE_U32(para);
×
212
  if (compress != 0) {
×
213
    const char* compressStr = columnCompressStr(compress);
×
214
    RAW_NULL_CHECK(compressStr);
×
215
    cJSON* compressJson = cJSON_CreateString(compressStr);
×
UNCOV
216
    RAW_NULL_CHECK(compressJson);
×
UNCOV
217
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "compress", compressJson));
×
218
    return code;
×
219
  }
UNCOV
220
  uint8_t level = COMPRESS_L2_TYPE_LEVEL_U32(para);
×
UNCOV
221
  if (level != 0) {
×
UNCOV
222
    const char* levelStr = columnLevelStr(level);
×
UNCOV
223
    RAW_NULL_CHECK(levelStr);
×
UNCOV
224
    cJSON* levelJson = cJSON_CreateString(levelStr);
×
UNCOV
225
    RAW_NULL_CHECK(levelJson);
×
UNCOV
226
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "level", levelJson));
×
UNCOV
227
    return code;
×
228
  }
229

UNCOV
230
end:
×
UNCOV
231
  return code;
×
232
}
233
static void buildAlterSTableJson(void* alterData, int32_t alterDataLen, cJSON** pJson) {
40✔
234
  if (alterData == NULL || pJson == NULL) {
40!
UNCOV
235
    uError("invalid parameter in %s", __func__);
×
UNCOV
236
    return;
×
237
  }
238
  SMAlterStbReq req = {0};
40✔
239
  cJSON*        json = NULL;
40✔
240
  char*         string = NULL;
40✔
241
  int32_t       code = 0;
40✔
242

243
  if (tDeserializeSMAlterStbReq(alterData, alterDataLen, &req) != 0) {
40!
UNCOV
244
    goto end;
×
245
  }
246

247
  json = cJSON_CreateObject();
40✔
248
  RAW_NULL_CHECK(json);
40!
249
  cJSON* type = cJSON_CreateString("alter");
40✔
250
  RAW_NULL_CHECK(type);
40!
251
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "type", type));
40!
252
  SName name = {0};
40✔
253
  RAW_RETURN_CHECK(tNameFromString(&name, req.name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE));
40!
254
  cJSON* tableType = cJSON_CreateString("super");
40✔
255
  RAW_NULL_CHECK(tableType);
40!
256
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableType", tableType));
40!
257
  cJSON* tableName = cJSON_CreateString(name.tname);
40✔
258
  RAW_NULL_CHECK(tableName);
40!
259
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableName", tableName));
40!
260

261
  cJSON* alterType = cJSON_CreateNumber(req.alterType);
40✔
262
  RAW_NULL_CHECK(alterType);
40!
263
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "alterType", alterType));
40!
264
  switch (req.alterType) {
40!
265
    case TSDB_ALTER_TABLE_ADD_TAG:
24✔
266
    case TSDB_ALTER_TABLE_ADD_COLUMN: {
267
      TAOS_FIELD* field = taosArrayGet(req.pFields, 0);
24✔
268
      RAW_NULL_CHECK(field);
24!
269
      cJSON* colName = cJSON_CreateString(field->name);
24✔
270
      RAW_NULL_CHECK(colName);
24!
271
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
24!
272
      cJSON* colType = cJSON_CreateNumber(field->type);
24✔
273
      RAW_NULL_CHECK(colType);
24!
274
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colType", colType));
24!
275

276
      if (field->type == TSDB_DATA_TYPE_BINARY || field->type == TSDB_DATA_TYPE_VARBINARY ||
24!
277
          field->type == TSDB_DATA_TYPE_GEOMETRY) {
24!
278
        int32_t length = field->bytes - VARSTR_HEADER_SIZE;
8✔
279
        cJSON*  cbytes = cJSON_CreateNumber(length);
8✔
280
        RAW_NULL_CHECK(cbytes);
8!
281
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
8!
282
      } else if (field->type == TSDB_DATA_TYPE_NCHAR) {
16!
UNCOV
283
        int32_t length = (field->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
284
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
285
        RAW_NULL_CHECK(cbytes);
×
286
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
287
      }
288
      break;
24✔
289
    }
290
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION: {
×
291
      SFieldWithOptions* field = taosArrayGet(req.pFields, 0);
×
292
      RAW_NULL_CHECK(field);
×
293
      cJSON* colName = cJSON_CreateString(field->name);
×
294
      RAW_NULL_CHECK(colName);
×
UNCOV
295
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
×
296
      cJSON* colType = cJSON_CreateNumber(field->type);
×
297
      RAW_NULL_CHECK(colType);
×
UNCOV
298
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colType", colType));
×
299

UNCOV
300
      if (field->type == TSDB_DATA_TYPE_BINARY || field->type == TSDB_DATA_TYPE_VARBINARY ||
×
UNCOV
301
          field->type == TSDB_DATA_TYPE_GEOMETRY) {
×
UNCOV
302
        int32_t length = field->bytes - VARSTR_HEADER_SIZE;
×
UNCOV
303
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
UNCOV
304
        RAW_NULL_CHECK(cbytes);
×
UNCOV
305
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
UNCOV
306
      } else if (field->type == TSDB_DATA_TYPE_NCHAR) {
×
UNCOV
307
        int32_t length = (field->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
UNCOV
308
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
UNCOV
309
        RAW_NULL_CHECK(cbytes);
×
UNCOV
310
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
311
      }
UNCOV
312
      RAW_RETURN_CHECK(setCompressOption(json, field->compress));
×
UNCOV
313
      break;
×
314
    }
315
    case TSDB_ALTER_TABLE_DROP_TAG:
8✔
316
    case TSDB_ALTER_TABLE_DROP_COLUMN: {
317
      TAOS_FIELD* field = taosArrayGet(req.pFields, 0);
8✔
318
      RAW_NULL_CHECK(field);
8!
319
      cJSON* colName = cJSON_CreateString(field->name);
8✔
320
      RAW_NULL_CHECK(colName);
8!
321
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
8!
322
      break;
8✔
323
    }
324
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
8✔
325
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES: {
326
      TAOS_FIELD* field = taosArrayGet(req.pFields, 0);
8✔
327
      RAW_NULL_CHECK(field);
8!
328
      cJSON* colName = cJSON_CreateString(field->name);
8✔
329
      RAW_NULL_CHECK(colName);
8!
330
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
8!
331
      cJSON* colType = cJSON_CreateNumber(field->type);
8✔
332
      RAW_NULL_CHECK(colType);
8!
333
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colType", colType));
8!
334
      if (field->type == TSDB_DATA_TYPE_BINARY || field->type == TSDB_DATA_TYPE_VARBINARY ||
8!
335
          field->type == TSDB_DATA_TYPE_GEOMETRY) {
8!
336
        int32_t length = field->bytes - VARSTR_HEADER_SIZE;
8✔
337
        cJSON*  cbytes = cJSON_CreateNumber(length);
8✔
338
        RAW_NULL_CHECK(cbytes);
8!
339
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
8!
340
      } else if (field->type == TSDB_DATA_TYPE_NCHAR) {
×
341
        int32_t length = (field->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
342
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
343
        RAW_NULL_CHECK(cbytes);
×
344
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
345
      }
346
      break;
8✔
347
    }
348
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
×
349
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME: {
350
      TAOS_FIELD* oldField = taosArrayGet(req.pFields, 0);
×
351
      RAW_NULL_CHECK(oldField);
×
352
      TAOS_FIELD* newField = taosArrayGet(req.pFields, 1);
×
353
      RAW_NULL_CHECK(newField);
×
UNCOV
354
      cJSON* colName = cJSON_CreateString(oldField->name);
×
355
      RAW_NULL_CHECK(colName);
×
356
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
×
UNCOV
357
      cJSON* colNewName = cJSON_CreateString(newField->name);
×
UNCOV
358
      RAW_NULL_CHECK(colNewName);
×
UNCOV
359
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colNewName", colNewName));
×
UNCOV
360
      break;
×
361
    }
UNCOV
362
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS: {
×
UNCOV
363
      TAOS_FIELD* field = taosArrayGet(req.pFields, 0);
×
UNCOV
364
      RAW_NULL_CHECK(field);
×
UNCOV
365
      cJSON* colName = cJSON_CreateString(field->name);
×
UNCOV
366
      RAW_NULL_CHECK(colName);
×
UNCOV
367
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
×
UNCOV
368
      RAW_RETURN_CHECK(setCompressOption(json, field->bytes));
×
UNCOV
369
      break;
×
370
    }
UNCOV
371
    default:
×
UNCOV
372
      break;
×
373
  }
374

375
end:
40✔
376
  tFreeSMAltertbReq(&req);
40✔
377
  *pJson = json;
40✔
378
}
379

380
static void processCreateStb(SMqMetaRsp* metaRsp, cJSON** pJson) {
78✔
381
  if (metaRsp == NULL || pJson == NULL) {
78!
UNCOV
382
    uError("invalid parameter in %s", __func__);
×
UNCOV
383
    return;
×
384
  }
385
  SVCreateStbReq req = {0};
78✔
386
  SDecoder       coder;
387

388
  uDebug("create stable data:%p", metaRsp);
78!
389
  // decode and process req
390
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
78✔
391
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
78✔
392
  tDecoderInit(&coder, data, len);
78✔
393

394
  if (tDecodeSVCreateStbReq(&coder, &req) < 0) {
78!
395
    goto end;
×
396
  }
397
  buildCreateTableJson(&req.schemaRow, &req.schemaTag, req.name, req.suid, TSDB_SUPER_TABLE, &req.colCmpr, pJson);
78✔
398

399
end:
78✔
400
  uDebug("create stable return");
78!
401
  tDecoderClear(&coder);
78✔
402
}
403

404
static void processAlterStb(SMqMetaRsp* metaRsp, cJSON** pJson) {
40✔
405
  if (metaRsp == NULL || pJson == NULL) {
40!
UNCOV
406
    uError("invalid parameter in %s", __func__);
×
UNCOV
407
    return;
×
408
  }
409
  SVCreateStbReq req = {0};
40✔
410
  SDecoder       coder = {0};
40✔
411
  uDebug("alter stable data:%p", metaRsp);
40!
412

413
  // decode and process req
414
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
40✔
415
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
40✔
416
  tDecoderInit(&coder, data, len);
40✔
417

418
  if (tDecodeSVCreateStbReq(&coder, &req) < 0) {
40!
UNCOV
419
    goto end;
×
420
  }
421
  buildAlterSTableJson(req.alterOriData, req.alterOriDataLen, pJson);
40✔
422

423
end:
40✔
424
  uDebug("alter stable return");
40!
425
  tDecoderClear(&coder);
40✔
426
}
427

428
static void buildChildElement(cJSON* json, SVCreateTbReq* pCreateReq) {
156✔
429
  if (json == NULL || pCreateReq == NULL) {
156!
UNCOV
430
    uError("invalid parameter in %s", __func__);
×
UNCOV
431
    return;
×
432
  }
433
  STag*   pTag = (STag*)pCreateReq->ctb.pTag;
156✔
434
  char*   sname = pCreateReq->ctb.stbName;
156✔
435
  char*   name = pCreateReq->name;
156✔
436
  SArray* tagName = pCreateReq->ctb.tagName;
156✔
437
  int64_t id = pCreateReq->uid;
156✔
438
  uint8_t tagNum = pCreateReq->ctb.tagNum;
156✔
439
  int32_t code = 0;
156✔
440
  SArray* pTagVals = NULL;
156✔
441
  char*   pJson = NULL;
156✔
442

443
  cJSON*  tableName = cJSON_CreateString(name);
156✔
444
  RAW_NULL_CHECK(tableName);
156!
445
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableName", tableName));
156!
446
  cJSON* using = cJSON_CreateString(sname);
156✔
447
  RAW_NULL_CHECK(using);
156!
448
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "using", using));
156!
449
  cJSON* tagNumJson = cJSON_CreateNumber(tagNum);
156✔
450
  RAW_NULL_CHECK(tagNumJson);
156!
451
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tagNum", tagNumJson));
156!
452

453
  cJSON* tags = cJSON_CreateArray();
156✔
454
  RAW_NULL_CHECK(tags);
156!
455
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tags", tags));
156!
456
  RAW_RETURN_CHECK(tTagToValArray(pTag, &pTagVals));
156!
457
  if (tTagIsJson(pTag)) {
156✔
458
    STag* p = (STag*)pTag;
20✔
459
    if (p->nTag == 0) {
20✔
460
      uError("p->nTag == 0");
10!
461
      goto end;
10✔
462
    }
463
    parseTagDatatoJson(pTag, &pJson, NULL);
10✔
464
    RAW_NULL_CHECK(pJson);
10!
465
    cJSON* tag = cJSON_CreateObject();
10✔
466
    RAW_NULL_CHECK(tag);
10!
467
    RAW_FALSE_CHECK(tmqAddJsonArrayItem(tags, tag));
10!
468
    STagVal* pTagVal = taosArrayGet(pTagVals, 0);
10✔
469
    RAW_NULL_CHECK(pTagVal);
10!
470
    char* ptname = taosArrayGet(tagName, 0);
10✔
471
    RAW_NULL_CHECK(ptname);
10!
472
    cJSON* tname = cJSON_CreateString(ptname);
10✔
473
    RAW_NULL_CHECK(tname);
10!
474
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "name", tname));
10!
475
    cJSON* ttype = cJSON_CreateNumber(TSDB_DATA_TYPE_JSON);
10✔
476
    RAW_NULL_CHECK(ttype);
10!
477
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "type", ttype));
10!
478
    cJSON* tvalue = cJSON_CreateString(pJson);
10✔
479
    RAW_NULL_CHECK(tvalue);
10!
480
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "value", tvalue));
10!
481
    goto end;
10✔
482
  }
483

484
  for (int i = 0; i < taosArrayGetSize(pTagVals); i++) {
465✔
485
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, i);
329✔
486
    RAW_NULL_CHECK(pTagVal);
329!
487
    cJSON* tag = cJSON_CreateObject();
329✔
488
    RAW_NULL_CHECK(tag);
329!
489
    RAW_FALSE_CHECK(tmqAddJsonArrayItem(tags, tag));
329!
490
    char* ptname = taosArrayGet(tagName, i);
329✔
491
    RAW_NULL_CHECK(ptname);
329!
492
    cJSON* tname = cJSON_CreateString(ptname);
329✔
493
    RAW_NULL_CHECK(tname);
329!
494
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "name", tname));
329!
495
    cJSON* ttype = cJSON_CreateNumber(pTagVal->type);
329✔
496
    RAW_NULL_CHECK(ttype);
329!
497
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "type", ttype));
329!
498

499
    cJSON* tvalue = NULL;
329✔
500
    if (IS_VAR_DATA_TYPE(pTagVal->type)) {
431!
501
      int64_t bufSize = 0;
102✔
502
      if (pTagVal->type == TSDB_DATA_TYPE_VARBINARY) {
102!
UNCOV
503
        bufSize = pTagVal->nData * 2 + 2 + 3;
×
504
      } else {
505
        bufSize = pTagVal->nData + 3;
102✔
506
      }
507
      char* buf = taosMemoryCalloc(bufSize, 1);
102!
508
      RAW_NULL_CHECK(buf);
102!
509
      if (dataConverToStr(buf, bufSize, pTagVal->type, pTagVal->pData, pTagVal->nData, NULL) != TSDB_CODE_SUCCESS) {
102!
UNCOV
510
        taosMemoryFree(buf);
×
UNCOV
511
        goto end;
×
512
      }
513

514
      tvalue = cJSON_CreateString(buf);
102✔
515
      taosMemoryFree(buf);
102!
516
      RAW_NULL_CHECK(tvalue);
102!
517
    } else {
518
      double val = 0;
227✔
519
      GET_TYPED_DATA(val, double, pTagVal->type, &pTagVal->i64);
227!
520
      tvalue = cJSON_CreateNumber(val);
227✔
521
      RAW_NULL_CHECK(tvalue);
227!
522
    }
523

524
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "value", tvalue));
329!
525
  }
526

527
end:
136✔
528
  taosMemoryFree(pJson);
156!
529
  taosArrayDestroy(pTagVals);
156✔
530
}
531

532
static void buildCreateCTableJson(SVCreateTbReq* pCreateReq, int32_t nReqs, cJSON** pJson) {
123✔
533
  if (pJson == NULL || pCreateReq == NULL) {
123!
UNCOV
534
    uError("invalid parameter in %s", __func__);
×
UNCOV
535
    return;
×
536
  }
537
  int32_t code = 0;
123✔
538
  char*   string = NULL;
123✔
539
  cJSON*  json = cJSON_CreateObject();
123✔
540
  RAW_NULL_CHECK(json);
123!
541
  cJSON* type = cJSON_CreateString("create");
123✔
542
  RAW_NULL_CHECK(type);
123!
543
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "type", type));
123!
544

545
  cJSON* tableType = cJSON_CreateString("child");
123✔
546
  RAW_NULL_CHECK(tableType);
123!
547
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableType", tableType));
123!
548

549
  buildChildElement(json, pCreateReq);
123✔
550
  cJSON* createList = cJSON_CreateArray();
123✔
551
  RAW_NULL_CHECK(createList);
123!
552
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "createList", createList));
123!
553

554
  for (int i = 0; nReqs > 1 && i < nReqs; i++) {
156✔
555
    cJSON* create = cJSON_CreateObject();
33✔
556
    RAW_NULL_CHECK(create);
33!
557
    buildChildElement(create, pCreateReq + i);
33✔
558
    RAW_FALSE_CHECK(tmqAddJsonArrayItem(createList, create));
33!
559
  }
560

561
end:
123✔
562
  *pJson = json;
123✔
563
}
564

565
static void processCreateTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
131✔
566
  if (pJson == NULL || metaRsp == NULL) {
131!
UNCOV
567
    uError("invalid parameter in %s", __func__);
×
UNCOV
568
    return;
×
569
  }
570
  SDecoder           decoder = {0};
131✔
571
  SVCreateTbBatchReq req = {0};
131✔
572
  SVCreateTbReq*     pCreateReq;
573
  // decode
574
  uDebug("create table data:%p", metaRsp);
131!
575
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
131✔
576
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
131✔
577
  tDecoderInit(&decoder, data, len);
131✔
578
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
131!
UNCOV
579
    goto end;
×
580
  }
581

582
  // loop to create table
583
  if (req.nReqs > 0) {
131!
584
    pCreateReq = req.pReqs;
131✔
585
    if (pCreateReq->type == TSDB_CHILD_TABLE) {
131✔
586
      buildCreateCTableJson(req.pReqs, req.nReqs, pJson);
113✔
587
    } else if (pCreateReq->type == TSDB_NORMAL_TABLE) {
18!
588
      buildCreateTableJson(&pCreateReq->ntb.schemaRow, NULL, pCreateReq->name, pCreateReq->uid, TSDB_NORMAL_TABLE,
18✔
589
                           &pCreateReq->colCmpr, pJson);
590
    }
591
  }
592

UNCOV
593
end:
×
594
  uDebug("create table return");
131!
595
  tDeleteSVCreateTbBatchReq(&req);
131✔
596
  tDecoderClear(&decoder);
131✔
597
}
598

599
static void processAutoCreateTable(SMqDataRsp* rsp, char** string) {
10✔
600
  if (rsp == NULL || string == NULL) {
10!
UNCOV
601
    uError("invalid parameter in %s", __func__);
×
UNCOV
602
    return;
×
603
  }
604
  SDecoder*      decoder = NULL;
10✔
605
  SVCreateTbReq* pCreateReq = NULL;
10✔
606
  int32_t        code = 0;
10✔
607
  uDebug("auto create table data:%p", rsp);
10!
608
  if (rsp->createTableNum <= 0) {
10!
UNCOV
609
    uError("processAutoCreateTable rsp->createTableNum <= 0");
×
UNCOV
610
    goto end;
×
611
  }
612

613
  decoder = taosMemoryCalloc(rsp->createTableNum, sizeof(SDecoder));
10!
614
  RAW_NULL_CHECK(decoder);
10!
615
  pCreateReq = taosMemoryCalloc(rsp->createTableNum, sizeof(SVCreateTbReq));
10!
616
  RAW_NULL_CHECK(pCreateReq);
10!
617

618
  // loop to create table
619
  for (int32_t iReq = 0; iReq < rsp->createTableNum; iReq++) {
34✔
620
    // decode
621
    void** data = taosArrayGet(rsp->createTableReq, iReq);
24✔
622
    RAW_NULL_CHECK(data);
24!
623
    int32_t* len = taosArrayGet(rsp->createTableLen, iReq);
24✔
624
    RAW_NULL_CHECK(len);
24!
625
    tDecoderInit(&decoder[iReq], *data, *len);
24✔
626
    if (tDecodeSVCreateTbReq(&decoder[iReq], pCreateReq + iReq) < 0) {
24!
627
      goto end;
×
628
    }
629

630
    if (pCreateReq[iReq].type != TSDB_CHILD_TABLE) {
24!
UNCOV
631
      uError("processAutoCreateTable pCreateReq[iReq].type != TSDB_CHILD_TABLE");
×
UNCOV
632
      goto end;
×
633
    }
634
  }
635
  cJSON* pJson = NULL;
10✔
636
  buildCreateCTableJson(pCreateReq, rsp->createTableNum, &pJson);
10✔
637
  *string = cJSON_PrintUnformatted(pJson);
10✔
638
  cJSON_Delete(pJson);
10✔
639

640
end:
10✔
641
  uDebug("auto created table return, sql json:%s", *string);
10!
642
  for (int i = 0; decoder && pCreateReq && i < rsp->createTableNum; i++) {
34!
643
    tDecoderClear(&decoder[i]);
24✔
644
    taosMemoryFreeClear(pCreateReq[i].comment);
24!
645
    if (pCreateReq[i].type == TSDB_CHILD_TABLE) {
24!
646
      taosArrayDestroy(pCreateReq[i].ctb.tagName);
24✔
647
    }
648
  }
649
  taosMemoryFree(decoder);
10!
650
  taosMemoryFree(pCreateReq);
10!
651
}
652

653
static void processAlterTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
24✔
654
  if (pJson == NULL || metaRsp == NULL) {
24!
UNCOV
655
    uError("invalid parameter in %s", __func__);
×
UNCOV
656
    return;
×
657
  }
658
  SDecoder     decoder = {0};
24✔
659
  SVAlterTbReq vAlterTbReq = {0};
24✔
660
  char*        string = NULL;
24✔
661
  cJSON*       json = NULL;
24✔
662
  int32_t      code = 0;
24✔
663

664
  uDebug("alter table data:%p", metaRsp);
24!
665
  // decode
666
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
24✔
667
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
24✔
668
  tDecoderInit(&decoder, data, len);
24✔
669
  if (tDecodeSVAlterTbReq(&decoder, &vAlterTbReq) < 0) {
24!
UNCOV
670
    uError("tDecodeSVAlterTbReq error");
×
671
    goto end;
×
672
  }
673

674
  json = cJSON_CreateObject();
24✔
675
  RAW_NULL_CHECK(json);
24!
676
  cJSON* type = cJSON_CreateString("alter");
24✔
677
  RAW_NULL_CHECK(type);
24!
678
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "type", type));
24!
679
  cJSON* tableType = cJSON_CreateString(vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
44✔
680
                                                vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL
20!
681
                                            ? "child"
682
                                            : "normal");
683
  RAW_NULL_CHECK(tableType);
24!
684
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableType", tableType));
24!
685
  cJSON* tableName = cJSON_CreateString(vAlterTbReq.tbName);
24✔
686
  RAW_NULL_CHECK(tableName);
24!
687
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableName", tableName));
24!
688
  cJSON* alterType = cJSON_CreateNumber(vAlterTbReq.action);
24✔
689
  RAW_NULL_CHECK(alterType);
24!
690
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "alterType", alterType));
24!
691

692
  switch (vAlterTbReq.action) {
24!
693
    case TSDB_ALTER_TABLE_ADD_COLUMN: {
4✔
694
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
4✔
695
      RAW_NULL_CHECK(colName);
4!
696
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
4!
697
      cJSON* colType = cJSON_CreateNumber(vAlterTbReq.type);
4✔
698
      RAW_NULL_CHECK(colType);
4!
699
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colType", colType));
4!
700

701
      if (vAlterTbReq.type == TSDB_DATA_TYPE_BINARY || vAlterTbReq.type == TSDB_DATA_TYPE_VARBINARY ||
4!
702
          vAlterTbReq.type == TSDB_DATA_TYPE_GEOMETRY) {
4!
UNCOV
703
        int32_t length = vAlterTbReq.bytes - VARSTR_HEADER_SIZE;
×
UNCOV
704
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
UNCOV
705
        RAW_NULL_CHECK(cbytes);
×
UNCOV
706
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
707
      } else if (vAlterTbReq.type == TSDB_DATA_TYPE_NCHAR) {
4!
UNCOV
708
        int32_t length = (vAlterTbReq.bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
709
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
710
        RAW_NULL_CHECK(cbytes);
×
711
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
712
      }
713
      break;
4✔
714
    }
UNCOV
715
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION: {
×
UNCOV
716
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
×
UNCOV
717
      RAW_NULL_CHECK(colName);
×
UNCOV
718
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
×
UNCOV
719
      cJSON* colType = cJSON_CreateNumber(vAlterTbReq.type);
×
UNCOV
720
      RAW_NULL_CHECK(colType);
×
UNCOV
721
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colType", colType));
×
722

UNCOV
723
      if (vAlterTbReq.type == TSDB_DATA_TYPE_BINARY || vAlterTbReq.type == TSDB_DATA_TYPE_VARBINARY ||
×
UNCOV
724
          vAlterTbReq.type == TSDB_DATA_TYPE_GEOMETRY) {
×
UNCOV
725
        int32_t length = vAlterTbReq.bytes - VARSTR_HEADER_SIZE;
×
UNCOV
726
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
UNCOV
727
        RAW_NULL_CHECK(cbytes);
×
UNCOV
728
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
UNCOV
729
      } else if (vAlterTbReq.type == TSDB_DATA_TYPE_NCHAR) {
×
UNCOV
730
        int32_t length = (vAlterTbReq.bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
UNCOV
731
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
UNCOV
732
        RAW_NULL_CHECK(cbytes);
×
UNCOV
733
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
734
      }
UNCOV
735
      RAW_RETURN_CHECK(setCompressOption(json, vAlterTbReq.compress));
×
UNCOV
736
      break;
×
737
    }
738
    case TSDB_ALTER_TABLE_DROP_COLUMN: {
4✔
739
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
4✔
740
      RAW_NULL_CHECK(colName);
4!
741
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
4!
742
      break;
4✔
743
    }
744
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES: {
4✔
745
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
4✔
746
      RAW_NULL_CHECK(colName);
4!
747
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
4!
748
      cJSON* colType = cJSON_CreateNumber(vAlterTbReq.colModType);
4✔
749
      RAW_NULL_CHECK(colType);
4!
750
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colType", colType));
4!
751
      if (vAlterTbReq.colModType == TSDB_DATA_TYPE_BINARY || vAlterTbReq.colModType == TSDB_DATA_TYPE_VARBINARY ||
4!
752
          vAlterTbReq.colModType == TSDB_DATA_TYPE_GEOMETRY) {
4!
UNCOV
753
        int32_t length = vAlterTbReq.colModBytes - VARSTR_HEADER_SIZE;
×
UNCOV
754
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
UNCOV
755
        RAW_NULL_CHECK(cbytes);
×
756
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
757
      } else if (vAlterTbReq.colModType == TSDB_DATA_TYPE_NCHAR) {
4!
758
        int32_t length = (vAlterTbReq.colModBytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
4✔
759
        cJSON*  cbytes = cJSON_CreateNumber(length);
4✔
760
        RAW_NULL_CHECK(cbytes);
4!
761
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
4!
762
      }
763
      break;
4✔
764
    }
765
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME: {
4✔
766
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
4✔
767
      RAW_NULL_CHECK(colName);
4!
768
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
4!
769
      cJSON* colNewName = cJSON_CreateString(vAlterTbReq.colNewName);
4✔
770
      RAW_NULL_CHECK(colNewName);
4!
771
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colNewName", colNewName));
4!
772
      break;
4✔
773
    }
774
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL: {
4✔
775
      cJSON* tagName = cJSON_CreateString(vAlterTbReq.tagName);
4✔
776
      RAW_NULL_CHECK(tagName);
4!
777
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", tagName));
4!
778

779
      bool isNull = vAlterTbReq.isNull;
4✔
780
      if (vAlterTbReq.tagType == TSDB_DATA_TYPE_JSON) {
4!
781
        STag* jsonTag = (STag*)vAlterTbReq.pTagVal;
×
782
        if (jsonTag->nTag == 0) isNull = true;
×
783
      }
784
      if (!isNull) {
4!
785
        char* buf = NULL;
4✔
786

787
        if (vAlterTbReq.tagType == TSDB_DATA_TYPE_JSON) {
4!
788
          if (!tTagIsJson(vAlterTbReq.pTagVal)) {
×
789
            uError("processAlterTable isJson false");
×
UNCOV
790
            goto end;
×
791
          }
792
          parseTagDatatoJson(vAlterTbReq.pTagVal, &buf, NULL);
×
793
          if (buf == NULL) {
×
794
            uError("parseTagDatatoJson failed, buf == NULL");
×
UNCOV
795
            goto end;
×
796
          }
797
        } else {
798
          int64_t bufSize = 0;
4✔
799
          if (vAlterTbReq.tagType == TSDB_DATA_TYPE_VARBINARY) {
4!
UNCOV
800
            bufSize = vAlterTbReq.nTagVal * 2 + 2 + 3;
×
801
          } else {
802
            bufSize = vAlterTbReq.nTagVal + 3;
4✔
803
          }
804
          buf = taosMemoryCalloc(bufSize, 1);
4!
805
          RAW_NULL_CHECK(buf);
4!
806
          if (dataConverToStr(buf, bufSize, vAlterTbReq.tagType, vAlterTbReq.pTagVal, vAlterTbReq.nTagVal, NULL) !=
4!
807
              TSDB_CODE_SUCCESS) {
808
            taosMemoryFree(buf);
×
809
            goto end;
×
810
          }
811
        }
812

813
        cJSON* colValue = cJSON_CreateString(buf);
4✔
814
        taosMemoryFree(buf);
4!
815
        RAW_NULL_CHECK(colValue);
4!
816
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colValue", colValue));
4!
817
      }
818

819
      cJSON* isNullCJson = cJSON_CreateBool(isNull);
4✔
820
      RAW_NULL_CHECK(isNullCJson);
4!
821
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colValueNull", isNullCJson));
4!
822
      break;
4✔
823
    }
UNCOV
824
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL: {
×
825
      int32_t nTags = taosArrayGetSize(vAlterTbReq.pMultiTag);
×
826
      if (nTags <= 0) {
×
827
        uError("processAlterTable parse multi tags error");
×
UNCOV
828
        goto end;
×
829
      }
830

UNCOV
831
      cJSON* tags = cJSON_CreateArray();
×
832
      RAW_NULL_CHECK(tags);
×
833
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tags", tags));
×
834

835
      for (int32_t i = 0; i < nTags; i++) {
×
836
        cJSON* member = cJSON_CreateObject();
×
837
        RAW_NULL_CHECK(member);
×
UNCOV
838
        RAW_FALSE_CHECK(tmqAddJsonArrayItem(tags, member));
×
839

UNCOV
840
        SMultiTagUpateVal* pTagVal = taosArrayGet(vAlterTbReq.pMultiTag, i);
×
UNCOV
841
        cJSON*             tagName = cJSON_CreateString(pTagVal->tagName);
×
UNCOV
842
        RAW_NULL_CHECK(tagName);
×
UNCOV
843
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(member, "colName", tagName));
×
844

UNCOV
845
        if (pTagVal->tagType == TSDB_DATA_TYPE_JSON) {
×
846
          uError("processAlterTable isJson false");
×
UNCOV
847
          goto end;
×
848
        }
UNCOV
849
        bool isNull = pTagVal->isNull;
×
UNCOV
850
        if (!isNull) {
×
UNCOV
851
          int64_t bufSize = 0;
×
UNCOV
852
          if (pTagVal->tagType == TSDB_DATA_TYPE_VARBINARY) {
×
UNCOV
853
            bufSize = pTagVal->nTagVal * 2 + 2 + 3;
×
854
          } else {
UNCOV
855
            bufSize = pTagVal->nTagVal + 3;
×
856
          }
UNCOV
857
          char* buf = taosMemoryCalloc(bufSize, 1);
×
UNCOV
858
          RAW_NULL_CHECK(buf);
×
UNCOV
859
          if (dataConverToStr(buf, bufSize, pTagVal->tagType, pTagVal->pTagVal, pTagVal->nTagVal, NULL) !=
×
860
              TSDB_CODE_SUCCESS) {
UNCOV
861
            taosMemoryFree(buf);
×
UNCOV
862
            goto end;
×
863
          }
UNCOV
864
          cJSON* colValue = cJSON_CreateString(buf);
×
865
          taosMemoryFree(buf);
×
866
          RAW_NULL_CHECK(colValue);
×
UNCOV
867
          RAW_FALSE_CHECK(tmqAddJsonObjectItem(member, "colValue", colValue));
×
868
        }
UNCOV
869
        cJSON* isNullCJson = cJSON_CreateBool(isNull);
×
UNCOV
870
        RAW_NULL_CHECK(isNullCJson);
×
UNCOV
871
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(member, "colValueNull", isNullCJson));
×
872
      }
UNCOV
873
      break;
×
874
    }
875

UNCOV
876
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS: {
×
UNCOV
877
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
×
UNCOV
878
      RAW_NULL_CHECK(colName);
×
UNCOV
879
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
×
UNCOV
880
      RAW_RETURN_CHECK(setCompressOption(json, vAlterTbReq.compress));
×
UNCOV
881
      break;
×
882
    }
883
    default:
4✔
884
      break;
4✔
885
  }
886

887
end:
24✔
888
  uDebug("alter table return");
24!
889
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
24!
UNCOV
890
    taosArrayDestroy(vAlterTbReq.pMultiTag);
×
891
  }
892
  tDecoderClear(&decoder);
24✔
893
  *pJson = json;
24✔
894
}
895

896
static void processDropSTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
8✔
897
  if (pJson == NULL || metaRsp == NULL) {
8!
UNCOV
898
    uError("invalid parameter in %s", __func__);
×
899
    return;
×
900
  }
901
  SDecoder     decoder = {0};
8✔
902
  SVDropStbReq req = {0};
8✔
903
  cJSON*       json = NULL;
8✔
904
  int32_t      code = 0;
8✔
905

906
  uDebug("processDropSTable data:%p", metaRsp);
8!
907

908
  // decode
909
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
8✔
910
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
8✔
911
  tDecoderInit(&decoder, data, len);
8✔
912
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
8!
UNCOV
913
    uError("tDecodeSVDropStbReq failed");
×
UNCOV
914
    goto end;
×
915
  }
916

917
  json = cJSON_CreateObject();
8✔
918
  RAW_NULL_CHECK(json);
8!
919
  cJSON* type = cJSON_CreateString("drop");
8✔
920
  RAW_NULL_CHECK(type);
8!
921
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "type", type));
8!
922
  cJSON* tableType = cJSON_CreateString("super");
8✔
923
  RAW_NULL_CHECK(tableType);
8!
924
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableType", tableType));
8!
925
  cJSON* tableName = cJSON_CreateString(req.name);
8✔
926
  RAW_NULL_CHECK(tableName);
8!
927
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableName", tableName));
8!
928

929
end:
8✔
930
  uDebug("processDropSTable return");
8!
931
  tDecoderClear(&decoder);
8✔
932
  *pJson = json;
8✔
933
}
934
static void processDeleteTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
3✔
935
  if (pJson == NULL || metaRsp == NULL) {
3!
936
    uError("invalid parameter in %s", __func__);
×
UNCOV
937
    return;
×
938
  }
939
  SDeleteRes req = {0};
3✔
940
  SDecoder   coder = {0};
3✔
941
  cJSON*     json = NULL;
3✔
942
  int32_t    code = 0;
3✔
943

944
  uDebug("processDeleteTable data:%p", metaRsp);
3!
945
  // decode and process req
946
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
3✔
947
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
3✔
948

949
  tDecoderInit(&coder, data, len);
3✔
950
  if (tDecodeDeleteRes(&coder, &req) < 0) {
3!
UNCOV
951
    uError("tDecodeDeleteRes failed");
×
UNCOV
952
    goto end;
×
953
  }
954

955
  //  getTbName(req.tableFName);
956
  char sql[256] = {0};
3✔
957
  (void)snprintf(sql, sizeof(sql), "delete from `%s` where `%s` >= %" PRId64 " and `%s` <= %" PRId64, req.tableFName,
3✔
958
                 req.tsColName, req.skey, req.tsColName, req.ekey);
959

960
  json = cJSON_CreateObject();
3✔
961
  RAW_NULL_CHECK(json);
3!
962
  cJSON* type = cJSON_CreateString("delete");
3✔
963
  RAW_NULL_CHECK(type);
3!
964
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "type", type));
3!
965
  cJSON* sqlJson = cJSON_CreateString(sql);
3✔
966
  RAW_NULL_CHECK(sqlJson);
3!
967
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "sql", sqlJson));
3!
968

969
end:
3✔
970
  uDebug("processDeleteTable return");
3!
971
  tDecoderClear(&coder);
3✔
972
  *pJson = json;
3✔
973
}
974

975
static void processDropTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
5✔
976
  if (pJson == NULL || metaRsp == NULL) {
5!
UNCOV
977
    uError("invalid parameter in %s", __func__);
×
UNCOV
978
    return;
×
979
  }
980
  SDecoder         decoder = {0};
5✔
981
  SVDropTbBatchReq req = {0};
5✔
982
  cJSON*           json = NULL;
5✔
983
  int32_t          code = 0;
5✔
984

985
  uDebug("processDropTable data:%p", metaRsp);
5!
986
  // decode
987
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
5✔
988
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
5✔
989
  tDecoderInit(&decoder, data, len);
5✔
990
  if (tDecodeSVDropTbBatchReq(&decoder, &req) < 0) {
5!
UNCOV
991
    uError("tDecodeSVDropTbBatchReq failed");
×
UNCOV
992
    goto end;
×
993
  }
994

995
  json = cJSON_CreateObject();
5✔
996
  RAW_NULL_CHECK(json);
5!
997
  cJSON* type = cJSON_CreateString("drop");
5✔
998
  RAW_NULL_CHECK(type);
5!
999
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "type", type));
5!
1000
  cJSON* tableNameList = cJSON_CreateArray();
5✔
1001
  RAW_NULL_CHECK(tableNameList);
5!
1002
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableNameList", tableNameList));
5!
1003

1004
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
11✔
1005
    SVDropTbReq* pDropTbReq = req.pReqs + iReq;
6✔
1006
    cJSON*       tableName = cJSON_CreateString(pDropTbReq->name);
6✔
1007
    RAW_NULL_CHECK(tableName);
6!
1008
    RAW_FALSE_CHECK(tmqAddJsonArrayItem(tableNameList, tableName));
6!
1009
  }
1010

1011
end:
5✔
1012
  uDebug("processDropTable return");
5!
1013
  tDecoderClear(&decoder);
5✔
1014
  *pJson = json;
5✔
1015
}
1016

1017
static int32_t taosCreateStb(TAOS* taos, void* meta, uint32_t metaLen) {
118✔
1018
  if (taos == NULL || meta == NULL) {
118!
UNCOV
1019
    uError("invalid parameter in %s", __func__);
×
UNCOV
1020
    return TSDB_CODE_INVALID_PARA;
×
1021
  }
1022
  SVCreateStbReq req = {0};
118✔
1023
  SDecoder       coder;
1024
  SMCreateStbReq pReq = {0};
118✔
1025
  int32_t        code = TSDB_CODE_SUCCESS;
118✔
1026
  SRequestObj*   pRequest = NULL;
118✔
1027

1028
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
118!
1029
  uDebug(LOG_ID_TAG " create stable, meta:%p, metaLen:%d", LOG_ID_VALUE, meta, metaLen);
118!
1030
  pRequest->syncQuery = true;
118✔
1031
  if (!pRequest->pDb) {
118!
UNCOV
1032
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
UNCOV
1033
    goto end;
×
1034
  }
1035
  // decode and process req
1036
  void*    data = POINTER_SHIFT(meta, sizeof(SMsgHead));
118✔
1037
  uint32_t len = metaLen - sizeof(SMsgHead);
118✔
1038
  tDecoderInit(&coder, data, len);
118✔
1039
  if (tDecodeSVCreateStbReq(&coder, &req) < 0) {
118!
1040
    code = TSDB_CODE_INVALID_PARA;
×
1041
    goto end;
×
1042
  }
1043

1044
  int8_t           createDefaultCompress = 0;
118✔
1045
  SColCmprWrapper* p = &req.colCmpr;
118✔
1046
  if (p->nCols == 0) {
118!
UNCOV
1047
    createDefaultCompress = 1;
×
1048
  }
1049
  // build create stable
1050
  pReq.pColumns = taosArrayInit(req.schemaRow.nCols, sizeof(SFieldWithOptions));
118✔
1051
  RAW_NULL_CHECK(pReq.pColumns);
118!
1052
  for (int32_t i = 0; i < req.schemaRow.nCols; i++) {
744✔
1053
    SSchema*          pSchema = req.schemaRow.pSchema + i;
626✔
1054
    SFieldWithOptions field = {.type = pSchema->type, .flags = pSchema->flags, .bytes = pSchema->bytes};
626✔
1055
    tstrncpy(field.name, pSchema->name, TSDB_COL_NAME_LEN);
626✔
1056

1057
    if (createDefaultCompress) {
626!
UNCOV
1058
      field.compress = createDefaultColCmprByType(pSchema->type);
×
1059
    } else {
1060
      SColCmpr* pCmp = &req.colCmpr.pColCmpr[i];
626✔
1061
      field.compress = pCmp->alg;
626✔
1062
    }
1063
    RAW_NULL_CHECK(taosArrayPush(pReq.pColumns, &field));
1,252!
1064
  }
1065
  pReq.pTags = taosArrayInit(req.schemaTag.nCols, sizeof(SField));
118✔
1066
  RAW_NULL_CHECK(pReq.pTags);
118!
1067
  for (int32_t i = 0; i < req.schemaTag.nCols; i++) {
440✔
1068
    SSchema* pSchema = req.schemaTag.pSchema + i;
322✔
1069
    SField   field = {.type = pSchema->type, .flags = pSchema->flags, .bytes = pSchema->bytes};
322✔
1070
    tstrncpy(field.name, pSchema->name, TSDB_COL_NAME_LEN);
322✔
1071
    RAW_NULL_CHECK(taosArrayPush(pReq.pTags, &field));
644!
1072
  }
1073

1074
  pReq.colVer = req.schemaRow.version;
118✔
1075
  pReq.tagVer = req.schemaTag.version;
118✔
1076
  pReq.numOfColumns = req.schemaRow.nCols;
118✔
1077
  pReq.numOfTags = req.schemaTag.nCols;
118✔
1078
  pReq.commentLen = -1;
118✔
1079
  pReq.suid = processSuid(req.suid, pRequest->pDb);
118✔
1080
  pReq.source = TD_REQ_FROM_TAOX;
118✔
1081
  pReq.igExists = true;
118✔
1082

1083
  uDebug(LOG_ID_TAG " create stable name:%s suid:%" PRId64 " processSuid:%" PRId64, LOG_ID_VALUE, req.name, req.suid,
118!
1084
         pReq.suid);
1085
  STscObj* pTscObj = pRequest->pTscObj;
118✔
1086
  SName    tableName = {0};
118✔
1087
  toName(pTscObj->acctId, pRequest->pDb, req.name, &tableName);
118✔
1088
  RAW_RETURN_CHECK(tNameExtractFullName(&tableName, pReq.name));
118!
1089
  SCmdMsgInfo pCmdMsg = {0};
118✔
1090
  pCmdMsg.epSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
118✔
1091
  pCmdMsg.msgType = TDMT_MND_CREATE_STB;
118✔
1092
  pCmdMsg.msgLen = tSerializeSMCreateStbReq(NULL, 0, &pReq);
118✔
1093
  if (pCmdMsg.msgLen <= 0) {
118!
UNCOV
1094
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1095
    goto end;
×
1096
  }
1097
  pCmdMsg.pMsg = taosMemoryMalloc(pCmdMsg.msgLen);
118!
1098
  RAW_NULL_CHECK(pCmdMsg.pMsg);
118!
1099
  if (tSerializeSMCreateStbReq(pCmdMsg.pMsg, pCmdMsg.msgLen, &pReq) <= 0) {
118!
UNCOV
1100
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1101
    taosMemoryFree(pCmdMsg.pMsg);
×
UNCOV
1102
    goto end;
×
1103
  }
1104

1105
  SQuery pQuery = {0};
118✔
1106
  pQuery.execMode = QUERY_EXEC_MODE_RPC;
118✔
1107
  pQuery.pCmdMsg = &pCmdMsg;
118✔
1108
  pQuery.msgType = pQuery.pCmdMsg->msgType;
118✔
1109
  pQuery.stableQuery = true;
118✔
1110

1111
  launchQueryImpl(pRequest, &pQuery, true, NULL);  // ignore, because return value is pRequest
118✔
1112

1113
  taosMemoryFree(pCmdMsg.pMsg);
118!
1114

1115
  if (pRequest->code == TSDB_CODE_SUCCESS) {
118!
1116
    SCatalog* pCatalog = NULL;
118✔
1117
    RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
118!
1118
    RAW_RETURN_CHECK(catalogRemoveTableMeta(pCatalog, &tableName));
118!
1119
  }
1120

1121
  code = pRequest->code;
118✔
1122

1123
end:
118✔
1124
  uDebug(LOG_ID_TAG " create stable return, msg:%s", LOG_ID_VALUE, tstrerror(code));
118!
1125
  destroyRequest(pRequest);
118✔
1126
  tFreeSMCreateStbReq(&pReq);
118✔
1127
  tDecoderClear(&coder);
118✔
1128
  return code;
118✔
1129
}
1130

1131
static int32_t taosDropStb(TAOS* taos, void* meta, uint32_t metaLen) {
8✔
1132
  if (taos == NULL || meta == NULL) {
8!
UNCOV
1133
    uError("invalid parameter in %s", __func__);
×
UNCOV
1134
    return TSDB_CODE_INVALID_PARA;
×
1135
  }
1136
  SVDropStbReq req = {0};
8✔
1137
  SDecoder     coder = {0};
8✔
1138
  SMDropStbReq pReq = {0};
8✔
1139
  int32_t      code = TSDB_CODE_SUCCESS;
8✔
1140
  SRequestObj* pRequest = NULL;
8✔
1141

1142
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
8!
1143
  uDebug(LOG_ID_TAG " drop stable, meta:%p, metaLen:%d", LOG_ID_VALUE, meta, metaLen);
8!
1144
  pRequest->syncQuery = true;
8✔
1145
  if (!pRequest->pDb) {
8!
UNCOV
1146
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
UNCOV
1147
    goto end;
×
1148
  }
1149
  // decode and process req
1150
  void*    data = POINTER_SHIFT(meta, sizeof(SMsgHead));
8✔
1151
  uint32_t len = metaLen - sizeof(SMsgHead);
8✔
1152
  tDecoderInit(&coder, data, len);
8✔
1153
  if (tDecodeSVDropStbReq(&coder, &req) < 0) {
8!
UNCOV
1154
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1155
    goto end;
×
1156
  }
1157

1158
  SCatalog* pCatalog = NULL;
8✔
1159
  RAW_RETURN_CHECK(catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog));
8!
1160
  SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
8✔
1161
                           .requestId = pRequest->requestId,
8✔
1162
                           .requestObjRefId = pRequest->self,
8✔
1163
                           .mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp)};
8✔
1164
  SName            pName = {0};
8✔
1165
  toName(pRequest->pTscObj->acctId, pRequest->pDb, req.name, &pName);
8✔
1166
  STableMeta* pTableMeta = NULL;
8✔
1167
  code = catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta);
8✔
1168
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
8✔
1169
    code = TSDB_CODE_SUCCESS;
2✔
1170
    taosMemoryFreeClear(pTableMeta);
2!
1171
    goto end;
2✔
1172
  }
1173
  if (code != TSDB_CODE_SUCCESS) {
6!
UNCOV
1174
    goto end;
×
1175
  }
1176
  pReq.suid = pTableMeta->uid;
6✔
1177
  taosMemoryFreeClear(pTableMeta);
6!
1178

1179
  // build drop stable
1180
  pReq.igNotExists = true;
6✔
1181
  pReq.source = TD_REQ_FROM_TAOX;
6✔
1182
  //  pReq.suid = processSuid(req.suid, pRequest->pDb);
1183

1184
  uDebug(LOG_ID_TAG " drop stable name:%s suid:%" PRId64 " new suid:%" PRId64, LOG_ID_VALUE, req.name, req.suid,
6!
1185
         pReq.suid);
1186
  STscObj* pTscObj = pRequest->pTscObj;
6✔
1187
  SName    tableName = {0};
6✔
1188
  toName(pTscObj->acctId, pRequest->pDb, req.name, &tableName);
6✔
1189
  if (tNameExtractFullName(&tableName, pReq.name) != 0) {
6!
UNCOV
1190
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1191
    goto end;
×
1192
  }
1193

1194
  SCmdMsgInfo pCmdMsg = {0};
6✔
1195
  pCmdMsg.epSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
6✔
1196
  pCmdMsg.msgType = TDMT_MND_DROP_STB;
6✔
1197
  pCmdMsg.msgLen = tSerializeSMDropStbReq(NULL, 0, &pReq);
6✔
1198
  if (pCmdMsg.msgLen <= 0) {
6!
UNCOV
1199
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1200
    goto end;
×
1201
  }
1202
  pCmdMsg.pMsg = taosMemoryMalloc(pCmdMsg.msgLen);
6!
1203
  RAW_NULL_CHECK(pCmdMsg.pMsg);
6!
1204
  if (tSerializeSMDropStbReq(pCmdMsg.pMsg, pCmdMsg.msgLen, &pReq) <= 0) {
6!
UNCOV
1205
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1206
    taosMemoryFree(pCmdMsg.pMsg);
×
UNCOV
1207
    goto end;
×
1208
  }
1209

1210
  SQuery pQuery = {0};
6✔
1211
  pQuery.execMode = QUERY_EXEC_MODE_RPC;
6✔
1212
  pQuery.pCmdMsg = &pCmdMsg;
6✔
1213
  pQuery.msgType = pQuery.pCmdMsg->msgType;
6✔
1214
  pQuery.stableQuery = true;
6✔
1215

1216
  launchQueryImpl(pRequest, &pQuery, true, NULL);  // ignore, because return value is pRequest
6✔
1217
  taosMemoryFree(pCmdMsg.pMsg);
6!
1218
  if (pRequest->code == TSDB_CODE_SUCCESS) {
6!
1219
    // ignore the error code
1220
    RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
6!
1221
    RAW_RETURN_CHECK(catalogRemoveTableMeta(pCatalog, &tableName));
6!
1222
  }
1223

1224
  code = pRequest->code;
6✔
1225

1226
end:
8✔
1227
  uDebug(LOG_ID_TAG " drop stable return, msg:%s", LOG_ID_VALUE, tstrerror(code));
8!
1228
  destroyRequest(pRequest);
8✔
1229
  tDecoderClear(&coder);
8✔
1230
  return code;
8✔
1231
}
1232

1233
typedef struct SVgroupCreateTableBatch {
1234
  SVCreateTbBatchReq req;
1235
  SVgroupInfo        info;
1236
  char               dbName[TSDB_DB_NAME_LEN];
1237
} SVgroupCreateTableBatch;
1238

1239
static void destroyCreateTbReqBatch(void* data) {
133✔
1240
  if (data == NULL) {
133!
UNCOV
1241
    uError("invalid parameter in %s", __func__);
×
UNCOV
1242
    return;
×
1243
  }
1244
  SVgroupCreateTableBatch* pTbBatch = (SVgroupCreateTableBatch*)data;
133✔
1245
  taosArrayDestroy(pTbBatch->req.pArray);
133✔
1246
}
1247

1248
static int32_t taosCreateTable(TAOS* taos, void* meta, uint32_t metaLen) {
131✔
1249
  if (taos == NULL || meta == NULL) {
131!
1250
    uError("invalid parameter in %s", __func__);
×
UNCOV
1251
    return TSDB_CODE_INVALID_PARA;
×
1252
  }
1253
  SVCreateTbBatchReq req = {0};
131✔
1254
  SDecoder           coder = {0};
131✔
1255
  int32_t            code = TSDB_CODE_SUCCESS;
131✔
1256
  SRequestObj*       pRequest = NULL;
131✔
1257
  SQuery*            pQuery = NULL;
131✔
1258
  SHashObj*          pVgroupHashmap = NULL;
131✔
1259
  SArray*            pTagList = taosArrayInit(0, POINTER_BYTES);
131✔
1260
  RAW_NULL_CHECK(pTagList);
131!
1261
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
131!
1262
  uDebug(LOG_ID_TAG " create table, meta:%p, metaLen:%d", LOG_ID_VALUE, meta, metaLen);
131!
1263

1264
  pRequest->syncQuery = true;
131✔
1265
  if (!pRequest->pDb) {
131!
UNCOV
1266
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
UNCOV
1267
    goto end;
×
1268
  }
1269
  // decode and process req
1270
  void*    data = POINTER_SHIFT(meta, sizeof(SMsgHead));
131✔
1271
  uint32_t len = metaLen - sizeof(SMsgHead);
131✔
1272
  tDecoderInit(&coder, data, len);
131✔
1273
  if (tDecodeSVCreateTbBatchReq(&coder, &req) < 0) {
131!
UNCOV
1274
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1275
    goto end;
×
1276
  }
1277

1278
  STscObj* pTscObj = pRequest->pTscObj;
131✔
1279

1280
  SVCreateTbReq* pCreateReq = NULL;
131✔
1281
  SCatalog*      pCatalog = NULL;
131✔
1282
  RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
131!
1283
  pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
131✔
1284
  RAW_NULL_CHECK(pVgroupHashmap);
131!
1285
  taosHashSetFreeFp(pVgroupHashmap, destroyCreateTbReqBatch);
131✔
1286

1287
  SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
131✔
1288
                           .requestId = pRequest->requestId,
131✔
1289
                           .requestObjRefId = pRequest->self,
131✔
1290
                           .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
131✔
1291

1292
  pRequest->tableList = taosArrayInit(req.nReqs, sizeof(SName));
131✔
1293
  RAW_NULL_CHECK(pRequest->tableList);
131!
1294
  // loop to create table
1295
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
272✔
1296
    pCreateReq = req.pReqs + iReq;
141✔
1297

1298
    SVgroupInfo pInfo = {0};
141✔
1299
    SName       pName = {0};
141✔
1300
    toName(pTscObj->acctId, pRequest->pDb, pCreateReq->name, &pName);
141✔
1301
    code = catalogGetTableHashVgroup(pCatalog, &conn, &pName, &pInfo);
141✔
1302
    if (code != TSDB_CODE_SUCCESS) {
141!
UNCOV
1303
      goto end;
×
1304
    }
1305

1306
    pCreateReq->flags |= TD_CREATE_IF_NOT_EXISTS;
141✔
1307
    // change tag cid to new cid
1308
    if (pCreateReq->type == TSDB_CHILD_TABLE) {
141✔
1309
      STableMeta* pTableMeta = NULL;
123✔
1310
      SName       sName = {0};
123✔
1311
      tb_uid_t    oldSuid = pCreateReq->ctb.suid;
123✔
1312
      //      pCreateReq->ctb.suid = processSuid(pCreateReq->ctb.suid, pRequest->pDb);
1313
      toName(pTscObj->acctId, pRequest->pDb, pCreateReq->ctb.stbName, &sName);
123✔
1314
      code = catalogGetTableMeta(pCatalog, &conn, &sName, &pTableMeta);
123✔
1315
      if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
123!
UNCOV
1316
        code = TSDB_CODE_SUCCESS;
×
UNCOV
1317
        taosMemoryFreeClear(pTableMeta);
×
UNCOV
1318
        continue;
×
1319
      }
1320

1321
      if (code != TSDB_CODE_SUCCESS) {
123!
UNCOV
1322
        goto end;
×
1323
      }
1324
      pCreateReq->ctb.suid = pTableMeta->uid;
123✔
1325

1326
      SArray* pTagVals = NULL;
123✔
1327
      code = tTagToValArray((STag*)pCreateReq->ctb.pTag, &pTagVals);
123✔
1328
      if (code != TSDB_CODE_SUCCESS) {
123!
UNCOV
1329
        taosMemoryFreeClear(pTableMeta);
×
UNCOV
1330
        goto end;
×
1331
      }
1332

1333
      bool rebuildTag = false;
123✔
1334
      for (int32_t i = 0; i < taosArrayGetSize(pCreateReq->ctb.tagName); i++) {
377✔
1335
        char* tName = taosArrayGet(pCreateReq->ctb.tagName, i);
254✔
1336
        if (tName == NULL) {
254!
UNCOV
1337
          continue;
×
1338
        }
1339
        for (int32_t j = pTableMeta->tableInfo.numOfColumns;
254✔
1340
             j < pTableMeta->tableInfo.numOfColumns + pTableMeta->tableInfo.numOfTags; j++) {
1,002✔
1341
          SSchema* tag = &pTableMeta->schema[j];
748✔
1342
          if (strcmp(tag->name, tName) == 0 && tag->type != TSDB_DATA_TYPE_JSON) {
748✔
1343
            STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, i);
240✔
1344
            if (pTagVal) {
240!
1345
              if (pTagVal->cid != tag->colId) {
240✔
1346
                pTagVal->cid = tag->colId;
21✔
1347
                rebuildTag = true;
21✔
1348
              }
1349
            } else {
UNCOV
1350
              uError("create tb invalid data %s, size:%d index:%d cid:%d", pCreateReq->name,
×
1351
                     (int)taosArrayGetSize(pTagVals), i, tag->colId);
1352
            }
1353
          }
1354
        }
1355
      }
1356
      taosMemoryFreeClear(pTableMeta);
123!
1357
      if (rebuildTag) {
123✔
1358
        STag* ppTag = NULL;
13✔
1359
        code = tTagNew(pTagVals, 1, false, &ppTag);
13✔
1360
        taosArrayDestroy(pTagVals);
13✔
1361
        pTagVals = NULL;
13✔
1362
        if (code != TSDB_CODE_SUCCESS) {
13!
UNCOV
1363
          goto end;
×
1364
        }
1365
        if (NULL == taosArrayPush(pTagList, &ppTag)) {
13!
UNCOV
1366
          tTagFree(ppTag);
×
UNCOV
1367
          goto end;
×
1368
        }
1369
        pCreateReq->ctb.pTag = (uint8_t*)ppTag;
13✔
1370
      }
1371
      taosArrayDestroy(pTagVals);
123✔
1372
    }
1373
    RAW_NULL_CHECK(taosArrayPush(pRequest->tableList, &pName));
282!
1374

1375
    SVgroupCreateTableBatch* pTableBatch = taosHashGet(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId));
141✔
1376
    if (pTableBatch == NULL) {
141✔
1377
      SVgroupCreateTableBatch tBatch = {0};
133✔
1378
      tBatch.info = pInfo;
133✔
1379
      tstrncpy(tBatch.dbName, pRequest->pDb, TSDB_DB_NAME_LEN);
133✔
1380

1381
      tBatch.req.pArray = taosArrayInit(4, sizeof(struct SVCreateTbReq));
133✔
1382
      RAW_NULL_CHECK(tBatch.req.pArray);
133!
1383
      RAW_NULL_CHECK(taosArrayPush(tBatch.req.pArray, pCreateReq));
266!
1384
      tBatch.req.source = TD_REQ_FROM_TAOX;
133✔
1385
      RAW_RETURN_CHECK(taosHashPut(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId), &tBatch, sizeof(tBatch)));
133!
1386
    } else {  // add to the correct vgroup
1387
      RAW_NULL_CHECK(taosArrayPush(pTableBatch->req.pArray, pCreateReq));
16!
1388
    }
1389
  }
1390

1391
  if (taosHashGetSize(pVgroupHashmap) == 0) {
131!
UNCOV
1392
    goto end;
×
1393
  }
1394
  SArray* pBufArray = NULL;
131✔
1395
  RAW_RETURN_CHECK(serializeVgroupsCreateTableBatch(pVgroupHashmap, &pBufArray));
131!
1396
  pQuery = NULL;
131✔
1397
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode**)&pQuery);
131✔
1398
  if (TSDB_CODE_SUCCESS != code) goto end;
131!
1399
  pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
131✔
1400
  pQuery->msgType = TDMT_VND_CREATE_TABLE;
131✔
1401
  pQuery->stableQuery = false;
131✔
1402
  code = nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT, &pQuery->pRoot);
131✔
1403
  if (TSDB_CODE_SUCCESS != code) goto end;
131!
1404
  RAW_NULL_CHECK(pQuery->pRoot);
131!
1405

1406
  RAW_RETURN_CHECK(rewriteToVnodeModifyOpStmt(pQuery, pBufArray));
131!
1407

1408
  launchQueryImpl(pRequest, pQuery, true, NULL);
131✔
1409
  if (pRequest->code == TSDB_CODE_SUCCESS) {
131!
1410
    RAW_RETURN_CHECK(removeMeta(pTscObj, pRequest->tableList, false));
131!
1411
  }
1412

1413
  code = pRequest->code;
131✔
1414

1415
end:
131✔
1416
  uDebug(LOG_ID_TAG " create table return, msg:%s", LOG_ID_VALUE, tstrerror(code));
131!
1417
  tDeleteSVCreateTbBatchReq(&req);
131✔
1418

1419
  taosHashCleanup(pVgroupHashmap);
131✔
1420
  destroyRequest(pRequest);
131✔
1421
  tDecoderClear(&coder);
131✔
1422
  qDestroyQuery(pQuery);
131✔
1423
  taosArrayDestroyP(pTagList, NULL);
131✔
1424
  return code;
131✔
1425
}
1426

1427
typedef struct SVgroupDropTableBatch {
1428
  SVDropTbBatchReq req;
1429
  SVgroupInfo      info;
1430
  char             dbName[TSDB_DB_NAME_LEN];
1431
} SVgroupDropTableBatch;
1432

1433
static void destroyDropTbReqBatch(void* data) {
3✔
1434
  if (data == NULL) {
3!
UNCOV
1435
    uError("invalid parameter in %s", __func__);
×
UNCOV
1436
    return;
×
1437
  }
1438
  SVgroupDropTableBatch* pTbBatch = (SVgroupDropTableBatch*)data;
3✔
1439
  taosArrayDestroy(pTbBatch->req.pArray);
3✔
1440
}
1441

1442
static int32_t taosDropTable(TAOS* taos, void* meta, uint32_t metaLen) {
5✔
1443
  if (taos == NULL || meta == NULL) {
5!
UNCOV
1444
    uError("invalid parameter in %s", __func__);
×
UNCOV
1445
    return TSDB_CODE_INVALID_PARA;
×
1446
  }
1447
  SVDropTbBatchReq req = {0};
5✔
1448
  SDecoder         coder = {0};
5✔
1449
  int32_t          code = TSDB_CODE_SUCCESS;
5✔
1450
  SRequestObj*     pRequest = NULL;
5✔
1451
  SQuery*          pQuery = NULL;
5✔
1452
  SHashObj*        pVgroupHashmap = NULL;
5✔
1453

1454
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
5!
1455
  uDebug(LOG_ID_TAG " drop table, meta:%p, len:%d", LOG_ID_VALUE, meta, metaLen);
5!
1456

1457
  pRequest->syncQuery = true;
5✔
1458
  if (!pRequest->pDb) {
5!
UNCOV
1459
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
UNCOV
1460
    goto end;
×
1461
  }
1462
  // decode and process req
1463
  void*    data = POINTER_SHIFT(meta, sizeof(SMsgHead));
5✔
1464
  uint32_t len = metaLen - sizeof(SMsgHead);
5✔
1465
  tDecoderInit(&coder, data, len);
5✔
1466
  if (tDecodeSVDropTbBatchReq(&coder, &req) < 0) {
5!
UNCOV
1467
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1468
    goto end;
×
1469
  }
1470

1471
  STscObj* pTscObj = pRequest->pTscObj;
5✔
1472

1473
  SVDropTbReq* pDropReq = NULL;
5✔
1474
  SCatalog*    pCatalog = NULL;
5✔
1475
  RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
5!
1476

1477
  pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
5✔
1478
  RAW_NULL_CHECK(pVgroupHashmap);
5!
1479
  taosHashSetFreeFp(pVgroupHashmap, destroyDropTbReqBatch);
5✔
1480

1481
  SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
5✔
1482
                           .requestId = pRequest->requestId,
5✔
1483
                           .requestObjRefId = pRequest->self,
5✔
1484
                           .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
5✔
1485
  pRequest->tableList = taosArrayInit(req.nReqs, sizeof(SName));
5✔
1486
  RAW_NULL_CHECK(pRequest->tableList);
5!
1487
  // loop to create table
1488
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
11✔
1489
    pDropReq = req.pReqs + iReq;
6✔
1490
    pDropReq->igNotExists = true;
6✔
1491
    //    pDropReq->suid = processSuid(pDropReq->suid, pRequest->pDb);
1492

1493
    SVgroupInfo pInfo = {0};
6✔
1494
    SName       pName = {0};
6✔
1495
    toName(pTscObj->acctId, pRequest->pDb, pDropReq->name, &pName);
6✔
1496
    RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &pInfo));
6!
1497

1498
    STableMeta* pTableMeta = NULL;
6✔
1499
    code = catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta);
6✔
1500
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
6✔
1501
      code = TSDB_CODE_SUCCESS;
2✔
1502
      taosMemoryFreeClear(pTableMeta);
2!
1503
      continue;
2✔
1504
    }
1505
    if (code != TSDB_CODE_SUCCESS) {
4!
UNCOV
1506
      goto end;
×
1507
    }
1508
    tb_uid_t oldSuid = pDropReq->suid;
4✔
1509
    pDropReq->suid = pTableMeta->suid;
4✔
1510
    taosMemoryFreeClear(pTableMeta);
4!
1511
    uDebug(LOG_ID_TAG " drop table name:%s suid:%" PRId64 " new suid:%" PRId64, LOG_ID_VALUE, pDropReq->name, oldSuid,
4!
1512
           pDropReq->suid);
1513

1514
    RAW_NULL_CHECK(taosArrayPush(pRequest->tableList, &pName));
8!
1515
    SVgroupDropTableBatch* pTableBatch = taosHashGet(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId));
4✔
1516
    if (pTableBatch == NULL) {
4✔
1517
      SVgroupDropTableBatch tBatch = {0};
3✔
1518
      tBatch.info = pInfo;
3✔
1519
      tBatch.req.pArray = taosArrayInit(TARRAY_MIN_SIZE, sizeof(SVDropTbReq));
3✔
1520
      RAW_NULL_CHECK(tBatch.req.pArray);
3!
1521
      RAW_NULL_CHECK(taosArrayPush(tBatch.req.pArray, pDropReq));
6!
1522
      RAW_RETURN_CHECK(taosHashPut(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId), &tBatch, sizeof(tBatch)));
3!
1523
    } else {  // add to the correct vgroup
1524
      RAW_NULL_CHECK(taosArrayPush(pTableBatch->req.pArray, pDropReq));
2!
1525
    }
1526
  }
1527

1528
  if (taosHashGetSize(pVgroupHashmap) == 0) {
5✔
1529
    goto end;
2✔
1530
  }
1531
  SArray* pBufArray = NULL;
3✔
1532
  RAW_RETURN_CHECK(serializeVgroupsDropTableBatch(pVgroupHashmap, &pBufArray));
3!
1533
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode**)&pQuery);
3✔
1534
  if (TSDB_CODE_SUCCESS != code) goto end;
3!
1535
  pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
3✔
1536
  pQuery->msgType = TDMT_VND_DROP_TABLE;
3✔
1537
  pQuery->stableQuery = false;
3✔
1538
  pQuery->pRoot = NULL;
3✔
1539
  code = nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT, &pQuery->pRoot);
3✔
1540
  if (TSDB_CODE_SUCCESS != code) goto end;
3!
1541
  RAW_RETURN_CHECK(rewriteToVnodeModifyOpStmt(pQuery, pBufArray));
3!
1542

1543
  launchQueryImpl(pRequest, pQuery, true, NULL);
3✔
1544
  if (pRequest->code == TSDB_CODE_SUCCESS) {
3!
1545
    RAW_RETURN_CHECK(removeMeta(pTscObj, pRequest->tableList, false));
3!
1546
  }
1547
  code = pRequest->code;
3✔
1548

1549
end:
5✔
1550
  uDebug(LOG_ID_TAG " drop table return, msg:%s", LOG_ID_VALUE, tstrerror(code));
5!
1551
  taosHashCleanup(pVgroupHashmap);
5✔
1552
  destroyRequest(pRequest);
5✔
1553
  tDecoderClear(&coder);
5✔
1554
  qDestroyQuery(pQuery);
5✔
1555
  return code;
5✔
1556
}
1557

1558
static int32_t taosDeleteData(TAOS* taos, void* meta, uint32_t metaLen) {
3✔
1559
  if (taos == NULL || meta == NULL) {
3!
UNCOV
1560
    uError("invalid parameter in %s", __func__);
×
UNCOV
1561
    return TSDB_CODE_INVALID_PARA;
×
1562
  }
1563
  SDeleteRes req = {0};
3✔
1564
  SDecoder   coder = {0};
3✔
1565
  char       sql[256] = {0};
3✔
1566
  int32_t    code = TSDB_CODE_SUCCESS;
3✔
1567

1568
  uDebug("connId:0x%" PRIx64 " delete data, meta:%p, len:%d", *(int64_t*)taos, meta, metaLen);
3!
1569

1570
  // decode and process req
1571
  void*    data = POINTER_SHIFT(meta, sizeof(SMsgHead));
3✔
1572
  uint32_t len = metaLen - sizeof(SMsgHead);
3✔
1573
  tDecoderInit(&coder, data, len);
3✔
1574
  if (tDecodeDeleteRes(&coder, &req) < 0) {
3!
UNCOV
1575
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1576
    goto end;
×
1577
  }
1578

1579
  (void)snprintf(sql, sizeof(sql), "delete from `%s` where `%s` >= %" PRId64 " and `%s` <= %" PRId64, req.tableFName,
3✔
1580
                 req.tsColName, req.skey, req.tsColName, req.ekey);
1581

1582
  TAOS_RES* res = taosQueryImpl(taos, sql, false, TD_REQ_FROM_TAOX);
3✔
1583
  RAW_NULL_CHECK(res);
3!
1584
  SRequestObj* pRequest = (SRequestObj*)res;
3✔
1585
  code = pRequest->code;
3✔
1586
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_PAR_GET_META_ERROR) {
3!
1587
    code = TSDB_CODE_SUCCESS;
1✔
1588
  }
1589
  taos_free_result(res);
3✔
1590

1591
end:
3✔
1592
  uDebug("connId:0x%" PRIx64 " delete data sql:%s, code:%s", *(int64_t*)taos, sql, tstrerror(code));
3!
1593
  tDecoderClear(&coder);
3✔
1594
  return code;
3✔
1595
}
1596

1597
static int32_t taosAlterTable(TAOS* taos, void* meta, uint32_t metaLen) {
24✔
1598
  if (taos == NULL || meta == NULL) {
24!
UNCOV
1599
    uError("invalid parameter in %s", __func__);
×
UNCOV
1600
    return TSDB_CODE_INVALID_PARA;
×
1601
  }
1602
  SVAlterTbReq   req = {0};
24✔
1603
  SDecoder       dcoder = {0};
24✔
1604
  int32_t        code = TSDB_CODE_SUCCESS;
24✔
1605
  SRequestObj*   pRequest = NULL;
24✔
1606
  SQuery*        pQuery = NULL;
24✔
1607
  SArray*        pArray = NULL;
24✔
1608
  SVgDataBlocks* pVgData = NULL;
24✔
1609

1610
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
24!
1611
  uDebug(LOG_ID_TAG " alter table, meta:%p, len:%d", LOG_ID_VALUE, meta, metaLen);
24!
1612
  pRequest->syncQuery = true;
24✔
1613
  if (!pRequest->pDb) {
24!
UNCOV
1614
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
UNCOV
1615
    goto end;
×
1616
  }
1617
  // decode and process req
1618
  void*    data = POINTER_SHIFT(meta, sizeof(SMsgHead));
24✔
1619
  uint32_t len = metaLen - sizeof(SMsgHead);
24✔
1620
  tDecoderInit(&dcoder, data, len);
24✔
1621
  if (tDecodeSVAlterTbReq(&dcoder, &req) < 0) {
24!
UNCOV
1622
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1623
    goto end;
×
1624
  }
1625

1626
  // do not deal TSDB_ALTER_TABLE_UPDATE_OPTIONS
1627
  if (req.action == TSDB_ALTER_TABLE_UPDATE_OPTIONS) {
24✔
1628
    goto end;
4✔
1629
  }
1630

1631
  STscObj*  pTscObj = pRequest->pTscObj;
20✔
1632
  SCatalog* pCatalog = NULL;
20✔
1633
  RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
20!
1634
  SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
20✔
1635
                           .requestId = pRequest->requestId,
20✔
1636
                           .requestObjRefId = pRequest->self,
20✔
1637
                           .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
20✔
1638

1639
  SVgroupInfo pInfo = {0};
20✔
1640
  SName       pName = {0};
20✔
1641
  toName(pTscObj->acctId, pRequest->pDb, req.tbName, &pName);
20✔
1642
  RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &pInfo));
20!
1643
  pArray = taosArrayInit(1, sizeof(void*));
20✔
1644
  RAW_NULL_CHECK(pArray);
20!
1645

1646
  pVgData = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
20!
1647
  RAW_NULL_CHECK(pVgData);
20!
1648
  pVgData->vg = pInfo;
20✔
1649

1650
  int tlen = 0;
20✔
1651
  req.source = TD_REQ_FROM_TAOX;
20✔
1652
  tEncodeSize(tEncodeSVAlterTbReq, &req, tlen, code);
20!
1653
  if (code != 0) {
20!
UNCOV
1654
    code = terrno;
×
UNCOV
1655
    goto end;
×
1656
  }
1657
  tlen += sizeof(SMsgHead);
20✔
1658
  void* pMsg = taosMemoryMalloc(tlen);
20!
1659
  RAW_NULL_CHECK(pMsg);
20!
1660
  ((SMsgHead*)pMsg)->vgId = htonl(pInfo.vgId);
20✔
1661
  ((SMsgHead*)pMsg)->contLen = htonl(tlen);
20✔
1662
  void*    pBuf = POINTER_SHIFT(pMsg, sizeof(SMsgHead));
20✔
1663
  SEncoder coder = {0};
20✔
1664
  tEncoderInit(&coder, pBuf, tlen - sizeof(SMsgHead));
20✔
1665
  code = tEncodeSVAlterTbReq(&coder, &req);
20✔
1666
  if (code != 0) {
20!
UNCOV
1667
    tEncoderClear(&coder);
×
UNCOV
1668
    code = terrno;
×
UNCOV
1669
    goto end;
×
1670
  }
1671
  tEncoderClear(&coder);
20✔
1672

1673
  pVgData->pData = pMsg;
20✔
1674
  pVgData->size = tlen;
20✔
1675

1676
  pVgData->numOfTables = 1;
20✔
1677
  RAW_NULL_CHECK(taosArrayPush(pArray, &pVgData));
20!
1678

1679
  pQuery = NULL;
20✔
1680
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode**)&pQuery);
20✔
1681
  if (NULL == pQuery) goto end;
20!
1682
  pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
20✔
1683
  pQuery->msgType = TDMT_VND_ALTER_TABLE;
20✔
1684
  pQuery->stableQuery = false;
20✔
1685
  pQuery->pRoot = NULL;
20✔
1686
  code = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, &pQuery->pRoot);
20✔
1687
  if (TSDB_CODE_SUCCESS != code) goto end;
20!
1688
  RAW_RETURN_CHECK(rewriteToVnodeModifyOpStmt(pQuery, pArray));
20!
1689

1690
  launchQueryImpl(pRequest, pQuery, true, NULL);
20✔
1691

1692
  pVgData = NULL;
20✔
1693
  pArray = NULL;
20✔
1694
  code = pRequest->code;
20✔
1695
  if (code == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
20✔
1696
    code = TSDB_CODE_SUCCESS;
1✔
1697
  }
1698

1699
  if (pRequest->code == TSDB_CODE_SUCCESS) {
20✔
1700
    SExecResult* pRes = &pRequest->body.resInfo.execRes;
19✔
1701
    if (pRes->res != NULL) {
19✔
1702
      code = handleAlterTbExecRes(pRes->res, pCatalog);
16✔
1703
    }
1704
  }
1705
end:
4✔
1706
  uDebug(LOG_ID_TAG " alter table return, meta:%p, len:%d, msg:%s", LOG_ID_VALUE, meta, metaLen, tstrerror(code));
24!
1707
  taosArrayDestroy(pArray);
24✔
1708
  if (pVgData) taosMemoryFreeClear(pVgData->pData);
24!
1709
  taosMemoryFreeClear(pVgData);
24!
1710
  destroyRequest(pRequest);
24✔
1711
  tDecoderClear(&dcoder);
24✔
1712
  qDestroyQuery(pQuery);
24✔
1713
  return code;
24✔
1714
}
1715

1716
int taos_write_raw_block_with_fields(TAOS* taos, int rows, char* pData, const char* tbname, TAOS_FIELD* fields,
1✔
1717
                                     int numFields) {
1718
  return taos_write_raw_block_with_fields_with_reqid(taos, rows, pData, tbname, fields, numFields, 0);
1✔
1719
}
1720

1721
int taos_write_raw_block_with_fields_with_reqid(TAOS* taos, int rows, char* pData, const char* tbname,
1✔
1722
                                                TAOS_FIELD* fields, int numFields, int64_t reqid) {
1723
  if (taos == NULL || pData == NULL || tbname == NULL) {
1!
UNCOV
1724
    uError("invalid parameter in %s", __func__);
×
UNCOV
1725
    return TSDB_CODE_INVALID_PARA;
×
1726
  }
1727
  int32_t     code = TSDB_CODE_SUCCESS;
1✔
1728
  STableMeta* pTableMeta = NULL;
1✔
1729
  SQuery*     pQuery = NULL;
1✔
1730
  SHashObj*   pVgHash = NULL;
1✔
1731

1732
  SRequestObj* pRequest = NULL;
1✔
1733
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, reqid));
1!
1734

1735
  uDebug(LOG_ID_TAG " write raw block with field, rows:%d, pData:%p, tbname:%s, fields:%p, numFields:%d", LOG_ID_VALUE,
1!
1736
         rows, pData, tbname, fields, numFields);
1737

1738
  pRequest->syncQuery = true;
1✔
1739
  if (!pRequest->pDb) {
1!
UNCOV
1740
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
UNCOV
1741
    goto end;
×
1742
  }
1743

1744
  SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}};
1✔
1745
  tstrncpy(pName.dbname, pRequest->pDb, sizeof(pName.dbname));
1✔
1746
  tstrncpy(pName.tname, tbname, sizeof(pName.tname));
1✔
1747

1748
  struct SCatalog* pCatalog = NULL;
1✔
1749
  RAW_RETURN_CHECK(catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog));
1!
1750

1751
  SRequestConnInfo conn = {0};
1✔
1752
  conn.pTrans = pRequest->pTscObj->pAppInfo->pTransporter;
1✔
1753
  conn.requestId = pRequest->requestId;
1✔
1754
  conn.requestObjRefId = pRequest->self;
1✔
1755
  conn.mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp);
1✔
1756

1757
  SVgroupInfo vgData = {0};
1✔
1758
  RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &vgData));
1!
1759
  RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta));
1!
1760
  RAW_RETURN_CHECK(smlInitHandle(&pQuery));
1!
1761
  pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
1✔
1762
  RAW_NULL_CHECK(pVgHash);
1!
1763
  RAW_RETURN_CHECK(
1!
1764
      taosHashPut(pVgHash, (const char*)&vgData.vgId, sizeof(vgData.vgId), (char*)&vgData, sizeof(vgData)));
1765
  RAW_RETURN_CHECK(rawBlockBindData(pQuery, pTableMeta, pData, NULL, fields, numFields, false, NULL, 0, false));
1!
1766
  RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash));
1!
1767

1768
  launchQueryImpl(pRequest, pQuery, true, NULL);
1✔
1769
  code = pRequest->code;
1✔
1770

1771
end:
1✔
1772
  uDebug(LOG_ID_TAG " write raw block with field return, msg:%s", LOG_ID_VALUE, tstrerror(code));
1!
1773
  taosMemoryFreeClear(pTableMeta);
1!
1774
  qDestroyQuery(pQuery);
1✔
1775
  destroyRequest(pRequest);
1✔
1776
  taosHashCleanup(pVgHash);
1✔
1777
  return code;
1✔
1778
}
1779

1780
int taos_write_raw_block(TAOS* taos, int rows, char* pData, const char* tbname) {
7✔
1781
  return taos_write_raw_block_with_reqid(taos, rows, pData, tbname, 0);
7✔
1782
}
1783

1784
int taos_write_raw_block_with_reqid(TAOS* taos, int rows, char* pData, const char* tbname, int64_t reqid) {
7✔
1785
  if (taos == NULL || pData == NULL || tbname == NULL) {
7!
UNCOV
1786
    return TSDB_CODE_INVALID_PARA;
×
1787
  }
1788
  int32_t     code = TSDB_CODE_SUCCESS;
7✔
1789
  STableMeta* pTableMeta = NULL;
7✔
1790
  SQuery*     pQuery = NULL;
7✔
1791
  SHashObj*   pVgHash = NULL;
7✔
1792

1793
  SRequestObj* pRequest = NULL;
7✔
1794
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, reqid));
7!
1795

1796
  uDebug(LOG_ID_TAG " write raw block, rows:%d, pData:%p, tbname:%s", LOG_ID_VALUE, rows, pData, tbname);
7!
1797

1798
  pRequest->syncQuery = true;
7✔
1799
  if (!pRequest->pDb) {
7!
UNCOV
1800
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
UNCOV
1801
    goto end;
×
1802
  }
1803

1804
  SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}};
7✔
1805
  tstrncpy(pName.dbname, pRequest->pDb, sizeof(pName.dbname));
7✔
1806
  tstrncpy(pName.tname, tbname, sizeof(pName.tname));
7✔
1807

1808
  struct SCatalog* pCatalog = NULL;
7✔
1809
  RAW_RETURN_CHECK(catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog));
7!
1810

1811
  SRequestConnInfo conn = {0};
7✔
1812
  conn.pTrans = pRequest->pTscObj->pAppInfo->pTransporter;
7✔
1813
  conn.requestId = pRequest->requestId;
7✔
1814
  conn.requestObjRefId = pRequest->self;
7✔
1815
  conn.mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp);
7✔
1816

1817
  SVgroupInfo vgData = {0};
7✔
1818
  RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &vgData));
7!
1819
  RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta));
7✔
1820
  RAW_RETURN_CHECK(smlInitHandle(&pQuery));
6!
1821
  pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
6✔
1822
  RAW_NULL_CHECK(pVgHash);
6!
1823
  RAW_RETURN_CHECK(
6!
1824
      taosHashPut(pVgHash, (const char*)&vgData.vgId, sizeof(vgData.vgId), (char*)&vgData, sizeof(vgData)));
1825
  RAW_RETURN_CHECK(rawBlockBindData(pQuery, pTableMeta, pData, NULL, NULL, 0, false, NULL, 0, false));
6✔
1826
  RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash));
4!
1827

1828
  launchQueryImpl(pRequest, pQuery, true, NULL);
4✔
1829
  code = pRequest->code;
4✔
1830

1831
end:
7✔
1832
  uDebug(LOG_ID_TAG " write raw block return, msg:%s", LOG_ID_VALUE, tstrerror(code));
7!
1833
  taosMemoryFreeClear(pTableMeta);
7!
1834
  qDestroyQuery(pQuery);
7✔
1835
  destroyRequest(pRequest);
7✔
1836
  taosHashCleanup(pVgHash);
7✔
1837
  return code;
7✔
1838
}
1839

1840
static void* getRawDataFromRes(void* pRetrieve) {
128✔
1841
  if (pRetrieve == NULL) {
128!
UNCOV
1842
    uError("invalid parameter in %s", __func__);
×
UNCOV
1843
    return NULL;
×
1844
  }
1845
  void* rawData = NULL;
128✔
1846
  // deal with compatibility
1847
  if (*(int64_t*)pRetrieve == 0) {
128!
UNCOV
1848
    rawData = ((SRetrieveTableRsp*)pRetrieve)->data;
×
1849
  } else if (*(int64_t*)pRetrieve == 1) {
128!
1850
    rawData = ((SRetrieveTableRspForTmq*)pRetrieve)->data;
128✔
1851
  }
1852
  return rawData;
128✔
1853
}
1854

1855
static int32_t buildCreateTbMap(SMqDataRsp* rsp, SHashObj* pHashObj) {
10✔
1856
  if (rsp == NULL || pHashObj == NULL) {
10!
UNCOV
1857
    uError("invalid parameter in %s", __func__);
×
UNCOV
1858
    return TSDB_CODE_INVALID_PARA;
×
1859
  }
1860
  // find schema data info
1861
  int32_t       code = 0;
10✔
1862
  SVCreateTbReq pCreateReq = {0};
10✔
1863
  SDecoder      decoderTmp = {0};
10✔
1864

1865
  for (int j = 0; j < rsp->createTableNum; j++) {
34✔
1866
    void** dataTmp = taosArrayGet(rsp->createTableReq, j);
24✔
1867
    RAW_NULL_CHECK(dataTmp);
24!
1868
    int32_t* lenTmp = taosArrayGet(rsp->createTableLen, j);
24✔
1869
    RAW_NULL_CHECK(lenTmp);
24!
1870

1871
    tDecoderInit(&decoderTmp, *dataTmp, *lenTmp);
24✔
1872
    RAW_RETURN_CHECK(tDecodeSVCreateTbReq(&decoderTmp, &pCreateReq));
24!
1873

1874
    if (pCreateReq.type != TSDB_CHILD_TABLE) {
24!
UNCOV
1875
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1876
      goto end;
×
1877
    }
1878
    if (taosHashGet(pHashObj, pCreateReq.name, strlen(pCreateReq.name)) == NULL) {
24✔
1879
      RAW_RETURN_CHECK(
22!
1880
          taosHashPut(pHashObj, pCreateReq.name, strlen(pCreateReq.name), &pCreateReq, sizeof(SVCreateTbReq)));
1881
    } else {
1882
      tDestroySVCreateTbReq(&pCreateReq, TSDB_MSG_FLG_DECODE);
2✔
1883
      pCreateReq = (SVCreateTbReq){0};
2✔
1884
    }
1885

1886
    tDecoderClear(&decoderTmp);
24✔
1887
  }
1888
  return 0;
10✔
1889

UNCOV
1890
end:
×
UNCOV
1891
  tDecoderClear(&decoderTmp);
×
UNCOV
1892
  tDestroySVCreateTbReq(&pCreateReq, TSDB_MSG_FLG_DECODE);
×
UNCOV
1893
  return code;
×
1894
}
1895

1896
typedef enum {
1897
  WRITE_RAW_INIT_START = 0,
1898
  WRITE_RAW_INIT_OK,
1899
  WRITE_RAW_INIT_FAIL,
1900
} WRITE_RAW_INIT_STATUS;
1901

1902
static SHashObj* writeRawCache = NULL;
1903
static int8_t    initFlag = 0;
1904
static int8_t    initedFlag = WRITE_RAW_INIT_START;
1905

1906
typedef struct {
1907
  SHashObj* pVgHash;
1908
  SHashObj* pNameHash;
1909
  SHashObj* pMetaHash;
1910
} rawCacheInfo;
1911

1912
typedef struct {
1913
  SVgroupInfo vgInfo;
1914
  int64_t     uid;
1915
  int64_t     suid;
1916
} tbInfo;
1917

1918
static void tmqFreeMeta(void* data) {
42✔
1919
  if (data == NULL) {
42!
UNCOV
1920
    uError("invalid parameter in %s", __func__);
×
UNCOV
1921
    return;
×
1922
  }
1923
  STableMeta* pTableMeta = *(STableMeta**)data;
42✔
1924
  taosMemoryFree(pTableMeta);
42!
1925
}
1926

UNCOV
1927
static void freeRawCache(void* data) {
×
UNCOV
1928
  if (data == NULL) {
×
UNCOV
1929
    uError("invalid parameter in %s", __func__);
×
UNCOV
1930
    return;
×
1931
  }
UNCOV
1932
  rawCacheInfo* pRawCache = (rawCacheInfo*)data;
×
UNCOV
1933
  taosHashCleanup(pRawCache->pMetaHash);
×
UNCOV
1934
  taosHashCleanup(pRawCache->pNameHash);
×
UNCOV
1935
  taosHashCleanup(pRawCache->pVgHash);
×
1936
}
1937

1938
static int32_t initRawCacheHash() {
15✔
1939
  if (writeRawCache == NULL) {
15!
1940
    writeRawCache = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
15✔
1941
    if (writeRawCache == NULL) {
15!
UNCOV
1942
      return terrno;
×
1943
    }
1944
    taosHashSetFreeFp(writeRawCache, freeRawCache);
15✔
1945
  }
1946
  return 0;
15✔
1947
}
1948

1949
static bool needRefreshMeta(void* rawData, STableMeta* pTableMeta, SSchemaWrapper* pSW) {
7✔
1950
  if (rawData == NULL || pTableMeta == NULL || pSW == NULL) {
7!
UNCOV
1951
    uError("invalid parameter in %s", __func__);
×
UNCOV
1952
    return false;
×
1953
  }
1954
  char* p = (char*)rawData;
7✔
1955
  // | version | total length | total rows | blankFill | total columns | flag seg| block group id | column schema | each
1956
  // column length |
1957
  p += sizeof(int32_t);
7✔
1958
  p += sizeof(int32_t);
7✔
1959
  p += sizeof(int32_t);
7✔
1960
  p += sizeof(int32_t);
7✔
1961
  p += sizeof(int32_t);
7✔
1962
  p += sizeof(uint64_t);
7✔
1963
  int8_t* fields = p;
7✔
1964

1965
  if (pSW->nCols != pTableMeta->tableInfo.numOfColumns) {
7✔
1966
    return true;
4✔
1967
  }
1968
  for (int i = 0; i < pSW->nCols; i++) {
17✔
1969
    int j = 0;
14✔
1970
    for (; j < pTableMeta->tableInfo.numOfColumns; j++) {
40!
1971
      SSchema* pColSchema = &pTableMeta->schema[j];
40✔
1972
      char*    fieldName = pSW->pSchema[i].name;
40✔
1973

1974
      if (strcmp(pColSchema->name, fieldName) == 0) {
40✔
1975
        if (*fields != pColSchema->type || *(int32_t*)(fields + sizeof(int8_t)) != pColSchema->bytes) {
14!
UNCOV
1976
          return true;
×
1977
        }
1978
        break;
14✔
1979
      }
1980
    }
1981
    fields += sizeof(int8_t) + sizeof(int32_t);
14✔
1982

1983
    if (j == pTableMeta->tableInfo.numOfColumns) return true;
14!
1984
  }
1985
  return false;
3✔
1986
}
1987

1988
static int32_t getRawCache(SHashObj** pVgHash, SHashObj** pNameHash, SHashObj** pMetaHash, void* key) {
47✔
1989
  if (pVgHash == NULL || pNameHash == NULL || pMetaHash == NULL || key == NULL) {
47!
UNCOV
1990
    uError("invalid parameter in %s", __func__);
×
UNCOV
1991
    return TSDB_CODE_INVALID_PARA;
×
1992
  }
1993
  int32_t code = 0;
47✔
1994
  void*   cacheInfo = taosHashGet(writeRawCache, &key, POINTER_BYTES);
47✔
1995
  if (cacheInfo == NULL) {
47!
1996
    *pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
47✔
1997
    RAW_NULL_CHECK(*pVgHash);
47!
1998
    *pNameHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
47✔
1999
    RAW_NULL_CHECK(*pNameHash);
47!
2000
    *pMetaHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
47✔
2001
    RAW_NULL_CHECK(*pMetaHash);
47!
2002
    taosHashSetFreeFp(*pMetaHash, tmqFreeMeta);
47✔
2003
    rawCacheInfo info = {*pVgHash, *pNameHash, *pMetaHash};
47✔
2004
    RAW_RETURN_CHECK(taosHashPut(writeRawCache, &key, POINTER_BYTES, &info, sizeof(rawCacheInfo)));
47!
2005
  } else {
UNCOV
2006
    rawCacheInfo* info = (rawCacheInfo*)cacheInfo;
×
UNCOV
2007
    *pVgHash = info->pVgHash;
×
UNCOV
2008
    *pNameHash = info->pNameHash;
×
UNCOV
2009
    *pMetaHash = info->pMetaHash;
×
2010
  }
2011

2012
  return 0;
47✔
UNCOV
2013
end:
×
UNCOV
2014
  taosHashCleanup(*pMetaHash);
×
UNCOV
2015
  taosHashCleanup(*pNameHash);
×
UNCOV
2016
  taosHashCleanup(*pVgHash);
×
UNCOV
2017
  return code;
×
2018
}
2019

2020
static int32_t buildRawRequest(TAOS* taos, SRequestObj** pRequest, SCatalog** pCatalog, SRequestConnInfo* conn) {
47✔
2021
  if (taos == NULL || pRequest == NULL || pCatalog == NULL || conn == NULL) {
47!
UNCOV
2022
    uError("invalid parameter in %s", __func__);
×
UNCOV
2023
    return TSDB_CODE_INVALID_PARA;
×
2024
  }
2025
  int32_t code = 0;
47✔
2026
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, pRequest, 0));
47!
2027
  (*pRequest)->syncQuery = true;
47✔
2028
  if (!(*pRequest)->pDb) {
47!
UNCOV
2029
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
UNCOV
2030
    goto end;
×
2031
  }
2032

2033
  RAW_RETURN_CHECK(catalogGetHandle((*pRequest)->pTscObj->pAppInfo->clusterId, pCatalog));
47!
2034
  conn->pTrans = (*pRequest)->pTscObj->pAppInfo->pTransporter;
47✔
2035
  conn->requestId = (*pRequest)->requestId;
47✔
2036
  conn->requestObjRefId = (*pRequest)->self;
47✔
2037
  conn->mgmtEps = getEpSet_s(&(*pRequest)->pTscObj->pAppInfo->mgmtEp);
47✔
2038

2039
end:
47✔
2040
  return code;
47✔
2041
}
2042

2043
typedef int32_t _raw_decode_func_(SDecoder* pDecoder, SMqDataRsp* pRsp);
2044
static int32_t  decodeRawData(SDecoder* decoder, void* data, uint32_t dataLen, _raw_decode_func_ func,
47✔
2045
                              SMqRspObj* rspObj) {
2046
  if (decoder == NULL || data == NULL || func == NULL || rspObj == NULL) {
47!
UNCOV
2047
    uError("invalid parameter in %s", __func__);
×
UNCOV
2048
    return TSDB_CODE_INVALID_PARA;
×
2049
  }
2050
  int8_t dataVersion = *(int8_t*)data;
47✔
2051
  if (dataVersion >= MQ_DATA_RSP_VERSION) {
47!
2052
    data = POINTER_SHIFT(data, sizeof(int8_t) + sizeof(int32_t));
47✔
2053
    if (dataLen < sizeof(int8_t) + sizeof(int32_t)) {
47!
UNCOV
2054
      return TSDB_CODE_INVALID_PARA;
×
2055
    }
2056
    dataLen -= sizeof(int8_t) + sizeof(int32_t);
47✔
2057
  }
2058

2059
  rspObj->resIter = -1;
47✔
2060
  tDecoderInit(decoder, data, dataLen);
47✔
2061
  int32_t code = func(decoder, &rspObj->dataRsp);
47✔
2062
  if (code != 0) {
47!
UNCOV
2063
    SET_ERROR_MSG("decode mq taosx data rsp failed");
×
2064
  }
2065
  return code;
47✔
2066
}
2067

2068
static int32_t processCacheMeta(SHashObj* pVgHash, SHashObj* pNameHash, SHashObj* pMetaHash,
128✔
2069
                                SVCreateTbReq* pCreateReqDst, SCatalog* pCatalog, SRequestConnInfo* conn, SName* pName,
2070
                                STableMeta** pMeta, SSchemaWrapper* pSW, void* rawData, int32_t retry) {
2071
  if (pVgHash == NULL || pNameHash == NULL || pMetaHash == NULL || pCatalog == NULL || conn == NULL || pName == NULL ||
128!
2072
      pMeta == NULL || pSW == NULL || rawData == NULL) {
128!
UNCOV
2073
    uError("invalid parameter in %s", __func__);
×
UNCOV
2074
    return TSDB_CODE_INVALID_PARA;
×
2075
  }
2076
  int32_t     code = 0;
128✔
2077
  STableMeta* pTableMeta = NULL;
128✔
2078
  tbInfo*     tmpInfo = (tbInfo*)taosHashGet(pNameHash, pName->tname, strlen(pName->tname));
128✔
2079
  if (tmpInfo == NULL || retry > 0) {
128!
2080
    tbInfo info = {0};
121✔
2081

2082
    RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, conn, pName, &info.vgInfo));
121!
2083
    if (pCreateReqDst && tmpInfo == NULL) {  // change stable name to get meta
121!
2084
      tstrncpy(pName->tname, pCreateReqDst->ctb.stbName, TSDB_TABLE_NAME_LEN);
24✔
2085
    }
2086
    RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, conn, pName, &pTableMeta));
121!
2087
    info.uid = pTableMeta->uid;
121✔
2088
    if (pTableMeta->tableType == TSDB_CHILD_TABLE) {
121✔
2089
      info.suid = pTableMeta->suid;
82✔
2090
    } else {
2091
      info.suid = pTableMeta->uid;
39✔
2092
    }
2093
    code = taosHashPut(pMetaHash, &info.suid, LONG_BYTES, &pTableMeta, POINTER_BYTES);
121✔
2094
    if (code != 0) {
121!
UNCOV
2095
      taosMemoryFree(pTableMeta);
×
UNCOV
2096
      goto end;
×
2097
    }
2098
    if (pCreateReqDst) {
121✔
2099
      pTableMeta->vgId = info.vgInfo.vgId;
24✔
2100
      pTableMeta->uid = pCreateReqDst->uid;
24✔
2101
      pCreateReqDst->ctb.suid = pTableMeta->suid;
24✔
2102
    }
2103

2104
    RAW_RETURN_CHECK(taosHashPut(pNameHash, pName->tname, strlen(pName->tname), &info, sizeof(tbInfo)));
121!
2105
    tmpInfo = (tbInfo*)taosHashGet(pNameHash, pName->tname, strlen(pName->tname));
121✔
2106
    RAW_RETURN_CHECK(
121!
2107
        taosHashPut(pVgHash, &info.vgInfo.vgId, sizeof(info.vgInfo.vgId), &info.vgInfo, sizeof(SVgroupInfo)));
2108
  }
2109

2110
  if (pTableMeta == NULL || retry > 0) {
128!
2111
    STableMeta** pTableMetaTmp = (STableMeta**)taosHashGet(pMetaHash, &tmpInfo->suid, LONG_BYTES);
7✔
2112
    if (pTableMetaTmp == NULL || retry > 0 || needRefreshMeta(rawData, *pTableMetaTmp, pSW)) {
7!
2113
      RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, conn, pName, &pTableMeta));
4!
2114
      code = taosHashPut(pMetaHash, &tmpInfo->suid, LONG_BYTES, &pTableMeta, POINTER_BYTES);
4✔
2115
      if (code != 0) {
4!
UNCOV
2116
        taosMemoryFree(pTableMeta);
×
UNCOV
2117
        goto end;
×
2118
      }
2119

2120
    } else {
2121
      pTableMeta = *pTableMetaTmp;
3✔
2122
      pTableMeta->uid = tmpInfo->uid;
3✔
2123
      pTableMeta->vgId = tmpInfo->vgInfo.vgId;
3✔
2124
    }
2125
  }
2126
  *pMeta = pTableMeta;
128✔
2127

2128
end:
128✔
2129
  return code;
128✔
2130
}
2131

2132
static int32_t tmqWriteRawDataImpl(TAOS* taos, void* data, uint32_t dataLen) {
37✔
2133
  if (taos == NULL || data == NULL) {
37!
UNCOV
2134
    uError("invalid parameter in %s", __func__);
×
UNCOV
2135
    return TSDB_CODE_INVALID_PARA;
×
2136
  }
2137
  int32_t   code = TSDB_CODE_SUCCESS;
37✔
2138
  SQuery*   pQuery = NULL;
37✔
2139
  SMqRspObj rspObj = {0};
37✔
2140
  SDecoder  decoder = {0};
37✔
2141

2142
  SRequestObj*     pRequest = NULL;
37✔
2143
  SCatalog*        pCatalog = NULL;
37✔
2144
  SRequestConnInfo conn = {0};
37✔
2145
  RAW_RETURN_CHECK(buildRawRequest(taos, &pRequest, &pCatalog, &conn));
37!
2146
  uDebug(LOG_ID_TAG " write raw data, data:%p, dataLen:%d", LOG_ID_VALUE, data, dataLen);
37!
2147
  RAW_RETURN_CHECK(decodeRawData(&decoder, data, dataLen, tDecodeMqDataRsp, &rspObj));
37!
2148

2149
  SHashObj* pVgHash = NULL;
37✔
2150
  SHashObj* pNameHash = NULL;
37✔
2151
  SHashObj* pMetaHash = NULL;
37✔
2152
  RAW_RETURN_CHECK(getRawCache(&pVgHash, &pNameHash, &pMetaHash, taos));
37!
2153
  int retry = 0;
37✔
2154
  while (1) {
2155
    RAW_RETURN_CHECK(smlInitHandle(&pQuery));
37!
2156
    uDebug(LOG_ID_TAG " write raw meta data block num:%d", LOG_ID_VALUE, rspObj.dataRsp.blockNum);
37!
2157
    while (++rspObj.resIter < rspObj.dataRsp.blockNum) {
138✔
2158
      if (!rspObj.dataRsp.withSchema) {
101!
UNCOV
2159
        goto end;
×
2160
      }
2161

2162
      const char* tbName = (const char*)taosArrayGetP(rspObj.dataRsp.blockTbName, rspObj.resIter);
101✔
2163
      RAW_NULL_CHECK(tbName);
101!
2164
      SSchemaWrapper* pSW = (SSchemaWrapper*)taosArrayGetP(rspObj.dataRsp.blockSchema, rspObj.resIter);
101✔
2165
      RAW_NULL_CHECK(pSW);
101!
2166
      void* pRetrieve = taosArrayGetP(rspObj.dataRsp.blockData, rspObj.resIter);
101✔
2167
      RAW_NULL_CHECK(pRetrieve);
101!
2168
      void* rawData = getRawDataFromRes(pRetrieve);
101✔
2169
      RAW_NULL_CHECK(rawData);
101!
2170

2171
      uDebug(LOG_ID_TAG " write raw data block tbname:%s", LOG_ID_VALUE, tbName);
101!
2172
      SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}};
101✔
2173
      tstrncpy(pName.dbname, pRequest->pDb, TSDB_DB_NAME_LEN);
101✔
2174
      tstrncpy(pName.tname, tbName, TSDB_TABLE_NAME_LEN);
101✔
2175

2176
      STableMeta* pTableMeta = NULL;
101✔
2177
      RAW_RETURN_CHECK(processCacheMeta(pVgHash, pNameHash, pMetaHash, NULL, pCatalog, &conn, &pName, &pTableMeta, pSW,
101!
2178
                                        rawData, retry));
2179
      char err[ERR_MSG_LEN] = {0};
101✔
2180
      code = rawBlockBindData(pQuery, pTableMeta, rawData, NULL, pSW, pSW->nCols, true, err, ERR_MSG_LEN, true);
101✔
2181
      if (code != TSDB_CODE_SUCCESS) {
101!
UNCOV
2182
        SET_ERROR_MSG("table:%s, err:%s", pName.tname, err);
×
UNCOV
2183
        goto end;
×
2184
      }
2185
    }
2186
    RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash));
37!
2187
    launchQueryImpl(pRequest, pQuery, true, NULL);
37✔
2188
    code = pRequest->code;
37✔
2189

2190
    if (NEED_CLIENT_HANDLE_ERROR(code) && retry++ < 3) {
37!
2191
      uInfo("write raw retry:%d/3 end code:%d, msg:%s", retry, code, tstrerror(code));
×
UNCOV
2192
      qDestroyQuery(pQuery);
×
UNCOV
2193
      pQuery = NULL;
×
UNCOV
2194
      rspObj.resIter = -1;
×
UNCOV
2195
      continue;
×
2196
    }
2197
    break;
37✔
2198
  }
2199

2200
end:
37✔
2201
  uDebug(LOG_ID_TAG " write raw data return, msg:%s", LOG_ID_VALUE, tstrerror(code));
37!
2202
  tDeleteMqDataRsp(&rspObj.dataRsp);
37✔
2203
  tDecoderClear(&decoder);
37✔
2204
  qDestroyQuery(pQuery);
37✔
2205
  destroyRequest(pRequest);
37✔
2206
  return code;
37✔
2207
}
2208

2209
static int32_t tmqWriteRawMetaDataImpl(TAOS* taos, void* data, uint32_t dataLen) {
10✔
2210
  if (taos == NULL || data == NULL) {
10!
2211
    uError("invalid parameter in %s", __func__);
×
UNCOV
2212
    return TSDB_CODE_INVALID_PARA;
×
2213
  }
2214
  int32_t   code = TSDB_CODE_SUCCESS;
10✔
2215
  SQuery*   pQuery = NULL;
10✔
2216
  SMqRspObj rspObj = {0};
10✔
2217
  SDecoder  decoder = {0};
10✔
2218
  SHashObj* pCreateTbHash = NULL;
10✔
2219

2220
  SRequestObj*     pRequest = NULL;
10✔
2221
  SCatalog*        pCatalog = NULL;
10✔
2222
  SRequestConnInfo conn = {0};
10✔
2223

2224
  RAW_RETURN_CHECK(buildRawRequest(taos, &pRequest, &pCatalog, &conn));
10!
2225
  uDebug(LOG_ID_TAG " write raw metadata, data:%p, dataLen:%d", LOG_ID_VALUE, data, dataLen);
10!
2226
  RAW_RETURN_CHECK(decodeRawData(&decoder, data, dataLen, tDecodeSTaosxRsp, &rspObj));
10!
2227

2228
  pCreateTbHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
10✔
2229
  RAW_NULL_CHECK(pCreateTbHash);
10!
2230
  RAW_RETURN_CHECK(buildCreateTbMap(&rspObj.dataRsp, pCreateTbHash));
10!
2231

2232
  SHashObj* pVgHash = NULL;
10✔
2233
  SHashObj* pNameHash = NULL;
10✔
2234
  SHashObj* pMetaHash = NULL;
10✔
2235
  RAW_RETURN_CHECK(getRawCache(&pVgHash, &pNameHash, &pMetaHash, taos));
10!
2236
  int retry = 0;
10✔
2237
  while (1) {
2238
    RAW_RETURN_CHECK(smlInitHandle(&pQuery));
10!
2239
    uDebug(LOG_ID_TAG " write raw meta data block num:%d", LOG_ID_VALUE, rspObj.dataRsp.blockNum);
10!
2240
    while (++rspObj.resIter < rspObj.dataRsp.blockNum) {
37✔
2241
      if (!rspObj.dataRsp.withSchema) {
27!
UNCOV
2242
        goto end;
×
2243
      }
2244

2245
      const char* tbName = (const char*)taosArrayGetP(rspObj.dataRsp.blockTbName, rspObj.resIter);
27✔
2246
      RAW_NULL_CHECK(tbName);
27!
2247
      SSchemaWrapper* pSW = (SSchemaWrapper*)taosArrayGetP(rspObj.dataRsp.blockSchema, rspObj.resIter);
27✔
2248
      RAW_NULL_CHECK(pSW);
27!
2249
      void* pRetrieve = taosArrayGetP(rspObj.dataRsp.blockData, rspObj.resIter);
27✔
2250
      RAW_NULL_CHECK(pRetrieve);
27!
2251
      void* rawData = getRawDataFromRes(pRetrieve);
27✔
2252
      RAW_NULL_CHECK(rawData);
27!
2253

2254
      uDebug(LOG_ID_TAG " write raw data block tbname:%s", LOG_ID_VALUE, tbName);
27!
2255
      SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}};
27✔
2256
      tstrncpy(pName.dbname, pRequest->pDb, TSDB_DB_NAME_LEN);
27✔
2257
      tstrncpy(pName.tname, tbName, TSDB_TABLE_NAME_LEN);
27✔
2258

2259
      // find schema data info
2260
      SVCreateTbReq* pCreateReqDst = (SVCreateTbReq*)taosHashGet(pCreateTbHash, pName.tname, strlen(pName.tname));
27✔
2261
      STableMeta*    pTableMeta = NULL;
27✔
2262
      RAW_RETURN_CHECK(processCacheMeta(pVgHash, pNameHash, pMetaHash, pCreateReqDst, pCatalog, &conn, &pName,
27!
2263
                                        &pTableMeta, pSW, rawData, retry));
2264
      char err[ERR_MSG_LEN] = {0};
27✔
2265
      code =
2266
          rawBlockBindData(pQuery, pTableMeta, rawData, pCreateReqDst, pSW, pSW->nCols, true, err, ERR_MSG_LEN, true);
27✔
2267
      if (code != TSDB_CODE_SUCCESS) {
27!
UNCOV
2268
        SET_ERROR_MSG("table:%s, err:%s", pName.tname, err);
×
UNCOV
2269
        goto end;
×
2270
      }
2271
    }
2272
    RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash));
10!
2273
    launchQueryImpl(pRequest, pQuery, true, NULL);
10✔
2274
    code = pRequest->code;
10✔
2275

2276
    if (NEED_CLIENT_HANDLE_ERROR(code) && retry++ < 3) {
10!
2277
      uInfo("write raw retry:%d/3 end code:%d, msg:%s", retry, code, tstrerror(code));
×
UNCOV
2278
      qDestroyQuery(pQuery);
×
UNCOV
2279
      pQuery = NULL;
×
UNCOV
2280
      rspObj.resIter = -1;
×
UNCOV
2281
      continue;
×
2282
    }
2283
    break;
10✔
2284
  }
2285

2286
end:
10✔
2287
  uDebug(LOG_ID_TAG " write raw metadata return, msg:%s", LOG_ID_VALUE, tstrerror(code));
10!
2288
  tDeleteSTaosxRsp(&rspObj.dataRsp);
10✔
2289
  void* pIter = taosHashIterate(pCreateTbHash, NULL);
10✔
2290
  while (pIter) {
32✔
2291
    tDestroySVCreateTbReq(pIter, TSDB_MSG_FLG_DECODE);
22✔
2292
    pIter = taosHashIterate(pCreateTbHash, pIter);
22✔
2293
  }
2294
  taosHashCleanup(pCreateTbHash);
10✔
2295
  tDecoderClear(&decoder);
10✔
2296
  qDestroyQuery(pQuery);
10✔
2297
  destroyRequest(pRequest);
10✔
2298
  return code;
10✔
2299
}
2300

2301
static void processSimpleMeta(SMqMetaRsp* pMetaRsp, cJSON** meta) {
289✔
2302
  if (pMetaRsp == NULL || meta == NULL) {
289!
UNCOV
2303
    uError("invalid parameter in %s", __func__);
×
UNCOV
2304
    return;
×
2305
  }
2306
  if (pMetaRsp->resMsgType == TDMT_VND_CREATE_STB) {
289✔
2307
    processCreateStb(pMetaRsp, meta);
78✔
2308
  } else if (pMetaRsp->resMsgType == TDMT_VND_ALTER_STB) {
211✔
2309
    processAlterStb(pMetaRsp, meta);
40✔
2310
  } else if (pMetaRsp->resMsgType == TDMT_VND_DROP_STB) {
171✔
2311
    processDropSTable(pMetaRsp, meta);
8✔
2312
  } else if (pMetaRsp->resMsgType == TDMT_VND_CREATE_TABLE) {
163✔
2313
    processCreateTable(pMetaRsp, meta);
131✔
2314
  } else if (pMetaRsp->resMsgType == TDMT_VND_ALTER_TABLE) {
32✔
2315
    processAlterTable(pMetaRsp, meta);
24✔
2316
  } else if (pMetaRsp->resMsgType == TDMT_VND_DROP_TABLE) {
8✔
2317
    processDropTable(pMetaRsp, meta);
5✔
2318
  } else if (pMetaRsp->resMsgType == TDMT_VND_DROP_TABLE) {
3!
UNCOV
2319
    processDropTable(pMetaRsp, meta);
×
2320
  } else if (pMetaRsp->resMsgType == TDMT_VND_DELETE) {
3!
2321
    processDeleteTable(pMetaRsp, meta);
3✔
2322
  }
2323
}
2324

2325
static void processBatchMetaToJson(SMqBatchMetaRsp* pMsgRsp, char** string) {
14✔
2326
  if (pMsgRsp == NULL || string == NULL) {
14!
2327
    uError("invalid parameter in %s", __func__);
×
2328
    return;
14✔
2329
  }
2330
  SDecoder        coder;
2331
  SMqBatchMetaRsp rsp = {0};
14✔
2332
  int32_t         code = 0;
14✔
2333
  cJSON*          pJson = NULL;
14✔
2334
  tDecoderInit(&coder, pMsgRsp->pMetaBuff, pMsgRsp->metaBuffLen);
14✔
2335
  if (tDecodeMqBatchMetaRsp(&coder, &rsp) < 0) {
14!
2336
    goto end;
×
2337
  }
2338

2339
  pJson = cJSON_CreateObject();
14✔
2340
  RAW_NULL_CHECK(pJson);
14!
2341
  RAW_FALSE_CHECK(cJSON_AddStringToObject(pJson, "tmq_meta_version", TMQ_META_VERSION));
14!
2342
  cJSON* pMetaArr = cJSON_CreateArray();
14✔
2343
  RAW_NULL_CHECK(pMetaArr);
14!
2344
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(pJson, "metas", pMetaArr));
14!
2345

2346
  int32_t num = taosArrayGetSize(rsp.batchMetaReq);
14✔
2347
  for (int32_t i = 0; i < num; i++) {
143✔
2348
    int32_t* len = taosArrayGet(rsp.batchMetaLen, i);
129✔
2349
    RAW_NULL_CHECK(len);
129!
2350
    void* tmpBuf = taosArrayGetP(rsp.batchMetaReq, i);
129✔
2351
    RAW_NULL_CHECK(tmpBuf);
129!
2352
    SDecoder   metaCoder = {0};
129✔
2353
    SMqMetaRsp metaRsp = {0};
129✔
2354
    tDecoderInit(&metaCoder, POINTER_SHIFT(tmpBuf, sizeof(SMqRspHead)), *len - sizeof(SMqRspHead));
129✔
2355
    if (tDecodeMqMetaRsp(&metaCoder, &metaRsp) < 0) {
129!
UNCOV
2356
      goto end;
×
2357
    }
2358
    cJSON* pItem = NULL;
129✔
2359
    processSimpleMeta(&metaRsp, &pItem);
129✔
2360
    tDeleteMqMetaRsp(&metaRsp);
129✔
2361
    RAW_FALSE_CHECK(tmqAddJsonArrayItem(pMetaArr, pItem));
129!
2362
  }
2363

2364
  tDeleteMqBatchMetaRsp(&rsp);
14✔
2365
  char* fullStr = cJSON_PrintUnformatted(pJson);
14✔
2366
  cJSON_Delete(pJson);
14✔
2367
  *string = fullStr;
14✔
2368
  return;
14✔
2369

UNCOV
2370
end:
×
UNCOV
2371
  cJSON_Delete(pJson);
×
UNCOV
2372
  tDeleteMqBatchMetaRsp(&rsp);
×
2373
}
2374

2375
char* tmq_get_json_meta(TAOS_RES* res) {
184✔
2376
  if (res == NULL) {
184!
UNCOV
2377
    uError("invalid parameter in %s", __func__);
×
UNCOV
2378
    return NULL;
×
2379
  }
2380
  uDebug("tmq_get_json_meta res:%p", res);
184!
2381
  if (!TD_RES_TMQ_META(res) && !TD_RES_TMQ_METADATA(res) && !TD_RES_TMQ_BATCH_META(res)) {
184!
2382
    return NULL;
×
2383
  }
2384

2385
  char*      string = NULL;
184✔
2386
  SMqRspObj* rspObj = (SMqRspObj*)res;
184✔
2387
  if (TD_RES_TMQ_METADATA(res)) {
184✔
2388
    processAutoCreateTable(&rspObj->dataRsp, &string);
10✔
2389
  } else if (TD_RES_TMQ_BATCH_META(res)) {
174✔
2390
    processBatchMetaToJson(&rspObj->batchMetaRsp, &string);
14✔
2391
  } else if (TD_RES_TMQ_META(res)) {
160!
2392
    cJSON* pJson = NULL;
160✔
2393
    processSimpleMeta(&rspObj->metaRsp, &pJson);
160✔
2394
    string = cJSON_PrintUnformatted(pJson);
160✔
2395
    cJSON_Delete(pJson);
160✔
2396
  } else {
UNCOV
2397
    uError("tmq_get_json_meta res:%d, invalid type", *(int8_t*)res);
×
2398
  }
2399

2400
  uDebug("tmq_get_json_meta string:%s", string);
184!
2401
  return string;
184✔
2402
}
2403

2404
void tmq_free_json_meta(char* jsonMeta) { taosMemoryFreeClear(jsonMeta); }
228!
2405

2406
static int32_t getOffSetLen(const SMqDataRsp* pRsp) {
47✔
2407
  if (pRsp == NULL) {
47!
UNCOV
2408
    uError("invalid parameter in %s", __func__);
×
UNCOV
2409
    return TSDB_CODE_INVALID_PARA;
×
2410
  }
2411
  SEncoder coder = {0};
47✔
2412
  tEncoderInit(&coder, NULL, 0);
47✔
2413
  if (tEncodeSTqOffsetVal(&coder, &pRsp->reqOffset) < 0) return -1;
47!
2414
  if (tEncodeSTqOffsetVal(&coder, &pRsp->rspOffset) < 0) return -1;
47!
2415
  int32_t pos = coder.pos;
47✔
2416
  tEncoderClear(&coder);
47✔
2417
  return pos;
47✔
2418
}
2419

2420
typedef int32_t __encode_func__(SEncoder* pEncoder, const SMqDataRsp* pRsp);
2421
static int32_t  encodeMqDataRsp(__encode_func__* encodeFunc, SMqDataRsp* rspObj, tmq_raw_data* raw) {
47✔
2422
  if (raw == NULL || encodeFunc == NULL || rspObj == NULL) {
47!
UNCOV
2423
    uError("invalid parameter in %s", __func__);
×
UNCOV
2424
    return TSDB_CODE_INVALID_PARA;
×
2425
  }
2426
  uint32_t len = 0;
47✔
2427
  int32_t  code = 0;
47✔
2428
  SEncoder encoder = {0};
47✔
2429
  void*    buf = NULL;
47✔
2430
  tEncodeSize(encodeFunc, rspObj, len, code);
47!
2431
  if (code < 0) {
47!
UNCOV
2432
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2433
    goto FAILED;
×
2434
  }
2435
  len += sizeof(int8_t) + sizeof(int32_t);
47✔
2436
  buf = taosMemoryCalloc(1, len);
47!
2437
  if (buf == NULL) {
47!
UNCOV
2438
    code = terrno;
×
UNCOV
2439
    goto FAILED;
×
2440
  }
2441
  tEncoderInit(&encoder, buf, len);
47✔
2442
  if (tEncodeI8(&encoder, MQ_DATA_RSP_VERSION) < 0) {
47!
2443
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2444
    goto FAILED;
×
2445
  }
2446
  int32_t offsetLen = getOffSetLen(rspObj);
47✔
2447
  if (offsetLen <= 0) {
47!
2448
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2449
    goto FAILED;
×
2450
  }
2451
  if (tEncodeI32(&encoder, offsetLen) < 0) {
47!
UNCOV
2452
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2453
    goto FAILED;
×
2454
  }
2455
  if (encodeFunc(&encoder, rspObj) < 0) {
47!
UNCOV
2456
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2457
    goto FAILED;
×
2458
  }
2459
  tEncoderClear(&encoder);
47✔
2460

2461
  raw->raw = buf;
47✔
2462
  raw->raw_len = len;
47✔
2463
  return code;
47✔
UNCOV
2464
FAILED:
×
UNCOV
2465
  tEncoderClear(&encoder);
×
UNCOV
2466
  taosMemoryFree(buf);
×
UNCOV
2467
  return code;
×
2468
}
2469

2470
int32_t tmq_get_raw(TAOS_RES* res, tmq_raw_data* raw) {
246✔
2471
  if (raw == NULL || res == NULL) {
246!
UNCOV
2472
    uError("invalid parameter in %s", __func__);
×
UNCOV
2473
    return TSDB_CODE_INVALID_PARA;
×
2474
  }
2475
  SMqRspObj* rspObj = ((SMqRspObj*)res);
246✔
2476
  if (TD_RES_TMQ_META(res)) {
246✔
2477
    raw->raw = rspObj->metaRsp.metaRsp;
185✔
2478
    raw->raw_len = rspObj->metaRsp.metaRspLen >= 0 ? rspObj->metaRsp.metaRspLen : 0;
185✔
2479
    raw->raw_type = rspObj->metaRsp.resMsgType;
185✔
2480
    uDebug("tmq get raw type meta:%p", raw);
185!
2481
  } else if (TD_RES_TMQ(res)) {
61✔
2482
    int32_t code = encodeMqDataRsp(tEncodeMqDataRsp, &rspObj->dataRsp, raw);
37✔
2483
    if (code != 0) {
37!
UNCOV
2484
      uError("tmq get raw type error:%d", terrno);
×
UNCOV
2485
      return code;
×
2486
    }
2487
    raw->raw_type = RES_TYPE__TMQ;
37✔
2488
    uDebug("tmq get raw type data:%p", raw);
37!
2489
  } else if (TD_RES_TMQ_METADATA(res)) {
24✔
2490
    int32_t code = encodeMqDataRsp(tEncodeSTaosxRsp, &rspObj->dataRsp, raw);
10✔
2491
    if (code != 0) {
10!
UNCOV
2492
      uError("tmq get raw type error:%d", terrno);
×
UNCOV
2493
      return code;
×
2494
    }
2495
    raw->raw_type = RES_TYPE__TMQ_METADATA;
10✔
2496
    uDebug("tmq get raw type metadata:%p", raw);
10!
2497
  } else if (TD_RES_TMQ_BATCH_META(res)) {
14!
2498
    raw->raw = rspObj->batchMetaRsp.pMetaBuff;
14✔
2499
    raw->raw_len = rspObj->batchMetaRsp.metaBuffLen;
14✔
2500
    raw->raw_type = rspObj->resType;
14✔
2501
    uDebug("tmq get raw batch meta:%p", raw);
14!
2502
  } else {
UNCOV
2503
    uError("tmq get raw error type:%d", *(int8_t*)res);
×
UNCOV
2504
    return TSDB_CODE_TMQ_INVALID_MSG;
×
2505
  }
2506
  return TSDB_CODE_SUCCESS;
246✔
2507
}
2508

2509
void tmq_free_raw(tmq_raw_data raw) {
246✔
2510
  uDebug("tmq free raw data type:%d", raw.raw_type);
246!
2511
  if (raw.raw_type == RES_TYPE__TMQ || raw.raw_type == RES_TYPE__TMQ_METADATA) {
246✔
2512
    taosMemoryFree(raw.raw);
47!
2513
  }
2514
  (void)memset(terrMsg, 0, ERR_MSG_LEN);
246✔
2515
}
246✔
2516

2517
static int32_t writeRawInit() {
350✔
2518
  while (atomic_load_8(&initedFlag) == WRITE_RAW_INIT_START) {
365✔
2519
    int8_t old = atomic_val_compare_exchange_8(&initFlag, 0, 1);
15✔
2520
    if (old == 0) {
15!
2521
      int32_t code = initRawCacheHash();
15✔
2522
      if (code != 0) {
15!
UNCOV
2523
        uError("tmq writeRawImpl init error:%d", code);
×
UNCOV
2524
        atomic_store_8(&initedFlag, WRITE_RAW_INIT_FAIL);
×
UNCOV
2525
        return code;
×
2526
      }
2527
      atomic_store_8(&initedFlag, WRITE_RAW_INIT_OK);
15✔
2528
    }
2529
  }
2530

2531
  if (atomic_load_8(&initedFlag) == WRITE_RAW_INIT_FAIL) {
350!
UNCOV
2532
    return TSDB_CODE_INTERNAL_ERROR;
×
2533
  }
2534
  return 0;
350✔
2535
}
2536

2537
static int32_t writeRawImpl(TAOS* taos, void* buf, uint32_t len, uint16_t type) {
350✔
2538
  if (taos == NULL || buf == NULL) {
350!
UNCOV
2539
    uError("invalid parameter in %s", __func__);
×
UNCOV
2540
    return TSDB_CODE_INVALID_PARA;
×
2541
  }
2542
  if (writeRawInit() != 0) {
350!
UNCOV
2543
    return TSDB_CODE_INTERNAL_ERROR;
×
2544
  }
2545

2546
  if (type == TDMT_VND_CREATE_STB) {
350✔
2547
    return taosCreateStb(taos, buf, len);
78✔
2548
  } else if (type == TDMT_VND_ALTER_STB) {
272✔
2549
    return taosCreateStb(taos, buf, len);
40✔
2550
  } else if (type == TDMT_VND_DROP_STB) {
232✔
2551
    return taosDropStb(taos, buf, len);
8✔
2552
  } else if (type == TDMT_VND_CREATE_TABLE) {
224✔
2553
    return taosCreateTable(taos, buf, len);
131✔
2554
  } else if (type == TDMT_VND_ALTER_TABLE) {
93✔
2555
    return taosAlterTable(taos, buf, len);
24✔
2556
  } else if (type == TDMT_VND_DROP_TABLE) {
69✔
2557
    return taosDropTable(taos, buf, len);
5✔
2558
  } else if (type == TDMT_VND_DELETE) {
64✔
2559
    return taosDeleteData(taos, buf, len);
3✔
2560
  } else if (type == RES_TYPE__TMQ_METADATA) {
61✔
2561
    return tmqWriteRawMetaDataImpl(taos, buf, len);
10✔
2562
  } else if (type == RES_TYPE__TMQ) {
51✔
2563
    return tmqWriteRawDataImpl(taos, buf, len);
37✔
2564
  } else if (type == RES_TYPE__TMQ_BATCH_META) {
14!
2565
    return tmqWriteBatchMetaDataImpl(taos, buf, len);
14✔
2566
  }
UNCOV
2567
  return TSDB_CODE_INVALID_PARA;
×
2568
}
2569

2570
int32_t tmq_write_raw(TAOS* taos, tmq_raw_data raw) {
234✔
2571
  if (taos == NULL || raw.raw == NULL || raw.raw_len <= 0) {
234!
2572
    SET_ERROR_MSG("taos:%p or data:%p is NULL or raw_len <= 0", taos, raw.raw);
13✔
2573
    return TSDB_CODE_INVALID_PARA;
13✔
2574
  }
2575

2576
  return writeRawImpl(taos, raw.raw, raw.raw_len, raw.raw_type);
221✔
2577
}
2578

2579
static int32_t tmqWriteBatchMetaDataImpl(TAOS* taos, void* meta, uint32_t metaLen) {
14✔
2580
  if (taos == NULL || meta == NULL) {
14!
UNCOV
2581
    uError("invalid parameter in %s", __func__);
×
UNCOV
2582
    return TSDB_CODE_INVALID_PARA;
×
2583
  }
2584
  SMqBatchMetaRsp rsp = {0};
14✔
2585
  SDecoder        coder = {0};
14✔
2586
  int32_t         code = TSDB_CODE_SUCCESS;
14✔
2587

2588
  // decode and process req
2589
  tDecoderInit(&coder, meta, metaLen);
14✔
2590
  if (tDecodeMqBatchMetaRsp(&coder, &rsp) < 0) {
14!
UNCOV
2591
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
2592
    goto end;
×
2593
  }
2594
  int32_t num = taosArrayGetSize(rsp.batchMetaReq);
14✔
2595
  for (int32_t i = 0; i < num; i++) {
143✔
2596
    int32_t* len = taosArrayGet(rsp.batchMetaLen, i);
129✔
2597
    RAW_NULL_CHECK(len);
129!
2598
    void* tmpBuf = taosArrayGetP(rsp.batchMetaReq, i);
129✔
2599
    RAW_NULL_CHECK(tmpBuf);
129!
2600
    SDecoder   metaCoder = {0};
129✔
2601
    SMqMetaRsp metaRsp = {0};
129✔
2602
    tDecoderInit(&metaCoder, POINTER_SHIFT(tmpBuf, sizeof(SMqRspHead)), *len - sizeof(SMqRspHead));
129✔
2603
    if (tDecodeMqMetaRsp(&metaCoder, &metaRsp) < 0) {
129!
UNCOV
2604
      code = TSDB_CODE_INVALID_PARA;
×
UNCOV
2605
      goto end;
×
2606
    }
2607
    code = writeRawImpl(taos, metaRsp.metaRsp, metaRsp.metaRspLen, metaRsp.resMsgType);
129✔
2608
    tDeleteMqMetaRsp(&metaRsp);
129✔
2609
    if (code != TSDB_CODE_SUCCESS) {
129!
UNCOV
2610
      goto end;
×
2611
    }
2612
  }
2613

2614
end:
14✔
2615
  tDeleteMqBatchMetaRsp(&rsp);
14✔
2616
  return code;
14✔
2617
}
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