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

taosdata / TDengine / #4848

12 Nov 2025 01:06AM UTC coverage: 63.27% (+0.6%) from 62.651%
#4848

push

travis-ci

web-flow
Merge f12882a7a into e27395247

33 of 36 new or added lines in 4 files covered. (91.67%)

2652 existing lines in 104 files now uncovered.

138980 of 219661 relevant lines covered (63.27%)

110230098.27 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

275
  cJSON* alterType = cJSON_CreateNumber(req.alterType);
1,630✔
276
  RAW_NULL_CHECK(alterType);
1,630✔
277
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "alterType", alterType));
1,630✔
278
  switch (req.alterType) {
1,630✔
279
    case TSDB_ALTER_TABLE_ADD_TAG:
978✔
280
    case TSDB_ALTER_TABLE_ADD_COLUMN: {
281
      TAOS_FIELD* field = taosArrayGet(req.pFields, 0);
978✔
282
      RAW_NULL_CHECK(field);
978✔
283
      cJSON* colName = cJSON_CreateString(field->name);
978✔
284
      RAW_NULL_CHECK(colName);
978✔
285
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
978✔
286
      cJSON* colType = cJSON_CreateNumber(field->type);
978✔
287
      RAW_NULL_CHECK(colType);
978✔
288
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colType", colType));
978✔
289

290
      if (field->type == TSDB_DATA_TYPE_BINARY || field->type == TSDB_DATA_TYPE_VARBINARY ||
978✔
291
          field->type == TSDB_DATA_TYPE_GEOMETRY) {
978✔
292
        int32_t length = field->bytes - VARSTR_HEADER_SIZE;
326✔
293
        cJSON*  cbytes = cJSON_CreateNumber(length);
326✔
294
        RAW_NULL_CHECK(cbytes);
326✔
295
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
326✔
296
      } else if (field->type == TSDB_DATA_TYPE_NCHAR) {
652✔
297
        int32_t length = (field->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
298
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
299
        RAW_NULL_CHECK(cbytes);
×
300
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
301
      } else if (IS_STR_DATA_BLOB(field->type)) {
652✔
302
        int32_t length = field->bytes - BLOBSTR_HEADER_SIZE;
×
303
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
304
        RAW_NULL_CHECK(cbytes);
×
305
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
306
      }
307
      break;
978✔
308
    }
309
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION: {
×
310
      SFieldWithOptions* field = taosArrayGet(req.pFields, 0);
×
311
      RAW_NULL_CHECK(field);
×
312
      cJSON* colName = cJSON_CreateString(field->name);
×
313
      RAW_NULL_CHECK(colName);
×
314
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
×
315
      cJSON* colType = cJSON_CreateNumber(field->type);
×
316
      RAW_NULL_CHECK(colType);
×
317
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colType", colType));
×
318

319
      if (field->type == TSDB_DATA_TYPE_BINARY || field->type == TSDB_DATA_TYPE_VARBINARY ||
×
320
          field->type == TSDB_DATA_TYPE_GEOMETRY) {
×
321
        int32_t length = field->bytes - VARSTR_HEADER_SIZE;
×
322
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
323
        RAW_NULL_CHECK(cbytes);
×
324
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
325
      } else if (field->type == TSDB_DATA_TYPE_NCHAR) {
×
326
        int32_t length = (field->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
327
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
328
        RAW_NULL_CHECK(cbytes);
×
329
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
330
      } else if (IS_STR_DATA_BLOB(field->type)) {
×
331
        int32_t length = field->bytes - BLOBSTR_HEADER_SIZE;
×
332
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
333
        RAW_NULL_CHECK(cbytes);
×
334
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
335
      }
336

337
      RAW_RETURN_CHECK(setCompressOption(json, field->compress));
×
338
      break;
×
339
    }
340
    case TSDB_ALTER_TABLE_DROP_TAG:
326✔
341
    case TSDB_ALTER_TABLE_DROP_COLUMN: {
342
      TAOS_FIELD* field = taosArrayGet(req.pFields, 0);
326✔
343
      RAW_NULL_CHECK(field);
326✔
344
      cJSON* colName = cJSON_CreateString(field->name);
326✔
345
      RAW_NULL_CHECK(colName);
326✔
346
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
326✔
347
      break;
326✔
348
    }
349
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
326✔
350
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES: {
351
      TAOS_FIELD* field = taosArrayGet(req.pFields, 0);
326✔
352
      RAW_NULL_CHECK(field);
326✔
353
      cJSON* colName = cJSON_CreateString(field->name);
326✔
354
      RAW_NULL_CHECK(colName);
326✔
355
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
326✔
356
      cJSON* colType = cJSON_CreateNumber(field->type);
326✔
357
      RAW_NULL_CHECK(colType);
326✔
358
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colType", colType));
326✔
359
      if (field->type == TSDB_DATA_TYPE_BINARY || field->type == TSDB_DATA_TYPE_VARBINARY ||
326✔
360
          field->type == TSDB_DATA_TYPE_GEOMETRY) {
326✔
361
        int32_t length = field->bytes - VARSTR_HEADER_SIZE;
326✔
362
        cJSON*  cbytes = cJSON_CreateNumber(length);
326✔
363
        RAW_NULL_CHECK(cbytes);
326✔
364
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
326✔
365
      } else if (field->type == TSDB_DATA_TYPE_NCHAR) {
×
366
        int32_t length = (field->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
367
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
368
        RAW_NULL_CHECK(cbytes);
×
369
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
370
      }
371
      break;
326✔
372
    }
373
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
×
374
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME: {
375
      TAOS_FIELD* oldField = taosArrayGet(req.pFields, 0);
×
376
      RAW_NULL_CHECK(oldField);
×
377
      TAOS_FIELD* newField = taosArrayGet(req.pFields, 1);
×
378
      RAW_NULL_CHECK(newField);
×
379
      cJSON* colName = cJSON_CreateString(oldField->name);
×
380
      RAW_NULL_CHECK(colName);
×
381
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
×
382
      cJSON* colNewName = cJSON_CreateString(newField->name);
×
383
      RAW_NULL_CHECK(colNewName);
×
384
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colNewName", colNewName));
×
385
      break;
×
386
    }
387
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS: {
×
388
      TAOS_FIELD* field = taosArrayGet(req.pFields, 0);
×
389
      RAW_NULL_CHECK(field);
×
390
      cJSON* colName = cJSON_CreateString(field->name);
×
391
      RAW_NULL_CHECK(colName);
×
392
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
×
393
      RAW_RETURN_CHECK(setCompressOption(json, field->bytes));
×
394
      break;
×
395
    }
396
    default:
×
397
      break;
×
398
  }
399

400
end:
1,630✔
401
  tFreeSMAltertbReq(&req);
1,630✔
402
  *pJson = json;
1,630✔
403
}
404

405
static void processCreateStb(SMqMetaRsp* metaRsp, cJSON** pJson) {
2,917✔
406
  if (metaRsp == NULL || pJson == NULL) {
2,917✔
407
    uError("invalid parameter in %s", __func__);
×
408
    return;
×
409
  }
410
  SVCreateStbReq req = {0};
2,917✔
411
  SDecoder       coder = {0};
2,917✔
412

413
  uDebug("create stable data:%p", metaRsp);
2,917✔
414
  // decode and process req
415
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
2,917✔
416
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
2,917✔
417
  tDecoderInit(&coder, data, len);
2,917✔
418

419
  if (tDecodeSVCreateStbReq(&coder, &req) < 0) {
2,917✔
420
    goto end;
×
421
  }
422
  buildCreateTableJson(&req.schemaRow, &req.schemaTag, req.pExtSchemas, req.name, req.suid, TSDB_SUPER_TABLE, &req.colCmpr, pJson);
2,917✔
423

424
end:
2,917✔
425
  uDebug("create stable return");
2,917✔
426
  tDecoderClear(&coder);
2,917✔
427
}
428

429
static void processAlterStb(SMqMetaRsp* metaRsp, cJSON** pJson) {
1,630✔
430
  if (metaRsp == NULL || pJson == NULL) {
1,630✔
431
    uError("invalid parameter in %s", __func__);
×
432
    return;
×
433
  }
434
  SVCreateStbReq req = {0};
1,630✔
435
  SDecoder       coder = {0};
1,630✔
436
  uDebug("alter stable data:%p", metaRsp);
1,630✔
437

438
  // decode and process req
439
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
1,630✔
440
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
1,630✔
441
  tDecoderInit(&coder, data, len);
1,630✔
442

443
  if (tDecodeSVCreateStbReq(&coder, &req) < 0) {
1,630✔
444
    goto end;
×
445
  }
446
  buildAlterSTableJson(req.alterOriData, req.alterOriDataLen, pJson);
1,630✔
447

448
end:
1,630✔
449
  uDebug("alter stable return");
1,630✔
450
  tDecoderClear(&coder);
1,630✔
451
}
452

453
static void buildChildElement(cJSON* json, SVCreateTbReq* pCreateReq) {
5,669✔
454
  if (json == NULL || pCreateReq == NULL) {
5,669✔
455
    uError("invalid parameter in %s", __func__);
×
456
    return;
×
457
  }
458
  STag*   pTag = (STag*)pCreateReq->ctb.pTag;
5,669✔
459
  char*   sname = pCreateReq->ctb.stbName;
5,669✔
460
  char*   name = pCreateReq->name;
5,669✔
461
  SArray* tagName = pCreateReq->ctb.tagName;
5,669✔
462
  int64_t id = pCreateReq->uid;
5,669✔
463
  uint8_t tagNum = pCreateReq->ctb.tagNum;
5,669✔
464
  int32_t code = 0;
5,669✔
465
  SArray* pTagVals = NULL;
5,669✔
466
  char*   pJson = NULL;
5,669✔
467

468
  cJSON* tableName = cJSON_CreateString(name);
5,669✔
469
  RAW_NULL_CHECK(tableName);
5,669✔
470
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableName", tableName));
5,669✔
471
  cJSON* using = cJSON_CreateString(sname);
5,669✔
472
  RAW_NULL_CHECK(using);
5,669✔
473
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "using", using));
5,669✔
474
  cJSON* tagNumJson = cJSON_CreateNumber(tagNum);
5,669✔
475
  RAW_NULL_CHECK(tagNumJson);
5,669✔
476
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tagNum", tagNumJson));
5,669✔
477

478
  cJSON* tags = cJSON_CreateArray();
5,669✔
479
  RAW_NULL_CHECK(tags);
5,669✔
480
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tags", tags));
5,669✔
481
  RAW_RETURN_CHECK(tTagToValArray(pTag, &pTagVals));
5,669✔
482
  if (tTagIsJson(pTag)) {
5,669✔
483
    STag* p = (STag*)pTag;
758✔
484
    if (p->nTag == 0) {
758✔
485
      uError("p->nTag == 0");
379✔
486
      goto end;
379✔
487
    }
488
    parseTagDatatoJson(pTag, &pJson, NULL);
379✔
489
    RAW_NULL_CHECK(pJson);
379✔
490
    cJSON* tag = cJSON_CreateObject();
379✔
491
    RAW_NULL_CHECK(tag);
379✔
492
    RAW_FALSE_CHECK(tmqAddJsonArrayItem(tags, tag));
379✔
493
    STagVal* pTagVal = taosArrayGet(pTagVals, 0);
379✔
494
    RAW_NULL_CHECK(pTagVal);
379✔
495
    char* ptname = taosArrayGet(tagName, 0);
379✔
496
    RAW_NULL_CHECK(ptname);
379✔
497
    cJSON* tname = cJSON_CreateString(ptname);
379✔
498
    RAW_NULL_CHECK(tname);
379✔
499
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "name", tname));
379✔
500
    cJSON* ttype = cJSON_CreateNumber(TSDB_DATA_TYPE_JSON);
379✔
501
    RAW_NULL_CHECK(ttype);
379✔
502
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "type", ttype));
379✔
503
    cJSON* tvalue = cJSON_CreateString(pJson);
379✔
504
    RAW_NULL_CHECK(tvalue);
379✔
505
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "value", tvalue));
379✔
506
    goto end;
379✔
507
  }
508

509
  for (int i = 0; i < taosArrayGetSize(pTagVals); i++) {
16,929✔
510
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, i);
12,018✔
511
    RAW_NULL_CHECK(pTagVal);
12,018✔
512
    cJSON* tag = cJSON_CreateObject();
12,018✔
513
    RAW_NULL_CHECK(tag);
12,018✔
514
    RAW_FALSE_CHECK(tmqAddJsonArrayItem(tags, tag));
12,018✔
515
    char* ptname = taosArrayGet(tagName, i);
12,018✔
516
    RAW_NULL_CHECK(ptname);
12,018✔
517
    cJSON* tname = cJSON_CreateString(ptname);
12,018✔
518
    RAW_NULL_CHECK(tname);
12,018✔
519
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "name", tname));
12,018✔
520
    cJSON* ttype = cJSON_CreateNumber(pTagVal->type);
12,018✔
521
    RAW_NULL_CHECK(ttype);
12,018✔
522
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "type", ttype));
12,018✔
523

524
    cJSON* tvalue = NULL;
12,018✔
525
    if (IS_VAR_DATA_TYPE(pTagVal->type)) {
15,852✔
526
      if (IS_STR_DATA_BLOB(pTagVal->type)) {
3,834✔
527
        goto end;
×
528
      }
529
      int64_t bufSize = 0;
3,834✔
530
      if (pTagVal->type == TSDB_DATA_TYPE_VARBINARY) {
3,834✔
531
        bufSize = pTagVal->nData * 2 + 2 + 3;
×
532
      } else {
533
        bufSize = pTagVal->nData + 3;
3,834✔
534
      }
535
      char* buf = taosMemoryCalloc(bufSize, 1);
3,834✔
536
      RAW_NULL_CHECK(buf);
3,834✔
537
      if (dataConverToStr(buf, bufSize, pTagVal->type, pTagVal->pData, pTagVal->nData, NULL) != TSDB_CODE_SUCCESS) {
3,834✔
538
        taosMemoryFree(buf);
×
539
        goto end;
×
540
      }
541

542
      tvalue = cJSON_CreateString(buf);
3,834✔
543
      taosMemoryFree(buf);
3,834✔
544
      RAW_NULL_CHECK(tvalue);
3,834✔
545
    } else {
546
      double val = 0;
8,184✔
547
      GET_TYPED_DATA(val, double, pTagVal->type, &pTagVal->i64,
8,184✔
548
                     0);  // currently tag type can't be decimal, so pass 0 as typeMod
549
      tvalue = cJSON_CreateNumber(val);
8,184✔
550
      RAW_NULL_CHECK(tvalue);
8,184✔
551
    }
552

553
    RAW_FALSE_CHECK(tmqAddJsonObjectItem(tag, "value", tvalue));
12,018✔
554
  }
555

556
end:
5,669✔
557
  taosMemoryFree(pJson);
5,669✔
558
  taosArrayDestroy(pTagVals);
5,669✔
559
}
560

561
static void buildCreateCTableJson(SVCreateTbReq* pCreateReq, int32_t nReqs, cJSON** pJson) {
4,375✔
562
  if (pJson == NULL || pCreateReq == NULL) {
4,375✔
563
    uError("invalid parameter in %s", __func__);
×
564
    return;
×
565
  }
566
  int32_t code = 0;
4,375✔
567
  char*   string = NULL;
4,375✔
568
  cJSON*  json = cJSON_CreateObject();
4,375✔
569
  RAW_NULL_CHECK(json);
4,375✔
570
  cJSON* type = cJSON_CreateString("create");
4,375✔
571
  RAW_NULL_CHECK(type);
4,375✔
572
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "type", type));
4,375✔
573

574
  cJSON* tableType = cJSON_CreateString("child");
4,375✔
575
  RAW_NULL_CHECK(tableType);
4,375✔
576
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableType", tableType));
4,375✔
577

578
  buildChildElement(json, pCreateReq);
4,375✔
579
  cJSON* createList = cJSON_CreateArray();
4,375✔
580
  RAW_NULL_CHECK(createList);
4,375✔
581
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "createList", createList));
4,375✔
582

583
  for (int i = 0; nReqs > 1 && i < nReqs; i++) {
5,669✔
584
    cJSON* create = cJSON_CreateObject();
1,294✔
585
    RAW_NULL_CHECK(create);
1,294✔
586
    buildChildElement(create, pCreateReq + i);
1,294✔
587
    RAW_FALSE_CHECK(tmqAddJsonArrayItem(createList, create));
1,294✔
588
  }
589

590
end:
4,375✔
591
  *pJson = json;
4,375✔
592
}
593

594
static void processCreateTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
4,882✔
595
  if (pJson == NULL || metaRsp == NULL) {
4,882✔
596
    uError("invalid parameter in %s", __func__);
×
597
    return;
×
598
  }
599
  SDecoder           decoder = {0};
4,882✔
600
  SVCreateTbBatchReq req = {0};
4,882✔
601
  SVCreateTbReq*     pCreateReq;
602
  // decode
603
  uDebug("create table data:%p", metaRsp);
4,882✔
604
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
4,882✔
605
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
4,882✔
606
  tDecoderInit(&decoder, data, len);
4,882✔
607
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
4,882✔
608
    goto end;
×
609
  }
610

611
  // loop to create table
612
  if (req.nReqs > 0) {
4,882✔
613
    pCreateReq = req.pReqs;
4,882✔
614
    if (pCreateReq->type == TSDB_CHILD_TABLE) {
4,882✔
615
      buildCreateCTableJson(req.pReqs, req.nReqs, pJson);
4,122✔
616
    } else if (pCreateReq->type == TSDB_NORMAL_TABLE) {
760✔
617
      buildCreateTableJson(&pCreateReq->ntb.schemaRow, NULL, pCreateReq->pExtSchemas, pCreateReq->name, pCreateReq->uid, TSDB_NORMAL_TABLE,
760✔
618
                           &pCreateReq->colCmpr, pJson);
619
    }
620
  }
621

622
end:
4,882✔
623
  uDebug("create table return");
4,882✔
624
  tDeleteSVCreateTbBatchReq(&req);
4,882✔
625
  tDecoderClear(&decoder);
4,882✔
626
}
627

628
static void processAutoCreateTable(SMqDataRsp* rsp, char** string) {
253✔
629
  if (rsp == NULL || string == NULL) {
253✔
630
    uError("invalid parameter in %s", __func__);
×
631
    return;
×
632
  }
633
  SDecoder*      decoder = NULL;
253✔
634
  SVCreateTbReq* pCreateReq = NULL;
253✔
635
  int32_t        code = 0;
253✔
636
  uDebug("auto create table data:%p", rsp);
253✔
637
  if (rsp->createTableNum <= 0) {
253✔
638
    uError("processAutoCreateTable rsp->createTableNum <= 0");
×
639
    goto end;
×
640
  }
641

642
  decoder = taosMemoryCalloc(rsp->createTableNum, sizeof(SDecoder));
253✔
643
  RAW_NULL_CHECK(decoder);
253✔
644
  pCreateReq = taosMemoryCalloc(rsp->createTableNum, sizeof(SVCreateTbReq));
253✔
645
  RAW_NULL_CHECK(pCreateReq);
253✔
646

647
  // loop to create table
648
  for (int32_t iReq = 0; iReq < rsp->createTableNum; iReq++) {
811✔
649
    // decode
650
    void** data = taosArrayGet(rsp->createTableReq, iReq);
558✔
651
    RAW_NULL_CHECK(data);
558✔
652
    int32_t* len = taosArrayGet(rsp->createTableLen, iReq);
558✔
653
    RAW_NULL_CHECK(len);
558✔
654
    tDecoderInit(&decoder[iReq], *data, *len);
558✔
655
    if (tDecodeSVCreateTbReq(&decoder[iReq], pCreateReq + iReq) < 0) {
558✔
656
      goto end;
×
657
    }
658

659
    if (pCreateReq[iReq].type != TSDB_CHILD_TABLE && pCreateReq[iReq].type != TSDB_NORMAL_TABLE) {
558✔
660
      uError("processAutoCreateTable pCreateReq[iReq].type != TSDB_CHILD_TABLE");
×
661
      goto end;
×
662
    }
663
  }
664
  cJSON* pJson = NULL;
253✔
665
  if (pCreateReq->type == TSDB_NORMAL_TABLE) {
253✔
UNCOV
666
    buildCreateTableJson(&pCreateReq->ntb.schemaRow, NULL, pCreateReq->pExtSchemas, pCreateReq->name, pCreateReq->uid, TSDB_NORMAL_TABLE,
×
667
                         &pCreateReq->colCmpr, &pJson);
668
  } else if (pCreateReq->type == TSDB_CHILD_TABLE) {
253✔
669
    buildCreateCTableJson(pCreateReq, rsp->createTableNum, &pJson);
253✔
670
  }
671

672
  *string = cJSON_PrintUnformatted(pJson);
253✔
673
  cJSON_Delete(pJson);
253✔
674

675
end:
253✔
676
  uDebug("auto created table return, sql json:%s", *string);
253✔
677
  for (int i = 0; decoder && pCreateReq && i < rsp->createTableNum; i++) {
811✔
678
    tDecoderClear(&decoder[i]);
558✔
679
    taosMemoryFreeClear(pCreateReq[i].comment);
558✔
680
    if (pCreateReq[i].type == TSDB_CHILD_TABLE) {
558✔
681
      taosArrayDestroy(pCreateReq[i].ctb.tagName);
558✔
682
    }
683
  }
684
  taosMemoryFree(decoder);
253✔
685
  taosMemoryFree(pCreateReq);
253✔
686
}
687

688
static void processAlterTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
1,134✔
689
  if (pJson == NULL || metaRsp == NULL) {
1,134✔
690
    uError("invalid parameter in %s", __func__);
×
691
    return;
×
692
  }
693
  SDecoder     decoder = {0};
1,134✔
694
  SVAlterTbReq vAlterTbReq = {0};
1,134✔
695
  char*        string = NULL;
1,134✔
696
  cJSON*       json = NULL;
1,134✔
697
  int32_t      code = 0;
1,134✔
698

699
  uDebug("alter table data:%p", metaRsp);
1,134✔
700
  // decode
701
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
1,134✔
702
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
1,134✔
703
  tDecoderInit(&decoder, data, len);
1,134✔
704
  if (tDecodeSVAlterTbReq(&decoder, &vAlterTbReq) < 0) {
1,134✔
705
    uError("tDecodeSVAlterTbReq error");
×
706
    goto end;
×
707
  }
708

709
  json = cJSON_CreateObject();
1,134✔
710
  RAW_NULL_CHECK(json);
1,134✔
711
  cJSON* type = cJSON_CreateString("alter");
1,134✔
712
  RAW_NULL_CHECK(type);
1,134✔
713
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "type", type));
1,134✔
714
  cJSON* tableType = cJSON_CreateString(vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
2,034✔
715
                                                vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL
900✔
716
                                            ? "child"
717
                                            : "normal");
718
  RAW_NULL_CHECK(tableType);
1,134✔
719
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableType", tableType));
1,134✔
720
  cJSON* tableName = cJSON_CreateString(vAlterTbReq.tbName);
1,134✔
721
  RAW_NULL_CHECK(tableName);
1,134✔
722
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableName", tableName));
1,134✔
723
  cJSON* alterType = cJSON_CreateNumber(vAlterTbReq.action);
1,134✔
724
  RAW_NULL_CHECK(alterType);
1,134✔
725
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "alterType", alterType));
1,134✔
726

727
  switch (vAlterTbReq.action) {
1,134✔
728
    case TSDB_ALTER_TABLE_ADD_COLUMN: {
180✔
729
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
180✔
730
      RAW_NULL_CHECK(colName);
180✔
731
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
180✔
732
      cJSON* colType = cJSON_CreateNumber(vAlterTbReq.type);
180✔
733
      RAW_NULL_CHECK(colType);
180✔
734
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colType", colType));
180✔
735

736
      if (vAlterTbReq.type == TSDB_DATA_TYPE_BINARY || vAlterTbReq.type == TSDB_DATA_TYPE_VARBINARY ||
180✔
737
          vAlterTbReq.type == TSDB_DATA_TYPE_GEOMETRY) {
180✔
738
        int32_t length = vAlterTbReq.bytes - VARSTR_HEADER_SIZE;
×
739
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
740
        RAW_NULL_CHECK(cbytes);
×
741
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
742
      } else if (vAlterTbReq.type == TSDB_DATA_TYPE_NCHAR) {
180✔
743
        int32_t length = (vAlterTbReq.bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
744
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
745
        RAW_NULL_CHECK(cbytes);
×
746
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
747
      }
748
      break;
180✔
749
    }
750
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION: {
×
751
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
×
752
      RAW_NULL_CHECK(colName);
×
753
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
×
754
      cJSON* colType = cJSON_CreateNumber(vAlterTbReq.type);
×
755
      RAW_NULL_CHECK(colType);
×
756
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colType", colType));
×
757

758
      if (vAlterTbReq.type == TSDB_DATA_TYPE_BINARY || vAlterTbReq.type == TSDB_DATA_TYPE_VARBINARY ||
×
759
          vAlterTbReq.type == TSDB_DATA_TYPE_GEOMETRY) {
×
760
        int32_t length = vAlterTbReq.bytes - VARSTR_HEADER_SIZE;
×
761
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
762
        RAW_NULL_CHECK(cbytes);
×
763
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
764
      } else if (vAlterTbReq.type == TSDB_DATA_TYPE_NCHAR) {
×
765
        int32_t length = (vAlterTbReq.bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
766
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
767
        RAW_NULL_CHECK(cbytes);
×
768
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
769
      }
770
      RAW_RETURN_CHECK(setCompressOption(json, vAlterTbReq.compress));
×
771
      break;
×
772
    }
773
    case TSDB_ALTER_TABLE_DROP_COLUMN: {
180✔
774
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
180✔
775
      RAW_NULL_CHECK(colName);
180✔
776
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
180✔
777
      break;
180✔
778
    }
779
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES: {
180✔
780
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
180✔
781
      RAW_NULL_CHECK(colName);
180✔
782
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
180✔
783
      cJSON* colType = cJSON_CreateNumber(vAlterTbReq.colModType);
180✔
784
      RAW_NULL_CHECK(colType);
180✔
785
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colType", colType));
180✔
786
      if (vAlterTbReq.colModType == TSDB_DATA_TYPE_BINARY || vAlterTbReq.colModType == TSDB_DATA_TYPE_VARBINARY ||
180✔
787
          vAlterTbReq.colModType == TSDB_DATA_TYPE_GEOMETRY) {
180✔
788
        int32_t length = vAlterTbReq.colModBytes - VARSTR_HEADER_SIZE;
×
789
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
790
        RAW_NULL_CHECK(cbytes);
×
791
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
×
792
      } else if (vAlterTbReq.colModType == TSDB_DATA_TYPE_NCHAR) {
180✔
793
        int32_t length = (vAlterTbReq.colModBytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
180✔
794
        cJSON*  cbytes = cJSON_CreateNumber(length);
180✔
795
        RAW_NULL_CHECK(cbytes);
180✔
796
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colLength", cbytes));
180✔
797
      }
798
      break;
180✔
799
    }
800
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME: {
180✔
801
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
180✔
802
      RAW_NULL_CHECK(colName);
180✔
803
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
180✔
804
      cJSON* colNewName = cJSON_CreateString(vAlterTbReq.colNewName);
180✔
805
      RAW_NULL_CHECK(colNewName);
180✔
806
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colNewName", colNewName));
180✔
807
      break;
180✔
808
    }
809
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL: {
234✔
810
      cJSON* tagName = cJSON_CreateString(vAlterTbReq.tagName);
234✔
811
      RAW_NULL_CHECK(tagName);
234✔
812
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", tagName));
234✔
813

814
      bool isNull = vAlterTbReq.isNull;
234✔
815
      if (vAlterTbReq.tagType == TSDB_DATA_TYPE_JSON) {
234✔
816
        STag* jsonTag = (STag*)vAlterTbReq.pTagVal;
×
817
        if (jsonTag->nTag == 0) isNull = true;
×
818
      }
819
      if (!isNull) {
234✔
820
        char* buf = NULL;
234✔
821

822
        if (vAlterTbReq.tagType == TSDB_DATA_TYPE_JSON) {
234✔
823
          if (!tTagIsJson(vAlterTbReq.pTagVal)) {
×
824
            uError("processAlterTable isJson false");
×
825
            goto end;
×
826
          }
827
          parseTagDatatoJson(vAlterTbReq.pTagVal, &buf, NULL);
×
828
          if (buf == NULL) {
×
829
            uError("parseTagDatatoJson failed, buf == NULL");
×
830
            goto end;
×
831
          }
832
        } else {
833
          int64_t bufSize = 0;
234✔
834
          if (vAlterTbReq.tagType == TSDB_DATA_TYPE_VARBINARY) {
234✔
835
            bufSize = vAlterTbReq.nTagVal * 2 + 2 + 3;
×
836
          } else {
837
            bufSize = vAlterTbReq.nTagVal + 32;
234✔
838
          }
839
          buf = taosMemoryCalloc(bufSize, 1);
234✔
840
          RAW_NULL_CHECK(buf);
234✔
841
          if (dataConverToStr(buf, bufSize, vAlterTbReq.tagType, vAlterTbReq.pTagVal, vAlterTbReq.nTagVal, NULL) !=
234✔
842
              TSDB_CODE_SUCCESS) {
843
            taosMemoryFree(buf);
×
844
            goto end;
×
845
          }
846
        }
847

848
        cJSON* colValue = cJSON_CreateString(buf);
234✔
849
        taosMemoryFree(buf);
234✔
850
        RAW_NULL_CHECK(colValue);
234✔
851
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colValue", colValue));
234✔
852
      }
853

854
      cJSON* isNullCJson = cJSON_CreateBool(isNull);
234✔
855
      RAW_NULL_CHECK(isNullCJson);
234✔
856
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colValueNull", isNullCJson));
234✔
857
      break;
234✔
858
    }
859
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL: {
×
860
      int32_t nTags = taosArrayGetSize(vAlterTbReq.pMultiTag);
×
861
      if (nTags <= 0) {
×
862
        uError("processAlterTable parse multi tags error");
×
863
        goto end;
×
864
      }
865

866
      cJSON* tags = cJSON_CreateArray();
×
867
      RAW_NULL_CHECK(tags);
×
868
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tags", tags));
×
869

870
      for (int32_t i = 0; i < nTags; i++) {
×
871
        cJSON* member = cJSON_CreateObject();
×
872
        RAW_NULL_CHECK(member);
×
873
        RAW_FALSE_CHECK(tmqAddJsonArrayItem(tags, member));
×
874

875
        SMultiTagUpateVal* pTagVal = taosArrayGet(vAlterTbReq.pMultiTag, i);
×
876
        cJSON*             tagName = cJSON_CreateString(pTagVal->tagName);
×
877
        RAW_NULL_CHECK(tagName);
×
878
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(member, "colName", tagName));
×
879

880
        if (pTagVal->tagType == TSDB_DATA_TYPE_JSON) {
×
881
          uError("processAlterTable isJson false");
×
882
          goto end;
×
883
        }
884
        bool isNull = pTagVal->isNull;
×
885
        if (!isNull) {
×
886
          int64_t bufSize = 0;
×
887
          if (pTagVal->tagType == TSDB_DATA_TYPE_VARBINARY) {
×
888
            bufSize = pTagVal->nTagVal * 2 + 2 + 3;
×
889
          } else {
890
            bufSize = pTagVal->nTagVal + 3;
×
891
          }
892
          char* buf = taosMemoryCalloc(bufSize, 1);
×
893
          RAW_NULL_CHECK(buf);
×
894
          if (dataConverToStr(buf, bufSize, pTagVal->tagType, pTagVal->pTagVal, pTagVal->nTagVal, NULL) !=
×
895
              TSDB_CODE_SUCCESS) {
896
            taosMemoryFree(buf);
×
897
            goto end;
×
898
          }
899
          cJSON* colValue = cJSON_CreateString(buf);
×
900
          taosMemoryFree(buf);
×
901
          RAW_NULL_CHECK(colValue);
×
902
          RAW_FALSE_CHECK(tmqAddJsonObjectItem(member, "colValue", colValue));
×
903
        }
904
        cJSON* isNullCJson = cJSON_CreateBool(isNull);
×
905
        RAW_NULL_CHECK(isNullCJson);
×
906
        RAW_FALSE_CHECK(tmqAddJsonObjectItem(member, "colValueNull", isNullCJson));
×
907
      }
908
      break;
×
909
    }
910

911
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS: {
×
912
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
×
913
      RAW_NULL_CHECK(colName);
×
914
      RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "colName", colName));
×
915
      RAW_RETURN_CHECK(setCompressOption(json, vAlterTbReq.compress));
×
916
      break;
×
917
    }
918
    default:
180✔
919
      break;
180✔
920
  }
921

922
end:
1,134✔
923
  uDebug("alter table return");
1,134✔
924
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
1,134✔
925
    taosArrayDestroy(vAlterTbReq.pMultiTag);
×
926
  }
927
  tDecoderClear(&decoder);
1,134✔
928
  *pJson = json;
1,134✔
929
}
930

931
static void processDropSTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
306✔
932
  if (pJson == NULL || metaRsp == NULL) {
306✔
933
    uError("invalid parameter in %s", __func__);
×
934
    return;
×
935
  }
936
  SDecoder     decoder = {0};
306✔
937
  SVDropStbReq req = {0};
306✔
938
  cJSON*       json = NULL;
306✔
939
  int32_t      code = 0;
306✔
940

941
  uDebug("processDropSTable data:%p", metaRsp);
306✔
942

943
  // decode
944
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
306✔
945
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
306✔
946
  tDecoderInit(&decoder, data, len);
306✔
947
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
306✔
948
    uError("tDecodeSVDropStbReq failed");
×
949
    goto end;
×
950
  }
951

952
  json = cJSON_CreateObject();
306✔
953
  RAW_NULL_CHECK(json);
306✔
954
  cJSON* type = cJSON_CreateString("drop");
306✔
955
  RAW_NULL_CHECK(type);
306✔
956
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "type", type));
306✔
957
  cJSON* tableType = cJSON_CreateString("super");
306✔
958
  RAW_NULL_CHECK(tableType);
306✔
959
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableType", tableType));
306✔
960
  cJSON* tableName = cJSON_CreateString(req.name);
306✔
961
  RAW_NULL_CHECK(tableName);
306✔
962
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableName", tableName));
306✔
963

964
end:
306✔
965
  uDebug("processDropSTable return");
306✔
966
  tDecoderClear(&decoder);
306✔
967
  *pJson = json;
306✔
968
}
969
static void processDeleteTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
126✔
970
  if (pJson == NULL || metaRsp == NULL) {
126✔
971
    uError("invalid parameter in %s", __func__);
×
972
    return;
×
973
  }
974
  SDeleteRes req = {0};
126✔
975
  SDecoder   coder = {0};
126✔
976
  cJSON*     json = NULL;
126✔
977
  int32_t    code = 0;
126✔
978

979
  uDebug("processDeleteTable data:%p", metaRsp);
126✔
980
  // decode and process req
981
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
126✔
982
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
126✔
983

984
  tDecoderInit(&coder, data, len);
126✔
985
  if (tDecodeDeleteRes(&coder, &req) < 0) {
126✔
986
    uError("tDecodeDeleteRes failed");
×
987
    goto end;
×
988
  }
989

990
  //  getTbName(req.tableFName);
991
  char sql[256] = {0};
126✔
992
  (void)snprintf(sql, sizeof(sql), "delete from `%s` where `%s` >= %" PRId64 " and `%s` <= %" PRId64, req.tableFName,
126✔
993
                 req.tsColName, req.skey, req.tsColName, req.ekey);
994

995
  json = cJSON_CreateObject();
126✔
996
  RAW_NULL_CHECK(json);
126✔
997
  cJSON* type = cJSON_CreateString("delete");
126✔
998
  RAW_NULL_CHECK(type);
126✔
999
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "type", type));
126✔
1000
  cJSON* sqlJson = cJSON_CreateString(sql);
126✔
1001
  RAW_NULL_CHECK(sqlJson);
126✔
1002
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "sql", sqlJson));
126✔
1003

1004
end:
126✔
1005
  uDebug("processDeleteTable return");
126✔
1006
  tDecoderClear(&coder);
126✔
1007
  *pJson = json;
126✔
1008
}
1009

1010
static void processDropTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
207✔
1011
  if (pJson == NULL || metaRsp == NULL) {
207✔
1012
    uError("invalid parameter in %s", __func__);
×
1013
    return;
×
1014
  }
1015
  SDecoder         decoder = {0};
207✔
1016
  SVDropTbBatchReq req = {0};
207✔
1017
  cJSON*           json = NULL;
207✔
1018
  int32_t          code = 0;
207✔
1019

1020
  uDebug("processDropTable data:%p", metaRsp);
207✔
1021
  // decode
1022
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
207✔
1023
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
207✔
1024
  tDecoderInit(&decoder, data, len);
207✔
1025
  if (tDecodeSVDropTbBatchReq(&decoder, &req) < 0) {
207✔
1026
    uError("tDecodeSVDropTbBatchReq failed");
×
1027
    goto end;
×
1028
  }
1029

1030
  json = cJSON_CreateObject();
207✔
1031
  RAW_NULL_CHECK(json);
207✔
1032
  cJSON* type = cJSON_CreateString("drop");
207✔
1033
  RAW_NULL_CHECK(type);
207✔
1034
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "type", type));
207✔
1035
  cJSON* tableNameList = cJSON_CreateArray();
207✔
1036
  RAW_NULL_CHECK(tableNameList);
207✔
1037
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(json, "tableNameList", tableNameList));
207✔
1038

1039
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
468✔
1040
    SVDropTbReq* pDropTbReq = req.pReqs + iReq;
261✔
1041
    cJSON*       tableName = cJSON_CreateString(pDropTbReq->name);
261✔
1042
    RAW_NULL_CHECK(tableName);
261✔
1043
    RAW_FALSE_CHECK(tmqAddJsonArrayItem(tableNameList, tableName));
261✔
1044
  }
1045

1046
end:
207✔
1047
  uDebug("processDropTable return");
207✔
1048
  tDecoderClear(&decoder);
207✔
1049
  *pJson = json;
207✔
1050
}
1051

1052
static int32_t taosCreateStb(TAOS* taos, void* meta, uint32_t metaLen) {
4,493✔
1053
  if (taos == NULL || meta == NULL) {
4,493✔
1054
    uError("invalid parameter in %s", __func__);
×
1055
    return TSDB_CODE_INVALID_PARA;
×
1056
  }
1057
  SVCreateStbReq req = {0};
4,493✔
1058
  SDecoder       coder = {0};
4,493✔
1059
  SMCreateStbReq pReq = {0};
4,493✔
1060
  int32_t        code = TSDB_CODE_SUCCESS;
4,493✔
1061
  SRequestObj*   pRequest = NULL;
4,493✔
1062

1063
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
4,493✔
1064
  uDebug(LOG_ID_TAG " create stable, meta:%p, metaLen:%d", LOG_ID_VALUE, meta, metaLen);
4,493✔
1065
  pRequest->syncQuery = true;
4,493✔
1066
  if (!pRequest->pDb) {
4,493✔
1067
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1068
    goto end;
×
1069
  }
1070
  // decode and process req
1071
  void*    data = POINTER_SHIFT(meta, sizeof(SMsgHead));
4,493✔
1072
  uint32_t len = metaLen - sizeof(SMsgHead);
4,493✔
1073
  tDecoderInit(&coder, data, len);
4,493✔
1074
  if (tDecodeSVCreateStbReq(&coder, &req) < 0) {
4,493✔
1075
    code = TSDB_CODE_INVALID_PARA;
×
1076
    goto end;
×
1077
  }
1078

1079
  int8_t           createDefaultCompress = 0;
4,493✔
1080
  SColCmprWrapper* p = &req.colCmpr;
4,493✔
1081
  if (p->nCols == 0) {
4,493✔
1082
    createDefaultCompress = 1;
×
1083
  }
1084
  // build create stable
1085
  pReq.pColumns = taosArrayInit(req.schemaRow.nCols, sizeof(SFieldWithOptions));
4,493✔
1086
  RAW_NULL_CHECK(pReq.pColumns);
4,493✔
1087
  for (int32_t i = 0; i < req.schemaRow.nCols; i++) {
31,725✔
1088
    SSchema*          pSchema = req.schemaRow.pSchema + i;
27,232✔
1089
    SFieldWithOptions field = {.type = pSchema->type, .flags = pSchema->flags, .bytes = pSchema->bytes};
27,232✔
1090
    tstrncpy(field.name, pSchema->name, TSDB_COL_NAME_LEN);
27,232✔
1091

1092
    if (createDefaultCompress) {
27,232✔
1093
      field.compress = createDefaultColCmprByType(pSchema->type);
×
1094
    } else {
1095
      SColCmpr* pCmp = &req.colCmpr.pColCmpr[i];
27,232✔
1096
      field.compress = pCmp->alg;
27,232✔
1097
    }
1098
    if (req.pExtSchemas) field.typeMod = req.pExtSchemas[i].typeMod;
27,232✔
1099
    RAW_NULL_CHECK(taosArrayPush(pReq.pColumns, &field));
54,464✔
1100
  }
1101
  pReq.pTags = taosArrayInit(req.schemaTag.nCols, sizeof(SField));
4,493✔
1102
  RAW_NULL_CHECK(pReq.pTags);
4,493✔
1103
  for (int32_t i = 0; i < req.schemaTag.nCols; i++) {
16,738✔
1104
    SSchema* pSchema = req.schemaTag.pSchema + i;
12,245✔
1105
    SField   field = {.type = pSchema->type, .flags = pSchema->flags, .bytes = pSchema->bytes};
12,245✔
1106
    tstrncpy(field.name, pSchema->name, TSDB_COL_NAME_LEN);
12,245✔
1107
    RAW_NULL_CHECK(taosArrayPush(pReq.pTags, &field));
24,490✔
1108
  }
1109

1110
  pReq.colVer = req.schemaRow.version;
4,493✔
1111
  pReq.tagVer = req.schemaTag.version;
4,493✔
1112
  pReq.numOfColumns = req.schemaRow.nCols;
4,493✔
1113
  pReq.numOfTags = req.schemaTag.nCols;
4,493✔
1114
  pReq.commentLen = -1;
4,493✔
1115
  pReq.suid = processSuid(req.suid, pRequest->pDb);
4,493✔
1116
  pReq.source = TD_REQ_FROM_TAOX;
4,493✔
1117
  pReq.igExists = true;
4,493✔
1118

1119
  uDebug(LOG_ID_TAG " create stable name:%s suid:%" PRId64 " processSuid:%" PRId64, LOG_ID_VALUE, req.name, req.suid,
4,493✔
1120
         pReq.suid);
1121
  STscObj* pTscObj = pRequest->pTscObj;
4,493✔
1122
  SName    tableName = {0};
4,493✔
1123
  toName(pTscObj->acctId, pRequest->pDb, req.name, &tableName);
4,493✔
1124
  RAW_RETURN_CHECK(tNameExtractFullName(&tableName, pReq.name));
4,493✔
1125
  SCmdMsgInfo pCmdMsg = {0};
4,493✔
1126
  pCmdMsg.epSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
4,493✔
1127
  pCmdMsg.msgType = TDMT_MND_CREATE_STB;
4,493✔
1128
  pCmdMsg.msgLen = tSerializeSMCreateStbReq(NULL, 0, &pReq);
4,493✔
1129
  if (pCmdMsg.msgLen <= 0) {
4,493✔
1130
    code = TSDB_CODE_INVALID_PARA;
×
1131
    goto end;
×
1132
  }
1133
  pCmdMsg.pMsg = taosMemoryMalloc(pCmdMsg.msgLen);
4,493✔
1134
  RAW_NULL_CHECK(pCmdMsg.pMsg);
4,493✔
1135
  if (tSerializeSMCreateStbReq(pCmdMsg.pMsg, pCmdMsg.msgLen, &pReq) <= 0) {
4,493✔
1136
    code = TSDB_CODE_INVALID_PARA;
×
1137
    taosMemoryFree(pCmdMsg.pMsg);
×
1138
    goto end;
×
1139
  }
1140

1141
  SQuery pQuery = {0};
4,493✔
1142
  pQuery.execMode = QUERY_EXEC_MODE_RPC;
4,493✔
1143
  pQuery.pCmdMsg = &pCmdMsg;
4,493✔
1144
  pQuery.msgType = pQuery.pCmdMsg->msgType;
4,493✔
1145
  pQuery.stableQuery = true;
4,493✔
1146

1147
  launchQueryImpl(pRequest, &pQuery, true, NULL);  // ignore, because return value is pRequest
4,493✔
1148

1149
  taosMemoryFree(pCmdMsg.pMsg);
4,493✔
1150

1151
  if (pRequest->code == TSDB_CODE_SUCCESS) {
4,493✔
1152
    SCatalog* pCatalog = NULL;
4,493✔
1153
    RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
4,493✔
1154
    RAW_RETURN_CHECK(catalogRemoveTableMeta(pCatalog, &tableName));
4,493✔
1155
  }
1156

1157
  code = pRequest->code;
4,493✔
1158

1159
end:
4,493✔
1160
  uDebug(LOG_ID_TAG " create stable return, msg:%s", LOG_ID_VALUE, tstrerror(code));
4,493✔
1161
  destroyRequest(pRequest);
4,493✔
1162
  tFreeSMCreateStbReq(&pReq);
4,493✔
1163
  tDecoderClear(&coder);
4,493✔
1164
  return code;
4,493✔
1165
}
1166

1167
static int32_t taosDropStb(TAOS* taos, void* meta, uint32_t metaLen) {
306✔
1168
  if (taos == NULL || meta == NULL) {
306✔
1169
    uError("invalid parameter in %s", __func__);
×
1170
    return TSDB_CODE_INVALID_PARA;
×
1171
  }
1172
  SVDropStbReq req = {0};
306✔
1173
  SDecoder     coder = {0};
306✔
1174
  SMDropStbReq pReq = {0};
306✔
1175
  int32_t      code = TSDB_CODE_SUCCESS;
306✔
1176
  SRequestObj* pRequest = NULL;
306✔
1177

1178
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
306✔
1179
  uDebug(LOG_ID_TAG " drop stable, meta:%p, metaLen:%d", LOG_ID_VALUE, meta, metaLen);
306✔
1180
  pRequest->syncQuery = true;
306✔
1181
  if (!pRequest->pDb) {
306✔
1182
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1183
    goto end;
×
1184
  }
1185
  // decode and process req
1186
  void*    data = POINTER_SHIFT(meta, sizeof(SMsgHead));
306✔
1187
  uint32_t len = metaLen - sizeof(SMsgHead);
306✔
1188
  tDecoderInit(&coder, data, len);
306✔
1189
  if (tDecodeSVDropStbReq(&coder, &req) < 0) {
306✔
1190
    code = TSDB_CODE_INVALID_PARA;
×
1191
    goto end;
×
1192
  }
1193

1194
  SCatalog* pCatalog = NULL;
306✔
1195
  RAW_RETURN_CHECK(catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog));
306✔
1196
  SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
306✔
1197
                           .requestId = pRequest->requestId,
306✔
1198
                           .requestObjRefId = pRequest->self,
306✔
1199
                           .mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp)};
306✔
1200
  SName            pName = {0};
306✔
1201
  toName(pRequest->pTscObj->acctId, pRequest->pDb, req.name, &pName);
306✔
1202
  STableMeta* pTableMeta = NULL;
306✔
1203
  code = catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta);
306✔
1204
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
306✔
1205
    code = TSDB_CODE_SUCCESS;
66✔
1206
    taosMemoryFreeClear(pTableMeta);
66✔
1207
    goto end;
66✔
1208
  }
1209
  if (code != TSDB_CODE_SUCCESS) {
240✔
1210
    goto end;
×
1211
  }
1212
  pReq.suid = pTableMeta->uid;
240✔
1213
  taosMemoryFreeClear(pTableMeta);
240✔
1214

1215
  // build drop stable
1216
  pReq.igNotExists = true;
240✔
1217
  pReq.source = TD_REQ_FROM_TAOX;
240✔
1218
  //  pReq.suid = processSuid(req.suid, pRequest->pDb);
1219

1220
  uDebug(LOG_ID_TAG " drop stable name:%s suid:%" PRId64 " new suid:%" PRId64, LOG_ID_VALUE, req.name, req.suid,
240✔
1221
         pReq.suid);
1222
  STscObj* pTscObj = pRequest->pTscObj;
240✔
1223
  SName    tableName = {0};
240✔
1224
  toName(pTscObj->acctId, pRequest->pDb, req.name, &tableName);
240✔
1225
  if (tNameExtractFullName(&tableName, pReq.name) != 0) {
240✔
1226
    code = TSDB_CODE_INVALID_PARA;
×
1227
    goto end;
×
1228
  }
1229

1230
  SCmdMsgInfo pCmdMsg = {0};
240✔
1231
  pCmdMsg.epSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
240✔
1232
  pCmdMsg.msgType = TDMT_MND_DROP_STB;
240✔
1233
  pCmdMsg.msgLen = tSerializeSMDropStbReq(NULL, 0, &pReq);
240✔
1234
  if (pCmdMsg.msgLen <= 0) {
240✔
1235
    code = TSDB_CODE_INVALID_PARA;
×
1236
    goto end;
×
1237
  }
1238
  pCmdMsg.pMsg = taosMemoryMalloc(pCmdMsg.msgLen);
240✔
1239
  RAW_NULL_CHECK(pCmdMsg.pMsg);
240✔
1240
  if (tSerializeSMDropStbReq(pCmdMsg.pMsg, pCmdMsg.msgLen, &pReq) <= 0) {
240✔
1241
    code = TSDB_CODE_INVALID_PARA;
×
1242
    taosMemoryFree(pCmdMsg.pMsg);
×
1243
    goto end;
×
1244
  }
1245

1246
  SQuery pQuery = {0};
240✔
1247
  pQuery.execMode = QUERY_EXEC_MODE_RPC;
240✔
1248
  pQuery.pCmdMsg = &pCmdMsg;
240✔
1249
  pQuery.msgType = pQuery.pCmdMsg->msgType;
240✔
1250
  pQuery.stableQuery = true;
240✔
1251

1252
  launchQueryImpl(pRequest, &pQuery, true, NULL);  // ignore, because return value is pRequest
240✔
1253
  taosMemoryFree(pCmdMsg.pMsg);
240✔
1254
  if (pRequest->code == TSDB_CODE_SUCCESS) {
240✔
1255
    // ignore the error code
1256
    RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
240✔
1257
    RAW_RETURN_CHECK(catalogRemoveTableMeta(pCatalog, &tableName));
240✔
1258
  }
1259

1260
  code = pRequest->code;
240✔
1261

1262
end:
306✔
1263
  uDebug(LOG_ID_TAG " drop stable return, msg:%s", LOG_ID_VALUE, tstrerror(code));
306✔
1264
  destroyRequest(pRequest);
306✔
1265
  tDecoderClear(&coder);
306✔
1266
  return code;
306✔
1267
}
1268

1269
typedef struct SVgroupCreateTableBatch {
1270
  SVCreateTbBatchReq req;
1271
  SVgroupInfo        info;
1272
  char               dbName[TSDB_DB_NAME_LEN];
1273
} SVgroupCreateTableBatch;
1274

1275
static void destroyCreateTbReqBatch(void* data) {
4,843✔
1276
  if (data == NULL) {
4,843✔
1277
    uError("invalid parameter in %s", __func__);
×
1278
    return;
×
1279
  }
1280
  SVgroupCreateTableBatch* pTbBatch = (SVgroupCreateTableBatch*)data;
4,843✔
1281
  taosArrayDestroy(pTbBatch->req.pArray);
4,843✔
1282
}
1283

1284
static int32_t taosCreateTable(TAOS* taos, void* meta, uint32_t metaLen) {
4,770✔
1285
  if (taos == NULL || meta == NULL) {
4,770✔
1286
    uError("invalid parameter in %s", __func__);
×
1287
    return TSDB_CODE_INVALID_PARA;
×
1288
  }
1289
  SVCreateTbBatchReq req = {0};
4,770✔
1290
  SDecoder           coder = {0};
4,770✔
1291
  int32_t            code = TSDB_CODE_SUCCESS;
4,770✔
1292
  SRequestObj*       pRequest = NULL;
4,770✔
1293
  SQuery*            pQuery = NULL;
4,770✔
1294
  SHashObj*          pVgroupHashmap = NULL;
4,770✔
1295
  SArray*            pTagList = taosArrayInit(0, POINTER_BYTES);
4,770✔
1296
  RAW_NULL_CHECK(pTagList);
4,770✔
1297
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
4,770✔
1298
  uDebug(LOG_ID_TAG " create table, meta:%p, metaLen:%d", LOG_ID_VALUE, meta, metaLen);
4,770✔
1299

1300
  pRequest->syncQuery = true;
4,770✔
1301
  if (!pRequest->pDb) {
4,770✔
1302
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1303
    goto end;
×
1304
  }
1305
  // decode and process req
1306
  void*    data = POINTER_SHIFT(meta, sizeof(SMsgHead));
4,770✔
1307
  uint32_t len = metaLen - sizeof(SMsgHead);
4,770✔
1308
  tDecoderInit(&coder, data, len);
4,770✔
1309
  if (tDecodeSVCreateTbBatchReq(&coder, &req) < 0) {
4,770✔
1310
    code = TSDB_CODE_INVALID_PARA;
×
1311
    goto end;
×
1312
  }
1313

1314
  STscObj* pTscObj = pRequest->pTscObj;
4,770✔
1315

1316
  SVCreateTbReq* pCreateReq = NULL;
4,770✔
1317
  SCatalog*      pCatalog = NULL;
4,770✔
1318
  RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
4,770✔
1319
  pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
4,770✔
1320
  RAW_NULL_CHECK(pVgroupHashmap);
4,770✔
1321
  taosHashSetFreeFp(pVgroupHashmap, destroyCreateTbReqBatch);
4,770✔
1322

1323
  SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
4,770✔
1324
                           .requestId = pRequest->requestId,
4,770✔
1325
                           .requestObjRefId = pRequest->self,
4,770✔
1326
                           .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
4,770✔
1327

1328
  pRequest->tableList = taosArrayInit(req.nReqs, sizeof(SName));
4,770✔
1329
  RAW_NULL_CHECK(pRequest->tableList);
4,770✔
1330
  // loop to create table
1331
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
10,169✔
1332
    pCreateReq = req.pReqs + iReq;
5,399✔
1333

1334
    SVgroupInfo pInfo = {0};
5,399✔
1335
    SName       pName = {0};
5,399✔
1336
    toName(pTscObj->acctId, pRequest->pDb, pCreateReq->name, &pName);
5,399✔
1337
    code = catalogGetTableHashVgroup(pCatalog, &conn, &pName, &pInfo);
5,399✔
1338
    if (code != TSDB_CODE_SUCCESS) {
5,399✔
1339
      goto end;
×
1340
    }
1341

1342
    pCreateReq->flags |= TD_CREATE_IF_NOT_EXISTS;
5,399✔
1343
    // change tag cid to new cid
1344
    if (pCreateReq->type == TSDB_CHILD_TABLE) {
5,399✔
1345
      STableMeta* pTableMeta = NULL;
4,697✔
1346
      SName       sName = {0};
4,697✔
1347
      tb_uid_t    oldSuid = pCreateReq->ctb.suid;
4,697✔
1348
      //      pCreateReq->ctb.suid = processSuid(pCreateReq->ctb.suid, pRequest->pDb);
1349
      toName(pTscObj->acctId, pRequest->pDb, pCreateReq->ctb.stbName, &sName);
4,697✔
1350
      code = catalogGetTableMeta(pCatalog, &conn, &sName, &pTableMeta);
4,697✔
1351
      if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
4,697✔
1352
        code = TSDB_CODE_SUCCESS;
×
1353
        taosMemoryFreeClear(pTableMeta);
×
1354
        continue;
×
1355
      }
1356

1357
      if (code != TSDB_CODE_SUCCESS) {
4,697✔
1358
        goto end;
×
1359
      }
1360
      pCreateReq->ctb.suid = pTableMeta->uid;
4,697✔
1361

1362
      SArray* pTagVals = NULL;
4,697✔
1363
      code = tTagToValArray((STag*)pCreateReq->ctb.pTag, &pTagVals);
4,697✔
1364
      if (code != TSDB_CODE_SUCCESS) {
4,697✔
1365
        taosMemoryFreeClear(pTableMeta);
×
1366
        goto end;
×
1367
      }
1368

1369
      bool rebuildTag = false;
4,697✔
1370
      for (int32_t i = 0; i < taosArrayGetSize(pCreateReq->ctb.tagName); i++) {
14,574✔
1371
        char* tName = taosArrayGet(pCreateReq->ctb.tagName, i);
9,877✔
1372
        if (tName == NULL) {
9,877✔
1373
          continue;
×
1374
        }
1375
        for (int32_t j = pTableMeta->tableInfo.numOfColumns;
9,877✔
1376
             j < pTableMeta->tableInfo.numOfColumns + pTableMeta->tableInfo.numOfTags; j++) {
38,867✔
1377
          SSchema* tag = &pTableMeta->schema[j];
28,990✔
1378
          if (strcmp(tag->name, tName) == 0 && tag->type != TSDB_DATA_TYPE_JSON) {
28,990✔
1379
            STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, i);
9,318✔
1380
            if (pTagVal) {
9,318✔
1381
              if (pTagVal->cid != tag->colId) {
9,318✔
1382
                pTagVal->cid = tag->colId;
776✔
1383
                rebuildTag = true;
776✔
1384
              }
1385
            } else {
1386
              uError("create tb invalid data %s, size:%d index:%d cid:%d", pCreateReq->name,
×
1387
                     (int)taosArrayGetSize(pTagVals), i, tag->colId);
1388
            }
1389
          }
1390
        }
1391
      }
1392
      taosMemoryFreeClear(pTableMeta);
4,697✔
1393
      if (rebuildTag) {
4,697✔
1394
        STag* ppTag = NULL;
490✔
1395
        code = tTagNew(pTagVals, 1, false, &ppTag);
490✔
1396
        taosArrayDestroy(pTagVals);
490✔
1397
        pTagVals = NULL;
490✔
1398
        if (code != TSDB_CODE_SUCCESS) {
490✔
1399
          goto end;
×
1400
        }
1401
        if (NULL == taosArrayPush(pTagList, &ppTag)) {
490✔
1402
          tTagFree(ppTag);
×
1403
          goto end;
×
1404
        }
1405
        pCreateReq->ctb.pTag = (uint8_t*)ppTag;
490✔
1406
      }
1407
      taosArrayDestroy(pTagVals);
4,697✔
1408
    }
1409
    RAW_NULL_CHECK(taosArrayPush(pRequest->tableList, &pName));
10,798✔
1410

1411
    SVgroupCreateTableBatch* pTableBatch = taosHashGet(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId));
5,399✔
1412
    if (pTableBatch == NULL) {
5,399✔
1413
      SVgroupCreateTableBatch tBatch = {0};
4,843✔
1414
      tBatch.info = pInfo;
4,843✔
1415
      tstrncpy(tBatch.dbName, pRequest->pDb, TSDB_DB_NAME_LEN);
4,843✔
1416

1417
      tBatch.req.pArray = taosArrayInit(4, sizeof(struct SVCreateTbReq));
4,843✔
1418
      RAW_NULL_CHECK(tBatch.req.pArray);
4,843✔
1419
      RAW_NULL_CHECK(taosArrayPush(tBatch.req.pArray, pCreateReq));
9,686✔
1420
      tBatch.req.source = TD_REQ_FROM_TAOX;
4,843✔
1421
      RAW_RETURN_CHECK(taosHashPut(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId), &tBatch, sizeof(tBatch)));
4,843✔
1422
    } else {  // add to the correct vgroup
1423
      RAW_NULL_CHECK(taosArrayPush(pTableBatch->req.pArray, pCreateReq));
1,112✔
1424
    }
1425
  }
1426

1427
  if (taosHashGetSize(pVgroupHashmap) == 0) {
4,770✔
1428
    goto end;
×
1429
  }
1430
  SArray* pBufArray = NULL;
4,770✔
1431
  RAW_RETURN_CHECK(serializeVgroupsCreateTableBatch(pVgroupHashmap, &pBufArray));
4,770✔
1432
  pQuery = NULL;
4,770✔
1433
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode**)&pQuery);
4,770✔
1434
  if (TSDB_CODE_SUCCESS != code) goto end;
4,770✔
1435
  pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
4,770✔
1436
  pQuery->msgType = TDMT_VND_CREATE_TABLE;
4,770✔
1437
  pQuery->stableQuery = false;
4,770✔
1438
  code = nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT, &pQuery->pRoot);
4,770✔
1439
  if (TSDB_CODE_SUCCESS != code) goto end;
4,770✔
1440
  RAW_NULL_CHECK(pQuery->pRoot);
4,770✔
1441

1442
  RAW_RETURN_CHECK(rewriteToVnodeModifyOpStmt(pQuery, pBufArray));
4,770✔
1443

1444
  launchQueryImpl(pRequest, pQuery, true, NULL);
4,770✔
1445
  if (pRequest->code == TSDB_CODE_SUCCESS) {
4,770✔
1446
    RAW_RETURN_CHECK(removeMeta(pTscObj, pRequest->tableList, false));
4,770✔
1447
  }
1448

1449
  code = pRequest->code;
4,770✔
1450

1451
end:
4,770✔
1452
  uDebug(LOG_ID_TAG " create table return, msg:%s", LOG_ID_VALUE, tstrerror(code));
4,770✔
1453
  tDeleteSVCreateTbBatchReq(&req);
4,770✔
1454

1455
  taosHashCleanup(pVgroupHashmap);
4,770✔
1456
  destroyRequest(pRequest);
4,770✔
1457
  tDecoderClear(&coder);
4,770✔
1458
  qDestroyQuery(pQuery);
4,770✔
1459
  taosArrayDestroyP(pTagList, NULL);
4,770✔
1460
  return code;
4,770✔
1461
}
1462

1463
typedef struct SVgroupDropTableBatch {
1464
  SVDropTbBatchReq req;
1465
  SVgroupInfo      info;
1466
  char             dbName[TSDB_DB_NAME_LEN];
1467
} SVgroupDropTableBatch;
1468

1469
static void destroyDropTbReqBatch(void* data) {
141✔
1470
  if (data == NULL) {
141✔
1471
    uError("invalid parameter in %s", __func__);
×
1472
    return;
×
1473
  }
1474
  SVgroupDropTableBatch* pTbBatch = (SVgroupDropTableBatch*)data;
141✔
1475
  taosArrayDestroy(pTbBatch->req.pArray);
141✔
1476
}
1477

1478
static int32_t taosDropTable(TAOS* taos, void* meta, uint32_t metaLen) {
207✔
1479
  if (taos == NULL || meta == NULL) {
207✔
1480
    uError("invalid parameter in %s", __func__);
×
1481
    return TSDB_CODE_INVALID_PARA;
×
1482
  }
1483
  SVDropTbBatchReq req = {0};
207✔
1484
  SDecoder         coder = {0};
207✔
1485
  int32_t          code = TSDB_CODE_SUCCESS;
207✔
1486
  SRequestObj*     pRequest = NULL;
207✔
1487
  SQuery*          pQuery = NULL;
207✔
1488
  SHashObj*        pVgroupHashmap = NULL;
207✔
1489

1490
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
207✔
1491
  uDebug(LOG_ID_TAG " drop table, meta:%p, len:%d", LOG_ID_VALUE, meta, metaLen);
207✔
1492

1493
  pRequest->syncQuery = true;
207✔
1494
  if (!pRequest->pDb) {
207✔
1495
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1496
    goto end;
×
1497
  }
1498
  // decode and process req
1499
  void*    data = POINTER_SHIFT(meta, sizeof(SMsgHead));
207✔
1500
  uint32_t len = metaLen - sizeof(SMsgHead);
207✔
1501
  tDecoderInit(&coder, data, len);
207✔
1502
  if (tDecodeSVDropTbBatchReq(&coder, &req) < 0) {
207✔
1503
    code = TSDB_CODE_INVALID_PARA;
×
1504
    goto end;
×
1505
  }
1506

1507
  STscObj* pTscObj = pRequest->pTscObj;
207✔
1508

1509
  SVDropTbReq* pDropReq = NULL;
207✔
1510
  SCatalog*    pCatalog = NULL;
207✔
1511
  RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
207✔
1512

1513
  pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
207✔
1514
  RAW_NULL_CHECK(pVgroupHashmap);
207✔
1515
  taosHashSetFreeFp(pVgroupHashmap, destroyDropTbReqBatch);
207✔
1516

1517
  SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
207✔
1518
                           .requestId = pRequest->requestId,
207✔
1519
                           .requestObjRefId = pRequest->self,
207✔
1520
                           .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
207✔
1521
  pRequest->tableList = taosArrayInit(req.nReqs, sizeof(SName));
207✔
1522
  RAW_NULL_CHECK(pRequest->tableList);
207✔
1523
  // loop to create table
1524
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
468✔
1525
    pDropReq = req.pReqs + iReq;
261✔
1526
    pDropReq->igNotExists = true;
261✔
1527
    //    pDropReq->suid = processSuid(pDropReq->suid, pRequest->pDb);
1528

1529
    SVgroupInfo pInfo = {0};
261✔
1530
    SName       pName = {0};
261✔
1531
    toName(pTscObj->acctId, pRequest->pDb, pDropReq->name, &pName);
261✔
1532
    RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &pInfo));
261✔
1533

1534
    STableMeta* pTableMeta = NULL;
261✔
1535
    code = catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta);
261✔
1536
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
261✔
1537
      code = TSDB_CODE_SUCCESS;
66✔
1538
      taosMemoryFreeClear(pTableMeta);
66✔
1539
      continue;
66✔
1540
    }
1541
    if (code != TSDB_CODE_SUCCESS) {
195✔
1542
      goto end;
×
1543
    }
1544
    tb_uid_t oldSuid = pDropReq->suid;
195✔
1545
    pDropReq->suid = pTableMeta->suid;
195✔
1546
    taosMemoryFreeClear(pTableMeta);
195✔
1547
    uDebug(LOG_ID_TAG " drop table name:%s suid:%" PRId64 " new suid:%" PRId64, LOG_ID_VALUE, pDropReq->name, oldSuid,
195✔
1548
           pDropReq->suid);
1549

1550
    RAW_NULL_CHECK(taosArrayPush(pRequest->tableList, &pName));
390✔
1551
    SVgroupDropTableBatch* pTableBatch = taosHashGet(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId));
195✔
1552
    if (pTableBatch == NULL) {
195✔
1553
      SVgroupDropTableBatch tBatch = {0};
141✔
1554
      tBatch.info = pInfo;
141✔
1555
      tBatch.req.pArray = taosArrayInit(TARRAY_MIN_SIZE, sizeof(SVDropTbReq));
141✔
1556
      RAW_NULL_CHECK(tBatch.req.pArray);
141✔
1557
      RAW_NULL_CHECK(taosArrayPush(tBatch.req.pArray, pDropReq));
282✔
1558
      RAW_RETURN_CHECK(taosHashPut(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId), &tBatch, sizeof(tBatch)));
141✔
1559
    } else {  // add to the correct vgroup
1560
      RAW_NULL_CHECK(taosArrayPush(pTableBatch->req.pArray, pDropReq));
108✔
1561
    }
1562
  }
1563

1564
  if (taosHashGetSize(pVgroupHashmap) == 0) {
207✔
1565
    goto end;
66✔
1566
  }
1567
  SArray* pBufArray = NULL;
141✔
1568
  RAW_RETURN_CHECK(serializeVgroupsDropTableBatch(pVgroupHashmap, &pBufArray));
141✔
1569
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode**)&pQuery);
141✔
1570
  if (TSDB_CODE_SUCCESS != code) goto end;
141✔
1571
  pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
141✔
1572
  pQuery->msgType = TDMT_VND_DROP_TABLE;
141✔
1573
  pQuery->stableQuery = false;
141✔
1574
  pQuery->pRoot = NULL;
141✔
1575
  code = nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT, &pQuery->pRoot);
141✔
1576
  if (TSDB_CODE_SUCCESS != code) goto end;
141✔
1577
  RAW_RETURN_CHECK(rewriteToVnodeModifyOpStmt(pQuery, pBufArray));
141✔
1578

1579
  launchQueryImpl(pRequest, pQuery, true, NULL);
141✔
1580
  if (pRequest->code == TSDB_CODE_SUCCESS) {
141✔
1581
    RAW_RETURN_CHECK(removeMeta(pTscObj, pRequest->tableList, false));
141✔
1582
  }
1583
  code = pRequest->code;
141✔
1584

1585
end:
207✔
1586
  uDebug(LOG_ID_TAG " drop table return, msg:%s", LOG_ID_VALUE, tstrerror(code));
207✔
1587
  taosHashCleanup(pVgroupHashmap);
207✔
1588
  destroyRequest(pRequest);
207✔
1589
  tDecoderClear(&coder);
207✔
1590
  qDestroyQuery(pQuery);
207✔
1591
  return code;
207✔
1592
}
1593

1594
static int32_t taosDeleteData(TAOS* taos, void* meta, uint32_t metaLen) {
126✔
1595
  if (taos == NULL || meta == NULL) {
126✔
1596
    uError("invalid parameter in %s", __func__);
×
1597
    return TSDB_CODE_INVALID_PARA;
×
1598
  }
1599
  SDeleteRes req = {0};
126✔
1600
  SDecoder   coder = {0};
126✔
1601
  char       sql[256] = {0};
126✔
1602
  int32_t    code = TSDB_CODE_SUCCESS;
126✔
1603

1604
  uDebug("connId:0x%" PRIx64 " delete data, meta:%p, len:%d", *(int64_t*)taos, meta, metaLen);
126✔
1605

1606
  // decode and process req
1607
  void*    data = POINTER_SHIFT(meta, sizeof(SMsgHead));
126✔
1608
  uint32_t len = metaLen - sizeof(SMsgHead);
126✔
1609
  tDecoderInit(&coder, data, len);
126✔
1610
  if (tDecodeDeleteRes(&coder, &req) < 0) {
126✔
1611
    code = TSDB_CODE_INVALID_PARA;
×
1612
    goto end;
×
1613
  }
1614

1615
  (void)snprintf(sql, sizeof(sql), "delete from `%s` where `%s` >= %" PRId64 " and `%s` <= %" PRId64, req.tableFName,
126✔
1616
                 req.tsColName, req.skey, req.tsColName, req.ekey);
1617

1618
  TAOS_RES* res = taosQueryImpl(taos, sql, false, TD_REQ_FROM_TAOX);
126✔
1619
  RAW_NULL_CHECK(res);
126✔
1620
  SRequestObj* pRequest = (SRequestObj*)res;
126✔
1621
  code = pRequest->code;
126✔
1622
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_PAR_GET_META_ERROR) {
126✔
1623
    code = TSDB_CODE_SUCCESS;
33✔
1624
  }
1625
  taos_free_result(res);
126✔
1626

1627
end:
126✔
1628
  uDebug("connId:0x%" PRIx64 " delete data sql:%s, code:%s", *(int64_t*)taos, sql, tstrerror(code));
126✔
1629
  tDecoderClear(&coder);
126✔
1630
  return code;
126✔
1631
}
1632

1633
static int32_t taosAlterTable(TAOS* taos, void* meta, uint32_t metaLen) {
1,080✔
1634
  if (taos == NULL || meta == NULL) {
1,080✔
1635
    uError("invalid parameter in %s", __func__);
×
1636
    return TSDB_CODE_INVALID_PARA;
×
1637
  }
1638
  SVAlterTbReq   req = {0};
1,080✔
1639
  SDecoder       dcoder = {0};
1,080✔
1640
  int32_t        code = TSDB_CODE_SUCCESS;
1,080✔
1641
  SRequestObj*   pRequest = NULL;
1,080✔
1642
  SQuery*        pQuery = NULL;
1,080✔
1643
  SArray*        pArray = NULL;
1,080✔
1644
  SVgDataBlocks* pVgData = NULL;
1,080✔
1645

1646
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
1,080✔
1647
  uDebug(LOG_ID_TAG " alter table, meta:%p, len:%d", LOG_ID_VALUE, meta, metaLen);
1,080✔
1648
  pRequest->syncQuery = true;
1,080✔
1649
  if (!pRequest->pDb) {
1,080✔
1650
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1651
    goto end;
×
1652
  }
1653
  // decode and process req
1654
  void*    data = POINTER_SHIFT(meta, sizeof(SMsgHead));
1,080✔
1655
  uint32_t len = metaLen - sizeof(SMsgHead);
1,080✔
1656
  tDecoderInit(&dcoder, data, len);
1,080✔
1657
  if (tDecodeSVAlterTbReq(&dcoder, &req) < 0) {
1,080✔
1658
    code = TSDB_CODE_INVALID_PARA;
×
1659
    goto end;
×
1660
  }
1661

1662
  // do not deal TSDB_ALTER_TABLE_UPDATE_OPTIONS
1663
  if (req.action == TSDB_ALTER_TABLE_UPDATE_OPTIONS) {
1,080✔
1664
    goto end;
180✔
1665
  }
1666

1667
  STscObj*  pTscObj = pRequest->pTscObj;
900✔
1668
  SCatalog* pCatalog = NULL;
900✔
1669
  RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
900✔
1670
  SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
900✔
1671
                           .requestId = pRequest->requestId,
900✔
1672
                           .requestObjRefId = pRequest->self,
900✔
1673
                           .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
900✔
1674

1675
  SVgroupInfo pInfo = {0};
900✔
1676
  SName       pName = {0};
900✔
1677
  toName(pTscObj->acctId, pRequest->pDb, req.tbName, &pName);
900✔
1678
  RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &pInfo));
900✔
1679
  pArray = taosArrayInit(1, sizeof(void*));
900✔
1680
  RAW_NULL_CHECK(pArray);
900✔
1681

1682
  pVgData = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
900✔
1683
  RAW_NULL_CHECK(pVgData);
900✔
1684
  pVgData->vg = pInfo;
900✔
1685

1686
  int tlen = 0;
900✔
1687
  req.source = TD_REQ_FROM_TAOX;
900✔
1688
  tEncodeSize(tEncodeSVAlterTbReq, &req, tlen, code);
900✔
1689
  if (code != 0) {
900✔
1690
    code = terrno;
×
1691
    goto end;
×
1692
  }
1693
  tlen += sizeof(SMsgHead);
900✔
1694
  void* pMsg = taosMemoryMalloc(tlen);
900✔
1695
  RAW_NULL_CHECK(pMsg);
900✔
1696
  ((SMsgHead*)pMsg)->vgId = htonl(pInfo.vgId);
900✔
1697
  ((SMsgHead*)pMsg)->contLen = htonl(tlen);
900✔
1698
  void*    pBuf = POINTER_SHIFT(pMsg, sizeof(SMsgHead));
900✔
1699
  SEncoder coder = {0};
900✔
1700
  tEncoderInit(&coder, pBuf, tlen - sizeof(SMsgHead));
900✔
1701
  code = tEncodeSVAlterTbReq(&coder, &req);
900✔
1702
  if (code != 0) {
900✔
1703
    tEncoderClear(&coder);
×
1704
    code = terrno;
×
1705
    goto end;
×
1706
  }
1707
  tEncoderClear(&coder);
900✔
1708

1709
  pVgData->pData = pMsg;
900✔
1710
  pVgData->size = tlen;
900✔
1711

1712
  pVgData->numOfTables = 1;
900✔
1713
  RAW_NULL_CHECK(taosArrayPush(pArray, &pVgData));
900✔
1714

1715
  pQuery = NULL;
900✔
1716
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode**)&pQuery);
900✔
1717
  if (NULL == pQuery) goto end;
900✔
1718
  pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
900✔
1719
  pQuery->msgType = TDMT_VND_ALTER_TABLE;
900✔
1720
  pQuery->stableQuery = false;
900✔
1721
  pQuery->pRoot = NULL;
900✔
1722
  code = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, &pQuery->pRoot);
900✔
1723
  if (TSDB_CODE_SUCCESS != code) goto end;
900✔
1724
  RAW_RETURN_CHECK(rewriteToVnodeModifyOpStmt(pQuery, pArray));
900✔
1725

1726
  launchQueryImpl(pRequest, pQuery, true, NULL);
900✔
1727

1728
  pVgData = NULL;
900✔
1729
  pArray = NULL;
900✔
1730
  code = pRequest->code;
900✔
1731
  if (code == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
900✔
1732
    code = TSDB_CODE_SUCCESS;
33✔
1733
  }
1734

1735
  if (pRequest->code == TSDB_CODE_SUCCESS) {
900✔
1736
    SExecResult* pRes = &pRequest->body.resInfo.execRes;
867✔
1737
    if (pRes->res != NULL) {
867✔
1738
      code = handleAlterTbExecRes(pRes->res, pCatalog);
720✔
1739
    }
1740
  }
1741
end:
1,080✔
1742
  uDebug(LOG_ID_TAG " alter table return, meta:%p, len:%d, msg:%s", LOG_ID_VALUE, meta, metaLen, tstrerror(code));
1,080✔
1743
  taosArrayDestroy(pArray);
1,080✔
1744
  if (pVgData) taosMemoryFreeClear(pVgData->pData);
1,080✔
1745
  taosMemoryFreeClear(pVgData);
1,080✔
1746
  destroyRequest(pRequest);
1,080✔
1747
  tDecoderClear(&dcoder);
1,080✔
1748
  qDestroyQuery(pQuery);
1,080✔
1749
  return code;
1,080✔
1750
}
1751

1752
int taos_write_raw_block_with_fields(TAOS* taos, int rows, char* pData, const char* tbname, TAOS_FIELD* fields,
31✔
1753
                                     int numFields) {
1754
  return taos_write_raw_block_with_fields_with_reqid(taos, rows, pData, tbname, fields, numFields, 0);
31✔
1755
}
1756

1757
int taos_write_raw_block_with_fields_with_reqid(TAOS* taos, int rows, char* pData, const char* tbname,
31✔
1758
                                                TAOS_FIELD* fields, int numFields, int64_t reqid) {
1759
  if (taos == NULL || pData == NULL || tbname == NULL) {
31✔
1760
    uError("invalid parameter in %s", __func__);
×
1761
    return TSDB_CODE_INVALID_PARA;
×
1762
  }
1763
  int32_t     code = TSDB_CODE_SUCCESS;
31✔
1764
  STableMeta* pTableMeta = NULL;
31✔
1765
  SQuery*     pQuery = NULL;
31✔
1766
  SHashObj*   pVgHash = NULL;
31✔
1767

1768
  SRequestObj* pRequest = NULL;
31✔
1769
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, reqid));
31✔
1770

1771
  uDebug(LOG_ID_TAG " write raw block with field, rows:%d, pData:%p, tbname:%s, fields:%p, numFields:%d", LOG_ID_VALUE,
31✔
1772
         rows, pData, tbname, fields, numFields);
1773

1774
  pRequest->syncQuery = true;
31✔
1775
  if (!pRequest->pDb) {
31✔
1776
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1777
    goto end;
×
1778
  }
1779

1780
  SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}};
31✔
1781
  tstrncpy(pName.dbname, pRequest->pDb, sizeof(pName.dbname));
31✔
1782
  tstrncpy(pName.tname, tbname, sizeof(pName.tname));
31✔
1783

1784
  struct SCatalog* pCatalog = NULL;
31✔
1785
  RAW_RETURN_CHECK(catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog));
31✔
1786

1787
  SRequestConnInfo conn = {0};
31✔
1788
  conn.pTrans = pRequest->pTscObj->pAppInfo->pTransporter;
31✔
1789
  conn.requestId = pRequest->requestId;
31✔
1790
  conn.requestObjRefId = pRequest->self;
31✔
1791
  conn.mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp);
31✔
1792

1793
  SVgroupInfo vgData = {0};
31✔
1794
  RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &vgData));
31✔
1795
  RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta));
31✔
1796
  RAW_RETURN_CHECK(smlInitHandle(&pQuery));
31✔
1797
  pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
31✔
1798
  RAW_NULL_CHECK(pVgHash);
31✔
1799
  RAW_RETURN_CHECK(
31✔
1800
      taosHashPut(pVgHash, (const char*)&vgData.vgId, sizeof(vgData.vgId), (char*)&vgData, sizeof(vgData)));
1801
  RAW_RETURN_CHECK(rawBlockBindData(pQuery, pTableMeta, pData, NULL, fields, numFields, false, NULL, 0, false));
31✔
1802
  RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash));
31✔
1803

1804
  launchQueryImpl(pRequest, pQuery, true, NULL);
31✔
1805
  code = pRequest->code;
31✔
1806

1807
end:
31✔
1808
  uDebug(LOG_ID_TAG " write raw block with field return, msg:%s", LOG_ID_VALUE, tstrerror(code));
31✔
1809
  taosMemoryFreeClear(pTableMeta);
31✔
1810
  qDestroyQuery(pQuery);
31✔
1811
  destroyRequest(pRequest);
31✔
1812
  taosHashCleanup(pVgHash);
31✔
1813
  return code;
31✔
1814
}
1815

1816
int taos_write_raw_block(TAOS* taos, int rows, char* pData, const char* tbname) {
217✔
1817
  return taos_write_raw_block_with_reqid(taos, rows, pData, tbname, 0);
217✔
1818
}
1819

1820
int taos_write_raw_block_with_reqid(TAOS* taos, int rows, char* pData, const char* tbname, int64_t reqid) {
217✔
1821
  if (taos == NULL || pData == NULL || tbname == NULL) {
217✔
1822
    return TSDB_CODE_INVALID_PARA;
×
1823
  }
1824
  int32_t     code = TSDB_CODE_SUCCESS;
217✔
1825
  STableMeta* pTableMeta = NULL;
217✔
1826
  SQuery*     pQuery = NULL;
217✔
1827
  SHashObj*   pVgHash = NULL;
217✔
1828

1829
  SRequestObj* pRequest = NULL;
217✔
1830
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, reqid));
217✔
1831

1832
  uDebug(LOG_ID_TAG " write raw block, rows:%d, pData:%p, tbname:%s", LOG_ID_VALUE, rows, pData, tbname);
217✔
1833

1834
  pRequest->syncQuery = true;
217✔
1835
  if (!pRequest->pDb) {
217✔
1836
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1837
    goto end;
×
1838
  }
1839

1840
  SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}};
217✔
1841
  tstrncpy(pName.dbname, pRequest->pDb, sizeof(pName.dbname));
217✔
1842
  tstrncpy(pName.tname, tbname, sizeof(pName.tname));
217✔
1843

1844
  struct SCatalog* pCatalog = NULL;
217✔
1845
  RAW_RETURN_CHECK(catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog));
217✔
1846

1847
  SRequestConnInfo conn = {0};
217✔
1848
  conn.pTrans = pRequest->pTscObj->pAppInfo->pTransporter;
217✔
1849
  conn.requestId = pRequest->requestId;
217✔
1850
  conn.requestObjRefId = pRequest->self;
217✔
1851
  conn.mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp);
217✔
1852

1853
  SVgroupInfo vgData = {0};
217✔
1854
  RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &vgData));
217✔
1855
  RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta));
217✔
1856
  RAW_RETURN_CHECK(smlInitHandle(&pQuery));
186✔
1857
  pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
186✔
1858
  RAW_NULL_CHECK(pVgHash);
186✔
1859
  RAW_RETURN_CHECK(
186✔
1860
      taosHashPut(pVgHash, (const char*)&vgData.vgId, sizeof(vgData.vgId), (char*)&vgData, sizeof(vgData)));
1861
  RAW_RETURN_CHECK(rawBlockBindData(pQuery, pTableMeta, pData, NULL, NULL, 0, false, NULL, 0, false));
186✔
1862
  RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash));
124✔
1863

1864
  launchQueryImpl(pRequest, pQuery, true, NULL);
124✔
1865
  code = pRequest->code;
124✔
1866

1867
end:
217✔
1868
  uDebug(LOG_ID_TAG " write raw block return, msg:%s", LOG_ID_VALUE, tstrerror(code));
217✔
1869
  taosMemoryFreeClear(pTableMeta);
217✔
1870
  qDestroyQuery(pQuery);
217✔
1871
  destroyRequest(pRequest);
217✔
1872
  taosHashCleanup(pVgHash);
217✔
1873
  return code;
217✔
1874
}
1875

1876
static void* getRawDataFromRes(void* pRetrieve) {
4,692✔
1877
  if (pRetrieve == NULL) {
4,692✔
1878
    uError("invalid parameter in %s", __func__);
×
1879
    return NULL;
×
1880
  }
1881
  void* rawData = NULL;
4,692✔
1882
  // deal with compatibility
1883
  if (*(int64_t*)pRetrieve == RETRIEVE_TABLE_RSP_VERSION) {
4,692✔
1884
    rawData = ((SRetrieveTableRsp*)pRetrieve)->data;
×
1885
  } else if (*(int64_t*)pRetrieve == RETRIEVE_TABLE_RSP_TMQ_VERSION ||
4,692✔
1886
             *(int64_t*)pRetrieve == RETRIEVE_TABLE_RSP_TMQ_RAW_VERSION) {
×
1887
    rawData = ((SRetrieveTableRspForTmq*)pRetrieve)->data;
4,692✔
1888
  }
1889
  return rawData;
4,692✔
1890
}
1891

1892
static int32_t buildCreateTbMap(SMqDataRsp* rsp, SHashObj* pHashObj) {
199✔
1893
  if (rsp == NULL || pHashObj == NULL) {
199✔
1894
    uError("invalid parameter in %s", __func__);
×
1895
    return TSDB_CODE_INVALID_PARA;
×
1896
  }
1897
  // find schema data info
1898
  int32_t       code = 0;
199✔
1899
  SVCreateTbReq pCreateReq = {0};
199✔
1900
  SDecoder      decoderTmp = {0};
199✔
1901

1902
  for (int j = 0; j < rsp->createTableNum; j++) {
703✔
1903
    void** dataTmp = taosArrayGet(rsp->createTableReq, j);
504✔
1904
    RAW_NULL_CHECK(dataTmp);
504✔
1905
    int32_t* lenTmp = taosArrayGet(rsp->createTableLen, j);
504✔
1906
    RAW_NULL_CHECK(lenTmp);
504✔
1907

1908
    tDecoderInit(&decoderTmp, *dataTmp, *lenTmp);
504✔
1909
    RAW_RETURN_CHECK(tDecodeSVCreateTbReq(&decoderTmp, &pCreateReq));
504✔
1910

1911
    if (pCreateReq.type != TSDB_CHILD_TABLE) {
504✔
1912
      code = TSDB_CODE_INVALID_MSG;
×
1913
      goto end;
×
1914
    }
1915
    if (taosHashGet(pHashObj, pCreateReq.name, strlen(pCreateReq.name)) == NULL) {
504✔
1916
      RAW_RETURN_CHECK(
504✔
1917
          taosHashPut(pHashObj, pCreateReq.name, strlen(pCreateReq.name), &pCreateReq, sizeof(SVCreateTbReq)));
1918
    } else {
1919
      tDestroySVCreateTbReq(&pCreateReq, TSDB_MSG_FLG_DECODE);
×
1920
      pCreateReq = (SVCreateTbReq){0};
×
1921
    }
1922

1923
    tDecoderClear(&decoderTmp);
504✔
1924
  }
1925
  return 0;
199✔
1926

1927
end:
×
1928
  tDecoderClear(&decoderTmp);
×
1929
  tDestroySVCreateTbReq(&pCreateReq, TSDB_MSG_FLG_DECODE);
×
1930
  return code;
×
1931
}
1932

1933
typedef enum {
1934
  WRITE_RAW_INIT_START = 0,
1935
  WRITE_RAW_INIT_OK,
1936
  WRITE_RAW_INIT_FAIL,
1937
} WRITE_RAW_INIT_STATUS;
1938

1939
static SHashObj* writeRawCache = NULL;
1940
static int8_t    initFlag = 0;
1941
static int8_t    initedFlag = WRITE_RAW_INIT_START;
1942

1943
typedef struct {
1944
  SHashObj* pVgHash;
1945
  SHashObj* pNameHash;
1946
  SHashObj* pMetaHash;
1947
} rawCacheInfo;
1948

1949
typedef struct {
1950
  SVgroupInfo vgInfo;
1951
  int64_t     uid;
1952
  int64_t     suid;
1953
} tbInfo;
1954

1955
static void tmqFreeMeta(void* data) {
1,365✔
1956
  if (data == NULL) {
1,365✔
1957
    uError("invalid parameter in %s", __func__);
×
1958
    return;
×
1959
  }
1960
  STableMeta* pTableMeta = *(STableMeta**)data;
1,365✔
1961
  taosMemoryFree(pTableMeta);
1,365✔
1962
}
1963

1964
static void freeRawCache(void* data) {
×
1965
  if (data == NULL) {
×
1966
    uError("invalid parameter in %s", __func__);
×
1967
    return;
×
1968
  }
1969
  rawCacheInfo* pRawCache = (rawCacheInfo*)data;
×
1970
  taosHashCleanup(pRawCache->pMetaHash);
×
1971
  taosHashCleanup(pRawCache->pNameHash);
×
1972
  taosHashCleanup(pRawCache->pVgHash);
×
1973
}
1974

1975
static int32_t initRawCacheHash() {
498✔
1976
  if (writeRawCache == NULL) {
498✔
1977
    writeRawCache = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
498✔
1978
    if (writeRawCache == NULL) {
498✔
1979
      return terrno;
×
1980
    }
1981
    taosHashSetFreeFp(writeRawCache, freeRawCache);
498✔
1982
  }
1983
  return 0;
498✔
1984
}
1985

1986
static bool needRefreshMeta(void* rawData, STableMeta* pTableMeta, SSchemaWrapper* pSW) {
551✔
1987
  if (rawData == NULL || pSW == NULL) {
551✔
1988
    return false;
×
1989
  }
1990
  if (pTableMeta == NULL) {
551✔
1991
    uError("invalid parameter in %s", __func__);
×
1992
    return false;
×
1993
  }
1994
  char* p = (char*)rawData;
551✔
1995
  // | version | total length | total rows | blankFill | total columns | flag seg| block group id | column schema | each
1996
  // column length |
1997
  p += sizeof(int32_t);
551✔
1998
  p += sizeof(int32_t);
551✔
1999
  p += sizeof(int32_t);
551✔
2000
  p += sizeof(int32_t);
551✔
2001
  p += sizeof(int32_t);
551✔
2002
  p += sizeof(uint64_t);
551✔
2003
  int8_t* fields = p;
551✔
2004

2005
  if (pSW->nCols != pTableMeta->tableInfo.numOfColumns) {
551✔
2006
    return true;
126✔
2007
  }
2008

2009
  for (int i = 0; i < pSW->nCols; i++) {
2,218✔
2010
    int j = 0;
1,793✔
2011
    for (; j < pTableMeta->tableInfo.numOfColumns; j++) {
4,715✔
2012
      SSchema*    pColSchema = &pTableMeta->schema[j];
4,715✔
2013
      SSchemaExt* pColExtSchema = &pTableMeta->schemaExt[j];
4,715✔
2014
      char*       fieldName = pSW->pSchema[i].name;
4,715✔
2015

2016
      if (strcmp(pColSchema->name, fieldName) == 0) {
4,715✔
2017
        if (checkSchema(pColSchema, pColExtSchema, fields, NULL, 0) != 0) {
1,793✔
2018
          return true;
×
2019
        }
2020
        break;
1,793✔
2021
      }
2022
    }
2023
    fields += sizeof(int8_t) + sizeof(int32_t);
1,793✔
2024

2025
    if (j == pTableMeta->tableInfo.numOfColumns) return true;
1,793✔
2026
  }
2027
  return false;
425✔
2028
}
2029

2030
static int32_t getRawCache(SHashObj** pVgHash, SHashObj** pNameHash, SHashObj** pMetaHash, void* key) {
1,707✔
2031
  if (pVgHash == NULL || pNameHash == NULL || pMetaHash == NULL || key == NULL) {
1,707✔
2032
    uError("invalid parameter in %s", __func__);
×
2033
    return TSDB_CODE_INVALID_PARA;
×
2034
  }
2035
  int32_t code = 0;
1,707✔
2036
  void*   cacheInfo = taosHashGet(writeRawCache, &key, POINTER_BYTES);
1,707✔
2037
  if (cacheInfo == NULL) {
1,707✔
2038
    *pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
1,707✔
2039
    RAW_NULL_CHECK(*pVgHash);
1,707✔
2040
    *pNameHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,707✔
2041
    RAW_NULL_CHECK(*pNameHash);
1,707✔
2042
    *pMetaHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
1,707✔
2043
    RAW_NULL_CHECK(*pMetaHash);
1,707✔
2044
    taosHashSetFreeFp(*pMetaHash, tmqFreeMeta);
1,707✔
2045
    rawCacheInfo info = {*pVgHash, *pNameHash, *pMetaHash};
1,707✔
2046
    RAW_RETURN_CHECK(taosHashPut(writeRawCache, &key, POINTER_BYTES, &info, sizeof(rawCacheInfo)));
1,707✔
2047
  } else {
2048
    rawCacheInfo* info = (rawCacheInfo*)cacheInfo;
×
2049
    *pVgHash = info->pVgHash;
×
2050
    *pNameHash = info->pNameHash;
×
2051
    *pMetaHash = info->pMetaHash;
×
2052
  }
2053

2054
  return 0;
1,707✔
2055
end:
×
2056
  taosHashCleanup(*pMetaHash);
×
2057
  taosHashCleanup(*pNameHash);
×
2058
  taosHashCleanup(*pVgHash);
×
2059
  return code;
×
2060
}
2061

2062
static int32_t buildRawRequest(TAOS* taos, SRequestObj** pRequest, SCatalog** pCatalog, SRequestConnInfo* conn) {
1,707✔
2063
  if (taos == NULL || pRequest == NULL || pCatalog == NULL || conn == NULL) {
1,707✔
2064
    uError("invalid parameter in %s", __func__);
×
2065
    return TSDB_CODE_INVALID_PARA;
×
2066
  }
2067
  int32_t code = 0;
1,707✔
2068
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, pRequest, 0));
1,707✔
2069
  (*pRequest)->syncQuery = true;
1,707✔
2070
  if (!(*pRequest)->pDb) {
1,707✔
2071
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
2072
    goto end;
×
2073
  }
2074

2075
  RAW_RETURN_CHECK(catalogGetHandle((*pRequest)->pTscObj->pAppInfo->clusterId, pCatalog));
1,707✔
2076
  conn->pTrans = (*pRequest)->pTscObj->pAppInfo->pTransporter;
1,707✔
2077
  conn->requestId = (*pRequest)->requestId;
1,707✔
2078
  conn->requestObjRefId = (*pRequest)->self;
1,707✔
2079
  conn->mgmtEps = getEpSet_s(&(*pRequest)->pTscObj->pAppInfo->mgmtEp);
1,707✔
2080

2081
end:
1,707✔
2082
  return code;
1,707✔
2083
}
2084

2085
typedef int32_t _raw_decode_func_(SDecoder* pDecoder, SMqDataRsp* pRsp);
2086
static int32_t  decodeRawData(SDecoder* decoder, void* data, uint32_t dataLen, _raw_decode_func_ func,
1,707✔
2087
                              SMqRspObj* rspObj) {
2088
  if (decoder == NULL || data == NULL || func == NULL || rspObj == NULL) {
1,707✔
2089
    uError("invalid parameter in %s", __func__);
×
2090
    return TSDB_CODE_INVALID_PARA;
×
2091
  }
2092
  int8_t dataVersion = *(int8_t*)data;
1,707✔
2093
  if (dataVersion >= MQ_DATA_RSP_VERSION) {
1,707✔
2094
    data = POINTER_SHIFT(data, sizeof(int8_t) + sizeof(int32_t));
1,707✔
2095
    if (dataLen < sizeof(int8_t) + sizeof(int32_t)) {
1,707✔
2096
      return TSDB_CODE_INVALID_PARA;
×
2097
    }
2098
    dataLen -= sizeof(int8_t) + sizeof(int32_t);
1,707✔
2099
  }
2100

2101
  rspObj->resIter = -1;
1,707✔
2102
  tDecoderInit(decoder, data, dataLen);
1,707✔
2103
  int32_t code = func(decoder, &rspObj->dataRsp);
1,707✔
2104
  if (code != 0) {
1,707✔
2105
    SET_ERROR_MSG("decode mq taosx data rsp failed");
×
2106
  }
2107
  return code;
1,707✔
2108
}
2109

2110
static int32_t processCacheMeta(SHashObj* pVgHash, SHashObj* pNameHash, SHashObj* pMetaHash,
4,692✔
2111
                                SVCreateTbReq* pCreateReqDst, SCatalog* pCatalog, SRequestConnInfo* conn, SName* pName,
2112
                                STableMeta** pMeta, SSchemaWrapper* pSW, void* rawData, int32_t retry) {
2113
  if (pVgHash == NULL || pNameHash == NULL || pMetaHash == NULL || pCatalog == NULL || conn == NULL || pName == NULL ||
4,692✔
2114
      pMeta == NULL) {
2115
    uError("invalid parameter in %s", __func__);
×
2116
    return TSDB_CODE_INVALID_PARA;
×
2117
  }
2118
  int32_t     code = 0;
4,692✔
2119
  STableMeta* pTableMeta = NULL;
4,692✔
2120
  tbInfo*     tmpInfo = (tbInfo*)taosHashGet(pNameHash, pName->tname, strlen(pName->tname));
4,692✔
2121
  if (tmpInfo == NULL || retry > 0) {
4,692✔
2122
    tbInfo info = {0};
4,141✔
2123

2124
    RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, conn, pName, &info.vgInfo));
4,141✔
2125
    if (pCreateReqDst && tmpInfo == NULL) {  // change stable name to get meta
4,141✔
2126
      tstrncpy(pName->tname, pCreateReqDst->ctb.stbName, TSDB_TABLE_NAME_LEN);
504✔
2127
    }
2128
    RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, conn, pName, &pTableMeta));
4,141✔
2129
    info.uid = pTableMeta->uid;
4,141✔
2130
    if (pTableMeta->tableType == TSDB_CHILD_TABLE) {
4,141✔
2131
      info.suid = pTableMeta->suid;
3,076✔
2132
    } else {
2133
      info.suid = pTableMeta->uid;
1,065✔
2134
    }
2135
    code = taosHashPut(pMetaHash, &info.suid, LONG_BYTES, &pTableMeta, POINTER_BYTES);
4,141✔
2136
    if (code != 0) {
4,141✔
2137
      taosMemoryFree(pTableMeta);
×
2138
      goto end;
×
2139
    }
2140
    uDebug("put table meta to hash1, suid:%" PRId64 ", metaHashSIze:%d, nameHashSize:%d, vgHashSize:%d", info.suid, taosHashGetSize(pMetaHash),
4,141✔
2141
           taosHashGetSize(pNameHash), taosHashGetSize(pVgHash));
2142
    if (pCreateReqDst) {
4,141✔
2143
      pTableMeta->vgId = info.vgInfo.vgId;
504✔
2144
      pTableMeta->uid = pCreateReqDst->uid;
504✔
2145
      pCreateReqDst->ctb.suid = pTableMeta->suid;
504✔
2146
    }
2147

2148
    RAW_RETURN_CHECK(taosHashPut(pNameHash, pName->tname, strlen(pName->tname), &info, sizeof(tbInfo)));
4,141✔
2149
    tmpInfo = (tbInfo*)taosHashGet(pNameHash, pName->tname, strlen(pName->tname));
4,141✔
2150
    RAW_RETURN_CHECK(
4,141✔
2151
        taosHashPut(pVgHash, &info.vgInfo.vgId, sizeof(info.vgInfo.vgId), &info.vgInfo, sizeof(SVgroupInfo)));
2152
  }
2153

2154
  if (pTableMeta == NULL || retry > 0) {
4,692✔
2155
    STableMeta** pTableMetaTmp = (STableMeta**)taosHashGet(pMetaHash, &tmpInfo->suid, LONG_BYTES);
551✔
2156
    if (pTableMetaTmp == NULL || retry > 0 || needRefreshMeta(rawData, *pTableMetaTmp, pSW)) {
551✔
2157
      RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, conn, pName, &pTableMeta));
126✔
2158
      code = taosHashPut(pMetaHash, &tmpInfo->suid, LONG_BYTES, &pTableMeta, POINTER_BYTES);
126✔
2159
      if (code != 0) {
126✔
2160
        taosMemoryFree(pTableMeta);
×
2161
        goto end;
×
2162
      }
2163
      uDebug("put table meta to hash2, suid:%" PRId64 ", metaHashSIze:%d, nameHashSize:%d, vgHashSize:%d", tmpInfo->suid, taosHashGetSize(pMetaHash),
126✔
2164
      taosHashGetSize(pNameHash), taosHashGetSize(pVgHash));
2165
    } else {
2166
      pTableMeta = *pTableMetaTmp;
425✔
2167
      pTableMeta->uid = tmpInfo->uid;
425✔
2168
      pTableMeta->vgId = tmpInfo->vgInfo.vgId;
425✔
2169
    }
2170
  }
2171
  *pMeta = pTableMeta;
4,692✔
2172

2173
end:
4,692✔
2174
  return code;
4,692✔
2175
}
2176

2177
static int32_t tmqWriteRawDataImpl(TAOS* taos, void* data, uint32_t dataLen) {
1,508✔
2178
  if (taos == NULL || data == NULL) {
1,508✔
2179
    uError("invalid parameter in %s", __func__);
×
2180
    return TSDB_CODE_INVALID_PARA;
×
2181
  }
2182
  int32_t   code = TSDB_CODE_SUCCESS;
1,508✔
2183
  SQuery*   pQuery = NULL;
1,508✔
2184
  SMqRspObj rspObj = {0};
1,508✔
2185
  SDecoder  decoder = {0};
1,508✔
2186

2187
  SRequestObj*     pRequest = NULL;
1,508✔
2188
  SCatalog*        pCatalog = NULL;
1,508✔
2189
  SRequestConnInfo conn = {0};
1,508✔
2190
  RAW_RETURN_CHECK(buildRawRequest(taos, &pRequest, &pCatalog, &conn));
1,508✔
2191
  uDebug(LOG_ID_TAG " write raw data, data:%p, dataLen:%d", LOG_ID_VALUE, data, dataLen);
1,508✔
2192
  RAW_RETURN_CHECK(decodeRawData(&decoder, data, dataLen, tDecodeMqDataRsp, &rspObj));
1,508✔
2193

2194
  SHashObj* pVgHash = NULL;
1,508✔
2195
  SHashObj* pNameHash = NULL;
1,508✔
2196
  SHashObj* pMetaHash = NULL;
1,508✔
2197
  RAW_RETURN_CHECK(getRawCache(&pVgHash, &pNameHash, &pMetaHash, taos));
1,508✔
2198
  int retry = 0;
1,508✔
2199
  while (1) {
2200
    RAW_RETURN_CHECK(smlInitHandle(&pQuery));
1,508✔
2201
    uDebug(LOG_ID_TAG " write raw meta data block num:%d", LOG_ID_VALUE, rspObj.dataRsp.blockNum);
1,508✔
2202
    while (++rspObj.resIter < rspObj.dataRsp.blockNum) {
5,318✔
2203
      if (!rspObj.dataRsp.withSchema) {
3,810✔
2204
        goto end;
×
2205
      }
2206

2207
      const char* tbName = (const char*)taosArrayGetP(rspObj.dataRsp.blockTbName, rspObj.resIter);
3,810✔
2208
      RAW_NULL_CHECK(tbName);
3,810✔
2209
      SSchemaWrapper* pSW = (SSchemaWrapper*)taosArrayGetP(rspObj.dataRsp.blockSchema, rspObj.resIter);
3,810✔
2210
      RAW_NULL_CHECK(pSW);
3,810✔
2211
      void* pRetrieve = taosArrayGetP(rspObj.dataRsp.blockData, rspObj.resIter);
3,810✔
2212
      RAW_NULL_CHECK(pRetrieve);
3,810✔
2213
      void* rawData = getRawDataFromRes(pRetrieve);
3,810✔
2214
      RAW_NULL_CHECK(rawData);
3,810✔
2215

2216
      uTrace(LOG_ID_TAG " write raw data block tbname:%s", LOG_ID_VALUE, tbName);
3,810✔
2217
      SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}};
3,810✔
2218
      tstrncpy(pName.dbname, pRequest->pDb, TSDB_DB_NAME_LEN);
3,810✔
2219
      tstrncpy(pName.tname, tbName, TSDB_TABLE_NAME_LEN);
3,810✔
2220

2221
      STableMeta* pTableMeta = NULL;
3,810✔
2222
      RAW_RETURN_CHECK(processCacheMeta(pVgHash, pNameHash, pMetaHash, NULL, pCatalog, &conn, &pName, &pTableMeta, pSW,
3,810✔
2223
                                        rawData, retry));
2224
      char err[ERR_MSG_LEN] = {0};
3,810✔
2225
      code = rawBlockBindData(pQuery, pTableMeta, rawData, NULL, pSW, pSW->nCols, true, err, ERR_MSG_LEN, true);
3,810✔
2226
      if (code != TSDB_CODE_SUCCESS) {
3,810✔
2227
        SET_ERROR_MSG("table:%s, err:%s", pName.tname, err);
×
2228
        goto end;
×
2229
      }
2230
    }
2231
    RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash));
1,508✔
2232
    launchQueryImpl(pRequest, pQuery, true, NULL);
1,508✔
2233
    code = pRequest->code;
1,508✔
2234

2235
    if (NEED_CLIENT_HANDLE_ERROR(code) && retry++ < 3) {
1,508✔
2236
      uInfo("write raw retry:%d/3 end code:%d, msg:%s", retry, code, tstrerror(code));
×
2237
      qDestroyQuery(pQuery);
×
2238
      pQuery = NULL;
×
2239
      rspObj.resIter = -1;
×
2240
      continue;
×
2241
    }
2242
    break;
1,508✔
2243
  }
2244

2245
end:
1,508✔
2246
  uDebug(LOG_ID_TAG " write raw data return, msg:%s", LOG_ID_VALUE, tstrerror(code));
1,508✔
2247
  tDeleteMqDataRsp(&rspObj.dataRsp);
1,508✔
2248
  tDecoderClear(&decoder);
1,508✔
2249
  qDestroyQuery(pQuery);
1,508✔
2250
  destroyRequest(pRequest);
1,508✔
2251
  return code;
1,508✔
2252
}
2253

2254
static int32_t tmqWriteRawMetaDataImpl(TAOS* taos, void* data, uint32_t dataLen) {
199✔
2255
  if (taos == NULL || data == NULL) {
199✔
2256
    uError("invalid parameter in %s", __func__);
×
2257
    return TSDB_CODE_INVALID_PARA;
×
2258
  }
2259
  int32_t   code = TSDB_CODE_SUCCESS;
199✔
2260
  SQuery*   pQuery = NULL;
199✔
2261
  SMqRspObj rspObj = {0};
199✔
2262
  SDecoder  decoder = {0};
199✔
2263
  SHashObj* pCreateTbHash = NULL;
199✔
2264

2265
  SRequestObj*     pRequest = NULL;
199✔
2266
  SCatalog*        pCatalog = NULL;
199✔
2267
  SRequestConnInfo conn = {0};
199✔
2268

2269
  RAW_RETURN_CHECK(buildRawRequest(taos, &pRequest, &pCatalog, &conn));
199✔
2270
  uDebug(LOG_ID_TAG " write raw metadata, data:%p, dataLen:%d", LOG_ID_VALUE, data, dataLen);
199✔
2271
  RAW_RETURN_CHECK(decodeRawData(&decoder, data, dataLen, tDecodeSTaosxRsp, &rspObj));
199✔
2272

2273
  pCreateTbHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
199✔
2274
  RAW_NULL_CHECK(pCreateTbHash);
199✔
2275
  RAW_RETURN_CHECK(buildCreateTbMap(&rspObj.dataRsp, pCreateTbHash));
199✔
2276

2277
  SHashObj* pVgHash = NULL;
199✔
2278
  SHashObj* pNameHash = NULL;
199✔
2279
  SHashObj* pMetaHash = NULL;
199✔
2280
  RAW_RETURN_CHECK(getRawCache(&pVgHash, &pNameHash, &pMetaHash, taos));
199✔
2281
  int retry = 0;
199✔
2282
  while (1) {
2283
    RAW_RETURN_CHECK(smlInitHandle(&pQuery));
199✔
2284
    uDebug(LOG_ID_TAG " write raw meta data block num:%d", LOG_ID_VALUE, rspObj.dataRsp.blockNum);
199✔
2285
    while (++rspObj.resIter < rspObj.dataRsp.blockNum) {
1,081✔
2286
      if (!rspObj.dataRsp.withSchema) {
882✔
2287
        goto end;
×
2288
      }
2289

2290
      const char* tbName = (const char*)taosArrayGetP(rspObj.dataRsp.blockTbName, rspObj.resIter);
882✔
2291
      RAW_NULL_CHECK(tbName);
882✔
2292
      SSchemaWrapper* pSW = (SSchemaWrapper*)taosArrayGetP(rspObj.dataRsp.blockSchema, rspObj.resIter);
882✔
2293
      RAW_NULL_CHECK(pSW);
882✔
2294
      void* pRetrieve = taosArrayGetP(rspObj.dataRsp.blockData, rspObj.resIter);
882✔
2295
      RAW_NULL_CHECK(pRetrieve);
882✔
2296
      void* rawData = getRawDataFromRes(pRetrieve);
882✔
2297
      RAW_NULL_CHECK(rawData);
882✔
2298

2299
      uTrace(LOG_ID_TAG " write raw data block tbname:%s", LOG_ID_VALUE, tbName);
882✔
2300
      SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}};
882✔
2301
      tstrncpy(pName.dbname, pRequest->pDb, TSDB_DB_NAME_LEN);
882✔
2302
      tstrncpy(pName.tname, tbName, TSDB_TABLE_NAME_LEN);
882✔
2303

2304
      // find schema data info
2305
      SVCreateTbReq* pCreateReqDst = (SVCreateTbReq*)taosHashGet(pCreateTbHash, pName.tname, strlen(pName.tname));
882✔
2306
      STableMeta*    pTableMeta = NULL;
882✔
2307
      RAW_RETURN_CHECK(processCacheMeta(pVgHash, pNameHash, pMetaHash, pCreateReqDst, pCatalog, &conn, &pName,
882✔
2308
                                        &pTableMeta, pSW, rawData, retry));
2309
      char err[ERR_MSG_LEN] = {0};
882✔
2310
      code =
2311
          rawBlockBindData(pQuery, pTableMeta, rawData, pCreateReqDst, pSW, pSW->nCols, true, err, ERR_MSG_LEN, true);
882✔
2312
      if (code != TSDB_CODE_SUCCESS) {
882✔
2313
        SET_ERROR_MSG("table:%s, err:%s", pName.tname, err);
×
2314
        goto end;
×
2315
      }
2316
    }
2317
    RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash));
199✔
2318
    launchQueryImpl(pRequest, pQuery, true, NULL);
199✔
2319
    code = pRequest->code;
199✔
2320

2321
    if (NEED_CLIENT_HANDLE_ERROR(code) && retry++ < 3) {
199✔
2322
      uInfo("write raw retry:%d/3 end code:%d, msg:%s", retry, code, tstrerror(code));
×
2323
      qDestroyQuery(pQuery);
×
2324
      pQuery = NULL;
×
2325
      rspObj.resIter = -1;
×
2326
      continue;
×
2327
    }
2328
    break;
199✔
2329
  }
2330

2331
end:
199✔
2332
  uDebug(LOG_ID_TAG " write raw metadata return, msg:%s", LOG_ID_VALUE, tstrerror(code));
199✔
2333
  tDeleteSTaosxRsp(&rspObj.dataRsp);
199✔
2334
  void* pIter = taosHashIterate(pCreateTbHash, NULL);
199✔
2335
  while (pIter) {
703✔
2336
    tDestroySVCreateTbReq(pIter, TSDB_MSG_FLG_DECODE);
504✔
2337
    pIter = taosHashIterate(pCreateTbHash, pIter);
504✔
2338
  }
2339
  taosHashCleanup(pCreateTbHash);
199✔
2340
  tDecoderClear(&decoder);
199✔
2341
  qDestroyQuery(pQuery);
199✔
2342
  destroyRequest(pRequest);
199✔
2343
  return code;
199✔
2344
}
2345

2346
static int32_t tmqWriteRawRawDataImpl(TAOS* taos, void* data, uint32_t dataLen) {
×
2347
  if (taos == NULL || data == NULL) {
×
2348
    uError("invalid parameter in %s", __func__);
×
2349
    return TSDB_CODE_INVALID_PARA;
×
2350
  }
2351
  int32_t   code = TSDB_CODE_SUCCESS;
×
2352
  SQuery*   pQuery = NULL;
×
2353
  SHashObj* pVgroupHash = NULL;
×
2354
  SMqRspObj rspObj = {0};
×
2355
  SDecoder  decoder = {0};
×
2356

2357
  SRequestObj*     pRequest = NULL;
×
2358
  SCatalog*        pCatalog = NULL;
×
2359
  SRequestConnInfo conn = {0};
×
2360

2361
  RAW_RETURN_CHECK(buildRawRequest(taos, &pRequest, &pCatalog, &conn));
×
2362
  uDebug(LOG_ID_TAG " write raw rawdata, data:%p, dataLen:%d", LOG_ID_VALUE, data, dataLen);
×
2363
  RAW_RETURN_CHECK(decodeRawData(&decoder, data, dataLen, tDecodeMqDataRsp, &rspObj));
×
2364

2365
  SHashObj* pVgHash = NULL;
×
2366
  SHashObj* pNameHash = NULL;
×
2367
  SHashObj* pMetaHash = NULL;
×
2368
  RAW_RETURN_CHECK(getRawCache(&pVgHash, &pNameHash, &pMetaHash, taos));
×
2369
  int retry = 0;
×
2370
  while (1) {
×
2371
    RAW_RETURN_CHECK(smlInitHandle(&pQuery));
×
2372
    uDebug(LOG_ID_TAG " write raw rawdata block num:%d", LOG_ID_VALUE, rspObj.dataRsp.blockNum);
×
2373
    SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)(pQuery)->pRoot;
×
2374
    pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
×
2375
    RAW_NULL_CHECK(pVgroupHash);
×
2376
    pStmt->pVgDataBlocks = taosArrayInit(8, POINTER_BYTES);
×
2377
    RAW_NULL_CHECK(pStmt->pVgDataBlocks);
×
2378

2379
    while (++rspObj.resIter < rspObj.dataRsp.blockNum) {
×
2380
      const char* tbName = (const char*)taosArrayGetP(rspObj.dataRsp.blockTbName, rspObj.resIter);
×
2381
      RAW_NULL_CHECK(tbName);
×
2382
      void* pRetrieve = taosArrayGetP(rspObj.dataRsp.blockData, rspObj.resIter);
×
2383
      RAW_NULL_CHECK(pRetrieve);
×
2384
      void* rawData = getRawDataFromRes(pRetrieve);
×
2385
      RAW_NULL_CHECK(rawData);
×
2386

2387
      uTrace(LOG_ID_TAG " write raw data block tbname:%s", LOG_ID_VALUE, tbName);
×
2388
      SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}};
×
2389
      tstrncpy(pName.dbname, pRequest->pDb, TSDB_DB_NAME_LEN);
×
2390
      tstrncpy(pName.tname, tbName, TSDB_TABLE_NAME_LEN);
×
2391

2392
      // find schema data info
2393
      STableMeta* pTableMeta = NULL;
×
2394
      RAW_RETURN_CHECK(processCacheMeta(pVgHash, pNameHash, pMetaHash, NULL, pCatalog, &conn, &pName, &pTableMeta, NULL,
×
2395
                                        NULL, retry));
2396
      char err[ERR_MSG_LEN] = {0};
×
2397
      code = rawBlockBindRawData(pVgroupHash, pStmt->pVgDataBlocks, pTableMeta, rawData);
×
2398
      if (code != TSDB_CODE_SUCCESS) {
×
2399
        SET_ERROR_MSG("table:%s, err:%s", pName.tname, err);
×
2400
        goto end;
×
2401
      }
2402
    }
2403
    taosHashCleanup(pVgroupHash);
×
2404
    pVgroupHash = NULL;
×
2405

2406
    RAW_RETURN_CHECK(smlBuildOutputRaw(pQuery, pVgHash));
×
2407
    launchQueryImpl(pRequest, pQuery, true, NULL);
×
2408
    code = pRequest->code;
×
2409

2410
    if (NEED_CLIENT_HANDLE_ERROR(code) && retry++ < 3) {
×
2411
      uInfo("write raw retry:%d/3 end code:%d, msg:%s", retry, code, tstrerror(code));
×
2412
      qDestroyQuery(pQuery);
×
2413
      pQuery = NULL;
×
2414
      rspObj.resIter = -1;
×
2415
      continue;
×
2416
    }
2417
    break;
×
2418
  }
2419

2420
end:
×
2421
  uDebug(LOG_ID_TAG " write raw rawdata return, msg:%s", LOG_ID_VALUE, tstrerror(code));
×
2422
  tDeleteMqDataRsp(&rspObj.dataRsp);
×
2423
  tDecoderClear(&decoder);
×
2424
  qDestroyQuery(pQuery);
×
2425
  taosHashCleanup(pVgroupHash);
×
2426
  destroyRequest(pRequest);
×
2427
  return code;
×
2428
}
2429

2430
static void processSimpleMeta(SMqMetaRsp* pMetaRsp, cJSON** meta) {
11,202✔
2431
  if (pMetaRsp == NULL || meta == NULL) {
11,202✔
2432
    uError("invalid parameter in %s", __func__);
×
2433
    return;
×
2434
  }
2435
  if (pMetaRsp->resMsgType == TDMT_VND_CREATE_STB) {
11,202✔
2436
    processCreateStb(pMetaRsp, meta);
2,917✔
2437
  } else if (pMetaRsp->resMsgType == TDMT_VND_ALTER_STB) {
8,285✔
2438
    processAlterStb(pMetaRsp, meta);
1,630✔
2439
  } else if (pMetaRsp->resMsgType == TDMT_VND_DROP_STB) {
6,655✔
2440
    processDropSTable(pMetaRsp, meta);
306✔
2441
  } else if (pMetaRsp->resMsgType == TDMT_VND_CREATE_TABLE) {
6,349✔
2442
    processCreateTable(pMetaRsp, meta);
4,882✔
2443
  } else if (pMetaRsp->resMsgType == TDMT_VND_ALTER_TABLE) {
1,467✔
2444
    processAlterTable(pMetaRsp, meta);
1,134✔
2445
  } else if (pMetaRsp->resMsgType == TDMT_VND_DROP_TABLE) {
333✔
2446
    processDropTable(pMetaRsp, meta);
207✔
2447
  } else if (pMetaRsp->resMsgType == TDMT_VND_DELETE) {
126✔
2448
    processDeleteTable(pMetaRsp, meta);
126✔
2449
  }
2450
}
2451

2452
static void processBatchMetaToJson(SMqBatchMetaRsp* pMsgRsp, char** string) {
612✔
2453
  if (pMsgRsp == NULL || string == NULL) {
612✔
2454
    uError("invalid parameter in %s", __func__);
×
2455
    return;
×
2456
  }
2457
  SDecoder        coder = {0};
612✔
2458
  SMqBatchMetaRsp rsp = {0};
612✔
2459
  int32_t         code = 0;
612✔
2460
  cJSON*          pJson = NULL;
612✔
2461
  tDecoderInit(&coder, pMsgRsp->pMetaBuff, pMsgRsp->metaBuffLen);
612✔
2462
  if (tDecodeMqBatchMetaRsp(&coder, &rsp) < 0) {
612✔
2463
    goto end;
×
2464
  }
2465

2466
  pJson = cJSON_CreateObject();
612✔
2467
  RAW_NULL_CHECK(pJson);
612✔
2468
  RAW_FALSE_CHECK(cJSON_AddStringToObject(pJson, "tmq_meta_version", TMQ_META_VERSION));
612✔
2469
  cJSON* pMetaArr = cJSON_CreateArray();
612✔
2470
  RAW_NULL_CHECK(pMetaArr);
612✔
2471
  RAW_FALSE_CHECK(tmqAddJsonObjectItem(pJson, "metas", pMetaArr));
612✔
2472

2473
  int32_t num = taosArrayGetSize(rsp.batchMetaReq);
612✔
2474
  for (int32_t i = 0; i < num; i++) {
5,095✔
2475
    int32_t* len = taosArrayGet(rsp.batchMetaLen, i);
4,483✔
2476
    RAW_NULL_CHECK(len);
4,483✔
2477
    void* tmpBuf = taosArrayGetP(rsp.batchMetaReq, i);
4,483✔
2478
    RAW_NULL_CHECK(tmpBuf);
4,483✔
2479
    SDecoder   metaCoder = {0};
4,483✔
2480
    SMqMetaRsp metaRsp = {0};
4,483✔
2481
    tDecoderInit(&metaCoder, POINTER_SHIFT(tmpBuf, sizeof(SMqRspHead)), *len - sizeof(SMqRspHead));
4,483✔
2482
    if (tDecodeMqMetaRsp(&metaCoder, &metaRsp) < 0) {
4,483✔
2483
      goto end;
×
2484
    }
2485
    cJSON* pItem = NULL;
4,483✔
2486
    processSimpleMeta(&metaRsp, &pItem);
4,483✔
2487
    tDeleteMqMetaRsp(&metaRsp);
4,483✔
2488
    RAW_FALSE_CHECK(tmqAddJsonArrayItem(pMetaArr, pItem));
4,483✔
2489
  }
2490

2491
  tDeleteMqBatchMetaRsp(&rsp);
612✔
2492
  char* fullStr = cJSON_PrintUnformatted(pJson);
612✔
2493
  cJSON_Delete(pJson);
612✔
2494
  *string = fullStr;
612✔
2495
  return;
612✔
2496

2497
end:
×
2498
  cJSON_Delete(pJson);
×
2499
  tDeleteMqBatchMetaRsp(&rsp);
×
2500
}
2501

2502
char* tmq_get_json_meta(TAOS_RES* res) {
7,584✔
2503
  if (res == NULL) {
7,584✔
2504
    uError("invalid parameter in %s", __func__);
×
2505
    return NULL;
×
2506
  }
2507
  uDebug("tmq_get_json_meta res:%p", res);
7,584✔
2508
  if (!TD_RES_TMQ_META(res) && !TD_RES_TMQ_METADATA(res) && !TD_RES_TMQ_BATCH_META(res)) {
7,584✔
2509
    return NULL;
×
2510
  }
2511

2512
  char*      string = NULL;
7,584✔
2513
  SMqRspObj* rspObj = (SMqRspObj*)res;
7,584✔
2514
  if (TD_RES_TMQ_METADATA(res)) {
7,584✔
2515
    processAutoCreateTable(&rspObj->dataRsp, &string);
253✔
2516
  } else if (TD_RES_TMQ_BATCH_META(res)) {
7,331✔
2517
    processBatchMetaToJson(&rspObj->batchMetaRsp, &string);
612✔
2518
  } else if (TD_RES_TMQ_META(res)) {
6,719✔
2519
    cJSON* pJson = NULL;
6,719✔
2520
    processSimpleMeta(&rspObj->metaRsp, &pJson);
6,719✔
2521
    string = cJSON_PrintUnformatted(pJson);
6,719✔
2522
    cJSON_Delete(pJson);
6,719✔
2523
  } else {
2524
    uError("tmq_get_json_meta res:%d, invalid type", *(int8_t*)res);
×
2525
  }
2526

2527
  uDebug("tmq_get_json_meta string:%s", string);
7,584✔
2528
  return string;
7,584✔
2529
}
2530

2531
void tmq_free_json_meta(char* jsonMeta) { taosMemoryFreeClear(jsonMeta); }
9,198✔
2532

2533
static int32_t getOffSetLen(const SMqDataRsp* pRsp) {
3,099✔
2534
  if (pRsp == NULL) {
3,099✔
2535
    uError("invalid parameter in %s", __func__);
×
2536
    return TSDB_CODE_INVALID_PARA;
×
2537
  }
2538
  SEncoder coder = {0};
3,099✔
2539
  tEncoderInit(&coder, NULL, 0);
3,099✔
2540
  if (tEncodeSTqOffsetVal(&coder, &pRsp->reqOffset) < 0) return -1;
3,099✔
2541
  if (tEncodeSTqOffsetVal(&coder, &pRsp->rspOffset) < 0) return -1;
3,099✔
2542
  int32_t pos = coder.pos;
3,099✔
2543
  tEncoderClear(&coder);
3,099✔
2544
  return pos;
3,099✔
2545
}
2546

2547
typedef int32_t __encode_func__(SEncoder* pEncoder, const SMqDataRsp* pRsp);
2548
static int32_t  encodeMqDataRsp(__encode_func__* encodeFunc, SMqDataRsp* rspObj, tmq_raw_data* raw) {
3,099✔
2549
  if (raw == NULL || encodeFunc == NULL || rspObj == NULL) {
3,099✔
2550
    uError("invalid parameter in %s", __func__);
×
2551
    return TSDB_CODE_INVALID_PARA;
×
2552
  }
2553
  uint32_t len = 0;
3,099✔
2554
  int32_t  code = 0;
3,099✔
2555
  SEncoder encoder = {0};
3,099✔
2556
  void*    buf = NULL;
3,099✔
2557
  tEncodeSize(encodeFunc, rspObj, len, code);
3,099✔
2558
  if (code < 0) {
3,099✔
2559
    code = TSDB_CODE_INVALID_MSG;
×
2560
    goto FAILED;
×
2561
  }
2562
  len += sizeof(int8_t) + sizeof(int32_t);
3,099✔
2563
  buf = taosMemoryCalloc(1, len);
3,099✔
2564
  if (buf == NULL) {
3,099✔
2565
    code = terrno;
×
2566
    goto FAILED;
×
2567
  }
2568
  tEncoderInit(&encoder, buf, len);
3,099✔
2569
  if (tEncodeI8(&encoder, MQ_DATA_RSP_VERSION) < 0) {
3,099✔
2570
    code = TSDB_CODE_INVALID_MSG;
×
2571
    goto FAILED;
×
2572
  }
2573
  int32_t offsetLen = getOffSetLen(rspObj);
3,099✔
2574
  if (offsetLen <= 0) {
3,099✔
2575
    code = TSDB_CODE_INVALID_MSG;
×
2576
    goto FAILED;
×
2577
  }
2578
  if (tEncodeI32(&encoder, offsetLen) < 0) {
3,099✔
2579
    code = TSDB_CODE_INVALID_MSG;
×
2580
    goto FAILED;
×
2581
  }
2582
  if (encodeFunc(&encoder, rspObj) < 0) {
3,099✔
2583
    code = TSDB_CODE_INVALID_MSG;
×
2584
    goto FAILED;
×
2585
  }
2586
  tEncoderClear(&encoder);
3,099✔
2587

2588
  raw->raw = buf;
3,099✔
2589
  raw->raw_len = len;
3,099✔
2590
  return code;
3,099✔
2591
FAILED:
×
2592
  tEncoderClear(&encoder);
×
2593
  taosMemoryFree(buf);
×
2594
  return code;
×
2595
}
2596

2597
int32_t tmq_get_raw(TAOS_RES* res, tmq_raw_data* raw) {
11,119✔
2598
  if (raw == NULL || res == NULL) {
11,119✔
2599
    uError("invalid parameter in %s", __func__);
×
2600
    return TSDB_CODE_INVALID_PARA;
×
2601
  }
2602
  *raw = (tmq_raw_data){0};
11,119✔
2603
  SMqRspObj* rspObj = ((SMqRspObj*)res);
11,119✔
2604
  if (TD_RES_TMQ_META(res)) {
11,119✔
2605
    raw->raw = rspObj->metaRsp.metaRsp;
7,408✔
2606
    raw->raw_len = rspObj->metaRsp.metaRspLen >= 0 ? rspObj->metaRsp.metaRspLen : 0;
7,408✔
2607
    raw->raw_type = rspObj->metaRsp.resMsgType;
7,408✔
2608
    uDebug("tmq get raw type meta:%p", raw);
7,408✔
2609
  } else if (TD_RES_TMQ(res)) {
3,711✔
2610
    int32_t code = encodeMqDataRsp(tEncodeMqDataRsp, &rspObj->dataRsp, raw);
2,900✔
2611
    if (code != 0) {
2,900✔
2612
      uError("tmq get raw type error:%d", terrno);
×
2613
      return code;
×
2614
    }
2615
    raw->raw_type = RES_TYPE__TMQ;
2,900✔
2616
    uDebug("tmq get raw type data:%p", raw);
2,900✔
2617
  } else if (TD_RES_TMQ_METADATA(res)) {
811✔
2618
    int32_t code = encodeMqDataRsp(tEncodeSTaosxRsp, &rspObj->dataRsp, raw);
199✔
2619
    if (code != 0) {
199✔
2620
      uError("tmq get raw type error:%d", terrno);
×
2621
      return code;
×
2622
    }
2623
    raw->raw_type = RES_TYPE__TMQ_METADATA;
199✔
2624
    uDebug("tmq get raw type metadata:%p", raw);
199✔
2625
  } else if (TD_RES_TMQ_BATCH_META(res)) {
612✔
2626
    raw->raw = rspObj->batchMetaRsp.pMetaBuff;
612✔
2627
    raw->raw_len = rspObj->batchMetaRsp.metaBuffLen;
612✔
2628
    raw->raw_type = rspObj->resType;
612✔
2629
    uDebug("tmq get raw batch meta:%p", raw);
612✔
2630
  } else if (TD_RES_TMQ_RAW(res)) {
×
2631
    raw->raw = rspObj->dataRsp.rawData;
×
2632
    rspObj->dataRsp.rawData = NULL;
×
2633
    raw->raw_len = rspObj->dataRsp.len;
×
2634
    raw->raw_type = rspObj->resType;
×
2635
    uDebug("tmq get raw raw:%p", raw);
×
2636
  } else {
2637
    uError("tmq get raw error type:%d", *(int8_t*)res);
×
2638
    return TSDB_CODE_TMQ_INVALID_MSG;
×
2639
  }
2640
  return TSDB_CODE_SUCCESS;
11,119✔
2641
}
2642

2643
void tmq_free_raw(tmq_raw_data raw) {
9,727✔
2644
  uDebug("tmq free raw data type:%d", raw.raw_type);
9,727✔
2645
  if (raw.raw_type == RES_TYPE__TMQ || raw.raw_type == RES_TYPE__TMQ_METADATA) {
9,727✔
2646
    taosMemoryFree(raw.raw);
1,707✔
2647
  } else if (raw.raw_type == RES_TYPE__TMQ_RAWDATA && raw.raw != NULL) {
8,020✔
2648
    taosMemoryFree(POINTER_SHIFT(raw.raw, -sizeof(SMqRspHead)));
×
2649
  }
2650
  (void)memset(terrMsg, 0, ERR_MSG_LEN);
9,727✔
2651
}
9,727✔
2652

2653
static int32_t writeRawInit() {
13,301✔
2654
  while (atomic_load_8(&initedFlag) == WRITE_RAW_INIT_START) {
13,799✔
2655
    int8_t old = atomic_val_compare_exchange_8(&initFlag, 0, 1);
498✔
2656
    if (old == 0) {
498✔
2657
      int32_t code = initRawCacheHash();
498✔
2658
      if (code != 0) {
498✔
2659
        uError("tmq writeRawImpl init error:%d", code);
×
2660
        atomic_store_8(&initedFlag, WRITE_RAW_INIT_FAIL);
×
2661
        return code;
×
2662
      }
2663
      atomic_store_8(&initedFlag, WRITE_RAW_INIT_OK);
498✔
2664
    }
2665
  }
2666

2667
  if (atomic_load_8(&initedFlag) == WRITE_RAW_INIT_FAIL) {
13,301✔
2668
    return TSDB_CODE_INTERNAL_ERROR;
×
2669
  }
2670
  return 0;
13,301✔
2671
}
2672

2673
static int32_t writeRawImpl(TAOS* taos, void* buf, uint32_t len, uint16_t type) {
13,301✔
2674
  if (taos == NULL || buf == NULL) {
13,301✔
2675
    uError("invalid parameter in %s", __func__);
×
2676
    return TSDB_CODE_INVALID_PARA;
×
2677
  }
2678
  if (writeRawInit() != 0) {
13,301✔
2679
    return TSDB_CODE_INTERNAL_ERROR;
×
2680
  }
2681

2682
  if (type == TDMT_VND_CREATE_STB) {
13,301✔
2683
    return taosCreateStb(taos, buf, len);
2,863✔
2684
  } else if (type == TDMT_VND_ALTER_STB) {
10,438✔
2685
    return taosCreateStb(taos, buf, len);
1,630✔
2686
  } else if (type == TDMT_VND_DROP_STB) {
8,808✔
2687
    return taosDropStb(taos, buf, len);
306✔
2688
  } else if (type == TDMT_VND_CREATE_TABLE) {
8,502✔
2689
    return taosCreateTable(taos, buf, len);
4,770✔
2690
  } else if (type == TDMT_VND_ALTER_TABLE) {
3,732✔
2691
    return taosAlterTable(taos, buf, len);
1,080✔
2692
  } else if (type == TDMT_VND_DROP_TABLE) {
2,652✔
2693
    return taosDropTable(taos, buf, len);
207✔
2694
  } else if (type == TDMT_VND_DELETE) {
2,445✔
2695
    return taosDeleteData(taos, buf, len);
126✔
2696
  } else if (type == RES_TYPE__TMQ_METADATA) {
2,319✔
2697
    return tmqWriteRawMetaDataImpl(taos, buf, len);
199✔
2698
  } else if (type == RES_TYPE__TMQ_RAWDATA) {
2,120✔
2699
    return tmqWriteRawRawDataImpl(taos, buf, len);
×
2700
  } else if (type == RES_TYPE__TMQ) {
2,120✔
2701
    return tmqWriteRawDataImpl(taos, buf, len);
1,508✔
2702
  } else if (type == RES_TYPE__TMQ_BATCH_META) {
612✔
2703
    return tmqWriteBatchMetaDataImpl(taos, buf, len);
612✔
2704
  }
2705
  return TSDB_CODE_INVALID_PARA;
×
2706
}
2707

2708
int32_t tmq_write_raw(TAOS* taos, tmq_raw_data raw) {
9,310✔
2709
  if (taos == NULL || raw.raw == NULL || raw.raw_len <= 0) {
9,310✔
2710
    SET_ERROR_MSG("taos:%p or data:%p is NULL or raw_len <= 0", taos, raw.raw);
492✔
2711
    return TSDB_CODE_INVALID_PARA;
492✔
2712
  }
2713
  taosClearErrMsg();  // clear global error message
8,818✔
2714

2715
  return writeRawImpl(taos, raw.raw, raw.raw_len, raw.raw_type);
8,818✔
2716
}
2717

2718
static int32_t tmqWriteBatchMetaDataImpl(TAOS* taos, void* meta, uint32_t metaLen) {
612✔
2719
  if (taos == NULL || meta == NULL) {
612✔
2720
    uError("invalid parameter in %s", __func__);
×
2721
    return TSDB_CODE_INVALID_PARA;
×
2722
  }
2723
  SMqBatchMetaRsp rsp = {0};
612✔
2724
  SDecoder        coder = {0};
612✔
2725
  int32_t         code = TSDB_CODE_SUCCESS;
612✔
2726

2727
  // decode and process req
2728
  tDecoderInit(&coder, meta, metaLen);
612✔
2729
  if (tDecodeMqBatchMetaRsp(&coder, &rsp) < 0) {
612✔
2730
    code = TSDB_CODE_INVALID_PARA;
×
2731
    goto end;
×
2732
  }
2733
  int32_t num = taosArrayGetSize(rsp.batchMetaReq);
612✔
2734
  for (int32_t i = 0; i < num; i++) {
5,095✔
2735
    int32_t* len = taosArrayGet(rsp.batchMetaLen, i);
4,483✔
2736
    RAW_NULL_CHECK(len);
4,483✔
2737
    void* tmpBuf = taosArrayGetP(rsp.batchMetaReq, i);
4,483✔
2738
    RAW_NULL_CHECK(tmpBuf);
4,483✔
2739
    SDecoder   metaCoder = {0};
4,483✔
2740
    SMqMetaRsp metaRsp = {0};
4,483✔
2741
    tDecoderInit(&metaCoder, POINTER_SHIFT(tmpBuf, sizeof(SMqRspHead)), *len - sizeof(SMqRspHead));
4,483✔
2742
    if (tDecodeMqMetaRsp(&metaCoder, &metaRsp) < 0) {
4,483✔
2743
      code = TSDB_CODE_INVALID_PARA;
×
2744
      goto end;
×
2745
    }
2746
    code = writeRawImpl(taos, metaRsp.metaRsp, metaRsp.metaRspLen, metaRsp.resMsgType);
4,483✔
2747
    tDeleteMqMetaRsp(&metaRsp);
4,483✔
2748
    if (code != TSDB_CODE_SUCCESS) {
4,483✔
2749
      goto end;
×
2750
    }
2751
  }
2752

2753
end:
612✔
2754
  tDeleteMqBatchMetaRsp(&rsp);
612✔
2755
  return code;
612✔
2756
}
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