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

taosdata / TDengine / #3530

16 Nov 2024 07:44AM UTC coverage: 60.219% (-0.7%) from 60.888%
#3530

push

travis-ci

web-flow
Update 03-ad.md

118417 of 252124 branches covered (46.97%)

Branch coverage included in aggregate %.

198982 of 274951 relevant lines covered (72.37%)

6072359.98 hits per line

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

67.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 int32_t  tmqWriteBatchMetaDataImpl(TAOS* taos, void* meta, int32_t metaLen);
56
static tb_uid_t processSuid(tb_uid_t suid, char* db) { return suid + MurmurHash3_32(db, strlen(db)); }
118✔
57
static void buildCreateTableJson(SSchemaWrapper* schemaRow, SSchemaWrapper* schemaTag, char* name, int64_t id, int8_t t,
96✔
58
                                 SColCmprWrapper* pColCmprRow, cJSON** pJson) {
59
  int32_t code = TSDB_CODE_SUCCESS;
96✔
60
  int8_t  buildDefaultCompress = 0;
96✔
61
  if (pColCmprRow->nCols <= 0) {
96!
62
    buildDefaultCompress = 1;
×
63
  }
64

65
  char*  string = NULL;
96✔
66
  cJSON* json = cJSON_CreateObject();
96✔
67
  RAW_NULL_CHECK(json);
96!
68
  cJSON* type = cJSON_CreateString("create");
96✔
69
  RAW_NULL_CHECK(type);
96!
70

71
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "type", type));
96!
72
  cJSON* tableType = cJSON_CreateString(t == TSDB_NORMAL_TABLE ? "normal" : "super");
96✔
73
  RAW_NULL_CHECK(tableType);
96!
74
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableType", tableType));
96!
75
  cJSON* tableName = cJSON_CreateString(name);
96✔
76
  RAW_NULL_CHECK(tableName);
96!
77
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableName", tableName));
96!
78

79
  cJSON* columns = cJSON_CreateArray();
96✔
80
  RAW_NULL_CHECK(columns);
96!
81
  for (int i = 0; i < schemaRow->nCols; i++) {
584✔
82
    cJSON* column = cJSON_CreateObject();
488✔
83
    RAW_NULL_CHECK(column);
488!
84
    SSchema* s = schemaRow->pSchema + i;
488✔
85
    cJSON*   cname = cJSON_CreateString(s->name);
488✔
86
    RAW_NULL_CHECK(cname);
488!
87
    RAW_FALSE_CHECK(cJSON_AddItemToObject(column, "name", cname));
488!
88
    cJSON* ctype = cJSON_CreateNumber(s->type);
488✔
89
    RAW_NULL_CHECK(ctype);
488!
90
    RAW_FALSE_CHECK(cJSON_AddItemToObject(column, "type", ctype));
488!
91
    if (s->type == TSDB_DATA_TYPE_BINARY || s->type == TSDB_DATA_TYPE_VARBINARY || s->type == TSDB_DATA_TYPE_GEOMETRY) {
546!
92
      int32_t length = s->bytes - VARSTR_HEADER_SIZE;
58✔
93
      cJSON*  cbytes = cJSON_CreateNumber(length);
58✔
94
      RAW_NULL_CHECK(cbytes);
58!
95
      RAW_FALSE_CHECK(cJSON_AddItemToObject(column, "length", cbytes));
58!
96
    } else if (s->type == TSDB_DATA_TYPE_NCHAR) {
430✔
97
      int32_t length = (s->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
8✔
98
      cJSON*  cbytes = cJSON_CreateNumber(length);
8✔
99
      RAW_NULL_CHECK(cbytes);
8!
100
      RAW_FALSE_CHECK(cJSON_AddItemToObject(column, "length", cbytes));
8!
101
    }
102
    cJSON* isPk = cJSON_CreateBool(s->flags & COL_IS_KEY);
488✔
103
    RAW_NULL_CHECK(isPk);
488!
104
    RAW_FALSE_CHECK(cJSON_AddItemToObject(column, "isPrimarykey", isPk));
488!
105
    RAW_FALSE_CHECK(cJSON_AddItemToArray(columns, column));
488!
106

107
    if (pColCmprRow == NULL) {
488!
108
      continue;
×
109
    }
110

111
    uint32_t alg = 0;
488✔
112
    if (buildDefaultCompress) {
488!
113
      alg = createDefaultColCmprByType(s->type);
×
114
    } else {
115
      SColCmpr* pColCmpr = pColCmprRow->pColCmpr + i;
488✔
116
      alg = pColCmpr->alg;
488✔
117
    }
118
    const char* encode = columnEncodeStr(COMPRESS_L1_TYPE_U32(alg));
488✔
119
    RAW_NULL_CHECK(encode);
488!
120
    const char* compress = columnCompressStr(COMPRESS_L2_TYPE_U32(alg));
488✔
121
    RAW_NULL_CHECK(compress);
488!
122
    const char* level = columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(alg));
488✔
123
    RAW_NULL_CHECK(level);
488!
124

125
    cJSON* encodeJson = cJSON_CreateString(encode);
488✔
126
    RAW_NULL_CHECK(encodeJson);
488!
127
    RAW_FALSE_CHECK(cJSON_AddItemToObject(column, "encode", encodeJson));
488!
128

129
    cJSON* compressJson = cJSON_CreateString(compress);
488✔
130
    RAW_NULL_CHECK(compressJson);
488!
131
    RAW_FALSE_CHECK(cJSON_AddItemToObject(column, "compress", compressJson));
488!
132

133
    cJSON* levelJson = cJSON_CreateString(level);
488✔
134
    RAW_NULL_CHECK(levelJson);
488!
135
    RAW_FALSE_CHECK(cJSON_AddItemToObject(column, "level", levelJson));
488!
136
  }
137
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "columns", columns));
96!
138

139
  cJSON* tags = cJSON_CreateArray();
96✔
140
  RAW_NULL_CHECK(tags);
96!
141
  for (int i = 0; schemaTag && i < schemaTag->nCols; i++) {
290✔
142
    cJSON* tag = cJSON_CreateObject();
194✔
143
    RAW_NULL_CHECK(tag);
194!
144
    SSchema* s = schemaTag->pSchema + i;
194✔
145
    cJSON*   tname = cJSON_CreateString(s->name);
194✔
146
    RAW_NULL_CHECK(tname);
194!
147
    RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "name", tname));
194!
148
    cJSON* ttype = cJSON_CreateNumber(s->type);
194✔
149
    RAW_NULL_CHECK(ttype);
194!
150
    RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "type", ttype));
194!
151
    if (s->type == TSDB_DATA_TYPE_BINARY || s->type == TSDB_DATA_TYPE_VARBINARY || s->type == TSDB_DATA_TYPE_GEOMETRY) {
202!
152
      int32_t length = s->bytes - VARSTR_HEADER_SIZE;
8✔
153
      cJSON*  cbytes = cJSON_CreateNumber(length);
8✔
154
      RAW_NULL_CHECK(cbytes);
8!
155
      RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "length", cbytes));
8!
156
    } else if (s->type == TSDB_DATA_TYPE_NCHAR) {
186✔
157
      int32_t length = (s->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
55✔
158
      cJSON*  cbytes = cJSON_CreateNumber(length);
55✔
159
      RAW_NULL_CHECK(cbytes);
55!
160
      RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "length", cbytes));
55!
161
    }
162
    RAW_FALSE_CHECK(cJSON_AddItemToArray(tags, tag));
194!
163
  }
164
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tags", tags));
96!
165

166
end:
96✔
167
  *pJson = json;
96✔
168
}
96✔
169

170
static int32_t setCompressOption(cJSON* json, uint32_t para) {
×
171
  uint8_t encode = COMPRESS_L1_TYPE_U32(para);
×
172
  int32_t code = 0;
×
173
  if (encode != 0) {
×
174
    const char* encodeStr = columnEncodeStr(encode);
×
175
    RAW_NULL_CHECK(encodeStr);
×
176
    cJSON* encodeJson = cJSON_CreateString(encodeStr);
×
177
    RAW_NULL_CHECK(encodeJson);
×
178
    RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "encode", encodeJson));
×
179
    return code;
×
180
  }
181
  uint8_t compress = COMPRESS_L2_TYPE_U32(para);
×
182
  if (compress != 0) {
×
183
    const char* compressStr = columnCompressStr(compress);
×
184
    RAW_NULL_CHECK(compressStr);
×
185
    cJSON* compressJson = cJSON_CreateString(compressStr);
×
186
    RAW_NULL_CHECK(compressJson);
×
187
    RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "compress", compressJson));
×
188
    return code;
×
189
  }
190
  uint8_t level = COMPRESS_L2_TYPE_LEVEL_U32(para);
×
191
  if (level != 0) {
×
192
    const char* levelStr = columnLevelStr(level);
×
193
    RAW_NULL_CHECK(levelStr);
×
194
    cJSON* levelJson = cJSON_CreateString(levelStr);
×
195
    RAW_NULL_CHECK(levelJson);
×
196
    RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "level", levelJson));
×
197
    return code;
×
198
  }
199

200
end:
×
201
  return code;
×
202
}
203
static void buildAlterSTableJson(void* alterData, int32_t alterDataLen, cJSON** pJson) {
40✔
204
  SMAlterStbReq req = {0};
40✔
205
  cJSON*        json = NULL;
40✔
206
  char*         string = NULL;
40✔
207
  int32_t       code = 0;
40✔
208

209
  if (tDeserializeSMAlterStbReq(alterData, alterDataLen, &req) != 0) {
40!
210
    goto end;
×
211
  }
212

213
  json = cJSON_CreateObject();
40✔
214
  RAW_NULL_CHECK(json);
40!
215
  cJSON* type = cJSON_CreateString("alter");
40✔
216
  RAW_NULL_CHECK(type);
40!
217
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "type", type));
40!
218
  SName name = {0};
40✔
219
  RAW_RETURN_CHECK(tNameFromString(&name, req.name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE));
40!
220
  cJSON* tableType = cJSON_CreateString("super");
40✔
221
  RAW_NULL_CHECK(tableType);
40!
222
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableType", tableType));
40!
223
  cJSON* tableName = cJSON_CreateString(name.tname);
40✔
224
  RAW_NULL_CHECK(tableName);
40!
225
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableName", tableName));
40!
226

227
  cJSON* alterType = cJSON_CreateNumber(req.alterType);
40✔
228
  RAW_NULL_CHECK(alterType);
40!
229
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "alterType", alterType));
40!
230
  switch (req.alterType) {
40!
231
    case TSDB_ALTER_TABLE_ADD_TAG:
24✔
232
    case TSDB_ALTER_TABLE_ADD_COLUMN: {
233
      TAOS_FIELD* field = taosArrayGet(req.pFields, 0);
24✔
234
      RAW_NULL_CHECK(field);
24!
235
      cJSON* colName = cJSON_CreateString(field->name);
24✔
236
      RAW_NULL_CHECK(colName);
24!
237
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName));
24!
238
      cJSON* colType = cJSON_CreateNumber(field->type);
24✔
239
      RAW_NULL_CHECK(colType);
24!
240
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colType", colType));
24!
241

242
      if (field->type == TSDB_DATA_TYPE_BINARY || field->type == TSDB_DATA_TYPE_VARBINARY ||
24!
243
          field->type == TSDB_DATA_TYPE_GEOMETRY) {
24!
244
        int32_t length = field->bytes - VARSTR_HEADER_SIZE;
8✔
245
        cJSON*  cbytes = cJSON_CreateNumber(length);
8✔
246
        RAW_NULL_CHECK(cbytes);
8!
247
        RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes));
8!
248
      } else if (field->type == TSDB_DATA_TYPE_NCHAR) {
16!
249
        int32_t length = (field->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
250
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
251
        RAW_NULL_CHECK(cbytes);
×
252
        RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes));
×
253
      }
254
      break;
24✔
255
    }
256
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION: {
×
257
      SFieldWithOptions* field = taosArrayGet(req.pFields, 0);
×
258
      RAW_NULL_CHECK(field);
×
259
      cJSON* colName = cJSON_CreateString(field->name);
×
260
      RAW_NULL_CHECK(colName);
×
261
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName));
×
262
      cJSON* colType = cJSON_CreateNumber(field->type);
×
263
      RAW_NULL_CHECK(colType);
×
264
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colType", colType));
×
265

266
      if (field->type == TSDB_DATA_TYPE_BINARY || field->type == TSDB_DATA_TYPE_VARBINARY ||
×
267
          field->type == TSDB_DATA_TYPE_GEOMETRY) {
×
268
        int32_t length = field->bytes - VARSTR_HEADER_SIZE;
×
269
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
270
        RAW_NULL_CHECK(cbytes);
×
271
        RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes));
×
272
      } else if (field->type == TSDB_DATA_TYPE_NCHAR) {
×
273
        int32_t length = (field->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
274
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
275
        RAW_NULL_CHECK(cbytes);
×
276
        RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes));
×
277
      }
278
      RAW_RETURN_CHECK(setCompressOption(json, field->compress));
×
279
      break;
×
280
    }
281
    case TSDB_ALTER_TABLE_DROP_TAG:
8✔
282
    case TSDB_ALTER_TABLE_DROP_COLUMN: {
283
      TAOS_FIELD* field = taosArrayGet(req.pFields, 0);
8✔
284
      RAW_NULL_CHECK(field);
8!
285
      cJSON* colName = cJSON_CreateString(field->name);
8✔
286
      RAW_NULL_CHECK(colName);
8!
287
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName));
8!
288
      break;
8✔
289
    }
290
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
8✔
291
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES: {
292
      TAOS_FIELD* field = taosArrayGet(req.pFields, 0);
8✔
293
      RAW_NULL_CHECK(field);
8!
294
      cJSON* colName = cJSON_CreateString(field->name);
8✔
295
      RAW_NULL_CHECK(colName);
8!
296
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName));
8!
297
      cJSON* colType = cJSON_CreateNumber(field->type);
8✔
298
      RAW_NULL_CHECK(colType);
8!
299
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colType", colType));
8!
300
      if (field->type == TSDB_DATA_TYPE_BINARY || field->type == TSDB_DATA_TYPE_VARBINARY ||
8!
301
          field->type == TSDB_DATA_TYPE_GEOMETRY) {
8!
302
        int32_t length = field->bytes - VARSTR_HEADER_SIZE;
8✔
303
        cJSON*  cbytes = cJSON_CreateNumber(length);
8✔
304
        RAW_NULL_CHECK(cbytes);
8!
305
        RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes));
8!
306
      } else if (field->type == TSDB_DATA_TYPE_NCHAR) {
×
307
        int32_t length = (field->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
308
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
309
        RAW_NULL_CHECK(cbytes);
×
310
        RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes));
×
311
      }
312
      break;
8✔
313
    }
314
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
×
315
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME: {
316
      TAOS_FIELD* oldField = taosArrayGet(req.pFields, 0);
×
317
      RAW_NULL_CHECK(oldField);
×
318
      TAOS_FIELD* newField = taosArrayGet(req.pFields, 1);
×
319
      RAW_NULL_CHECK(newField);
×
320
      cJSON* colName = cJSON_CreateString(oldField->name);
×
321
      RAW_NULL_CHECK(colName);
×
322
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName));
×
323
      cJSON* colNewName = cJSON_CreateString(newField->name);
×
324
      RAW_NULL_CHECK(colNewName);
×
325
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colNewName", colNewName));
×
326
      break;
×
327
    }
328
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS: {
×
329
      TAOS_FIELD* field = taosArrayGet(req.pFields, 0);
×
330
      RAW_NULL_CHECK(field);
×
331
      cJSON* colName = cJSON_CreateString(field->name);
×
332
      RAW_NULL_CHECK(colName);
×
333
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName));
×
334
      RAW_RETURN_CHECK(setCompressOption(json, field->bytes));
×
335
      break;
×
336
    }
337
    default:
×
338
      break;
×
339
  }
340

341
end:
40✔
342
  tFreeSMAltertbReq(&req);
40✔
343
  *pJson = json;
40✔
344
}
40✔
345

346
static void processCreateStb(SMqMetaRsp* metaRsp, cJSON** pJson) {
78✔
347
  SVCreateStbReq req = {0};
78✔
348
  SDecoder       coder;
349

350
  uDebug("create stable data:%p", metaRsp);
78!
351
  // decode and process req
352
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
78✔
353
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
78✔
354
  tDecoderInit(&coder, data, len);
78✔
355

356
  if (tDecodeSVCreateStbReq(&coder, &req) < 0) {
78!
357
    goto end;
×
358
  }
359
  buildCreateTableJson(&req.schemaRow, &req.schemaTag, req.name, req.suid, TSDB_SUPER_TABLE, &req.colCmpr, pJson);
78✔
360

361
end:
78✔
362
  uDebug("create stable return");
78!
363
  tDecoderClear(&coder);
78✔
364
}
78✔
365

366
static void processAlterStb(SMqMetaRsp* metaRsp, cJSON** pJson) {
40✔
367
  SVCreateStbReq req = {0};
40✔
368
  SDecoder       coder = {0};
40✔
369
  uDebug("alter stable data:%p", metaRsp);
40!
370

371
  // decode and process req
372
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
40✔
373
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
40✔
374
  tDecoderInit(&coder, data, len);
40✔
375

376
  if (tDecodeSVCreateStbReq(&coder, &req) < 0) {
40!
377
    goto end;
×
378
  }
379
  buildAlterSTableJson(req.alterOriData, req.alterOriDataLen, pJson);
40✔
380

381
end:
40✔
382
  uDebug("alter stable return");
40!
383
  tDecoderClear(&coder);
40✔
384
}
40✔
385

386
static void buildChildElement(cJSON* json, SVCreateTbReq* pCreateReq) {
156✔
387
  STag*   pTag = (STag*)pCreateReq->ctb.pTag;
156✔
388
  char*   sname = pCreateReq->ctb.stbName;
156✔
389
  char*   name = pCreateReq->name;
156✔
390
  SArray* tagName = pCreateReq->ctb.tagName;
156✔
391
  int64_t id = pCreateReq->uid;
156✔
392
  uint8_t tagNum = pCreateReq->ctb.tagNum;
156✔
393
  int32_t code = 0;
156✔
394
  cJSON*  tags = NULL;
156✔
395
  cJSON*  tableName = cJSON_CreateString(name);
156✔
396
  RAW_NULL_CHECK(tableName);
156!
397
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableName", tableName));
156!
398
  cJSON* using = cJSON_CreateString(sname);
156✔
399
  RAW_NULL_CHECK(using);
156!
400
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "using", using));
156!
401
  cJSON* tagNumJson = cJSON_CreateNumber(tagNum);
156✔
402
  RAW_NULL_CHECK(tagNumJson);
156!
403
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tagNum", tagNumJson));
156!
404

405
  tags = cJSON_CreateArray();
156✔
406
  RAW_NULL_CHECK(tags);
156!
407
  SArray* pTagVals = NULL;
156✔
408
  RAW_RETURN_CHECK(tTagToValArray(pTag, &pTagVals));
156!
409

410
  if (tTagIsJson(pTag)) {
156✔
411
    STag* p = (STag*)pTag;
20✔
412
    if (p->nTag == 0) {
20✔
413
      uError("p->nTag == 0");
10!
414
      goto end;
10✔
415
    }
416
    char* pJson = NULL;
10✔
417
    parseTagDatatoJson(pTag, &pJson);
10✔
418
    if (pJson == NULL) {
10!
419
      uError("parseTagDatatoJson failed, pJson == NULL");
×
420
      goto end;
×
421
    }
422
    cJSON* tag = cJSON_CreateObject();
10✔
423
    RAW_NULL_CHECK(tag);
10!
424
    STagVal* pTagVal = taosArrayGet(pTagVals, 0);
10✔
425
    RAW_NULL_CHECK(pTagVal);
10!
426
    char* ptname = taosArrayGet(tagName, 0);
10✔
427
    RAW_NULL_CHECK(ptname);
10!
428
    cJSON* tname = cJSON_CreateString(ptname);
10✔
429
    RAW_NULL_CHECK(tname);
10!
430
    RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "name", tname));
10!
431
    cJSON* ttype = cJSON_CreateNumber(TSDB_DATA_TYPE_JSON);
10✔
432
    RAW_NULL_CHECK(ttype);
10!
433
    RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "type", ttype));
10!
434
    cJSON* tvalue = cJSON_CreateString(pJson);
10✔
435
    RAW_NULL_CHECK(tvalue);
10!
436
    RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "value", tvalue));
10!
437
    RAW_FALSE_CHECK(cJSON_AddItemToArray(tags, tag));
10!
438
    taosMemoryFree(pJson);
10✔
439
    goto end;
10✔
440
  }
441

442
  for (int i = 0; i < taosArrayGetSize(pTagVals); i++) {
465✔
443
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, i);
329✔
444
    RAW_NULL_CHECK(pTagVal);
329!
445
    cJSON* tag = cJSON_CreateObject();
329✔
446
    RAW_NULL_CHECK(tag);
329!
447
    char* ptname = taosArrayGet(tagName, i);
329✔
448
    RAW_NULL_CHECK(ptname);
329!
449
    cJSON* tname = cJSON_CreateString(ptname);
329✔
450
    RAW_NULL_CHECK(tname);
329!
451
    RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "name", tname));
329!
452
    cJSON* ttype = cJSON_CreateNumber(pTagVal->type);
329✔
453
    RAW_NULL_CHECK(ttype);
329!
454
    RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "type", ttype));
329!
455

456
    cJSON* tvalue = NULL;
329✔
457
    if (IS_VAR_DATA_TYPE(pTagVal->type)) {
431!
458
      char*   buf = NULL;
102✔
459
      int64_t bufSize = 0;
102✔
460
      if (pTagVal->type == TSDB_DATA_TYPE_VARBINARY) {
102!
461
        bufSize = pTagVal->nData * 2 + 2 + 3;
×
462
      } else {
463
        bufSize = pTagVal->nData + 3;
102✔
464
      }
465
      buf = taosMemoryCalloc(bufSize, 1);
102✔
466

467
      RAW_NULL_CHECK(buf);
102!
468
      if (!buf) goto end;
102!
469
      if (dataConverToStr(buf, bufSize, pTagVal->type, pTagVal->pData, pTagVal->nData, NULL) != TSDB_CODE_SUCCESS) {
102!
470
        taosMemoryFree(buf);
×
471
        goto end;
×
472
      }
473

474
      tvalue = cJSON_CreateString(buf);
102✔
475
      RAW_NULL_CHECK(tvalue);
102!
476
      taosMemoryFree(buf);
102✔
477
    } else {
478
      double val = 0;
227✔
479
      GET_TYPED_DATA(val, double, pTagVal->type, &pTagVal->i64);
227!
480
      tvalue = cJSON_CreateNumber(val);
227✔
481
      RAW_NULL_CHECK(tvalue);
227!
482
    }
483

484
    RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "value", tvalue));
329!
485
    RAW_FALSE_CHECK(cJSON_AddItemToArray(tags, tag));
329!
486
  }
487

488
end:
136✔
489
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tags", tags));
156!
490
  taosArrayDestroy(pTagVals);
156✔
491
}
156✔
492

493
static void buildCreateCTableJson(SVCreateTbReq* pCreateReq, int32_t nReqs, cJSON** pJson) {
123✔
494
  int32_t code = 0;
123✔
495
  char*   string = NULL;
123✔
496
  cJSON*  json = cJSON_CreateObject();
123✔
497
  RAW_NULL_CHECK(json);
123!
498
  cJSON* type = cJSON_CreateString("create");
123✔
499
  RAW_NULL_CHECK(type);
123!
500
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "type", type));
123!
501

502
  cJSON* tableType = cJSON_CreateString("child");
123✔
503
  RAW_NULL_CHECK(tableType);
123!
504
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableType", tableType));
123!
505

506
  buildChildElement(json, pCreateReq);
123✔
507
  cJSON* createList = cJSON_CreateArray();
123✔
508
  RAW_NULL_CHECK(createList);
123!
509
  for (int i = 0; nReqs > 1 && i < nReqs; i++) {
156✔
510
    cJSON* create = cJSON_CreateObject();
33✔
511
    RAW_NULL_CHECK(create);
33!
512
    buildChildElement(create, pCreateReq + i);
33✔
513
    RAW_FALSE_CHECK(cJSON_AddItemToArray(createList, create));
33!
514
  }
515
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "createList", createList));
123!
516

517
end:
123✔
518
  *pJson = json;
123✔
519
}
123✔
520

521
static void processCreateTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
131✔
522
  SDecoder           decoder = {0};
131✔
523
  SVCreateTbBatchReq req = {0};
131✔
524
  SVCreateTbReq*     pCreateReq;
525
  // decode
526
  uDebug("create table data:%p", metaRsp);
131!
527
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
131✔
528
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
131✔
529
  tDecoderInit(&decoder, data, len);
131✔
530
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
131!
531
    goto end;
×
532
  }
533

534
  // loop to create table
535
  if (req.nReqs > 0) {
131!
536
    pCreateReq = req.pReqs;
131✔
537
    if (pCreateReq->type == TSDB_CHILD_TABLE) {
131✔
538
      buildCreateCTableJson(req.pReqs, req.nReqs, pJson);
113✔
539
    } else if (pCreateReq->type == TSDB_NORMAL_TABLE) {
18!
540
      buildCreateTableJson(&pCreateReq->ntb.schemaRow, NULL, pCreateReq->name, pCreateReq->uid, TSDB_NORMAL_TABLE,
18✔
541
                           &pCreateReq->colCmpr, pJson);
542
    }
543
  }
544

545
end:
×
546
  uDebug("create table return");
131!
547
  tDeleteSVCreateTbBatchReq(&req);
131✔
548
  tDecoderClear(&decoder);
131✔
549
}
131✔
550

551
static void processAutoCreateTable(SMqDataRsp* rsp, char** string) {
10✔
552
  SDecoder*      decoder = NULL;
10✔
553
  SVCreateTbReq* pCreateReq = NULL;
10✔
554
  int32_t        code = 0;
10✔
555
  uDebug("auto create table data:%p", rsp);
10!
556
  if (rsp->createTableNum <= 0) {
10!
557
    uError("processAutoCreateTable rsp->createTableNum <= 0");
×
558
    goto end;
×
559
  }
560

561
  decoder = taosMemoryCalloc(rsp->createTableNum, sizeof(SDecoder));
10✔
562
  RAW_NULL_CHECK(decoder);
10!
563
  pCreateReq = taosMemoryCalloc(rsp->createTableNum, sizeof(SVCreateTbReq));
10✔
564
  RAW_NULL_CHECK(pCreateReq);
10!
565

566
  // loop to create table
567
  for (int32_t iReq = 0; iReq < rsp->createTableNum; iReq++) {
34✔
568
    // decode
569
    void** data = taosArrayGet(rsp->createTableReq, iReq);
24✔
570
    RAW_NULL_CHECK(data);
24!
571
    int32_t* len = taosArrayGet(rsp->createTableLen, iReq);
24✔
572
    RAW_NULL_CHECK(len);
24!
573
    tDecoderInit(&decoder[iReq], *data, *len);
24✔
574
    if (tDecodeSVCreateTbReq(&decoder[iReq], pCreateReq + iReq) < 0) {
24!
575
      goto end;
×
576
    }
577

578
    if (pCreateReq[iReq].type != TSDB_CHILD_TABLE) {
24!
579
      uError("processAutoCreateTable pCreateReq[iReq].type != TSDB_CHILD_TABLE");
×
580
      goto end;
×
581
    }
582
  }
583
  cJSON* pJson = NULL;
10✔
584
  buildCreateCTableJson(pCreateReq, rsp->createTableNum, &pJson);
10✔
585
  *string = cJSON_PrintUnformatted(pJson);
10✔
586
  cJSON_Delete(pJson);
10✔
587

588
end:
10✔
589
  uDebug("auto created table return, sql json:%s", *string);
10!
590
  for (int i = 0; decoder && pCreateReq && i < rsp->createTableNum; i++) {
34!
591
    tDecoderClear(&decoder[i]);
24✔
592
    taosMemoryFreeClear(pCreateReq[i].comment);
24!
593
    if (pCreateReq[i].type == TSDB_CHILD_TABLE) {
24!
594
      taosArrayDestroy(pCreateReq[i].ctb.tagName);
24✔
595
    }
596
  }
597
  taosMemoryFree(decoder);
10✔
598
  taosMemoryFree(pCreateReq);
10✔
599
}
10✔
600

601
static void processAlterTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
24✔
602
  SDecoder     decoder = {0};
24✔
603
  SVAlterTbReq vAlterTbReq = {0};
24✔
604
  char*        string = NULL;
24✔
605
  cJSON*       json = NULL;
24✔
606
  int32_t      code = 0;
24✔
607

608
  uDebug("alter table data:%p", metaRsp);
24!
609
  // decode
610
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
24✔
611
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
24✔
612
  tDecoderInit(&decoder, data, len);
24✔
613
  if (tDecodeSVAlterTbReq(&decoder, &vAlterTbReq) < 0) {
24!
614
    uError("tDecodeSVAlterTbReq error");
×
615
    goto end;
×
616
  }
617

618
  json = cJSON_CreateObject();
24✔
619
  RAW_NULL_CHECK(json);
24!
620
  cJSON* type = cJSON_CreateString("alter");
24✔
621
  RAW_NULL_CHECK(type);
24!
622
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "type", type));
24!
623
  cJSON* tableType = cJSON_CreateString(vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ? "child" : "normal");
24✔
624
  RAW_NULL_CHECK(tableType);
24!
625
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableType", tableType));
24!
626
  cJSON* tableName = cJSON_CreateString(vAlterTbReq.tbName);
24✔
627
  RAW_NULL_CHECK(tableName);
24!
628
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableName", tableName));
24!
629
  cJSON* alterType = cJSON_CreateNumber(vAlterTbReq.action);
24✔
630
  RAW_NULL_CHECK(alterType);
24!
631
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "alterType", alterType));
24!
632

633
  switch (vAlterTbReq.action) {
24!
634
    case TSDB_ALTER_TABLE_ADD_COLUMN: {
4✔
635
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
4✔
636
      RAW_NULL_CHECK(colName);
4!
637
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName));
4!
638
      cJSON* colType = cJSON_CreateNumber(vAlterTbReq.type);
4✔
639
      RAW_NULL_CHECK(colType);
4!
640
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colType", colType));
4!
641

642
      if (vAlterTbReq.type == TSDB_DATA_TYPE_BINARY || vAlterTbReq.type == TSDB_DATA_TYPE_VARBINARY ||
4!
643
          vAlterTbReq.type == TSDB_DATA_TYPE_GEOMETRY) {
4!
644
        int32_t length = vAlterTbReq.bytes - VARSTR_HEADER_SIZE;
×
645
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
646
        RAW_NULL_CHECK(cbytes);
×
647
        RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes));
×
648
      } else if (vAlterTbReq.type == TSDB_DATA_TYPE_NCHAR) {
4!
649
        int32_t length = (vAlterTbReq.bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
650
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
651
        RAW_NULL_CHECK(cbytes);
×
652
        RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes));
×
653
      }
654
      break;
4✔
655
    }
656
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION: {
×
657
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
×
658
      RAW_NULL_CHECK(colName);
×
659
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName));
×
660
      cJSON* colType = cJSON_CreateNumber(vAlterTbReq.type);
×
661
      RAW_NULL_CHECK(colType);
×
662
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colType", colType));
×
663

664
      if (vAlterTbReq.type == TSDB_DATA_TYPE_BINARY || vAlterTbReq.type == TSDB_DATA_TYPE_VARBINARY ||
×
665
          vAlterTbReq.type == TSDB_DATA_TYPE_GEOMETRY) {
×
666
        int32_t length = vAlterTbReq.bytes - VARSTR_HEADER_SIZE;
×
667
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
668
        RAW_NULL_CHECK(cbytes);
×
669
        RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes));
×
670
      } else if (vAlterTbReq.type == TSDB_DATA_TYPE_NCHAR) {
×
671
        int32_t length = (vAlterTbReq.bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
×
672
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
673
        RAW_NULL_CHECK(cbytes);
×
674
        RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes));
×
675
      }
676
      RAW_RETURN_CHECK(setCompressOption(json, vAlterTbReq.compress));
×
677
      break;
×
678
    }
679
    case TSDB_ALTER_TABLE_DROP_COLUMN: {
4✔
680
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
4✔
681
      RAW_NULL_CHECK(colName);
4!
682
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName));
4!
683
      break;
4✔
684
    }
685
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES: {
4✔
686
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
4✔
687
      RAW_NULL_CHECK(colName);
4!
688
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName));
4!
689
      cJSON* colType = cJSON_CreateNumber(vAlterTbReq.colModType);
4✔
690
      RAW_NULL_CHECK(colType);
4!
691
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colType", colType));
4!
692
      if (vAlterTbReq.colModType == TSDB_DATA_TYPE_BINARY || vAlterTbReq.colModType == TSDB_DATA_TYPE_VARBINARY ||
4!
693
          vAlterTbReq.colModType == TSDB_DATA_TYPE_GEOMETRY) {
4!
694
        int32_t length = vAlterTbReq.colModBytes - VARSTR_HEADER_SIZE;
×
695
        cJSON*  cbytes = cJSON_CreateNumber(length);
×
696
        RAW_NULL_CHECK(cbytes);
×
697
        RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes));
×
698
      } else if (vAlterTbReq.colModType == TSDB_DATA_TYPE_NCHAR) {
4!
699
        int32_t length = (vAlterTbReq.colModBytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
4✔
700
        cJSON*  cbytes = cJSON_CreateNumber(length);
4✔
701
        RAW_NULL_CHECK(cbytes);
4!
702
        RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes));
4!
703
      }
704
      break;
4✔
705
    }
706
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME: {
4✔
707
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
4✔
708
      RAW_NULL_CHECK(colName);
4!
709
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName));
4!
710
      cJSON* colNewName = cJSON_CreateString(vAlterTbReq.colNewName);
4✔
711
      RAW_NULL_CHECK(colNewName);
4!
712
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colNewName", colNewName));
4!
713
      break;
4✔
714
    }
715
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL: {
4✔
716
      cJSON* tagName = cJSON_CreateString(vAlterTbReq.tagName);
4✔
717
      RAW_NULL_CHECK(tagName);
4!
718
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", tagName));
4!
719

720
      bool isNull = vAlterTbReq.isNull;
4✔
721
      if (vAlterTbReq.tagType == TSDB_DATA_TYPE_JSON) {
4!
722
        STag* jsonTag = (STag*)vAlterTbReq.pTagVal;
×
723
        if (jsonTag->nTag == 0) isNull = true;
×
724
      }
725
      if (!isNull) {
4!
726
        char* buf = NULL;
4✔
727

728
        if (vAlterTbReq.tagType == TSDB_DATA_TYPE_JSON) {
4!
729
          if (!tTagIsJson(vAlterTbReq.pTagVal)) {
×
730
            uError("processAlterTable isJson false");
×
731
            goto end;
×
732
          }
733
          parseTagDatatoJson(vAlterTbReq.pTagVal, &buf);
×
734
          if (buf == NULL) {
×
735
            uError("parseTagDatatoJson failed, buf == NULL");
×
736
            goto end;
×
737
          }
738
        } else {
739
          int64_t bufSize = 0;
4✔
740
          if (vAlterTbReq.tagType == TSDB_DATA_TYPE_VARBINARY) {
4!
741
            bufSize = vAlterTbReq.nTagVal * 2 + 2 + 3;
×
742
          } else {
743
            bufSize = vAlterTbReq.nTagVal + 3;
4✔
744
          }
745
          buf = taosMemoryCalloc(bufSize, 1);
4✔
746
          RAW_NULL_CHECK(buf);
4!
747
          if (dataConverToStr(buf, bufSize, vAlterTbReq.tagType, vAlterTbReq.pTagVal, vAlterTbReq.nTagVal, NULL) !=
4!
748
              TSDB_CODE_SUCCESS) {
749
            taosMemoryFree(buf);
×
750
            goto end;
×
751
          }
752
        }
753

754
        cJSON* colValue = cJSON_CreateString(buf);
4✔
755
        RAW_NULL_CHECK(colValue);
4!
756
        RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colValue", colValue));
4!
757
        taosMemoryFree(buf);
4✔
758
      }
759

760
      cJSON* isNullCJson = cJSON_CreateBool(isNull);
4✔
761
      RAW_NULL_CHECK(isNullCJson);
4!
762
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colValueNull", isNullCJson));
4!
763
      break;
4✔
764
    }
765
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS: {
×
766
      cJSON* colName = cJSON_CreateString(vAlterTbReq.colName);
×
767
      RAW_NULL_CHECK(colName);
×
768
      RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName));
×
769
      RAW_RETURN_CHECK(setCompressOption(json, vAlterTbReq.compress));
×
770
      break;
×
771
    }
772
    default:
4✔
773
      break;
4✔
774
  }
775

776
end:
24✔
777
  uDebug("alter table return");
24!
778
  tDecoderClear(&decoder);
24✔
779
  *pJson = json;
24✔
780
}
24✔
781

782
static void processDropSTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
8✔
783
  SDecoder     decoder = {0};
8✔
784
  SVDropStbReq req = {0};
8✔
785
  cJSON*       json = NULL;
8✔
786
  int32_t      code = 0;
8✔
787

788
  uDebug("processDropSTable data:%p", metaRsp);
8!
789

790
  // decode
791
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
8✔
792
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
8✔
793
  tDecoderInit(&decoder, data, len);
8✔
794
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
8!
795
    uError("tDecodeSVDropStbReq failed");
×
796
    goto end;
×
797
  }
798

799
  json = cJSON_CreateObject();
8✔
800
  RAW_NULL_CHECK(json);
8!
801
  cJSON* type = cJSON_CreateString("drop");
8✔
802
  RAW_NULL_CHECK(type);
8!
803
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "type", type));
8!
804
  cJSON* tableType = cJSON_CreateString("super");
8✔
805
  RAW_NULL_CHECK(tableType);
8!
806
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableType", tableType));
8!
807
  cJSON* tableName = cJSON_CreateString(req.name);
8✔
808
  RAW_NULL_CHECK(tableName);
8!
809
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableName", tableName));
8!
810

811
end:
8✔
812
  uDebug("processDropSTable return");
8!
813
  tDecoderClear(&decoder);
8✔
814
  *pJson = json;
8✔
815
}
8✔
816
static void processDeleteTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
3✔
817
  SDeleteRes req = {0};
3✔
818
  SDecoder   coder = {0};
3✔
819
  cJSON*     json = NULL;
3✔
820
  int32_t    code = 0;
3✔
821

822
  uDebug("processDeleteTable data:%p", metaRsp);
3!
823
  // decode and process req
824
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
3✔
825
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
3✔
826

827
  tDecoderInit(&coder, data, len);
3✔
828
  if (tDecodeDeleteRes(&coder, &req) < 0) {
3!
829
    uError("tDecodeDeleteRes failed");
×
830
    goto end;
×
831
  }
832

833
  //  getTbName(req.tableFName);
834
  char sql[256] = {0};
3✔
835
  (void)snprintf(sql, sizeof(sql), "delete from `%s` where `%s` >= %" PRId64 " and `%s` <= %" PRId64, req.tableFName,
3✔
836
                 req.tsColName, req.skey, req.tsColName, req.ekey);
837

838
  json = cJSON_CreateObject();
3✔
839
  RAW_NULL_CHECK(json);
3!
840
  cJSON* type = cJSON_CreateString("delete");
3✔
841
  RAW_NULL_CHECK(type);
3!
842
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "type", type));
3!
843
  cJSON* sqlJson = cJSON_CreateString(sql);
3✔
844
  RAW_NULL_CHECK(sqlJson);
3!
845
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "sql", sqlJson));
3!
846

847
end:
3✔
848
  uDebug("processDeleteTable return");
3!
849
  tDecoderClear(&coder);
3✔
850
  *pJson = json;
3✔
851
}
3✔
852

853
static void processDropTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
5✔
854
  SDecoder         decoder = {0};
5✔
855
  SVDropTbBatchReq req = {0};
5✔
856
  cJSON*           json = NULL;
5✔
857
  int32_t          code = 0;
5✔
858

859
  uDebug("processDropTable data:%p", metaRsp);
5!
860
  // decode
861
  void*   data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead));
5✔
862
  int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead);
5✔
863
  tDecoderInit(&decoder, data, len);
5✔
864
  if (tDecodeSVDropTbBatchReq(&decoder, &req) < 0) {
5!
865
    uError("tDecodeSVDropTbBatchReq failed");
×
866
    goto end;
×
867
  }
868

869
  json = cJSON_CreateObject();
5✔
870
  RAW_NULL_CHECK(json);
5!
871
  cJSON* type = cJSON_CreateString("drop");
5✔
872
  RAW_NULL_CHECK(type);
5!
873
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "type", type));
5!
874
  cJSON* tableNameList = cJSON_CreateArray();
5✔
875
  RAW_NULL_CHECK(tableNameList);
5!
876
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
11✔
877
    SVDropTbReq* pDropTbReq = req.pReqs + iReq;
6✔
878
    cJSON*       tableName = cJSON_CreateString(pDropTbReq->name);
6✔
879
    RAW_NULL_CHECK(tableName);
6!
880
    RAW_FALSE_CHECK(cJSON_AddItemToArray(tableNameList, tableName));
6!
881
  }
882
  RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableNameList", tableNameList));
5!
883

884
end:
5✔
885
  uDebug("processDropTable return");
5!
886
  tDecoderClear(&decoder);
5✔
887
  *pJson = json;
5✔
888
}
5✔
889

890
static int32_t taosCreateStb(TAOS* taos, void* meta, int32_t metaLen) {
118✔
891
  SVCreateStbReq req = {0};
118✔
892
  SDecoder       coder;
893
  SMCreateStbReq pReq = {0};
118✔
894
  int32_t        code = TSDB_CODE_SUCCESS;
118✔
895
  SRequestObj*   pRequest = NULL;
118✔
896

897
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
118!
898
  uDebug(LOG_ID_TAG " create stable, meta:%p, metaLen:%d", LOG_ID_VALUE, meta, metaLen);
118!
899
  pRequest->syncQuery = true;
118✔
900
  if (!pRequest->pDb) {
118!
901
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
902
    goto end;
×
903
  }
904
  // decode and process req
905
  void*   data = POINTER_SHIFT(meta, sizeof(SMsgHead));
118✔
906
  int32_t len = metaLen - sizeof(SMsgHead);
118✔
907
  tDecoderInit(&coder, data, len);
118✔
908
  if (tDecodeSVCreateStbReq(&coder, &req) < 0) {
118!
909
    code = TSDB_CODE_INVALID_PARA;
×
910
    goto end;
×
911
  }
912

913
  int8_t           createDefaultCompress = 0;
118✔
914
  SColCmprWrapper* p = &req.colCmpr;
118✔
915
  if (p->nCols == 0) {
118!
916
    createDefaultCompress = 1;
×
917
  }
918
  // build create stable
919
  pReq.pColumns = taosArrayInit(req.schemaRow.nCols, sizeof(SFieldWithOptions));
118✔
920
  RAW_NULL_CHECK(pReq.pColumns);
118!
921
  for (int32_t i = 0; i < req.schemaRow.nCols; i++) {
744✔
922
    SSchema*          pSchema = req.schemaRow.pSchema + i;
626✔
923
    SFieldWithOptions field = {.type = pSchema->type, .flags = pSchema->flags, .bytes = pSchema->bytes};
626✔
924
    tstrncpy(field.name, pSchema->name, TSDB_COL_NAME_LEN);
626✔
925

926
    if (createDefaultCompress) {
626!
927
      field.compress = createDefaultColCmprByType(pSchema->type);
×
928
    } else {
929
      SColCmpr* pCmp = &req.colCmpr.pColCmpr[i];
626✔
930
      field.compress = pCmp->alg;
626✔
931
    }
932
    RAW_NULL_CHECK(taosArrayPush(pReq.pColumns, &field));
1,252!
933
  }
934
  pReq.pTags = taosArrayInit(req.schemaTag.nCols, sizeof(SField));
118✔
935
  RAW_NULL_CHECK(pReq.pTags);
118!
936
  for (int32_t i = 0; i < req.schemaTag.nCols; i++) {
440✔
937
    SSchema* pSchema = req.schemaTag.pSchema + i;
322✔
938
    SField   field = {.type = pSchema->type, .flags = pSchema->flags, .bytes = pSchema->bytes};
322✔
939
    tstrncpy(field.name, pSchema->name, TSDB_COL_NAME_LEN);
322✔
940
    RAW_NULL_CHECK(taosArrayPush(pReq.pTags, &field));
644!
941
  }
942

943
  pReq.colVer = req.schemaRow.version;
118✔
944
  pReq.tagVer = req.schemaTag.version;
118✔
945
  pReq.numOfColumns = req.schemaRow.nCols;
118✔
946
  pReq.numOfTags = req.schemaTag.nCols;
118✔
947
  pReq.commentLen = -1;
118✔
948
  pReq.suid = processSuid(req.suid, pRequest->pDb);
118✔
949
  pReq.source = TD_REQ_FROM_TAOX;
118✔
950
  pReq.igExists = true;
118✔
951

952
  uDebug(LOG_ID_TAG " create stable name:%s suid:%" PRId64 " processSuid:%" PRId64, LOG_ID_VALUE, req.name, req.suid,
118!
953
         pReq.suid);
954
  STscObj* pTscObj = pRequest->pTscObj;
118✔
955
  SName    tableName = {0};
118✔
956
  toName(pTscObj->acctId, pRequest->pDb, req.name, &tableName);
118✔
957
  RAW_RETURN_CHECK(tNameExtractFullName(&tableName, pReq.name));
118!
958
  SCmdMsgInfo pCmdMsg = {0};
118✔
959
  pCmdMsg.epSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
118✔
960
  pCmdMsg.msgType = TDMT_MND_CREATE_STB;
118✔
961
  pCmdMsg.msgLen = tSerializeSMCreateStbReq(NULL, 0, &pReq);
118✔
962
  if (pCmdMsg.msgLen <= 0) {
118!
963
    code = TSDB_CODE_INVALID_PARA;
×
964
    goto end;
×
965
  }
966
  pCmdMsg.pMsg = taosMemoryMalloc(pCmdMsg.msgLen);
118✔
967
  RAW_NULL_CHECK(pCmdMsg.pMsg);
118!
968
  if (tSerializeSMCreateStbReq(pCmdMsg.pMsg, pCmdMsg.msgLen, &pReq) <= 0) {
118!
969
    code = TSDB_CODE_INVALID_PARA;
×
970
    taosMemoryFree(pCmdMsg.pMsg);
×
971
    goto end;
×
972
  }
973

974
  SQuery pQuery = {0};
118✔
975
  pQuery.execMode = QUERY_EXEC_MODE_RPC;
118✔
976
  pQuery.pCmdMsg = &pCmdMsg;
118✔
977
  pQuery.msgType = pQuery.pCmdMsg->msgType;
118✔
978
  pQuery.stableQuery = true;
118✔
979

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

982
  taosMemoryFree(pCmdMsg.pMsg);
118✔
983

984
  if (pRequest->code == TSDB_CODE_SUCCESS) {
118!
985
    SCatalog* pCatalog = NULL;
118✔
986
    RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
118!
987
    RAW_RETURN_CHECK(catalogRemoveTableMeta(pCatalog, &tableName));
118!
988
  }
989

990
  code = pRequest->code;
118✔
991

992
end:
118✔
993
  uDebug(LOG_ID_TAG " create stable return, msg:%s", LOG_ID_VALUE, tstrerror(code));
118!
994
  destroyRequest(pRequest);
118✔
995
  tFreeSMCreateStbReq(&pReq);
118✔
996
  tDecoderClear(&coder);
118✔
997
  return code;
118✔
998
}
999

1000
static int32_t taosDropStb(TAOS* taos, void* meta, int32_t metaLen) {
8✔
1001
  SVDropStbReq req = {0};
8✔
1002
  SDecoder     coder = {0};
8✔
1003
  SMDropStbReq pReq = {0};
8✔
1004
  int32_t      code = TSDB_CODE_SUCCESS;
8✔
1005
  SRequestObj* pRequest = NULL;
8✔
1006

1007
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
8!
1008
  uDebug(LOG_ID_TAG " drop stable, meta:%p, metaLen:%d", LOG_ID_VALUE, meta, metaLen);
8!
1009
  pRequest->syncQuery = true;
8✔
1010
  if (!pRequest->pDb) {
8!
1011
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1012
    goto end;
×
1013
  }
1014
  // decode and process req
1015
  void*   data = POINTER_SHIFT(meta, sizeof(SMsgHead));
8✔
1016
  int32_t len = metaLen - sizeof(SMsgHead);
8✔
1017
  tDecoderInit(&coder, data, len);
8✔
1018
  if (tDecodeSVDropStbReq(&coder, &req) < 0) {
8!
1019
    code = TSDB_CODE_INVALID_PARA;
×
1020
    goto end;
×
1021
  }
1022

1023
  SCatalog* pCatalog = NULL;
8✔
1024
  RAW_RETURN_CHECK(catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog));
8!
1025
  SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
8✔
1026
                           .requestId = pRequest->requestId,
8✔
1027
                           .requestObjRefId = pRequest->self,
8✔
1028
                           .mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp)};
8✔
1029
  SName            pName = {0};
8✔
1030
  toName(pRequest->pTscObj->acctId, pRequest->pDb, req.name, &pName);
8✔
1031
  STableMeta* pTableMeta = NULL;
8✔
1032
  code = catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta);
8✔
1033
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
8✔
1034
    code = TSDB_CODE_SUCCESS;
2✔
1035
    taosMemoryFreeClear(pTableMeta);
2!
1036
    goto end;
2✔
1037
  }
1038
  if (code != TSDB_CODE_SUCCESS) {
6!
1039
    goto end;
×
1040
  }
1041
  pReq.suid = pTableMeta->uid;
6✔
1042
  taosMemoryFreeClear(pTableMeta);
6!
1043

1044
  // build drop stable
1045
  pReq.igNotExists = true;
6✔
1046
  pReq.source = TD_REQ_FROM_TAOX;
6✔
1047
  //  pReq.suid = processSuid(req.suid, pRequest->pDb);
1048

1049
  uDebug(LOG_ID_TAG " drop stable name:%s suid:%" PRId64 " new suid:%" PRId64, LOG_ID_VALUE, req.name, req.suid,
6!
1050
         pReq.suid);
1051
  STscObj* pTscObj = pRequest->pTscObj;
6✔
1052
  SName    tableName = {0};
6✔
1053
  toName(pTscObj->acctId, pRequest->pDb, req.name, &tableName);
6✔
1054
  if (tNameExtractFullName(&tableName, pReq.name) != 0) {
6!
1055
    code = TSDB_CODE_INVALID_PARA;
×
1056
    goto end;
×
1057
  }
1058

1059
  SCmdMsgInfo pCmdMsg = {0};
6✔
1060
  pCmdMsg.epSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
6✔
1061
  pCmdMsg.msgType = TDMT_MND_DROP_STB;
6✔
1062
  pCmdMsg.msgLen = tSerializeSMDropStbReq(NULL, 0, &pReq);
6✔
1063
  if (pCmdMsg.msgLen <= 0) {
6!
1064
    code = TSDB_CODE_INVALID_PARA;
×
1065
    goto end;
×
1066
  }
1067
  pCmdMsg.pMsg = taosMemoryMalloc(pCmdMsg.msgLen);
6✔
1068
  RAW_NULL_CHECK(pCmdMsg.pMsg);
6!
1069
  if (tSerializeSMDropStbReq(pCmdMsg.pMsg, pCmdMsg.msgLen, &pReq) <= 0) {
6!
1070
    code = TSDB_CODE_INVALID_PARA;
×
1071
    taosMemoryFree(pCmdMsg.pMsg);
×
1072
    goto end;
×
1073
  }
1074

1075
  SQuery pQuery = {0};
6✔
1076
  pQuery.execMode = QUERY_EXEC_MODE_RPC;
6✔
1077
  pQuery.pCmdMsg = &pCmdMsg;
6✔
1078
  pQuery.msgType = pQuery.pCmdMsg->msgType;
6✔
1079
  pQuery.stableQuery = true;
6✔
1080

1081
  launchQueryImpl(pRequest, &pQuery, true, NULL);  // ignore, because return value is pRequest
6✔
1082
  taosMemoryFree(pCmdMsg.pMsg);
6✔
1083
  if (pRequest->code == TSDB_CODE_SUCCESS) {
6!
1084
    // ignore the error code
1085
    RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
6!
1086
    RAW_RETURN_CHECK(catalogRemoveTableMeta(pCatalog, &tableName));
6!
1087
  }
1088

1089
  code = pRequest->code;
6✔
1090

1091
end:
8✔
1092
  uDebug(LOG_ID_TAG " drop stable return, msg:%s", LOG_ID_VALUE, tstrerror(code));
8!
1093
  destroyRequest(pRequest);
8✔
1094
  tDecoderClear(&coder);
8✔
1095
  return code;
8✔
1096
}
1097

1098
typedef struct SVgroupCreateTableBatch {
1099
  SVCreateTbBatchReq req;
1100
  SVgroupInfo        info;
1101
  char               dbName[TSDB_DB_NAME_LEN];
1102
} SVgroupCreateTableBatch;
1103

1104
static void destroyCreateTbReqBatch(void* data) {
133✔
1105
  SVgroupCreateTableBatch* pTbBatch = (SVgroupCreateTableBatch*)data;
133✔
1106
  taosArrayDestroy(pTbBatch->req.pArray);
133✔
1107
}
133✔
1108

1109
static int32_t taosCreateTable(TAOS* taos, void* meta, int32_t metaLen) {
131✔
1110
  SVCreateTbBatchReq req = {0};
131✔
1111
  SDecoder           coder = {0};
131✔
1112
  int32_t            code = TSDB_CODE_SUCCESS;
131✔
1113
  SRequestObj*       pRequest = NULL;
131✔
1114
  SQuery*            pQuery = NULL;
131✔
1115
  SHashObj*          pVgroupHashmap = NULL;
131✔
1116
  SArray*            pTagList = taosArrayInit(0, POINTER_BYTES);
131✔
1117
  RAW_NULL_CHECK(pTagList);
131!
1118
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
131!
1119
  uDebug(LOG_ID_TAG " create table, meta:%p, metaLen:%d", LOG_ID_VALUE, meta, metaLen);
131!
1120

1121
  pRequest->syncQuery = true;
131✔
1122
  if (!pRequest->pDb) {
131!
1123
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1124
    goto end;
×
1125
  }
1126
  // decode and process req
1127
  void*   data = POINTER_SHIFT(meta, sizeof(SMsgHead));
131✔
1128
  int32_t len = metaLen - sizeof(SMsgHead);
131✔
1129
  tDecoderInit(&coder, data, len);
131✔
1130
  if (tDecodeSVCreateTbBatchReq(&coder, &req) < 0) {
131!
1131
    code = TSDB_CODE_INVALID_PARA;
×
1132
    goto end;
×
1133
  }
1134

1135
  STscObj* pTscObj = pRequest->pTscObj;
131✔
1136

1137
  SVCreateTbReq* pCreateReq = NULL;
131✔
1138
  SCatalog*      pCatalog = NULL;
131✔
1139
  RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
131!
1140
  pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
131✔
1141
  RAW_NULL_CHECK(pVgroupHashmap);
131!
1142
  taosHashSetFreeFp(pVgroupHashmap, destroyCreateTbReqBatch);
131✔
1143

1144
  SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
131✔
1145
                           .requestId = pRequest->requestId,
131✔
1146
                           .requestObjRefId = pRequest->self,
131✔
1147
                           .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
131✔
1148

1149
  pRequest->tableList = taosArrayInit(req.nReqs, sizeof(SName));
131✔
1150
  RAW_NULL_CHECK(pRequest->tableList);
131!
1151
  // loop to create table
1152
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
272✔
1153
    pCreateReq = req.pReqs + iReq;
141✔
1154

1155
    SVgroupInfo pInfo = {0};
141✔
1156
    SName       pName = {0};
141✔
1157
    toName(pTscObj->acctId, pRequest->pDb, pCreateReq->name, &pName);
141✔
1158
    code = catalogGetTableHashVgroup(pCatalog, &conn, &pName, &pInfo);
141✔
1159
    if (code != TSDB_CODE_SUCCESS) {
141!
1160
      goto end;
×
1161
    }
1162

1163
    pCreateReq->flags |= TD_CREATE_IF_NOT_EXISTS;
141✔
1164
    // change tag cid to new cid
1165
    if (pCreateReq->type == TSDB_CHILD_TABLE) {
141✔
1166
      STableMeta* pTableMeta = NULL;
123✔
1167
      SName       sName = {0};
123✔
1168
      tb_uid_t    oldSuid = pCreateReq->ctb.suid;
123✔
1169
      //      pCreateReq->ctb.suid = processSuid(pCreateReq->ctb.suid, pRequest->pDb);
1170
      toName(pTscObj->acctId, pRequest->pDb, pCreateReq->ctb.stbName, &sName);
123✔
1171
      code = catalogGetTableMeta(pCatalog, &conn, &sName, &pTableMeta);
123✔
1172
      if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
123!
1173
        code = TSDB_CODE_SUCCESS;
×
1174
        taosMemoryFreeClear(pTableMeta);
×
1175
        continue;
×
1176
      }
1177

1178
      if (code != TSDB_CODE_SUCCESS) {
123!
1179
        goto end;
×
1180
      }
1181
      pCreateReq->ctb.suid = pTableMeta->uid;
123✔
1182

1183
      SArray* pTagVals = NULL;
123✔
1184
      code = tTagToValArray((STag*)pCreateReq->ctb.pTag, &pTagVals);
123✔
1185
      if (code != TSDB_CODE_SUCCESS) {
123!
1186
        taosMemoryFreeClear(pTableMeta);
×
1187
        goto end;
×
1188
      }
1189

1190
      bool rebuildTag = false;
123✔
1191
      for (int32_t i = 0; i < taosArrayGetSize(pCreateReq->ctb.tagName); i++) {
377✔
1192
        char* tName = taosArrayGet(pCreateReq->ctb.tagName, i);
254✔
1193
        if (tName == NULL) {
254!
1194
          continue;
×
1195
        }
1196
        for (int32_t j = pTableMeta->tableInfo.numOfColumns;
254✔
1197
             j < pTableMeta->tableInfo.numOfColumns + pTableMeta->tableInfo.numOfTags; j++) {
1,002✔
1198
          SSchema* tag = &pTableMeta->schema[j];
748✔
1199
          if (strcmp(tag->name, tName) == 0 && tag->type != TSDB_DATA_TYPE_JSON) {
748✔
1200
            STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, i);
240✔
1201
            if (pTagVal) {
240!
1202
              if (pTagVal->cid != tag->colId) {
240✔
1203
                pTagVal->cid = tag->colId;
21✔
1204
                rebuildTag = true;
21✔
1205
              }
1206
            } else {
1207
              uError("create tb invalid data %s, size:%d index:%d cid:%d", pCreateReq->name,
×
1208
                     (int)taosArrayGetSize(pTagVals), i, tag->colId);
1209
            }
1210
          }
1211
        }
1212
      }
1213
      taosMemoryFreeClear(pTableMeta);
123!
1214
      if (rebuildTag) {
123✔
1215
        STag* ppTag = NULL;
13✔
1216
        code = tTagNew(pTagVals, 1, false, &ppTag);
13✔
1217
        taosArrayDestroy(pTagVals);
13✔
1218
        pTagVals = NULL;
13✔
1219
        if (code != TSDB_CODE_SUCCESS) {
13!
1220
          goto end;
×
1221
        }
1222
        if (NULL == taosArrayPush(pTagList, &ppTag)) {
13!
1223
          tTagFree(ppTag);
×
1224
          goto end;
×
1225
        }
1226
        pCreateReq->ctb.pTag = (uint8_t*)ppTag;
13✔
1227
      }
1228
      taosArrayDestroy(pTagVals);
123✔
1229
    }
1230
    RAW_NULL_CHECK(taosArrayPush(pRequest->tableList, &pName));
282!
1231

1232
    SVgroupCreateTableBatch* pTableBatch = taosHashGet(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId));
141✔
1233
    if (pTableBatch == NULL) {
141✔
1234
      SVgroupCreateTableBatch tBatch = {0};
133✔
1235
      tBatch.info = pInfo;
133✔
1236
      tstrncpy(tBatch.dbName, pRequest->pDb, TSDB_DB_NAME_LEN);
133✔
1237

1238
      tBatch.req.pArray = taosArrayInit(4, sizeof(struct SVCreateTbReq));
133✔
1239
      RAW_NULL_CHECK(tBatch.req.pArray);
133!
1240
      RAW_NULL_CHECK(taosArrayPush(tBatch.req.pArray, pCreateReq));
266!
1241
      tBatch.req.source = TD_REQ_FROM_TAOX;
133✔
1242
      RAW_RETURN_CHECK(taosHashPut(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId), &tBatch, sizeof(tBatch)));
133!
1243
    } else {  // add to the correct vgroup
1244
      RAW_NULL_CHECK(taosArrayPush(pTableBatch->req.pArray, pCreateReq));
16!
1245
    }
1246
  }
1247

1248
  if (taosHashGetSize(pVgroupHashmap) == 0) {
131!
1249
    goto end;
×
1250
  }
1251
  SArray* pBufArray = NULL;
131✔
1252
  RAW_RETURN_CHECK(serializeVgroupsCreateTableBatch(pVgroupHashmap, &pBufArray));
131!
1253
  pQuery = NULL;
131✔
1254
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode**)&pQuery);
131✔
1255
  if (TSDB_CODE_SUCCESS != code) goto end;
131!
1256
  pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
131✔
1257
  pQuery->msgType = TDMT_VND_CREATE_TABLE;
131✔
1258
  pQuery->stableQuery = false;
131✔
1259
  code = nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT, &pQuery->pRoot);
131✔
1260
  if (TSDB_CODE_SUCCESS != code) goto end;
131!
1261
  RAW_NULL_CHECK(pQuery->pRoot);
131!
1262

1263
  RAW_RETURN_CHECK(rewriteToVnodeModifyOpStmt(pQuery, pBufArray));
131!
1264

1265
  launchQueryImpl(pRequest, pQuery, true, NULL);
131✔
1266
  if (pRequest->code == TSDB_CODE_SUCCESS) {
131!
1267
    RAW_RETURN_CHECK(removeMeta(pTscObj, pRequest->tableList, false));
131!
1268
  }
1269

1270
  code = pRequest->code;
131✔
1271

1272
end:
131✔
1273
  uDebug(LOG_ID_TAG " create table return, msg:%s", LOG_ID_VALUE, tstrerror(code));
131!
1274
  tDeleteSVCreateTbBatchReq(&req);
131✔
1275

1276
  taosHashCleanup(pVgroupHashmap);
131✔
1277
  destroyRequest(pRequest);
131✔
1278
  tDecoderClear(&coder);
131✔
1279
  qDestroyQuery(pQuery);
131✔
1280
  taosArrayDestroyP(pTagList, taosMemoryFree);
131✔
1281
  return code;
131✔
1282
}
1283

1284
typedef struct SVgroupDropTableBatch {
1285
  SVDropTbBatchReq req;
1286
  SVgroupInfo      info;
1287
  char             dbName[TSDB_DB_NAME_LEN];
1288
} SVgroupDropTableBatch;
1289

1290
static void destroyDropTbReqBatch(void* data) {
3✔
1291
  SVgroupDropTableBatch* pTbBatch = (SVgroupDropTableBatch*)data;
3✔
1292
  taosArrayDestroy(pTbBatch->req.pArray);
3✔
1293
}
3✔
1294

1295
static int32_t taosDropTable(TAOS* taos, void* meta, int32_t metaLen) {
5✔
1296
  SVDropTbBatchReq req = {0};
5✔
1297
  SDecoder         coder = {0};
5✔
1298
  int32_t          code = TSDB_CODE_SUCCESS;
5✔
1299
  SRequestObj*     pRequest = NULL;
5✔
1300
  SQuery*          pQuery = NULL;
5✔
1301
  SHashObj*        pVgroupHashmap = NULL;
5✔
1302

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

1306
  pRequest->syncQuery = true;
5✔
1307
  if (!pRequest->pDb) {
5!
1308
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1309
    goto end;
×
1310
  }
1311
  // decode and process req
1312
  void*   data = POINTER_SHIFT(meta, sizeof(SMsgHead));
5✔
1313
  int32_t len = metaLen - sizeof(SMsgHead);
5✔
1314
  tDecoderInit(&coder, data, len);
5✔
1315
  if (tDecodeSVDropTbBatchReq(&coder, &req) < 0) {
5!
1316
    code = TSDB_CODE_INVALID_PARA;
×
1317
    goto end;
×
1318
  }
1319

1320
  STscObj* pTscObj = pRequest->pTscObj;
5✔
1321

1322
  SVDropTbReq* pDropReq = NULL;
5✔
1323
  SCatalog*    pCatalog = NULL;
5✔
1324
  RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
5!
1325

1326
  pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
5✔
1327
  RAW_NULL_CHECK(pVgroupHashmap);
5!
1328
  taosHashSetFreeFp(pVgroupHashmap, destroyDropTbReqBatch);
5✔
1329

1330
  SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
5✔
1331
                           .requestId = pRequest->requestId,
5✔
1332
                           .requestObjRefId = pRequest->self,
5✔
1333
                           .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
5✔
1334
  pRequest->tableList = taosArrayInit(req.nReqs, sizeof(SName));
5✔
1335
  RAW_NULL_CHECK(pRequest->tableList);
5!
1336
  // loop to create table
1337
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
11✔
1338
    pDropReq = req.pReqs + iReq;
6✔
1339
    pDropReq->igNotExists = true;
6✔
1340
    //    pDropReq->suid = processSuid(pDropReq->suid, pRequest->pDb);
1341

1342
    SVgroupInfo pInfo = {0};
6✔
1343
    SName       pName = {0};
6✔
1344
    toName(pTscObj->acctId, pRequest->pDb, pDropReq->name, &pName);
6✔
1345
    RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &pInfo));
6!
1346

1347
    STableMeta* pTableMeta = NULL;
6✔
1348
    code = catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta);
6✔
1349
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
6✔
1350
      code = TSDB_CODE_SUCCESS;
2✔
1351
      taosMemoryFreeClear(pTableMeta);
2!
1352
      continue;
2✔
1353
    }
1354
    if (code != TSDB_CODE_SUCCESS) {
4!
1355
      goto end;
×
1356
    }
1357
    tb_uid_t oldSuid = pDropReq->suid;
4✔
1358
    pDropReq->suid = pTableMeta->suid;
4✔
1359
    taosMemoryFreeClear(pTableMeta);
4!
1360
    uDebug(LOG_ID_TAG " drop table name:%s suid:%" PRId64 " new suid:%" PRId64, LOG_ID_VALUE, pDropReq->name, oldSuid,
4!
1361
           pDropReq->suid);
1362

1363
    RAW_NULL_CHECK(taosArrayPush(pRequest->tableList, &pName));
8!
1364
    SVgroupDropTableBatch* pTableBatch = taosHashGet(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId));
4✔
1365
    if (pTableBatch == NULL) {
4✔
1366
      SVgroupDropTableBatch tBatch = {0};
3✔
1367
      tBatch.info = pInfo;
3✔
1368
      tBatch.req.pArray = taosArrayInit(TARRAY_MIN_SIZE, sizeof(SVDropTbReq));
3✔
1369
      RAW_NULL_CHECK(tBatch.req.pArray);
3!
1370
      RAW_NULL_CHECK(taosArrayPush(tBatch.req.pArray, pDropReq));
6!
1371
      RAW_RETURN_CHECK(taosHashPut(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId), &tBatch, sizeof(tBatch)));
3!
1372
    } else {  // add to the correct vgroup
1373
      RAW_NULL_CHECK(taosArrayPush(pTableBatch->req.pArray, pDropReq));
2!
1374
    }
1375
  }
1376

1377
  if (taosHashGetSize(pVgroupHashmap) == 0) {
5✔
1378
    goto end;
2✔
1379
  }
1380
  SArray* pBufArray = NULL;
3✔
1381
  RAW_RETURN_CHECK(serializeVgroupsDropTableBatch(pVgroupHashmap, &pBufArray));
3!
1382
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode**)&pQuery);
3✔
1383
  if (TSDB_CODE_SUCCESS != code) goto end;
3!
1384
  pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
3✔
1385
  pQuery->msgType = TDMT_VND_DROP_TABLE;
3✔
1386
  pQuery->stableQuery = false;
3✔
1387
  pQuery->pRoot = NULL;
3✔
1388
  code = nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT, &pQuery->pRoot);
3✔
1389
  if (TSDB_CODE_SUCCESS != code) goto end;
3!
1390
  RAW_RETURN_CHECK(rewriteToVnodeModifyOpStmt(pQuery, pBufArray));
3!
1391

1392
  launchQueryImpl(pRequest, pQuery, true, NULL);
3✔
1393
  if (pRequest->code == TSDB_CODE_SUCCESS) {
3!
1394
    RAW_RETURN_CHECK(removeMeta(pTscObj, pRequest->tableList, false));
3!
1395
  }
1396
  code = pRequest->code;
3✔
1397

1398
end:
5✔
1399
  uDebug(LOG_ID_TAG " drop table return, msg:%s", LOG_ID_VALUE, tstrerror(code));
5!
1400
  taosHashCleanup(pVgroupHashmap);
5✔
1401
  destroyRequest(pRequest);
5✔
1402
  tDecoderClear(&coder);
5✔
1403
  qDestroyQuery(pQuery);
5✔
1404
  return code;
5✔
1405
}
1406

1407
static int32_t taosDeleteData(TAOS* taos, void* meta, int32_t metaLen) {
3✔
1408
  SDeleteRes req = {0};
3✔
1409
  SDecoder   coder = {0};
3✔
1410
  char       sql[256] = {0};
3✔
1411
  int32_t    code = TSDB_CODE_SUCCESS;
3✔
1412

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

1415
  // decode and process req
1416
  void*   data = POINTER_SHIFT(meta, sizeof(SMsgHead));
3✔
1417
  int32_t len = metaLen - sizeof(SMsgHead);
3✔
1418
  tDecoderInit(&coder, data, len);
3✔
1419
  if (tDecodeDeleteRes(&coder, &req) < 0) {
3!
1420
    code = TSDB_CODE_INVALID_PARA;
×
1421
    goto end;
×
1422
  }
1423

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

1427
  TAOS_RES* res = taosQueryImpl(taos, sql, false, TD_REQ_FROM_TAOX);
3✔
1428
  RAW_NULL_CHECK(res);
3!
1429
  SRequestObj* pRequest = (SRequestObj*)res;
3✔
1430
  code = pRequest->code;
3✔
1431
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_PAR_GET_META_ERROR) {
3!
1432
    code = TSDB_CODE_SUCCESS;
1✔
1433
  }
1434
  taos_free_result(res);
3✔
1435

1436
end:
3✔
1437
  uDebug("connId:0x%" PRIx64 " delete data sql:%s, code:%s", *(int64_t*)taos, sql, tstrerror(code));
3!
1438
  tDecoderClear(&coder);
3✔
1439
  return code;
3✔
1440
}
1441

1442
static int32_t taosAlterTable(TAOS* taos, void* meta, int32_t metaLen) {
24✔
1443
  SVAlterTbReq   req = {0};
24✔
1444
  SDecoder       dcoder = {0};
24✔
1445
  int32_t        code = TSDB_CODE_SUCCESS;
24✔
1446
  SRequestObj*   pRequest = NULL;
24✔
1447
  SQuery*        pQuery = NULL;
24✔
1448
  SArray*        pArray = NULL;
24✔
1449
  SVgDataBlocks* pVgData = NULL;
24✔
1450

1451
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0));
24!
1452
  uDebug(LOG_ID_TAG " alter table, meta:%p, len:%d", LOG_ID_VALUE, meta, metaLen);
24!
1453
  pRequest->syncQuery = true;
24✔
1454
  if (!pRequest->pDb) {
24!
1455
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1456
    goto end;
×
1457
  }
1458
  // decode and process req
1459
  void*   data = POINTER_SHIFT(meta, sizeof(SMsgHead));
24✔
1460
  int32_t len = metaLen - sizeof(SMsgHead);
24✔
1461
  tDecoderInit(&dcoder, data, len);
24✔
1462
  if (tDecodeSVAlterTbReq(&dcoder, &req) < 0) {
24!
1463
    code = TSDB_CODE_INVALID_PARA;
×
1464
    goto end;
×
1465
  }
1466

1467
  // do not deal TSDB_ALTER_TABLE_UPDATE_OPTIONS
1468
  if (req.action == TSDB_ALTER_TABLE_UPDATE_OPTIONS) {
24✔
1469
    goto end;
4✔
1470
  }
1471

1472
  STscObj*  pTscObj = pRequest->pTscObj;
20✔
1473
  SCatalog* pCatalog = NULL;
20✔
1474
  RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog));
20!
1475
  SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
20✔
1476
                           .requestId = pRequest->requestId,
20✔
1477
                           .requestObjRefId = pRequest->self,
20✔
1478
                           .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
20✔
1479

1480
  SVgroupInfo pInfo = {0};
20✔
1481
  SName       pName = {0};
20✔
1482
  toName(pTscObj->acctId, pRequest->pDb, req.tbName, &pName);
20✔
1483
  RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &pInfo));
20!
1484
  pArray = taosArrayInit(1, sizeof(void*));
20✔
1485
  RAW_NULL_CHECK(pArray);
20!
1486

1487
  pVgData = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
20✔
1488
  RAW_NULL_CHECK(pVgData);
20!
1489
  pVgData->vg = pInfo;
20✔
1490

1491
  int tlen = 0;
20✔
1492
  req.source = TD_REQ_FROM_TAOX;
20✔
1493
  tEncodeSize(tEncodeSVAlterTbReq, &req, tlen, code);
20!
1494
  if (code != 0) {
20!
1495
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1496
    goto end;
×
1497
  }
1498
  tlen += sizeof(SMsgHead);
20✔
1499
  void* pMsg = taosMemoryMalloc(tlen);
20✔
1500
  RAW_NULL_CHECK(pMsg);
20!
1501
  ((SMsgHead*)pMsg)->vgId = htonl(pInfo.vgId);
20✔
1502
  ((SMsgHead*)pMsg)->contLen = htonl(tlen);
20✔
1503
  void*    pBuf = POINTER_SHIFT(pMsg, sizeof(SMsgHead));
20✔
1504
  SEncoder coder = {0};
20✔
1505
  tEncoderInit(&coder, pBuf, tlen - sizeof(SMsgHead));
20✔
1506
  code = tEncodeSVAlterTbReq(&coder, &req);
20✔
1507
  if (code != 0) {
20!
1508
    tEncoderClear(&coder);
×
1509
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1510
    goto end;
×
1511
  }
1512
  tEncoderClear(&coder);
20✔
1513

1514
  pVgData->pData = pMsg;
20✔
1515
  pVgData->size = tlen;
20✔
1516

1517
  pVgData->numOfTables = 1;
20✔
1518
  RAW_NULL_CHECK(taosArrayPush(pArray, &pVgData));
20!
1519

1520
  pQuery = NULL;
20✔
1521
  code = nodesMakeNode(QUERY_NODE_QUERY, (SNode**)&pQuery);
20✔
1522
  if (NULL == pQuery) goto end;
20!
1523
  pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
20✔
1524
  pQuery->msgType = TDMT_VND_ALTER_TABLE;
20✔
1525
  pQuery->stableQuery = false;
20✔
1526
  pQuery->pRoot = NULL;
20✔
1527
  code = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, &pQuery->pRoot);
20✔
1528
  if (TSDB_CODE_SUCCESS != code) goto end;
20!
1529
  RAW_RETURN_CHECK(rewriteToVnodeModifyOpStmt(pQuery, pArray));
20!
1530

1531
  launchQueryImpl(pRequest, pQuery, true, NULL);
20✔
1532

1533
  pVgData = NULL;
20✔
1534
  pArray = NULL;
20✔
1535
  code = pRequest->code;
20✔
1536
  if (code == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
20✔
1537
    code = TSDB_CODE_SUCCESS;
1✔
1538
  }
1539

1540
  if (pRequest->code == TSDB_CODE_SUCCESS) {
20✔
1541
    SExecResult* pRes = &pRequest->body.resInfo.execRes;
19✔
1542
    if (pRes->res != NULL) {
19✔
1543
      code = handleAlterTbExecRes(pRes->res, pCatalog);
16✔
1544
    }
1545
  }
1546
end:
4✔
1547
  uDebug(LOG_ID_TAG " alter table return, meta:%p, len:%d, msg:%s", LOG_ID_VALUE, meta, metaLen, tstrerror(code));
24!
1548
  taosArrayDestroy(pArray);
24✔
1549
  if (pVgData) taosMemoryFreeClear(pVgData->pData);
24!
1550
  taosMemoryFreeClear(pVgData);
24!
1551
  destroyRequest(pRequest);
24✔
1552
  tDecoderClear(&dcoder);
24✔
1553
  qDestroyQuery(pQuery);
24✔
1554
  return code;
24✔
1555
}
1556

1557
int taos_write_raw_block_with_fields(TAOS* taos, int rows, char* pData, const char* tbname, TAOS_FIELD* fields,
1✔
1558
                                     int numFields) {
1559
  return taos_write_raw_block_with_fields_with_reqid(taos, rows, pData, tbname, fields, numFields, 0);
1✔
1560
}
1561

1562
int taos_write_raw_block_with_fields_with_reqid(TAOS* taos, int rows, char* pData, const char* tbname,
1✔
1563
                                                TAOS_FIELD* fields, int numFields, int64_t reqid) {
1564
  if (!taos || !pData || !tbname) {
1!
1565
    return TSDB_CODE_INVALID_PARA;
×
1566
  }
1567
  int32_t     code = TSDB_CODE_SUCCESS;
1✔
1568
  STableMeta* pTableMeta = NULL;
1✔
1569
  SQuery*     pQuery = NULL;
1✔
1570
  SHashObj*   pVgHash = NULL;
1✔
1571

1572
  SRequestObj* pRequest = NULL;
1✔
1573
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, reqid));
1!
1574

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

1578
  pRequest->syncQuery = true;
1✔
1579
  if (!pRequest->pDb) {
1!
1580
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1581
    goto end;
×
1582
  }
1583

1584
  SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}};
1✔
1585
  tstrncpy(pName.dbname, pRequest->pDb, sizeof(pName.dbname));
1✔
1586
  tstrncpy(pName.tname, tbname, sizeof(pName.tname));
1✔
1587

1588
  struct SCatalog* pCatalog = NULL;
1✔
1589
  RAW_RETURN_CHECK(catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog));
1!
1590

1591
  SRequestConnInfo conn = {0};
1✔
1592
  conn.pTrans = pRequest->pTscObj->pAppInfo->pTransporter;
1✔
1593
  conn.requestId = pRequest->requestId;
1✔
1594
  conn.requestObjRefId = pRequest->self;
1✔
1595
  conn.mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp);
1✔
1596

1597
  SVgroupInfo vgData = {0};
1✔
1598
  RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &vgData));
1!
1599
  RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta));
1!
1600
  RAW_RETURN_CHECK(smlInitHandle(&pQuery));
1!
1601
  pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
1✔
1602
  RAW_NULL_CHECK(pVgHash);
1!
1603
  RAW_RETURN_CHECK(
1!
1604
      taosHashPut(pVgHash, (const char*)&vgData.vgId, sizeof(vgData.vgId), (char*)&vgData, sizeof(vgData)));
1605
  RAW_RETURN_CHECK(rawBlockBindData(pQuery, pTableMeta, pData, NULL, fields, numFields, false, NULL, 0, false));
1!
1606
  RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash));
1!
1607

1608
  launchQueryImpl(pRequest, pQuery, true, NULL);
1✔
1609
  code = pRequest->code;
1✔
1610

1611
end:
1✔
1612
  uDebug(LOG_ID_TAG " write raw block with field return, msg:%s", LOG_ID_VALUE, tstrerror(code));
1!
1613
  taosMemoryFreeClear(pTableMeta);
1!
1614
  qDestroyQuery(pQuery);
1✔
1615
  destroyRequest(pRequest);
1✔
1616
  taosHashCleanup(pVgHash);
1✔
1617
  return code;
1✔
1618
}
1619

1620
int taos_write_raw_block(TAOS* taos, int rows, char* pData, const char* tbname) {
7✔
1621
  return taos_write_raw_block_with_reqid(taos, rows, pData, tbname, 0);
7✔
1622
}
1623

1624
int taos_write_raw_block_with_reqid(TAOS* taos, int rows, char* pData, const char* tbname, int64_t reqid) {
7✔
1625
  if (!taos || !pData || !tbname) {
7!
1626
    return TSDB_CODE_INVALID_PARA;
×
1627
  }
1628
  int32_t     code = TSDB_CODE_SUCCESS;
7✔
1629
  STableMeta* pTableMeta = NULL;
7✔
1630
  SQuery*     pQuery = NULL;
7✔
1631
  SHashObj*   pVgHash = NULL;
7✔
1632

1633
  SRequestObj* pRequest = NULL;
7✔
1634
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, reqid));
7!
1635

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

1638
  pRequest->syncQuery = true;
7✔
1639
  if (!pRequest->pDb) {
7!
1640
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1641
    goto end;
×
1642
  }
1643

1644
  SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}};
7✔
1645
  tstrncpy(pName.dbname, pRequest->pDb, sizeof(pName.dbname));
7✔
1646
  tstrncpy(pName.tname, tbname, sizeof(pName.tname));
7✔
1647

1648
  struct SCatalog* pCatalog = NULL;
7✔
1649
  RAW_RETURN_CHECK(catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog));
7!
1650

1651
  SRequestConnInfo conn = {0};
7✔
1652
  conn.pTrans = pRequest->pTscObj->pAppInfo->pTransporter;
7✔
1653
  conn.requestId = pRequest->requestId;
7✔
1654
  conn.requestObjRefId = pRequest->self;
7✔
1655
  conn.mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp);
7✔
1656

1657
  SVgroupInfo vgData = {0};
7✔
1658
  RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &vgData));
7!
1659
  RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta));
7✔
1660
  RAW_RETURN_CHECK(smlInitHandle(&pQuery));
6!
1661
  pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
6✔
1662
  RAW_NULL_CHECK(pVgHash);
6!
1663
  RAW_RETURN_CHECK(
6!
1664
      taosHashPut(pVgHash, (const char*)&vgData.vgId, sizeof(vgData.vgId), (char*)&vgData, sizeof(vgData)));
1665
  RAW_RETURN_CHECK(rawBlockBindData(pQuery, pTableMeta, pData, NULL, NULL, 0, false, NULL, 0, false));
6✔
1666
  RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash));
4!
1667

1668
  launchQueryImpl(pRequest, pQuery, true, NULL);
4✔
1669
  code = pRequest->code;
4✔
1670

1671
end:
7✔
1672
  uDebug(LOG_ID_TAG " write raw block return, msg:%s", LOG_ID_VALUE, tstrerror(code));
7!
1673
  taosMemoryFreeClear(pTableMeta);
7✔
1674
  qDestroyQuery(pQuery);
7✔
1675
  destroyRequest(pRequest);
7✔
1676
  taosHashCleanup(pVgHash);
7✔
1677
  return code;
7✔
1678
}
1679

1680
static void* getRawDataFromRes(void* pRetrieve) {
128✔
1681
  void* rawData = NULL;
128✔
1682
  // deal with compatibility
1683
  if (*(int64_t*)pRetrieve == 0) {
128!
1684
    rawData = ((SRetrieveTableRsp*)pRetrieve)->data;
×
1685
  } else if (*(int64_t*)pRetrieve == 1) {
128!
1686
    rawData = ((SRetrieveTableRspForTmq*)pRetrieve)->data;
128✔
1687
  }
1688
  return rawData;
128✔
1689
}
1690

1691
static int32_t buildCreateTbMap(SMqDataRsp* rsp, SHashObj* pHashObj) {
10✔
1692
  // find schema data info
1693
  int32_t       code = 0;
10✔
1694
  SVCreateTbReq pCreateReq = {0};
10✔
1695
  SDecoder      decoderTmp = {0};
10✔
1696

1697
  for (int j = 0; j < rsp->createTableNum; j++) {
34✔
1698
    void** dataTmp = taosArrayGet(rsp->createTableReq, j);
24✔
1699
    RAW_NULL_CHECK(dataTmp);
24!
1700
    int32_t* lenTmp = taosArrayGet(rsp->createTableLen, j);
24✔
1701
    RAW_NULL_CHECK(lenTmp);
24!
1702

1703
    tDecoderInit(&decoderTmp, *dataTmp, *lenTmp);
24✔
1704
    RAW_RETURN_CHECK(tDecodeSVCreateTbReq(&decoderTmp, &pCreateReq));
24!
1705

1706
    if (pCreateReq.type != TSDB_CHILD_TABLE) {
24!
1707
      code = TSDB_CODE_INVALID_MSG;
×
1708
      goto end;
×
1709
    }
1710
    if (taosHashGet(pHashObj, pCreateReq.name, strlen(pCreateReq.name)) == NULL) {
24✔
1711
      RAW_RETURN_CHECK(
22!
1712
          taosHashPut(pHashObj, pCreateReq.name, strlen(pCreateReq.name), &pCreateReq, sizeof(SVCreateTbReq)));
1713
    } else {
1714
      tDestroySVCreateTbReq(&pCreateReq, TSDB_MSG_FLG_DECODE);
2✔
1715
      pCreateReq = (SVCreateTbReq){0};
2✔
1716
    }
1717

1718
    tDecoderClear(&decoderTmp);
24✔
1719
  }
1720
  return 0;
10✔
1721

1722
end:
×
1723
  tDecoderClear(&decoderTmp);
×
1724
  tDestroySVCreateTbReq(&pCreateReq, TSDB_MSG_FLG_DECODE);
×
1725
  return code;
×
1726
}
1727

1728
typedef enum {
1729
  WRITE_RAW_INIT_START = 0,
1730
  WRITE_RAW_INIT_OK,
1731
  WRITE_RAW_INIT_FAIL,
1732
} WRITE_RAW_INIT_STATUS;
1733

1734
static SHashObj* writeRawCache = NULL;
1735
static int8_t    initFlag = 0;
1736
static int8_t    initedFlag = WRITE_RAW_INIT_START;
1737

1738
typedef struct {
1739
  SHashObj* pVgHash;
1740
  SHashObj* pNameHash;
1741
  SHashObj* pMetaHash;
1742
} rawCacheInfo;
1743

1744
typedef struct {
1745
  SVgroupInfo vgInfo;
1746
  int64_t     uid;
1747
  int64_t     suid;
1748
} tbInfo;
1749

1750
static void tmqFreeMeta(void* data) {
42✔
1751
  STableMeta* pTableMeta = *(STableMeta**)data;
42✔
1752
  taosMemoryFree(pTableMeta);
42✔
1753
}
42✔
1754

1755
static void freeRawCache(void* data) {
×
1756
  rawCacheInfo* pRawCache = (rawCacheInfo*)data;
×
1757
  taosHashCleanup(pRawCache->pMetaHash);
×
1758
  taosHashCleanup(pRawCache->pNameHash);
×
1759
  taosHashCleanup(pRawCache->pVgHash);
×
1760
}
×
1761

1762
static int32_t initRawCacheHash() {
15✔
1763
  if (writeRawCache == NULL) {
15!
1764
    writeRawCache = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
15✔
1765
    if (writeRawCache == NULL) {
15!
1766
      return terrno;
×
1767
    }
1768
    taosHashSetFreeFp(writeRawCache, freeRawCache);
15✔
1769
  }
1770
  return 0;
15✔
1771
}
1772

1773
static bool needRefreshMeta(void* rawData, STableMeta* pTableMeta, SSchemaWrapper* pSW) {
7✔
1774
  char* p = (char*)rawData;
7✔
1775
  // | version | total length | total rows | blankFill | total columns | flag seg| block group id | column schema | each
1776
  // column length |
1777
  p += sizeof(int32_t);
7✔
1778
  p += sizeof(int32_t);
7✔
1779
  p += sizeof(int32_t);
7✔
1780
  p += sizeof(int32_t);
7✔
1781
  p += sizeof(int32_t);
7✔
1782
  p += sizeof(uint64_t);
7✔
1783
  int8_t* fields = p;
7✔
1784

1785
  if (pSW->nCols != pTableMeta->tableInfo.numOfColumns) {
7✔
1786
    return true;
4✔
1787
  }
1788
  for (int i = 0; i < pSW->nCols; i++) {
17✔
1789
    int j = 0;
14✔
1790
    for (; j < pTableMeta->tableInfo.numOfColumns; j++) {
40!
1791
      SSchema* pColSchema = &pTableMeta->schema[j];
40✔
1792
      char*    fieldName = pSW->pSchema[i].name;
40✔
1793

1794
      if (strcmp(pColSchema->name, fieldName) == 0) {
40✔
1795
        if (*fields != pColSchema->type || *(int32_t*)(fields + sizeof(int8_t)) != pColSchema->bytes) {
14!
1796
          return true;
×
1797
        }
1798
        break;
14✔
1799
      }
1800
    }
1801
    fields += sizeof(int8_t) + sizeof(int32_t);
14✔
1802

1803
    if (j == pTableMeta->tableInfo.numOfColumns) return true;
14!
1804
  }
1805
  return false;
3✔
1806
}
1807

1808
static int32_t getRawCache(SHashObj** pVgHash, SHashObj** pNameHash, SHashObj** pMetaHash, void* key) {
47✔
1809
  int32_t code = 0;
47✔
1810
  void*   cacheInfo = taosHashGet(writeRawCache, &key, POINTER_BYTES);
47✔
1811
  if (cacheInfo == NULL) {
47!
1812
    *pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
47✔
1813
    RAW_NULL_CHECK(*pVgHash);
47!
1814
    *pNameHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
47✔
1815
    RAW_NULL_CHECK(*pNameHash);
47!
1816
    *pMetaHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
47✔
1817
    RAW_NULL_CHECK(*pMetaHash);
47!
1818
    taosHashSetFreeFp(*pMetaHash, tmqFreeMeta);
47✔
1819
    rawCacheInfo info = {*pVgHash, *pNameHash, *pMetaHash};
47✔
1820
    RAW_RETURN_CHECK(taosHashPut(writeRawCache, &key, POINTER_BYTES, &info, sizeof(rawCacheInfo)));
47!
1821
  } else {
1822
    rawCacheInfo* info = (rawCacheInfo*)cacheInfo;
×
1823
    *pVgHash = info->pVgHash;
×
1824
    *pNameHash = info->pNameHash;
×
1825
    *pMetaHash = info->pMetaHash;
×
1826
  }
1827

1828
  return 0;
47✔
1829
end:
×
1830
  taosHashCleanup(*pMetaHash);
×
1831
  taosHashCleanup(*pNameHash);
×
1832
  taosHashCleanup(*pVgHash);
×
1833
  return code;
×
1834
}
1835

1836
static int32_t buildRawRequest(TAOS* taos, SRequestObj** pRequest, SCatalog** pCatalog, SRequestConnInfo* conn) {
47✔
1837
  int32_t code = 0;
47✔
1838
  RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, pRequest, 0));
47!
1839
  (*pRequest)->syncQuery = true;
47✔
1840
  if (!(*pRequest)->pDb) {
47!
1841
    code = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
1842
    goto end;
×
1843
  }
1844

1845
  RAW_RETURN_CHECK(catalogGetHandle((*pRequest)->pTscObj->pAppInfo->clusterId, pCatalog));
47!
1846
  conn->pTrans = (*pRequest)->pTscObj->pAppInfo->pTransporter;
47✔
1847
  conn->requestId = (*pRequest)->requestId;
47✔
1848
  conn->requestObjRefId = (*pRequest)->self;
47✔
1849
  conn->mgmtEps = getEpSet_s(&(*pRequest)->pTscObj->pAppInfo->mgmtEp);
47✔
1850

1851
end:
47✔
1852
  return code;
47✔
1853
}
1854

1855
typedef int32_t _raw_decode_func_(SDecoder* pDecoder, SMqDataRsp* pRsp);
1856
static int32_t  decodeRawData(SDecoder* decoder, void* data, int32_t dataLen, _raw_decode_func_ func,
47✔
1857
                              SMqRspObj* rspObj) {
1858
  int8_t dataVersion = *(int8_t*)data;
47✔
1859
  if (dataVersion >= MQ_DATA_RSP_VERSION) {
47!
1860
    data = POINTER_SHIFT(data, sizeof(int8_t) + sizeof(int32_t));
47✔
1861
    dataLen -= sizeof(int8_t) + sizeof(int32_t);
47✔
1862
  }
1863

1864
  rspObj->resIter = -1;
47✔
1865
  tDecoderInit(decoder, data, dataLen);
47✔
1866
  int32_t code = func(decoder, &rspObj->dataRsp);
47✔
1867
  if (code != 0) {
47!
1868
    SET_ERROR_MSG("decode mq taosx data rsp failed");
×
1869
  }
1870
  return code;
47✔
1871
}
1872

1873
static int32_t processCacheMeta(SHashObj* pVgHash, SHashObj* pNameHash, SHashObj* pMetaHash,
128✔
1874
                                SVCreateTbReq* pCreateReqDst, SCatalog* pCatalog, SRequestConnInfo* conn, SName* pName,
1875
                                STableMeta** pMeta, SSchemaWrapper* pSW, void* rawData, int32_t retry) {
1876
  int32_t     code = 0;
128✔
1877
  STableMeta* pTableMeta = NULL;
128✔
1878
  tbInfo*     tmpInfo = (tbInfo*)taosHashGet(pNameHash, pName->tname, strlen(pName->tname));
128✔
1879
  if (tmpInfo == NULL || retry > 0) {
128!
1880
    tbInfo info = {0};
121✔
1881

1882
    RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, conn, pName, &info.vgInfo));
121!
1883
    if (pCreateReqDst && tmpInfo == NULL) {  // change stable name to get meta
121!
1884
      tstrncpy(pName->tname, pCreateReqDst->ctb.stbName, TSDB_TABLE_NAME_LEN);
24✔
1885
    }
1886
    RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, conn, pName, &pTableMeta));
121!
1887
    info.uid = pTableMeta->uid;
121✔
1888
    if (pTableMeta->tableType == TSDB_CHILD_TABLE) {
121✔
1889
      info.suid = pTableMeta->suid;
82✔
1890
    } else {
1891
      info.suid = pTableMeta->uid;
39✔
1892
    }
1893
    code = taosHashPut(pMetaHash, &info.suid, LONG_BYTES, &pTableMeta, POINTER_BYTES);
121✔
1894
    if (code != 0) {
121!
1895
      taosMemoryFree(pTableMeta);
×
1896
      goto end;
×
1897
    }
1898
    if (pCreateReqDst) {
121✔
1899
      pTableMeta->vgId = info.vgInfo.vgId;
24✔
1900
      pTableMeta->uid = pCreateReqDst->uid;
24✔
1901
      pCreateReqDst->ctb.suid = pTableMeta->suid;
24✔
1902
    }
1903

1904
    RAW_RETURN_CHECK(taosHashPut(pNameHash, pName->tname, strlen(pName->tname), &info, sizeof(tbInfo)));
121!
1905
    tmpInfo = (tbInfo*)taosHashGet(pNameHash, pName->tname, strlen(pName->tname));
121✔
1906
    RAW_RETURN_CHECK(
121!
1907
        taosHashPut(pVgHash, &info.vgInfo.vgId, sizeof(info.vgInfo.vgId), &info.vgInfo, sizeof(SVgroupInfo)));
1908
  }
1909

1910
  if (pTableMeta == NULL || retry > 0) {
128!
1911
    STableMeta** pTableMetaTmp = (STableMeta**)taosHashGet(pMetaHash, &tmpInfo->suid, LONG_BYTES);
7✔
1912
    if (pTableMetaTmp == NULL || retry > 0 || needRefreshMeta(rawData, *pTableMetaTmp, pSW)) {
7!
1913
      RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, conn, pName, &pTableMeta));
4!
1914
      code = taosHashPut(pMetaHash, &tmpInfo->suid, LONG_BYTES, &pTableMeta, POINTER_BYTES);
4✔
1915
      if (code != 0) {
4!
1916
        taosMemoryFree(pTableMeta);
×
1917
        goto end;
×
1918
      }
1919

1920
    } else {
1921
      pTableMeta = *pTableMetaTmp;
3✔
1922
      pTableMeta->uid = tmpInfo->uid;
3✔
1923
      pTableMeta->vgId = tmpInfo->vgInfo.vgId;
3✔
1924
    }
1925
  }
1926
  *pMeta = pTableMeta;
128✔
1927

1928
end:
128✔
1929
  return code;
128✔
1930
}
1931

1932
static int32_t tmqWriteRawDataImpl(TAOS* taos, void* data, int32_t dataLen) {
37✔
1933
  int32_t   code = TSDB_CODE_SUCCESS;
37✔
1934
  SQuery*   pQuery = NULL;
37✔
1935
  SMqRspObj rspObj = {0};
37✔
1936
  SDecoder  decoder = {0};
37✔
1937

1938
  SRequestObj*     pRequest = NULL;
37✔
1939
  SCatalog*        pCatalog = NULL;
37✔
1940
  SRequestConnInfo conn = {0};
37✔
1941
  RAW_RETURN_CHECK(buildRawRequest(taos, &pRequest, &pCatalog, &conn));
37!
1942
  uDebug(LOG_ID_TAG " write raw data, data:%p, dataLen:%d", LOG_ID_VALUE, data, dataLen);
37!
1943
  RAW_RETURN_CHECK(decodeRawData(&decoder, data, dataLen, tDecodeMqDataRsp, &rspObj));
37!
1944

1945
  SHashObj* pVgHash = NULL;
37✔
1946
  SHashObj* pNameHash = NULL;
37✔
1947
  SHashObj* pMetaHash = NULL;
37✔
1948
  RAW_RETURN_CHECK(getRawCache(&pVgHash, &pNameHash, &pMetaHash, taos));
37!
1949
  int retry = 0;
37✔
1950
  while (1) {
1951
    RAW_RETURN_CHECK(smlInitHandle(&pQuery));
37!
1952
    uDebug(LOG_ID_TAG " write raw meta data block num:%d", LOG_ID_VALUE, rspObj.dataRsp.blockNum);
37!
1953
    while (++rspObj.resIter < rspObj.dataRsp.blockNum) {
138✔
1954
      if (!rspObj.dataRsp.withSchema) {
101!
1955
        goto end;
×
1956
      }
1957

1958
      const char* tbName = (const char*)taosArrayGetP(rspObj.dataRsp.blockTbName, rspObj.resIter);
101✔
1959
      RAW_NULL_CHECK(tbName);
101!
1960
      SSchemaWrapper* pSW = (SSchemaWrapper*)taosArrayGetP(rspObj.dataRsp.blockSchema, rspObj.resIter);
101✔
1961
      RAW_NULL_CHECK(pSW);
101!
1962
      void* pRetrieve = taosArrayGetP(rspObj.dataRsp.blockData, rspObj.resIter);
101✔
1963
      RAW_NULL_CHECK(pRetrieve);
101!
1964
      void* rawData = getRawDataFromRes(pRetrieve);
101✔
1965
      RAW_NULL_CHECK(rawData);
101!
1966

1967
      uDebug(LOG_ID_TAG " write raw data block tbname:%s", LOG_ID_VALUE, tbName);
101!
1968
      SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}};
101✔
1969
      tstrncpy(pName.dbname, pRequest->pDb, TSDB_DB_NAME_LEN);
101✔
1970
      tstrncpy(pName.tname, tbName, TSDB_TABLE_NAME_LEN);
101✔
1971

1972
      STableMeta* pTableMeta = NULL;
101✔
1973
      RAW_RETURN_CHECK(processCacheMeta(pVgHash, pNameHash, pMetaHash, NULL, pCatalog, &conn, &pName, &pTableMeta, pSW,
101!
1974
                                        rawData, retry));
1975
      char err[ERR_MSG_LEN] = {0};
101✔
1976
      code = rawBlockBindData(pQuery, pTableMeta, rawData, NULL, pSW, pSW->nCols, true, err, ERR_MSG_LEN, true);
101✔
1977
      if (code != TSDB_CODE_SUCCESS) {
101!
1978
        SET_ERROR_MSG("table:%s, err:%s", pName.tname, err);
×
1979
        goto end;
×
1980
      }
1981
    }
1982
    RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash));
37!
1983
    launchQueryImpl(pRequest, pQuery, true, NULL);
37✔
1984
    code = pRequest->code;
37✔
1985

1986
    if (NEED_CLIENT_HANDLE_ERROR(code) && retry++ < 3) {
37!
1987
      uInfo("write raw retry:%d/3 end code:%d, msg:%s", retry, code, tstrerror(code));
×
1988
      qDestroyQuery(pQuery);
×
1989
      pQuery = NULL;
×
1990
      rspObj.resIter = -1;
×
1991
      continue;
×
1992
    }
1993
    break;
37✔
1994
  }
1995

1996
end:
37✔
1997
  uDebug(LOG_ID_TAG " write raw data return, msg:%s", LOG_ID_VALUE, tstrerror(code));
37!
1998
  tDeleteMqDataRsp(&rspObj.dataRsp);
37✔
1999
  tDecoderClear(&decoder);
37✔
2000
  qDestroyQuery(pQuery);
37✔
2001
  destroyRequest(pRequest);
37✔
2002
  return code;
37✔
2003
}
2004

2005
static int32_t tmqWriteRawMetaDataImpl(TAOS* taos, void* data, int32_t dataLen) {
10✔
2006
  int32_t   code = TSDB_CODE_SUCCESS;
10✔
2007
  SQuery*   pQuery = NULL;
10✔
2008
  SMqRspObj rspObj = {0};
10✔
2009
  SDecoder  decoder = {0};
10✔
2010
  SHashObj* pCreateTbHash = NULL;
10✔
2011

2012
  SRequestObj*     pRequest = NULL;
10✔
2013
  SCatalog*        pCatalog = NULL;
10✔
2014
  SRequestConnInfo conn = {0};
10✔
2015

2016
  RAW_RETURN_CHECK(buildRawRequest(taos, &pRequest, &pCatalog, &conn));
10!
2017
  uDebug(LOG_ID_TAG " write raw metadata, data:%p, dataLen:%d", LOG_ID_VALUE, data, dataLen);
10!
2018
  RAW_RETURN_CHECK(decodeRawData(&decoder, data, dataLen, tDecodeSTaosxRsp, &rspObj));
10!
2019

2020
  pCreateTbHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
10✔
2021
  RAW_NULL_CHECK(pCreateTbHash);
10!
2022
  RAW_RETURN_CHECK(buildCreateTbMap(&rspObj.dataRsp, pCreateTbHash));
10!
2023

2024
  SHashObj* pVgHash = NULL;
10✔
2025
  SHashObj* pNameHash = NULL;
10✔
2026
  SHashObj* pMetaHash = NULL;
10✔
2027
  RAW_RETURN_CHECK(getRawCache(&pVgHash, &pNameHash, &pMetaHash, taos));
10!
2028
  int retry = 0;
10✔
2029
  while (1) {
2030
    RAW_RETURN_CHECK(smlInitHandle(&pQuery));
10!
2031
    uDebug(LOG_ID_TAG " write raw meta data block num:%d", LOG_ID_VALUE, rspObj.dataRsp.blockNum);
10!
2032
    while (++rspObj.resIter < rspObj.dataRsp.blockNum) {
37✔
2033
      if (!rspObj.dataRsp.withSchema) {
27!
2034
        goto end;
×
2035
      }
2036

2037
      const char* tbName = (const char*)taosArrayGetP(rspObj.dataRsp.blockTbName, rspObj.resIter);
27✔
2038
      RAW_NULL_CHECK(tbName);
27!
2039
      SSchemaWrapper* pSW = (SSchemaWrapper*)taosArrayGetP(rspObj.dataRsp.blockSchema, rspObj.resIter);
27✔
2040
      RAW_NULL_CHECK(pSW);
27!
2041
      void* pRetrieve = taosArrayGetP(rspObj.dataRsp.blockData, rspObj.resIter);
27✔
2042
      RAW_NULL_CHECK(pRetrieve);
27!
2043
      void* rawData = getRawDataFromRes(pRetrieve);
27✔
2044
      RAW_NULL_CHECK(rawData);
27!
2045

2046
      uDebug(LOG_ID_TAG " write raw data block tbname:%s", LOG_ID_VALUE, tbName);
27!
2047
      SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}};
27✔
2048
      tstrncpy(pName.dbname, pRequest->pDb, TSDB_DB_NAME_LEN);
27✔
2049
      tstrncpy(pName.tname, tbName, TSDB_TABLE_NAME_LEN);
27✔
2050

2051
      // find schema data info
2052
      SVCreateTbReq* pCreateReqDst = (SVCreateTbReq*)taosHashGet(pCreateTbHash, pName.tname, strlen(pName.tname));
27✔
2053
      STableMeta*    pTableMeta = NULL;
27✔
2054
      RAW_RETURN_CHECK(processCacheMeta(pVgHash, pNameHash, pMetaHash, pCreateReqDst, pCatalog, &conn, &pName,
27!
2055
                                        &pTableMeta, pSW, rawData, retry));
2056
      char err[ERR_MSG_LEN] = {0};
27✔
2057
      code =
2058
          rawBlockBindData(pQuery, pTableMeta, rawData, pCreateReqDst, pSW, pSW->nCols, true, err, ERR_MSG_LEN, true);
27✔
2059
      if (code != TSDB_CODE_SUCCESS) {
27!
2060
        SET_ERROR_MSG("table:%s, err:%s", pName.tname, err);
×
2061
        goto end;
×
2062
      }
2063
    }
2064
    RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash));
10!
2065
    launchQueryImpl(pRequest, pQuery, true, NULL);
10✔
2066
    code = pRequest->code;
10✔
2067

2068
    if (NEED_CLIENT_HANDLE_ERROR(code) && retry++ < 3) {
10!
2069
      uInfo("write raw retry:%d/3 end code:%d, msg:%s", retry, code, tstrerror(code));
×
2070
      qDestroyQuery(pQuery);
×
2071
      pQuery = NULL;
×
2072
      rspObj.resIter = -1;
×
2073
      continue;
×
2074
    }
2075
    break;
10✔
2076
  }
2077

2078
end:
10✔
2079
  uDebug(LOG_ID_TAG " write raw metadata return, msg:%s", LOG_ID_VALUE, tstrerror(code));
10!
2080
  tDeleteSTaosxRsp(&rspObj.dataRsp);
10✔
2081
  void* pIter = taosHashIterate(pCreateTbHash, NULL);
10✔
2082
  while (pIter) {
32✔
2083
    tDestroySVCreateTbReq(pIter, TSDB_MSG_FLG_DECODE);
22✔
2084
    pIter = taosHashIterate(pCreateTbHash, pIter);
22✔
2085
  }
2086
  taosHashCleanup(pCreateTbHash);
10✔
2087
  tDecoderClear(&decoder);
10✔
2088
  qDestroyQuery(pQuery);
10✔
2089
  destroyRequest(pRequest);
10✔
2090
  return code;
10✔
2091
}
2092

2093
static void processSimpleMeta(SMqMetaRsp* pMetaRsp, cJSON** meta) {
289✔
2094
  if (pMetaRsp->resMsgType == TDMT_VND_CREATE_STB) {
289✔
2095
    processCreateStb(pMetaRsp, meta);
78✔
2096
  } else if (pMetaRsp->resMsgType == TDMT_VND_ALTER_STB) {
211✔
2097
    processAlterStb(pMetaRsp, meta);
40✔
2098
  } else if (pMetaRsp->resMsgType == TDMT_VND_DROP_STB) {
171✔
2099
    processDropSTable(pMetaRsp, meta);
8✔
2100
  } else if (pMetaRsp->resMsgType == TDMT_VND_CREATE_TABLE) {
163✔
2101
    processCreateTable(pMetaRsp, meta);
131✔
2102
  } else if (pMetaRsp->resMsgType == TDMT_VND_ALTER_TABLE) {
32✔
2103
    processAlterTable(pMetaRsp, meta);
24✔
2104
  } else if (pMetaRsp->resMsgType == TDMT_VND_DROP_TABLE) {
8✔
2105
    processDropTable(pMetaRsp, meta);
5✔
2106
  } else if (pMetaRsp->resMsgType == TDMT_VND_DROP_TABLE) {
3!
2107
    processDropTable(pMetaRsp, meta);
×
2108
  } else if (pMetaRsp->resMsgType == TDMT_VND_DELETE) {
3!
2109
    processDeleteTable(pMetaRsp, meta);
3✔
2110
  }
2111
}
289✔
2112

2113
static void processBatchMetaToJson(SMqBatchMetaRsp* pMsgRsp, char** string) {
14✔
2114
  SDecoder        coder;
2115
  SMqBatchMetaRsp rsp = {0};
14✔
2116
  int32_t         code = 0;
14✔
2117
  cJSON*          pJson = NULL;
14✔
2118
  tDecoderInit(&coder, pMsgRsp->pMetaBuff, pMsgRsp->metaBuffLen);
14✔
2119
  if (tDecodeMqBatchMetaRsp(&coder, &rsp) < 0) {
14!
2120
    goto end;
×
2121
  }
2122

2123
  pJson = cJSON_CreateObject();
14✔
2124
  RAW_NULL_CHECK(pJson);
14!
2125
  RAW_FALSE_CHECK(cJSON_AddStringToObject(pJson, "tmq_meta_version", TMQ_META_VERSION));
14!
2126
  cJSON* pMetaArr = cJSON_CreateArray();
14✔
2127
  RAW_NULL_CHECK(pMetaArr);
14!
2128
  int32_t num = taosArrayGetSize(rsp.batchMetaReq);
14✔
2129
  for (int32_t i = 0; i < num; i++) {
143✔
2130
    int32_t* len = taosArrayGet(rsp.batchMetaLen, i);
129✔
2131
    RAW_NULL_CHECK(len);
129!
2132
    void* tmpBuf = taosArrayGetP(rsp.batchMetaReq, i);
129✔
2133
    RAW_NULL_CHECK(tmpBuf);
129!
2134
    SDecoder   metaCoder = {0};
129✔
2135
    SMqMetaRsp metaRsp = {0};
129✔
2136
    tDecoderInit(&metaCoder, POINTER_SHIFT(tmpBuf, sizeof(SMqRspHead)), *len - sizeof(SMqRspHead));
129✔
2137
    if (tDecodeMqMetaRsp(&metaCoder, &metaRsp) < 0) {
129!
2138
      goto end;
×
2139
    }
2140
    cJSON* pItem = NULL;
129✔
2141
    processSimpleMeta(&metaRsp, &pItem);
129✔
2142
    tDeleteMqMetaRsp(&metaRsp);
129✔
2143
    RAW_FALSE_CHECK(cJSON_AddItemToArray(pMetaArr, pItem));
129!
2144
  }
2145

2146
  RAW_FALSE_CHECK(cJSON_AddItemToObject(pJson, "metas", pMetaArr));
14!
2147
  tDeleteMqBatchMetaRsp(&rsp);
14✔
2148
  char* fullStr = cJSON_PrintUnformatted(pJson);
14✔
2149
  cJSON_Delete(pJson);
14✔
2150
  *string = fullStr;
14✔
2151
  return;
14✔
2152

2153
end:
×
2154
  cJSON_Delete(pJson);
×
2155
  tDeleteMqBatchMetaRsp(&rsp);
×
2156
}
2157

2158
char* tmq_get_json_meta(TAOS_RES* res) {
184✔
2159
  if (res == NULL) return NULL;
184!
2160
  uDebug("tmq_get_json_meta res:%p", res);
184!
2161
  if (!TD_RES_TMQ_META(res) && !TD_RES_TMQ_METADATA(res) && !TD_RES_TMQ_BATCH_META(res)) {
184!
2162
    return NULL;
×
2163
  }
2164

2165
  char*      string = NULL;
184✔
2166
  SMqRspObj* rspObj = (SMqRspObj*)res;
184✔
2167
  if (TD_RES_TMQ_METADATA(res)) {
184✔
2168
    processAutoCreateTable(&rspObj->dataRsp, &string);
10✔
2169
  } else if (TD_RES_TMQ_BATCH_META(res)) {
174✔
2170
    processBatchMetaToJson(&rspObj->batchMetaRsp, &string);
14✔
2171
  } else if (TD_RES_TMQ_META(res)) {
160!
2172
    cJSON* pJson = NULL;
160✔
2173
    processSimpleMeta(&rspObj->metaRsp, &pJson);
160✔
2174
    string = cJSON_PrintUnformatted(pJson);
160✔
2175
    cJSON_Delete(pJson);
160✔
2176
  } else {
2177
    uError("tmq_get_json_meta res:%d, invalid type", *(int8_t*)res);
×
2178
  }
2179

2180
  uDebug("tmq_get_json_meta string:%s", string);
184!
2181
  return string;
184✔
2182
}
2183

2184
void tmq_free_json_meta(char* jsonMeta) { taosMemoryFreeClear(jsonMeta); }
228!
2185

2186
static int32_t getOffSetLen(const SMqDataRsp* pRsp) {
47✔
2187
  SEncoder coder = {0};
47✔
2188
  tEncoderInit(&coder, NULL, 0);
47✔
2189
  if (tEncodeSTqOffsetVal(&coder, &pRsp->reqOffset) < 0) return -1;
47!
2190
  if (tEncodeSTqOffsetVal(&coder, &pRsp->rspOffset) < 0) return -1;
47!
2191
  int32_t pos = coder.pos;
47✔
2192
  tEncoderClear(&coder);
47✔
2193
  return pos;
47✔
2194
}
2195

2196
typedef int32_t __encode_func__(SEncoder* pEncoder, const SMqDataRsp* pRsp);
2197
static int32_t  encodeMqDataRsp(__encode_func__* encodeFunc, SMqDataRsp* rspObj, tmq_raw_data* raw) {
47✔
2198
  int32_t  len = 0;
47✔
2199
  int32_t  code = 0;
47✔
2200
  SEncoder encoder = {0};
47✔
2201
  void*    buf = NULL;
47✔
2202
  tEncodeSize(encodeFunc, rspObj, len, code);
47!
2203
  if (code < 0) {
47!
2204
    code = TSDB_CODE_INVALID_MSG;
×
2205
    goto FAILED;
×
2206
  }
2207
  len += sizeof(int8_t) + sizeof(int32_t);
47✔
2208
  buf = taosMemoryCalloc(1, len);
47✔
2209
  if (buf == NULL) {
47!
2210
    code = terrno;
×
2211
    goto FAILED;
×
2212
  }
2213
  tEncoderInit(&encoder, buf, len);
47✔
2214
  if (tEncodeI8(&encoder, MQ_DATA_RSP_VERSION) < 0) {
47!
2215
    code = TSDB_CODE_INVALID_MSG;
×
2216
    goto FAILED;
×
2217
  }
2218
  int32_t offsetLen = getOffSetLen(rspObj);
47✔
2219
  if (offsetLen <= 0) {
47!
2220
    code = TSDB_CODE_INVALID_MSG;
×
2221
    goto FAILED;
×
2222
  }
2223
  if (tEncodeI32(&encoder, offsetLen) < 0) {
47!
2224
    code = TSDB_CODE_INVALID_MSG;
×
2225
    goto FAILED;
×
2226
  }
2227
  if (encodeFunc(&encoder, rspObj) < 0) {
47!
2228
    code = TSDB_CODE_INVALID_MSG;
×
2229
    goto FAILED;
×
2230
  }
2231
  tEncoderClear(&encoder);
47✔
2232

2233
  raw->raw = buf;
47✔
2234
  raw->raw_len = len;
47✔
2235
  return code;
47✔
2236
FAILED:
×
2237
  tEncoderClear(&encoder);
×
2238
  taosMemoryFree(buf);
×
2239
  return code;
×
2240
}
2241

2242
int32_t tmq_get_raw(TAOS_RES* res, tmq_raw_data* raw) {
246✔
2243
  if (!raw || !res) {
246!
2244
    return TSDB_CODE_INVALID_PARA;
×
2245
  }
2246
  SMqRspObj* rspObj = ((SMqRspObj*)res);
246✔
2247
  if (TD_RES_TMQ_META(res)) {
246✔
2248
    raw->raw = rspObj->metaRsp.metaRsp;
185✔
2249
    raw->raw_len = rspObj->metaRsp.metaRspLen;
185✔
2250
    raw->raw_type = rspObj->metaRsp.resMsgType;
185✔
2251
    uDebug("tmq get raw type meta:%p", raw);
185!
2252
  } else if (TD_RES_TMQ(res)) {
61✔
2253
    int32_t code = encodeMqDataRsp(tEncodeMqDataRsp, &rspObj->dataRsp, raw);
37✔
2254
    if (code != 0) {
37!
2255
      uError("tmq get raw type error:%d", terrno);
×
2256
      return code;
×
2257
    }
2258
    raw->raw_type = RES_TYPE__TMQ;
37✔
2259
    uDebug("tmq get raw type data:%p", raw);
37!
2260
  } else if (TD_RES_TMQ_METADATA(res)) {
24✔
2261
    int32_t code = encodeMqDataRsp(tEncodeSTaosxRsp, &rspObj->dataRsp, raw);
10✔
2262
    if (code != 0) {
10!
2263
      uError("tmq get raw type error:%d", terrno);
×
2264
      return code;
×
2265
    }
2266
    raw->raw_type = RES_TYPE__TMQ_METADATA;
10✔
2267
    uDebug("tmq get raw type metadata:%p", raw);
10!
2268
  } else if (TD_RES_TMQ_BATCH_META(res)) {
14!
2269
    raw->raw = rspObj->batchMetaRsp.pMetaBuff;
14✔
2270
    raw->raw_len = rspObj->batchMetaRsp.metaBuffLen;
14✔
2271
    raw->raw_type = rspObj->resType;
14✔
2272
    uDebug("tmq get raw batch meta:%p", raw);
14!
2273
  } else {
2274
    uError("tmq get raw error type:%d", *(int8_t*)res);
×
2275
    return TSDB_CODE_TMQ_INVALID_MSG;
×
2276
  }
2277
  return TSDB_CODE_SUCCESS;
246✔
2278
}
2279

2280
void tmq_free_raw(tmq_raw_data raw) {
246✔
2281
  uDebug("tmq free raw data type:%d", raw.raw_type);
246!
2282
  if (raw.raw_type == RES_TYPE__TMQ || raw.raw_type == RES_TYPE__TMQ_METADATA) {
246✔
2283
    taosMemoryFree(raw.raw);
47✔
2284
  }
2285
  (void)memset(terrMsg, 0, ERR_MSG_LEN);
246✔
2286
}
246✔
2287

2288
static int32_t writeRawInit() {
350✔
2289
  while (atomic_load_8(&initedFlag) == WRITE_RAW_INIT_START) {
365✔
2290
    int8_t old = atomic_val_compare_exchange_8(&initFlag, 0, 1);
15✔
2291
    if (old == 0) {
15!
2292
      int32_t code = initRawCacheHash();
15✔
2293
      if (code != 0) {
15!
2294
        uError("tmq writeRawImpl init error:%d", code);
×
2295
        atomic_store_8(&initedFlag, WRITE_RAW_INIT_FAIL);
×
2296
        return code;
×
2297
      }
2298
      atomic_store_8(&initedFlag, WRITE_RAW_INIT_OK);
15✔
2299
    }
2300
  }
2301

2302
  if (atomic_load_8(&initedFlag) == WRITE_RAW_INIT_FAIL) {
350!
2303
    return TSDB_CODE_INTERNAL_ERROR;
×
2304
  }
2305
  return 0;
350✔
2306
}
2307

2308
static int32_t writeRawImpl(TAOS* taos, void* buf, uint32_t len, uint16_t type) {
350✔
2309
  if (writeRawInit() != 0) {
350!
2310
    return TSDB_CODE_INTERNAL_ERROR;
×
2311
  }
2312

2313
  if (type == TDMT_VND_CREATE_STB) {
350✔
2314
    return taosCreateStb(taos, buf, len);
78✔
2315
  } else if (type == TDMT_VND_ALTER_STB) {
272✔
2316
    return taosCreateStb(taos, buf, len);
40✔
2317
  } else if (type == TDMT_VND_DROP_STB) {
232✔
2318
    return taosDropStb(taos, buf, len);
8✔
2319
  } else if (type == TDMT_VND_CREATE_TABLE) {
224✔
2320
    return taosCreateTable(taos, buf, len);
131✔
2321
  } else if (type == TDMT_VND_ALTER_TABLE) {
93✔
2322
    return taosAlterTable(taos, buf, len);
24✔
2323
  } else if (type == TDMT_VND_DROP_TABLE) {
69✔
2324
    return taosDropTable(taos, buf, len);
5✔
2325
  } else if (type == TDMT_VND_DELETE) {
64✔
2326
    return taosDeleteData(taos, buf, len);
3✔
2327
  } else if (type == RES_TYPE__TMQ_METADATA) {
61✔
2328
    return tmqWriteRawMetaDataImpl(taos, buf, len);
10✔
2329
  } else if (type == RES_TYPE__TMQ) {
51✔
2330
    return tmqWriteRawDataImpl(taos, buf, len);
37✔
2331
  } else if (type == RES_TYPE__TMQ_BATCH_META) {
14!
2332
    return tmqWriteBatchMetaDataImpl(taos, buf, len);
14✔
2333
  }
2334
  return TSDB_CODE_INVALID_PARA;
×
2335
}
2336

2337
int32_t tmq_write_raw(TAOS* taos, tmq_raw_data raw) {
234✔
2338
  if (taos == NULL || raw.raw == NULL || raw.raw_len <= 0) {
234!
2339
    SET_ERROR_MSG("taos:%p or data:%p is NULL or raw_len <= 0", taos, raw.raw);
13✔
2340
    return TSDB_CODE_INVALID_PARA;
13✔
2341
  }
2342

2343
  return writeRawImpl(taos, raw.raw, raw.raw_len, raw.raw_type);
221✔
2344
}
2345

2346
static int32_t tmqWriteBatchMetaDataImpl(TAOS* taos, void* meta, int32_t metaLen) {
14✔
2347
  if (taos == NULL || meta == NULL) {
14!
2348
    return TSDB_CODE_INVALID_PARA;
×
2349
  }
2350
  SMqBatchMetaRsp rsp = {0};
14✔
2351
  SDecoder        coder = {0};
14✔
2352
  int32_t         code = TSDB_CODE_SUCCESS;
14✔
2353

2354
  // decode and process req
2355
  tDecoderInit(&coder, meta, metaLen);
14✔
2356
  if (tDecodeMqBatchMetaRsp(&coder, &rsp) < 0) {
14!
2357
    code = TSDB_CODE_INVALID_PARA;
×
2358
    goto end;
×
2359
  }
2360
  int32_t num = taosArrayGetSize(rsp.batchMetaReq);
14✔
2361
  for (int32_t i = 0; i < num; i++) {
143✔
2362
    int32_t* len = taosArrayGet(rsp.batchMetaLen, i);
129✔
2363
    RAW_NULL_CHECK(len);
129!
2364
    void* tmpBuf = taosArrayGetP(rsp.batchMetaReq, i);
129✔
2365
    RAW_NULL_CHECK(tmpBuf);
129!
2366
    SDecoder   metaCoder = {0};
129✔
2367
    SMqMetaRsp metaRsp = {0};
129✔
2368
    tDecoderInit(&metaCoder, POINTER_SHIFT(tmpBuf, sizeof(SMqRspHead)), *len - sizeof(SMqRspHead));
129✔
2369
    if (tDecodeMqMetaRsp(&metaCoder, &metaRsp) < 0) {
129!
2370
      code = TSDB_CODE_INVALID_PARA;
×
2371
      goto end;
×
2372
    }
2373
    code = writeRawImpl(taos, metaRsp.metaRsp, metaRsp.metaRspLen, metaRsp.resMsgType);
129✔
2374
    tDeleteMqMetaRsp(&metaRsp);
129✔
2375
    if (code != TSDB_CODE_SUCCESS) {
129!
2376
      goto end;
×
2377
    }
2378
  }
2379

2380
end:
14✔
2381
  tDeleteMqBatchMetaRsp(&rsp);
14✔
2382
  return code;
14✔
2383
}
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