parInsertUtil.c 19.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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/>.
 */
X
Xiaoyu Wang 已提交
15

X
Xiaoyu Wang 已提交
16
#include "parInsertUtil.h"
17 18

#include "catalog.h"
X
Xiaoyu Wang 已提交
19
#include "parInt.h"
X
Xiaoyu Wang 已提交
20
#include "parUtil.h"
X
Xiaoyu Wang 已提交
21
#include "querynodes.h"
C
Cary Xu 已提交
22
#include "tRealloc.h"
wmmhello's avatar
wmmhello 已提交
23
#include "tdatablock.h"
24

25 26 27 28 29 30 31 32 33 34
void qDestroyBoundColInfo(void* pInfo) {
  if (NULL == pInfo) {
    return;
  }

  SBoundColInfo* pBoundInfo = (SBoundColInfo*)pInfo;

  taosMemoryFreeClear(pBoundInfo->pColIndex);
}

X
Xiaoyu Wang 已提交
35 36 37 38 39 40 41 42
static char* tableNameGetPosition(SToken* pToken, char target) {
  bool inEscape = false;
  bool inQuote = false;
  char quotaStr = 0;

  for (uint32_t i = 0; i < pToken->n; ++i) {
    if (*(pToken->z + i) == target && (!inEscape) && (!inQuote)) {
      return pToken->z + i;
43 44
    }

X
Xiaoyu Wang 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    if (*(pToken->z + i) == TS_ESCAPE_CHAR) {
      if (!inQuote) {
        inEscape = !inEscape;
      }
    }

    if (*(pToken->z + i) == '\'' || *(pToken->z + i) == '"') {
      if (!inEscape) {
        if (!inQuote) {
          quotaStr = *(pToken->z + i);
          inQuote = !inQuote;
        } else if (quotaStr == *(pToken->z + i)) {
          inQuote = !inQuote;
        }
      }
60 61 62
    }
  }

X
Xiaoyu Wang 已提交
63
  return NULL;
64 65
}

X
Xiaoyu Wang 已提交
66 67 68 69 70 71 72 73
int32_t insCreateSName(SName* pName, SToken* pTableName, int32_t acctId, const char* dbName, SMsgBuf* pMsgBuf) {
  const char* msg1 = "name too long";
  const char* msg2 = "invalid database name";
  const char* msg3 = "db is not specified";
  const char* msg4 = "invalid table name";

  int32_t code = TSDB_CODE_SUCCESS;
  char*   p = tableNameGetPosition(pTableName, TS_PATH_DELIMITER[0]);
D
stmt  
dapan1121 已提交
74

X
Xiaoyu Wang 已提交
75 76 77 78
  if (p != NULL) {  // db has been specified in sql string so we ignore current db path
    int32_t dbLen = p - pTableName->z;
    if (dbLen <= 0) {
      return buildInvalidOperationMsg(pMsgBuf, msg2);
D
stmt  
dapan1121 已提交
79
    }
X
Xiaoyu Wang 已提交
80 81 82
    char name[TSDB_DB_FNAME_LEN] = {0};
    strncpy(name, pTableName->z, dbLen);
    int32_t actualDbLen = strdequote(name);
X
Xiaoyu Wang 已提交
83

X
Xiaoyu Wang 已提交
84 85 86 87
    code = tNameSetDbName(pName, acctId, name, actualDbLen);
    if (code != TSDB_CODE_SUCCESS) {
      return buildInvalidOperationMsg(pMsgBuf, msg1);
    }
X
Xiaoyu Wang 已提交
88

X
Xiaoyu Wang 已提交
89 90 91 92
    int32_t tbLen = pTableName->n - dbLen - 1;
    if (tbLen <= 0) {
      return buildInvalidOperationMsg(pMsgBuf, msg4);
    }
D
stmt  
dapan1121 已提交
93

X
Xiaoyu Wang 已提交
94 95 96
    char tbname[TSDB_TABLE_FNAME_LEN] = {0};
    strncpy(tbname, p + 1, tbLen);
    /*tbLen = */ strdequote(tbname);
D
stmt  
dapan1121 已提交
97

X
Xiaoyu Wang 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    code = tNameFromString(pName, tbname, T_NAME_TABLE);
    if (code != 0) {
      return buildInvalidOperationMsg(pMsgBuf, msg1);
    }
  } else {  // get current DB name first, and then set it into path
    if (pTableName->n >= TSDB_TABLE_NAME_LEN) {
      return buildInvalidOperationMsg(pMsgBuf, msg1);
    }

    char name[TSDB_TABLE_FNAME_LEN] = {0};
    strncpy(name, pTableName->z, pTableName->n);
    strdequote(name);

    if (dbName == NULL) {
      return buildInvalidOperationMsg(pMsgBuf, msg3);
    }
X
Xiaoyu Wang 已提交
114

X
Xiaoyu Wang 已提交
115 116 117 118 119
    code = tNameSetDbName(pName, acctId, dbName, strlen(dbName));
    if (code != TSDB_CODE_SUCCESS) {
      code = buildInvalidOperationMsg(pMsgBuf, msg2);
      return code;
    }
X
Xiaoyu Wang 已提交
120

X
Xiaoyu Wang 已提交
121 122 123
    code = tNameFromString(pName, name, T_NAME_TABLE);
    if (code != 0) {
      code = buildInvalidOperationMsg(pMsgBuf, msg1);
D
dapan1121 已提交
124 125 126
    }
  }

X
Xiaoyu Wang 已提交
127 128 129 130
  if (NULL != strchr(pName->tname, '.')) {
    code = generateSyntaxErrMsgExt(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, "The table name cannot contain '.'");
  }

X
Xiaoyu Wang 已提交
131
  return code;
D
stmt  
dapan1121 已提交
132 133
}

D
dapan1121 已提交
134
int16_t insFindCol(SToken* pColname, int16_t start, int16_t end, SSchema* pSchema) {
X
Xiaoyu Wang 已提交
135 136 137 138 139
  while (start < end) {
    if (strlen(pSchema[start].name) == pColname->n && strncmp(pColname->z, pSchema[start].name, pColname->n) == 0) {
      return start;
    }
    ++start;
D
stmt  
dapan1121 已提交
140
  }
X
Xiaoyu Wang 已提交
141 142
  return -1;
}
D
stmt  
dapan1121 已提交
143

X
Xiaoyu Wang 已提交
144
void insBuildCreateTbReq(SVCreateTbReq* pTbReq, const char* tname, STag* pTag, int64_t suid, const char* sname,
145
                         SArray* tagName, uint8_t tagNum, int32_t ttl) {
X
Xiaoyu Wang 已提交
146
  pTbReq->type = TD_CHILD_TABLE;
147
  pTbReq->name = taosStrdup(tname);
X
Xiaoyu Wang 已提交
148 149
  pTbReq->ctb.suid = suid;
  pTbReq->ctb.tagNum = tagNum;
150
  if (sname) pTbReq->ctb.stbName = taosStrdup(sname);
X
Xiaoyu Wang 已提交
151
  pTbReq->ctb.pTag = (uint8_t*)pTag;
H
Haojun Liao 已提交
152
  pTbReq->ctb.tagName = taosArrayDup(tagName, NULL);
153
  pTbReq->ttl = ttl;
X
Xiaoyu Wang 已提交
154 155 156 157
  pTbReq->commentLen = -1;

  return;
}
D
stmt  
dapan1121 已提交
158

D
dapan1121 已提交
159
static void initBoundCols(int32_t ncols, int16_t* pBoundCols) {
X
Xiaoyu Wang 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  for (int32_t i = 0; i < ncols; ++i) {
    pBoundCols[i] = i;
  }
}

static void initColValues(STableMeta* pTableMeta, SArray* pValues) {
  SSchema* pSchemas = getTableColumnSchema(pTableMeta);
  for (int32_t i = 0; i < pTableMeta->tableInfo.numOfColumns; ++i) {
    SColVal val = COL_VAL_NONE(pSchemas[i].colId, pSchemas[i].type);
    taosArrayPush(pValues, &val);
  }
}

int32_t insInitBoundColsInfo(int32_t numOfBound, SBoundColInfo* pInfo) {
  pInfo->numOfCols = numOfBound;
  pInfo->numOfBound = numOfBound;
D
dapan1121 已提交
176
  pInfo->pColIndex = taosMemoryCalloc(numOfBound, sizeof(int16_t));
X
Xiaoyu Wang 已提交
177 178 179 180 181 182 183
  if (NULL == pInfo->pColIndex) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  initBoundCols(numOfBound, pInfo->pColIndex);
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
184
void insCheckTableDataOrder(STableDataCxt* pTableCxt, TSKEY tsKey) {
X
Xiaoyu Wang 已提交
185
  // once the data block is disordered, we do NOT keep last timestamp any more
X
Xiaoyu Wang 已提交
186 187 188 189
  if (!pTableCxt->ordered) {
    return;
  }

X
Xiaoyu Wang 已提交
190
  if (tsKey < pTableCxt->lastTs) {
X
Xiaoyu Wang 已提交
191 192 193
    pTableCxt->ordered = false;
  }

X
Xiaoyu Wang 已提交
194 195 196 197
  if (tsKey == pTableCxt->lastTs) {
    pTableCxt->duplicateTs = true;
  }

X
Xiaoyu Wang 已提交
198 199 200 201
  pTableCxt->lastTs = tsKey;
  return;
}

X
Xiaoyu Wang 已提交
202
void insDestroyBoundColInfo(SBoundColInfo* pInfo) { taosMemoryFreeClear(pInfo->pColIndex); }
X
Xiaoyu Wang 已提交
203

H
Hongze Cheng 已提交
204 205
static int32_t createTableDataCxt(STableMeta* pTableMeta, SVCreateTbReq** pCreateTbReq, STableDataCxt** pOutput,
                                  bool colMode) {
X
Xiaoyu Wang 已提交
206 207 208 209 210 211 212
  STableDataCxt* pTableCxt = taosMemoryCalloc(1, sizeof(STableDataCxt));
  if (NULL == pTableCxt) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  int32_t code = TSDB_CODE_SUCCESS;

X
Xiaoyu Wang 已提交
213
  pTableCxt->lastTs = 0;
X
Xiaoyu Wang 已提交
214 215
  pTableCxt->ordered = true;
  pTableCxt->duplicateTs = false;
X
Xiaoyu Wang 已提交
216

X
Xiaoyu Wang 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  pTableCxt->pMeta = tableMetaDup(pTableMeta);
  if (NULL == pTableCxt->pMeta) {
    code = TSDB_CODE_OUT_OF_MEMORY;
  }
  if (TSDB_CODE_SUCCESS == code) {
    pTableCxt->pSchema =
        tBuildTSchema(getTableColumnSchema(pTableMeta), pTableMeta->tableInfo.numOfColumns, pTableMeta->sversion);
    if (NULL == pTableCxt->pSchema) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = insInitBoundColsInfo(pTableMeta->tableInfo.numOfColumns, &pTableCxt->boundColsInfo);
  }
  if (TSDB_CODE_SUCCESS == code) {
    pTableCxt->pValues = taosArrayInit(pTableMeta->tableInfo.numOfColumns, sizeof(SColVal));
    if (NULL == pTableCxt->pValues) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    } else {
      initColValues(pTableMeta, pTableCxt->pValues);
    }
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
240 241
    pTableCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
    if (NULL == pTableCxt->pData) {
X
Xiaoyu Wang 已提交
242
      code = TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
243
    } else {
X
Xiaoyu Wang 已提交
244
      pTableCxt->pData->flags = NULL != *pCreateTbReq ? SUBMIT_REQ_AUTO_CREATE_TABLE : 0;
D
dapan1121 已提交
245
      pTableCxt->pData->flags |= colMode ? SUBMIT_REQ_COLUMN_DATA_FORMAT : 0;
X
Xiaoyu Wang 已提交
246 247 248
      pTableCxt->pData->suid = pTableMeta->suid;
      pTableCxt->pData->uid = pTableMeta->uid;
      pTableCxt->pData->sver = pTableMeta->sversion;
X
Xiaoyu Wang 已提交
249 250
      pTableCxt->pData->pCreateTbReq = *pCreateTbReq;
      *pCreateTbReq = NULL;
D
dapan1121 已提交
251 252 253 254 255 256 257 258 259 260
      if (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
        pTableCxt->pData->aCol = taosArrayInit(128, sizeof(SColData));
        if (NULL == pTableCxt->pData->aCol) {
          code = TSDB_CODE_OUT_OF_MEMORY;
        }
      } else {
        pTableCxt->pData->aRowP = taosArrayInit(128, POINTER_BYTES);
        if (NULL == pTableCxt->pData->aRowP) {
          code = TSDB_CODE_OUT_OF_MEMORY;
        }
X
Xiaoyu Wang 已提交
261
      }
X
Xiaoyu Wang 已提交
262 263 264 265 266
    }
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pOutput = pTableCxt;
D
dapan1121 已提交
267
    qDebug("tableDataCxt created, uid:%" PRId64 ", vgId:%d", pTableMeta->uid, pTableMeta->vgId);
X
Xiaoyu Wang 已提交
268 269 270 271 272 273 274
  } else {
    taosMemoryFree(pTableCxt);
  }

  return code;
}

X
Xiaoyu Wang 已提交
275 276 277 278 279 280 281 282
static void resetColValues(SArray* pValues) {
  int32_t num = taosArrayGetSize(pValues);
  for (int32_t i = 0; i < num; ++i) {
    SColVal* pVal = taosArrayGet(pValues, i);
    pVal->flag = CV_FLAG_NONE;
  }
}

X
Xiaoyu Wang 已提交
283
int32_t insGetTableDataCxt(SHashObj* pHash, void* id, int32_t idLen, STableMeta* pTableMeta,
D
dapan1121 已提交
284
                           SVCreateTbReq** pCreateTbReq, STableDataCxt** pTableCxt, bool colMode) {
wmmhello's avatar
wmmhello 已提交
285 286 287
  STableDataCxt** tmp = (STableDataCxt**)taosHashGet(pHash, id, idLen);
  if (NULL != tmp) {
    *pTableCxt = *tmp;
X
Xiaoyu Wang 已提交
288
    resetColValues((*pTableCxt)->pValues);
X
Xiaoyu Wang 已提交
289 290
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
291
  int32_t code = createTableDataCxt(pTableMeta, pCreateTbReq, pTableCxt, colMode);
X
Xiaoyu Wang 已提交
292
  if (TSDB_CODE_SUCCESS == code) {
293
    void* pData = *pTableCxt;  // deal scan coverity
X
Xiaoyu Wang 已提交
294
    code = taosHashPut(pHash, id, idLen, &pData, POINTER_BYTES);
X
Xiaoyu Wang 已提交
295 296 297 298
  }
  return code;
}

X
Xiaoyu Wang 已提交
299 300 301 302 303 304 305
static void destroyColVal(void* p) {
  SColVal* pVal = p;
  if (TSDB_DATA_TYPE_NCHAR == pVal->type) {
    taosMemoryFree(pVal->value.pData);
  }
}

X
Xiaoyu Wang 已提交
306 307 308 309 310 311 312
void insDestroyTableDataCxt(STableDataCxt* pTableCxt) {
  if (NULL == pTableCxt) {
    return;
  }

  taosMemoryFreeClear(pTableCxt->pMeta);
  tDestroyTSchema(pTableCxt->pSchema);
X
Xiaoyu Wang 已提交
313
  insDestroyBoundColInfo(&pTableCxt->boundColsInfo);
X
Xiaoyu Wang 已提交
314
  taosArrayDestroyEx(pTableCxt->pValues, destroyColVal);
H
Hongze Cheng 已提交
315
  if (pTableCxt->pData) {
H
Hongze Cheng 已提交
316
    tDestroySSubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
H
Hongze Cheng 已提交
317 318
    taosMemoryFree(pTableCxt->pData);
  }
X
Xiaoyu Wang 已提交
319
  taosMemoryFree(pTableCxt);
X
Xiaoyu Wang 已提交
320 321 322 323 324 325 326
}

void insDestroyVgroupDataCxt(SVgroupDataCxt* pVgCxt) {
  if (NULL == pVgCxt) {
    return;
  }

H
Hongze Cheng 已提交
327
  tDestroySSubmitReq2(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
D
dapan1121 已提交
328
  taosMemoryFree(pVgCxt->pData);
X
Xiaoyu Wang 已提交
329
  taosMemoryFree(pVgCxt);
X
Xiaoyu Wang 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
}

void insDestroyVgroupDataCxtList(SArray* pVgCxtList) {
  if (NULL == pVgCxtList) {
    return;
  }

  size_t size = taosArrayGetSize(pVgCxtList);
  for (int32_t i = 0; i < size; i++) {
    void* p = taosArrayGetP(pVgCxtList, i);
    insDestroyVgroupDataCxt(p);
  }

  taosArrayDestroy(pVgCxtList);
}

void insDestroyVgroupDataCxtHashMap(SHashObj* pVgCxtHash) {
  if (NULL == pVgCxtHash) {
    return;
  }

  void** p = taosHashIterate(pVgCxtHash, NULL);
  while (p) {
    insDestroyVgroupDataCxt(*(SVgroupDataCxt**)p);

    p = taosHashIterate(pVgCxtHash, p);
  }

  taosHashCleanup(pVgCxtHash);
}

void insDestroyTableDataCxtHashMap(SHashObj* pTableCxtHash) {
  if (NULL == pTableCxtHash) {
    return;
  }

  void** p = taosHashIterate(pTableCxtHash, NULL);
  while (p) {
    insDestroyTableDataCxt(*(STableDataCxt**)p);

    p = taosHashIterate(pTableCxtHash, p);
  }

  taosHashCleanup(pTableCxtHash);
}

static int32_t fillVgroupDataCxt(STableDataCxt* pTableCxt, SVgroupDataCxt* pVgCxt) {
X
Xiaoyu Wang 已提交
377 378 379
  if (NULL == pVgCxt->pData->aSubmitTbData) {
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
    if (NULL == pVgCxt->pData->aSubmitTbData) {
X
Xiaoyu Wang 已提交
380 381 382
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
X
Xiaoyu Wang 已提交
383
  taosArrayPush(pVgCxt->pData->aSubmitTbData, pTableCxt->pData);
X
Xiaoyu Wang 已提交
384
  taosMemoryFreeClear(pTableCxt->pData);
X
Xiaoyu Wang 已提交
385

D
dapan1121 已提交
386 387
  qDebug("add tableDataCxt uid:%" PRId64 " to vgId:%d", pTableCxt->pMeta->uid, pVgCxt->vgId);

X
Xiaoyu Wang 已提交
388 389 390
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
static int32_t createVgroupDataCxt(STableDataCxt* pTableCxt, SHashObj* pVgroupHash, SArray* pVgroupList,
                                   SVgroupDataCxt** pOutput) {
  SVgroupDataCxt* pVgCxt = taosMemoryCalloc(1, sizeof(SVgroupDataCxt));
  if (NULL == pVgCxt) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  pVgCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitReq2));
  if (NULL == pVgCxt->pData) {
    insDestroyVgroupDataCxt(pVgCxt);
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  pVgCxt->vgId = pTableCxt->pMeta->vgId;
  int32_t code = taosHashPut(pVgroupHash, &pVgCxt->vgId, sizeof(pVgCxt->vgId), &pVgCxt, POINTER_BYTES);
  if (TSDB_CODE_SUCCESS == code) {
    taosArrayPush(pVgroupList, &pVgCxt);
407
//    uDebug("td23101 2vgId:%d, uid:%" PRIu64, pVgCxt->vgId, pTableCxt->pMeta->uid);
X
Xiaoyu Wang 已提交
408 409 410 411 412 413 414
    *pOutput = pVgCxt;
  } else {
    insDestroyVgroupDataCxt(pVgCxt);
  }
  return code;
}

D
dapan1121 已提交
415 416 417 418 419 420 421 422 423 424 425 426
int insColDataComp(const void* lp, const void* rp) {
  SColData* pLeft = (SColData*)lp;
  SColData* pRight = (SColData*)rp;
  if (pLeft->cid < pRight->cid) {
    return -1;
  } else if (pLeft->cid > pRight->cid) {
    return 1;
  }

  return 0;
}

X
Xiaoyu Wang 已提交
427 428 429 430 431 432
int32_t insMergeTableDataCxt(SHashObj* pTableHash, SArray** pVgDataBlocks) {
  SHashObj* pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, false);
  SArray*   pVgroupList = taosArrayInit(8, POINTER_BYTES);
  if (NULL == pVgroupHash || NULL == pVgroupList) {
    taosHashCleanup(pVgroupHash);
    taosArrayDestroy(pVgroupList);
H
Hongze Cheng 已提交
433
    return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
434 435 436
  }

  int32_t code = TSDB_CODE_SUCCESS;
H
Hongze Cheng 已提交
437 438
  bool    colFormat = false;

X
Xiaoyu Wang 已提交
439
  void* p = taosHashIterate(pTableHash, NULL);
D
dapan1121 已提交
440 441 442 443
  if (p) {
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
    colFormat = (0 != (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT));
  }
H
Hongze Cheng 已提交
444

X
Xiaoyu Wang 已提交
445 446
  while (TSDB_CODE_SUCCESS == code && NULL != p) {
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
D
dapan1121 已提交
447
    if (colFormat) {
H
Hongze Cheng 已提交
448
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
D
dapan1121 已提交
449 450 451 452
      if (pCol->nVal <= 0) {
        p = taosHashIterate(pTableHash, p);
        continue;
      }
H
Hongze Cheng 已提交
453

D
dapan1121 已提交
454 455 456
      if (pTableCxt->pData->pCreateTbReq) {
        pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
      }
H
Hongze Cheng 已提交
457

D
dapan1121 已提交
458
      taosArraySort(pTableCxt->pData->aCol, insColDataComp);
H
Hongze Cheng 已提交
459

D
dapan1121 已提交
460
      tColDataSortMerge(pTableCxt->pData->aCol);
D
dapan1121 已提交
461
    } else {
462 463 464 465 466 467
      if (!pTableCxt->ordered) {
        tRowSort(pTableCxt->pData->aRowP);
      }
      if (!pTableCxt->ordered || pTableCxt->duplicateTs) {
        code = tRowMerge(pTableCxt->pData->aRowP, pTableCxt->pSchema, 0);
      }
D
dapan1121 已提交
468
    }
H
Hongze Cheng 已提交
469

X
Xiaoyu Wang 已提交
470
    if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
471
      SVgroupDataCxt* pVgCxt = NULL;
X
Xiaoyu Wang 已提交
472
      int32_t         vgId = pTableCxt->pMeta->vgId;
X
Xiaoyu Wang 已提交
473 474
      void**          p = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
      if (NULL == p) {
X
Xiaoyu Wang 已提交
475
        code = createVgroupDataCxt(pTableCxt, pVgroupHash, pVgroupList, &pVgCxt);
X
Xiaoyu Wang 已提交
476 477
      } else {
        pVgCxt = *(SVgroupDataCxt**)p;
X
Xiaoyu Wang 已提交
478 479
      }
      if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
480 481 482 483 484 485 486 487 488 489 490 491
        code = fillVgroupDataCxt(pTableCxt, pVgCxt);
      }
    }
    if (TSDB_CODE_SUCCESS == code) {
      p = taosHashIterate(pTableHash, p);
    }
  }

  taosHashCleanup(pVgroupHash);
  if (TSDB_CODE_SUCCESS == code) {
    *pVgDataBlocks = pVgroupList;
  } else {
D
dapan1121 已提交
492
    insDestroyVgroupDataCxtList(pVgroupList);
X
Xiaoyu Wang 已提交
493 494 495 496 497
  }

  return code;
}

X
Xiaoyu Wang 已提交
498 499 500 501 502
static int32_t buildSubmitReq(int32_t vgId, SSubmitReq2* pReq, void** pData, uint32_t* pLen) {
  int32_t  code = TSDB_CODE_SUCCESS;
  uint32_t len = 0;
  void*    pBuf = NULL;
  tEncodeSize(tEncodeSSubmitReq2, pReq, len, code);
X
Xiaoyu Wang 已提交
503 504
  if (TSDB_CODE_SUCCESS == code) {
    SEncoder encoder;
505
    len += sizeof(SSubmitReq2Msg);
X
Xiaoyu Wang 已提交
506 507
    pBuf = taosMemoryMalloc(len);
    if (NULL == pBuf) {
H
Hongze Cheng 已提交
508
      return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
509
    }
510 511 512 513
    ((SSubmitReq2Msg*)pBuf)->header.vgId = htonl(vgId);
    ((SSubmitReq2Msg*)pBuf)->header.contLen = htonl(len);
    ((SSubmitReq2Msg*)pBuf)->version = htobe64(1);
    tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SSubmitReq2Msg)), len - sizeof(SSubmitReq2Msg));
X
Xiaoyu Wang 已提交
514 515 516
    code = tEncodeSSubmitReq2(&encoder, pReq);
    tEncoderClear(&encoder);
  }
X
Xiaoyu Wang 已提交
517 518 519 520 521 522

  if (TSDB_CODE_SUCCESS == code) {
    *pData = pBuf;
    *pLen = len;
  } else {
    taosMemoryFree(pBuf);
X
Xiaoyu Wang 已提交
523 524 525 526
  }
  return code;
}

X
Xiaoyu Wang 已提交
527 528 529 530 531 532
static void destroyVgDataBlocks(void* p) {
  SVgDataBlocks* pVg = p;
  taosMemoryFree(pVg->pData);
  taosMemoryFree(pVg);
}

X
Xiaoyu Wang 已提交
533 534 535 536
int32_t insBuildVgDataBlocks(SHashObj* pVgroupsHashObj, SArray* pVgDataCxtList, SArray** pVgDataBlocks) {
  size_t  numOfVg = taosArrayGetSize(pVgDataCxtList);
  SArray* pDataBlocks = taosArrayInit(numOfVg, POINTER_BYTES);
  if (NULL == pDataBlocks) {
H
Hongze Cheng 已提交
537
    return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
538 539 540 541 542 543 544
  }

  int32_t code = TSDB_CODE_SUCCESS;
  for (size_t i = 0; TSDB_CODE_SUCCESS == code && i < numOfVg; ++i) {
    SVgroupDataCxt* src = taosArrayGetP(pVgDataCxtList, i);
    SVgDataBlocks*  dst = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
    if (NULL == dst) {
H
Hongze Cheng 已提交
545
      code = TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
546 547
    }
    if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
548
      dst->numOfTables = taosArrayGetSize(src->pData->aSubmitTbData);
X
Xiaoyu Wang 已提交
549
      code = taosHashGetDup(pVgroupsHashObj, (const char*)&src->vgId, sizeof(src->vgId), &dst->vg);
550
//      uError("td23101 3vgId:%d, numEps:%d", src->vgId, dst->vg.epSet.numOfEps);
X
Xiaoyu Wang 已提交
551 552
    }
    if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
553
      code = buildSubmitReq(src->vgId, src->pData, &dst->pData, &dst->size);
X
Xiaoyu Wang 已提交
554 555
    }
    if (TSDB_CODE_SUCCESS == code) {
H
Hongze Cheng 已提交
556
      code = (NULL == taosArrayPush(pDataBlocks, &dst) ? TSDB_CODE_OUT_OF_MEMORY : TSDB_CODE_SUCCESS);
X
Xiaoyu Wang 已提交
557 558 559 560 561 562
    }
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pVgDataBlocks = pDataBlocks;
  } else {
X
Xiaoyu Wang 已提交
563
    taosArrayDestroyP(pDataBlocks, destroyVgDataBlocks);
X
Xiaoyu Wang 已提交
564 565 566 567
  }

  return code;
}
wmmhello's avatar
wmmhello 已提交
568

569
static bool findFileds(SSchema* pSchema, TAOS_FIELD* fields, int numFields) {
wmmhello's avatar
wmmhello 已提交
570
  for (int i = 0; i < numFields; i++) {
571 572
    if(strcmp(pSchema->name, fields[i].name) == 0){
      return true;
wmmhello's avatar
wmmhello 已提交
573 574 575
    }
  }

576
  return false;
wmmhello's avatar
wmmhello 已提交
577 578
}

X
Xiaoyu Wang 已提交
579
int rawBlockBindData(SQuery* query, STableMeta* pTableMeta, void* data, SVCreateTbReq* pCreateTb, TAOS_FIELD* tFields,
580
                     int numFields, bool needChangeLength) {
wmmhello's avatar
wmmhello 已提交
581
  void* tmp = taosHashGet(((SVnodeModifyOpStmt*)(query->pRoot))->pTableBlockHashObj, &pTableMeta->uid, sizeof(pTableMeta->uid));
wmmhello's avatar
wmmhello 已提交
582
  STableDataCxt* pTableCxt = NULL;
X
Xiaoyu Wang 已提交
583 584
  int            ret = insGetTableDataCxt(((SVnodeModifyOpStmt*)(query->pRoot))->pTableBlockHashObj, &pTableMeta->uid,
                                          sizeof(pTableMeta->uid), pTableMeta, &pCreateTb, &pTableCxt, true);
wmmhello's avatar
wmmhello 已提交
585 586 587 588
  if (ret != TSDB_CODE_SUCCESS) {
    uError("insGetTableDataCxt error");
    goto end;
  }
wmmhello's avatar
wmmhello 已提交
589

590
  if(tmp == NULL){
wmmhello's avatar
wmmhello 已提交
591
    ret = initTableColSubmitData(pTableCxt);
wmmhello's avatar
wmmhello 已提交
592
    if (ret != TSDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
593
      uError("initTableColSubmitData error");
wmmhello's avatar
wmmhello 已提交
594 595
      goto end;
    }
596
  }
wmmhello's avatar
wmmhello 已提交
597

wmmhello's avatar
wmmhello 已提交
598
  char* p = (char*)data;
X
Xiaoyu Wang 已提交
599 600
  // | version | total length | total rows | total columns | flag seg| block group id | column schema | each column
  // length |
wmmhello's avatar
wmmhello 已提交
601 602 603 604 605 606 607 608 609 610 611 612
  p += sizeof(int32_t);
  p += sizeof(int32_t);

  int32_t numOfRows = *(int32_t*)p;
  p += sizeof(int32_t);

  int32_t numOfCols = *(int32_t*)p;
  p += sizeof(int32_t);

  p += sizeof(int32_t);
  p += sizeof(uint64_t);

X
Xiaoyu Wang 已提交
613
  int8_t* fields = p;
wmmhello's avatar
wmmhello 已提交
614 615 616 617 618 619 620
  p += numOfCols * (sizeof(int8_t) + sizeof(int32_t));

  int32_t* colLength = (int32_t*)p;
  p += sizeof(int32_t) * numOfCols;

  char* pStart = p;

X
Xiaoyu Wang 已提交
621 622
  SSchema*       pSchema = getTableColumnSchema(pTableCxt->pMeta);
  SBoundColInfo* boundInfo = &pTableCxt->boundColsInfo;
wmmhello's avatar
wmmhello 已提交
623

624
  if (tFields != NULL && numFields != numOfCols) {
625
    uError("numFields:%d != numOfCols:%d", numFields, numOfCols);
wmmhello's avatar
wmmhello 已提交
626 627 628
    ret = TSDB_CODE_INVALID_PARA;
    goto end;
  }
629
  if (tFields != NULL && numFields > boundInfo->numOfBound) {
630 631 632 633
    uError("numFields:%d > boundInfo->numOfBound:%d", numFields, boundInfo->numOfBound);
    ret = TSDB_CODE_INVALID_PARA;
    goto end;
  }
wmmhello's avatar
wmmhello 已提交
634
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
635
    SSchema*  pColSchema = &pSchema[c];
wmmhello's avatar
wmmhello 已提交
636
    SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, c);
637
    if(tFields == NULL || findFileds(pColSchema, tFields, numFields)){
638 639 640 641 642
      if (*fields != pColSchema->type && *(int32_t*)(fields + sizeof(int8_t)) != pColSchema->bytes) {
        uError("type or bytes not equal");
        ret = TSDB_CODE_INVALID_PARA;
        goto end;
      }
wmmhello's avatar
wmmhello 已提交
643

644 645 646 647 648 649 650
      int8_t* offset = pStart;
      if (IS_VAR_DATA_TYPE(pColSchema->type)) {
        pStart += numOfRows * sizeof(int32_t);
      } else {
        pStart += BitmapLen(numOfRows);
      }
      char* pData = pStart;
wmmhello's avatar
wmmhello 已提交
651

652 653 654 655 656 657 658 659 660
      tColDataAddValueByDataBlock(pCol, pColSchema->type, pColSchema->bytes, numOfRows, offset, pData);
      fields += sizeof(int8_t) + sizeof(int32_t);
      if (needChangeLength) {
        pStart += htonl(colLength[c]);
      } else {
        pStart += colLength[c];
      }
    }else{
      tColDataAddValueByDataBlock(pCol, pColSchema->type, pColSchema->bytes, numOfRows, NULL, NULL);
661
    }
wmmhello's avatar
wmmhello 已提交
662 663 664 665
  }

end:
  return ret;
666
}