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

16
#include "os.h"
H
Haojun Liao 已提交
17 18
#include "query.h"
#include "tglobal.h"
L
Liu Jicong 已提交
19
#include "tmsg.h"
20
#include "trpc.h"
L
Liu Jicong 已提交
21
#include "tsched.h"
dengyihao's avatar
dengyihao 已提交
22
// clang-format off
D
dapan1121 已提交
23
#include "cJSON.h"
24

L
Liu Jicong 已提交
25 26
#define VALIDNUMOFCOLS(x) ((x) >= TSDB_MIN_COLUMNS && (x) <= TSDB_MAX_COLUMNS)
#define VALIDNUMOFTAGS(x) ((x) >= 0 && (x) <= TSDB_MAX_TAGS)
27 28 29

static struct SSchema _s = {
    .colId = TSDB_TBNAME_COLUMN_INDEX,
L
Liu Jicong 已提交
30
    .type = TSDB_DATA_TYPE_BINARY,
31 32 33 34
    .bytes = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE,
    .name = "tbname",
};

L
Liu Jicong 已提交
35
const SSchema* tGetTbnameColumnSchema() { return &_s; }
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

static bool doValidateSchema(SSchema* pSchema, int32_t numOfCols, int32_t maxLen) {
  int32_t rowLen = 0;

  for (int32_t i = 0; i < numOfCols; ++i) {
    // 1. valid types
    if (!isValidDataType(pSchema[i].type)) {
      return false;
    }

    // 2. valid length for each type
    if (pSchema[i].type == TSDB_DATA_TYPE_BINARY) {
      if (pSchema[i].bytes > TSDB_MAX_BINARY_LEN) {
        return false;
      }
    } else if (pSchema[i].type == TSDB_DATA_TYPE_NCHAR) {
      if (pSchema[i].bytes > TSDB_MAX_NCHAR_LEN) {
        return false;
      }
D
Dingle Zhang 已提交
55 56 57 58
    } else if (pSchema[i].type == TSDB_DATA_TYPE_GEOMETRY) {
      if (pSchema[i].bytes > TSDB_MAX_GEOMETRY_LEN) {
        return false;
      }
59 60 61 62 63 64 65 66
    } else {
      if (pSchema[i].bytes != tDataTypes[pSchema[i].type].bytes) {
        return false;
      }
    }

    // 3. valid column names
    for (int32_t j = i + 1; j < numOfCols; ++j) {
D
dapan1121 已提交
67
      if (strncmp(pSchema[i].name, pSchema[j].name, sizeof(pSchema[i].name) - 1) == 0) {
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        return false;
      }
    }

    rowLen += pSchema[i].bytes;
  }

  return rowLen <= maxLen;
}

bool tIsValidSchema(struct SSchema* pSchema, int32_t numOfCols, int32_t numOfTags) {
  if (!VALIDNUMOFCOLS(numOfCols)) {
    return false;
  }

  if (!VALIDNUMOFTAGS(numOfTags)) {
    return false;
  }

  /* first column must be the timestamp, which is a primary key */
  if (pSchema[0].type != TSDB_DATA_TYPE_TIMESTAMP) {
    return false;
  }

  if (!doValidateSchema(pSchema, numOfCols, TSDB_MAX_BYTES_PER_ROW)) {
    return false;
  }

  if (!doValidateSchema(&pSchema[numOfCols], numOfTags, TSDB_MAX_TAGS_LEN)) {
    return false;
  }

  return true;
H
Haojun Liao 已提交
101 102
}

D
dapan1121 已提交
103
static SSchedQueue pTaskQueue = {0};
H
Haojun Liao 已提交
104 105

int32_t initTaskQueue() {
S
Shengliang Guan 已提交
106
  int32_t queueSize = tsMaxShellConns * 2;
D
dapan1121 已提交
107 108
  void *p = taosInitScheduler(queueSize, tsNumOfTaskQueueThreads, "tsc", &pTaskQueue);
  if (NULL == p) {
H
Haojun Liao 已提交
109 110 111 112
    qError("failed to init task queue");
    return -1;
  }

S
Shengliang Guan 已提交
113
  qDebug("task queue is initialized, numOfThreads: %d", tsNumOfTaskQueueThreads);
L
Liu Jicong 已提交
114
  return 0;
H
Haojun Liao 已提交
115 116 117
}

int32_t cleanupTaskQueue() {
D
dapan1121 已提交
118
  taosCleanUpScheduler(&pTaskQueue);
L
Liu Jicong 已提交
119
  return 0;
H
Haojun Liao 已提交
120 121 122
}

static void execHelper(struct SSchedMsg* pSchedMsg) {
L
Liu Jicong 已提交
123 124
  __async_exec_fn_t execFn = (__async_exec_fn_t)pSchedMsg->ahandle;
  int32_t           code = execFn(pSchedMsg->thandle);
H
Haojun Liao 已提交
125
  if (code != 0 && pSchedMsg->msg != NULL) {
L
Liu Jicong 已提交
126
    *(int32_t*)pSchedMsg->msg = code;
H
Haojun Liao 已提交
127 128 129 130 131
  }
}

int32_t taosAsyncExec(__async_exec_fn_t execFn, void* execParam, int32_t* code) {
  SSchedMsg schedMsg = {0};
L
Liu Jicong 已提交
132
  schedMsg.fp = execHelper;
H
Haojun Liao 已提交
133 134
  schedMsg.ahandle = execFn;
  schedMsg.thandle = execParam;
L
Liu Jicong 已提交
135
  schedMsg.msg = code;
H
Haojun Liao 已提交
136

dengyihao's avatar
dengyihao 已提交
137
  return taosScheduleTask(&pTaskQueue, &schedMsg);
H
Haojun Liao 已提交
138
}
139

D
dapan1121 已提交
140
void destroySendMsgInfo(SMsgSendInfo* pMsgBody) {
D
dapan1121 已提交
141 142 143
  if (NULL == pMsgBody) {
    return;
  }
144

D
dapan1121 已提交
145 146 147 148 149 150 151
  taosMemoryFreeClear(pMsgBody->target.dbFName);
  taosMemoryFreeClear(pMsgBody->msgInfo.pData);
  if (pMsgBody->paramFreeFp) {
    (*pMsgBody->paramFreeFp)(pMsgBody->param);
  }
  taosMemoryFreeClear(pMsgBody);
}
dengyihao's avatar
dengyihao 已提交
152 153 154 155 156 157
void destroyAhandle(void *ahandle) {
  SMsgSendInfo *pSendInfo = ahandle;
  if (pSendInfo == NULL) return;

  destroySendMsgInfo(pSendInfo);
}
D
dapan1121 已提交
158

D
dapan1121 已提交
159
int32_t asyncSendMsgToServerExt(void* pTransporter, SEpSet* epSet, int64_t* pTransporterId, SMsgSendInfo* pInfo,
160
                                bool persistHandle, void* rpcCtx) {
L
Liu Jicong 已提交
161
  char* pMsg = rpcMallocCont(pInfo->msgInfo.len);
162
  if (NULL == pMsg) {
L
Liu Jicong 已提交
163
    qError("0x%" PRIx64 " msg:%s malloc failed", pInfo->requestId, TMSG_INFO(pInfo->msgType));
D
dapan1121 已提交
164
    destroySendMsgInfo(pInfo);
S
Shengliang Guan 已提交
165
    terrno = TSDB_CODE_OUT_OF_MEMORY;
166
    return terrno;
167 168 169
  }

  memcpy(pMsg, pInfo->msgInfo.pData, pInfo->msgInfo.len);
dengyihao's avatar
dengyihao 已提交
170 171 172 173 174 175
  SRpcMsg rpcMsg = {
    .msgType = pInfo->msgType,
    .pCont = pMsg,
    .contLen = pInfo->msgInfo.len,
    .info.ahandle = (void*)pInfo,
    .info.handle = pInfo->msgInfo.handle,
176
    .info.persistHandle = persistHandle,
dengyihao's avatar
dengyihao 已提交
177 178
    .code = 0
  };
dengyihao's avatar
dengyihao 已提交
179
  TRACE_SET_ROOTID(&rpcMsg.info.traceId, pInfo->requestId);
D
dapan1121 已提交
180 181 182 183 184
  int code = rpcSendRequestWithCtx(pTransporter, epSet, &rpcMsg, pTransporterId, rpcCtx);
  if (code) {
    destroySendMsgInfo(pInfo);
  }
  return code;
L
Liu Jicong 已提交
185
}
D
dapan1121 已提交
186

D
dapan1121 已提交
187
int32_t asyncSendMsgToServer(void* pTransporter, SEpSet* epSet, int64_t* pTransporterId, SMsgSendInfo* pInfo) {
188
  return asyncSendMsgToServerExt(pTransporter, epSet, pTransporterId, pInfo, false, NULL);
D
dapan1121 已提交
189 190
}

dengyihao's avatar
dengyihao 已提交
191
char* jobTaskStatusStr(int32_t status) {
D
dapan1121 已提交
192 193 194
  switch (status) {
    case JOB_TASK_STATUS_NULL:
      return "NULL";
D
dapan1121 已提交
195 196 197
    case JOB_TASK_STATUS_INIT:
      return "INIT";
    case JOB_TASK_STATUS_EXEC:
D
dapan1121 已提交
198
      return "EXECUTING";
D
dapan1121 已提交
199
    case JOB_TASK_STATUS_PART_SUCC:
D
dapan1121 已提交
200
      return "PARTIAL_SUCCEED";
D
dapan1121 已提交
201 202
    case JOB_TASK_STATUS_FETCH:
      return "FETCHING";
D
dapan1121 已提交
203
    case JOB_TASK_STATUS_SUCC:
D
dapan1121 已提交
204
      return "SUCCEED";
D
dapan1121 已提交
205
    case JOB_TASK_STATUS_FAIL:
D
dapan1121 已提交
206
      return "FAILED";
D
dapan1121 已提交
207
    case JOB_TASK_STATUS_DROP:
D
dapan1121 已提交
208 209 210 211 212 213 214
      return "DROPPING";
    default:
      break;
  }

  return "UNKNOWN";
}
D
dapan1121 已提交
215

D
dapan1121 已提交
216
#if 0
217
SSchema createSchema(int8_t type, int32_t bytes, col_id_t colId, const char* name) {
D
dapan1121 已提交
218
  SSchema s = {0};
dengyihao's avatar
dengyihao 已提交
219
  s.type = type;
D
dapan1121 已提交
220 221 222 223 224 225
  s.bytes = bytes;
  s.colId = colId;

  tstrncpy(s.name, name, tListLen(s.name));
  return s;
}
D
dapan1121 已提交
226
#endif
D
dapan1121 已提交
227

228 229 230 231 232
void freeSTableMetaRspPointer(void *p) {
  tFreeSTableMetaRsp(*(void**)p);
  taosMemoryFreeClear(*(void**)p);
}

D
dapan1121 已提交
233
void destroyQueryExecRes(SExecResult* pRes) {
D
dapan1121 已提交
234 235 236 237 238
  if (NULL == pRes || NULL == pRes->res) {
    return;
  }

  switch (pRes->msgType) {
239
    case TDMT_VND_CREATE_TABLE: {
240
      taosArrayDestroyEx((SArray*)pRes->res, freeSTableMetaRspPointer);
241 242 243
      break;
    }
    case TDMT_MND_CREATE_STB:
D
dapan1121 已提交
244 245
    case TDMT_VND_ALTER_TABLE:
    case TDMT_MND_ALTER_STB: {
246
      tFreeSTableMetaRsp(pRes->res);
D
dapan1121 已提交
247 248 249 250
      taosMemoryFreeClear(pRes->res);
      break;
    }
    case TDMT_VND_SUBMIT: {
251
      tDestroySSubmitRsp2((SSubmitRsp2*)pRes->res, TSDB_MSG_FLG_DECODE);
D
dapan1121 已提交
252
      taosMemoryFreeClear(pRes->res);
D
dapan1121 已提交
253
      break;
dengyihao's avatar
dengyihao 已提交
254
    }
255
    case TDMT_SCH_QUERY:
D
dapan1121 已提交
256
    case TDMT_SCH_MERGE_QUERY: {
D
dapan1121 已提交
257 258 259 260 261 262 263
      taosArrayDestroy((SArray*)pRes->res);
      break;
    }
    default:
      qError("invalid exec result for request type %d", pRes->msgType);
  }
}
dengyihao's avatar
dengyihao 已提交
264
// clang-format on
D
dapan1121 已提交
265

dengyihao's avatar
dengyihao 已提交
266
int32_t dataConverToStr(char* str, int type, void* buf, int32_t bufSize, int32_t* len) {
D
dapan1121 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
  int32_t n = 0;

  switch (type) {
    case TSDB_DATA_TYPE_NULL:
      n = sprintf(str, "null");
      break;

    case TSDB_DATA_TYPE_BOOL:
      n = sprintf(str, (*(int8_t*)buf) ? "true" : "false");
      break;

    case TSDB_DATA_TYPE_TINYINT:
      n = sprintf(str, "%d", *(int8_t*)buf);
      break;

    case TSDB_DATA_TYPE_SMALLINT:
      n = sprintf(str, "%d", *(int16_t*)buf);
      break;

    case TSDB_DATA_TYPE_INT:
      n = sprintf(str, "%d", *(int32_t*)buf);
      break;

    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TIMESTAMP:
      n = sprintf(str, "%" PRId64, *(int64_t*)buf);
      break;

    case TSDB_DATA_TYPE_FLOAT:
      n = sprintf(str, "%e", GET_FLOAT_VAL(buf));
      break;

    case TSDB_DATA_TYPE_DOUBLE:
      n = sprintf(str, "%e", GET_DOUBLE_VAL(buf));
      break;

    case TSDB_DATA_TYPE_BINARY:
D
Dingle Zhang 已提交
304
    case TSDB_DATA_TYPE_GEOMETRY:
D
dapan1121 已提交
305
      if (bufSize < 0) {
dengyihao's avatar
dengyihao 已提交
306
        //        tscError("invalid buf size");
D
dapan1121 已提交
307 308 309 310 311 312 313 314
        return TSDB_CODE_TSC_INVALID_VALUE;
      }

      *str = '"';
      memcpy(str + 1, buf, bufSize);
      *(str + bufSize + 1) = '"';
      n = bufSize + 2;
      break;
wmmhello's avatar
wmmhello 已提交
315 316 317 318 319
    case TSDB_DATA_TYPE_NCHAR:
      if (bufSize < 0) {
        //        tscError("invalid buf size");
        return TSDB_CODE_TSC_INVALID_VALUE;
      }
D
dapan1121 已提交
320

wmmhello's avatar
wmmhello 已提交
321
      *str = '"';
dengyihao's avatar
dengyihao 已提交
322
      int32_t length = taosUcs4ToMbs((TdUcs4*)buf, bufSize, str + 1);
wmmhello's avatar
wmmhello 已提交
323 324 325 326 327 328
      if (length <= 0) {
        return TSDB_CODE_TSC_INVALID_VALUE;
      }
      *(str + length + 1) = '"';
      n = length + 2;
      break;
D
dapan1121 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    case TSDB_DATA_TYPE_UTINYINT:
      n = sprintf(str, "%d", *(uint8_t*)buf);
      break;

    case TSDB_DATA_TYPE_USMALLINT:
      n = sprintf(str, "%d", *(uint16_t*)buf);
      break;

    case TSDB_DATA_TYPE_UINT:
      n = sprintf(str, "%u", *(uint32_t*)buf);
      break;

    case TSDB_DATA_TYPE_UBIGINT:
      n = sprintf(str, "%" PRIu64, *(uint64_t*)buf);
      break;

    default:
dengyihao's avatar
dengyihao 已提交
346
      //      tscError("unsupported type:%d", type);
D
dapan1121 已提交
347 348 349
      return TSDB_CODE_TSC_INVALID_VALUE;
  }

dengyihao's avatar
dengyihao 已提交
350
  if (len) *len = n;
D
dapan1121 已提交
351 352 353 354

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
355
char* parseTagDatatoJson(void* p) {
dengyihao's avatar
dengyihao 已提交
356
  char*   string = NULL;
D
dapan1121 已提交
357
  SArray* pTagVals = NULL;
dengyihao's avatar
dengyihao 已提交
358
  cJSON*  json = NULL;
D
dapan1121 已提交
359 360 361 362 363
  if (tTagToValArray((const STag*)p, &pTagVals) != 0) {
    goto end;
  }

  int16_t nCols = taosArrayGetSize(pTagVals);
wmmhello's avatar
wmmhello 已提交
364 365 366
  if (nCols == 0) {
    goto end;
  }
dengyihao's avatar
dengyihao 已提交
367
  char tagJsonKey[256] = {0};
wmmhello's avatar
wmmhello 已提交
368 369 370 371
  json = cJSON_CreateObject();
  if (json == NULL) {
    goto end;
  }
D
dapan1121 已提交
372 373 374
  for (int j = 0; j < nCols; ++j) {
    STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j);
    // json key  encode by binary
D
dapan1121 已提交
375
    tstrncpy(tagJsonKey, pTagVal->pKey, sizeof(tagJsonKey));
D
dapan1121 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
    // json value
    char type = pTagVal->type;
    if (type == TSDB_DATA_TYPE_NULL) {
      cJSON* value = cJSON_CreateNull();
      if (value == NULL) {
        goto end;
      }
      cJSON_AddItemToObject(json, tagJsonKey, value);
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
      cJSON* value = NULL;
      if (pTagVal->nData > 0) {
        char*   tagJsonValue = taosMemoryCalloc(pTagVal->nData, 1);
        int32_t length = taosUcs4ToMbs((TdUcs4*)pTagVal->pData, pTagVal->nData, tagJsonValue);
        if (length < 0) {
          qError("charset:%s to %s. val:%s convert json value failed.", DEFAULT_UNICODE_ENCODEC, tsCharset,
dengyihao's avatar
dengyihao 已提交
391
                 pTagVal->pData);
D
dapan1121 已提交
392 393 394 395 396 397 398 399 400 401 402
          taosMemoryFree(tagJsonValue);
          goto end;
        }
        value = cJSON_CreateString(tagJsonValue);
        taosMemoryFree(tagJsonValue);
        if (value == NULL) {
          goto end;
        }
      } else if (pTagVal->nData == 0) {
        value = cJSON_CreateString("");
      } else {
D
dapan1121 已提交
403
        goto end;
D
dapan1121 已提交
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
      }

      cJSON_AddItemToObject(json, tagJsonKey, value);
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
      double jsonVd = *(double*)(&pTagVal->i64);
      cJSON* value = cJSON_CreateNumber(jsonVd);
      if (value == NULL) {
        goto end;
      }
      cJSON_AddItemToObject(json, tagJsonKey, value);
    } else if (type == TSDB_DATA_TYPE_BOOL) {
      char   jsonVd = *(char*)(&pTagVal->i64);
      cJSON* value = cJSON_CreateBool(jsonVd);
      if (value == NULL) {
        goto end;
      }
      cJSON_AddItemToObject(json, tagJsonKey, value);
    } else {
D
dapan1121 已提交
422
      goto end;
D
dapan1121 已提交
423 424 425 426 427
    }
  }
  string = cJSON_PrintUnformatted(json);
end:
  cJSON_Delete(json);
wmmhello's avatar
wmmhello 已提交
428
  taosArrayDestroy(pTagVals);
dengyihao's avatar
dengyihao 已提交
429
  if (string == NULL) {
430
    string = taosStrdup(TSDB_DATA_NULL_STR_L);
wmmhello's avatar
wmmhello 已提交
431
  }
D
dapan1121 已提交
432 433 434
  return string;
}

D
dapan1121 已提交
435 436 437 438 439 440
int32_t cloneTableMeta(STableMeta* pSrc, STableMeta** pDst) {
  if (NULL == pSrc) {
    *pDst = NULL;
    return TSDB_CODE_SUCCESS;
  }

X
Xiaoyu Wang 已提交
441 442
  int32_t numOfField = pSrc->tableInfo.numOfColumns + pSrc->tableInfo.numOfTags;
  if (numOfField > TSDB_MAX_COL_TAG_NUM || numOfField < TSDB_MIN_COLUMNS) {
D
dapan1121 已提交
443 444 445 446 447
    *pDst = NULL;
    qError("too many column and tag num:%d,%d", pSrc->tableInfo.numOfColumns, pSrc->tableInfo.numOfTags);
    return TSDB_CODE_INVALID_PARA;
  }

X
Xiaoyu Wang 已提交
448
  int32_t metaSize = sizeof(STableMeta) + numOfField * sizeof(SSchema);
D
dapan1121 已提交
449 450
  *pDst = taosMemoryMalloc(metaSize);
  if (NULL == *pDst) {
S
Shengliang Guan 已提交
451
    return TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
452 453 454 455 456
  }
  memcpy(*pDst, pSrc, metaSize);
  return TSDB_CODE_SUCCESS;
}

457 458 459 460 461 462 463 464 465 466 467 468
void getColumnTypeFromMeta(STableMeta* pMeta, char* pName, ETableColumnType* pType) {
  int32_t nums = pMeta->tableInfo.numOfTags + pMeta->tableInfo.numOfColumns;
  for (int32_t i = 0; i < nums; ++i) {
    if (0 == strcmp(pName, pMeta->schema[i].name)) {
      *pType = (i < pMeta->tableInfo.numOfColumns) ? TCOL_TYPE_COLUMN : TCOL_TYPE_TAG;
      return;
    }
  }

  *pType = TCOL_TYPE_NONE;
}

469 470 471 472 473 474 475 476 477 478 479
void freeVgInfo(SDBVgInfo* vgInfo) {
  if (NULL == vgInfo) {
    return;
  }

  taosHashCleanup(vgInfo->vgHash);
  taosArrayDestroy(vgInfo->vgArray);

  taosMemoryFreeClear(vgInfo);
}

D
dapan1121 已提交
480 481 482 483 484
int32_t cloneDbVgInfo(SDBVgInfo* pSrc, SDBVgInfo** pDst) {
  if (NULL == pSrc) {
    *pDst = NULL;
    return TSDB_CODE_SUCCESS;
  }
dengyihao's avatar
dengyihao 已提交
485

D
dapan1121 已提交
486 487
  *pDst = taosMemoryMalloc(sizeof(*pSrc));
  if (NULL == *pDst) {
S
Shengliang Guan 已提交
488
    return TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
489 490 491
  }
  memcpy(*pDst, pSrc, sizeof(*pSrc));
  if (pSrc->vgHash) {
dengyihao's avatar
dengyihao 已提交
492 493
    (*pDst)->vgHash = taosHashInit(taosHashGetSize(pSrc->vgHash), taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true,
                                   HASH_ENTRY_LOCK);
D
dapan1121 已提交
494
    if (NULL == (*pDst)->vgHash) {
D
dapan1121 已提交
495
      taosMemoryFreeClear(*pDst);
S
Shengliang Guan 已提交
496
      return TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
497 498 499
    }

    SVgroupInfo* vgInfo = NULL;
dengyihao's avatar
dengyihao 已提交
500
    void*        pIter = taosHashIterate(pSrc->vgHash, NULL);
D
dapan1121 已提交
501 502 503
    while (pIter) {
      vgInfo = pIter;
      int32_t* vgId = taosHashGetKey(pIter, NULL);
dengyihao's avatar
dengyihao 已提交
504

D
dapan1121 已提交
505 506
      if (0 != taosHashPut((*pDst)->vgHash, vgId, sizeof(*vgId), vgInfo, sizeof(*vgInfo))) {
        qError("taosHashPut failed, vgId:%d", vgInfo->vgId);
dengyihao's avatar
dengyihao 已提交
507
        taosHashCancelIterate(pSrc->vgHash, pIter);
508
        freeVgInfo(*pDst);
D
dapan1121 已提交
509
        return TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
510
      }
dengyihao's avatar
dengyihao 已提交
511

D
dapan1121 已提交
512 513 514
      pIter = taosHashIterate(pSrc->vgHash, pIter);
    }
  }
dengyihao's avatar
dengyihao 已提交
515

D
dapan1121 已提交
516 517
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
518 519 520 521 522 523 524 525 526

int32_t cloneSVreateTbReq(SVCreateTbReq* pSrc, SVCreateTbReq** pDst) {
  if (NULL == pSrc) {
    *pDst = NULL;
    return TSDB_CODE_SUCCESS;
  }

  *pDst = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
  if (NULL == *pDst) {
H
Hongze Cheng 已提交
527
    return TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
528 529 530
  }

  (*pDst)->flags = pSrc->flags;
D
dapan1121 已提交
531
  if (pSrc->name) {
532
    (*pDst)->name = taosStrdup(pSrc->name);
D
dapan1121 已提交
533
  }
D
dapan1121 已提交
534
  (*pDst)->uid = pSrc->uid;
535
  (*pDst)->btime = pSrc->btime;
D
dapan1121 已提交
536 537
  (*pDst)->ttl = pSrc->ttl;
  (*pDst)->commentLen = pSrc->commentLen;
D
dapan1121 已提交
538
  if (pSrc->comment) {
539
    (*pDst)->comment = taosStrdup(pSrc->comment);
D
dapan1121 已提交
540
  }
D
dapan1121 已提交
541 542 543
  (*pDst)->type = pSrc->type;

  if (pSrc->type == TSDB_CHILD_TABLE) {
D
dapan1121 已提交
544
    if (pSrc->ctb.stbName) {
545
      (*pDst)->ctb.stbName = taosStrdup(pSrc->ctb.stbName);
D
dapan1121 已提交
546
    }
D
dapan1121 已提交
547 548
    (*pDst)->ctb.tagNum = pSrc->ctb.tagNum;
    (*pDst)->ctb.suid = pSrc->ctb.suid;
D
dapan1121 已提交
549 550 551
    if (pSrc->ctb.tagName) {
      (*pDst)->ctb.tagName = taosArrayDup(pSrc->ctb.tagName, NULL);
    }
H
Hongze Cheng 已提交
552
    STag* pTag = (STag*)pSrc->ctb.pTag;
D
dapan1121 已提交
553 554 555 556
    if (pTag) {
      (*pDst)->ctb.pTag = taosMemoryMalloc(pTag->len);
      memcpy((*pDst)->ctb.pTag, pTag, pTag->len);
    }
D
dapan1121 已提交
557 558 559
  } else {
    (*pDst)->ntb.schemaRow.nCols = pSrc->ntb.schemaRow.nCols;
    (*pDst)->ntb.schemaRow.version = pSrc->ntb.schemaRow.nCols;
D
dapan1121 已提交
560 561 562 563
    if (pSrc->ntb.schemaRow.nCols > 0 && pSrc->ntb.schemaRow.pSchema) {
      (*pDst)->ntb.schemaRow.pSchema = taosMemoryMalloc(pSrc->ntb.schemaRow.nCols * sizeof(SSchema));
      memcpy((*pDst)->ntb.schemaRow.pSchema, pSrc->ntb.schemaRow.pSchema, pSrc->ntb.schemaRow.nCols * sizeof(SSchema));
    }
D
dapan1121 已提交
564 565 566 567
  }

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
568 569 570 571 572 573 574 575

void freeDbCfgInfo(SDbCfgInfo *pInfo) {
  if (pInfo) {
    taosArrayDestroy(pInfo->pRetensions);
  }
  taosMemoryFree(pInfo);
}