parInsertUtil.c 20.5 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 407 408 409 410 411 412 413
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);
    *pOutput = pVgCxt;
  } else {
    insDestroyVgroupDataCxt(pVgCxt);
  }
  return code;
}

D
dapan1121 已提交
414 415 416 417 418 419 420 421 422 423 424 425
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 已提交
426 427 428 429 430 431
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 已提交
432
    return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
433 434 435
  }

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

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

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

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

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

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

X
Xiaoyu Wang 已提交
469
    if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
470
      SVgroupDataCxt* pVgCxt = NULL;
X
Xiaoyu Wang 已提交
471
      int32_t         vgId = pTableCxt->pMeta->vgId;
X
Xiaoyu Wang 已提交
472 473
      void**          p = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
      if (NULL == p) {
X
Xiaoyu Wang 已提交
474
        code = createVgroupDataCxt(pTableCxt, pVgroupHash, pVgroupList, &pVgCxt);
X
Xiaoyu Wang 已提交
475 476
      } else {
        pVgCxt = *(SVgroupDataCxt**)p;
X
Xiaoyu Wang 已提交
477 478
      }
      if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
479 480 481 482 483 484 485 486 487 488 489 490
        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 已提交
491
    insDestroyVgroupDataCxtList(pVgroupList);
X
Xiaoyu Wang 已提交
492 493 494 495 496
  }

  return code;
}

X
Xiaoyu Wang 已提交
497 498 499 500 501
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 已提交
502 503
  if (TSDB_CODE_SUCCESS == code) {
    SEncoder encoder;
504
    len += sizeof(SSubmitReq2Msg);
X
Xiaoyu Wang 已提交
505 506
    pBuf = taosMemoryMalloc(len);
    if (NULL == pBuf) {
H
Hongze Cheng 已提交
507
      return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
508
    }
509 510 511 512
    ((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 已提交
513 514 515
    code = tEncodeSSubmitReq2(&encoder, pReq);
    tEncoderClear(&encoder);
  }
X
Xiaoyu Wang 已提交
516 517 518 519 520 521

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

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

X
Xiaoyu Wang 已提交
532 533 534 535
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 已提交
536
    return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
537 538 539 540 541 542 543
  }

  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 已提交
544
      code = TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
545 546
    }
    if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
547
      dst->numOfTables = taosArrayGetSize(src->pData->aSubmitTbData);
X
Xiaoyu Wang 已提交
548 549 550
      code = taosHashGetDup(pVgroupsHashObj, (const char*)&src->vgId, sizeof(src->vgId), &dst->vg);
    }
    if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
551
      code = buildSubmitReq(src->vgId, src->pData, &dst->pData, &dst->size);
X
Xiaoyu Wang 已提交
552 553
    }
    if (TSDB_CODE_SUCCESS == code) {
H
Hongze Cheng 已提交
554
      code = (NULL == taosArrayPush(pDataBlocks, &dst) ? TSDB_CODE_OUT_OF_MEMORY : TSDB_CODE_SUCCESS);
X
Xiaoyu Wang 已提交
555 556 557 558 559 560
    }
  }

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

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

X
Xiaoyu Wang 已提交
567
static int bindFileds(SBoundColInfo* pBoundInfo, SSchema* pSchema, TAOS_FIELD* fields, int numFields) {
wmmhello's avatar
wmmhello 已提交
568 569 570 571 572 573 574 575 576
  bool* pUseCols = taosMemoryCalloc(pBoundInfo->numOfCols, sizeof(bool));
  if (NULL == pUseCols) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  pBoundInfo->numOfBound = 0;

  int16_t lastColIdx = -1;  // last column found
  int32_t code = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
577
  for (int i = 0; i < numFields; i++) {
wmmhello's avatar
wmmhello 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
    SToken token;
    token.z = fields[i].name;
    token.n = strlen(fields[i].name);

    int16_t t = lastColIdx + 1;
    int16_t index = insFindCol(&token, t, pBoundInfo->numOfCols, pSchema);
    if (index < 0 && t > 0) {
      index = insFindCol(&token, 0, t, pSchema);
    }
    if (index < 0) {
      uError("can not find column name:%s", token.z);
      code = TSDB_CODE_PAR_INVALID_COLUMN;
      break;
    } else if (pUseCols[index]) {
      code = TSDB_CODE_PAR_INVALID_COLUMN;
      uError("duplicated column name:%s", token.z);
      break;
    } else {
      lastColIdx = index;
      pUseCols[index] = true;
      pBoundInfo->pColIndex[pBoundInfo->numOfBound] = index;
      ++pBoundInfo->numOfBound;
    }
  }

  if (TSDB_CODE_SUCCESS == code && !pUseCols[0]) {
    uError("primary timestamp column can not be null:");
    code = TSDB_CODE_PAR_INVALID_COLUMN;
  }

  taosMemoryFree(pUseCols);
  return code;
}

X
Xiaoyu Wang 已提交
612
int rawBlockBindData(SQuery* query, STableMeta* pTableMeta, void* data, SVCreateTbReq* pCreateTb, TAOS_FIELD* tFields,
613
                     int numFields, bool needChangeLength) {
wmmhello's avatar
wmmhello 已提交
614
  STableDataCxt* pTableCxt = NULL;
X
Xiaoyu Wang 已提交
615 616
  int            ret = insGetTableDataCxt(((SVnodeModifyOpStmt*)(query->pRoot))->pTableBlockHashObj, &pTableMeta->uid,
                                          sizeof(pTableMeta->uid), pTableMeta, &pCreateTb, &pTableCxt, true);
wmmhello's avatar
wmmhello 已提交
617 618 619 620
  if (ret != TSDB_CODE_SUCCESS) {
    uError("insGetTableDataCxt error");
    goto end;
  }
X
Xiaoyu Wang 已提交
621
  if (tFields != NULL) {
wmmhello's avatar
wmmhello 已提交
622 623 624 625 626 627
    ret = bindFileds(&pTableCxt->boundColsInfo, getTableColumnSchema(pTableMeta), tFields, numFields);
    if (ret != TSDB_CODE_SUCCESS) {
      uError("bindFileds error");
      goto end;
    }
  }
wmmhello's avatar
wmmhello 已提交
628 629 630
  // no need to bind, because select * get all fields
  ret = initTableColSubmitData(pTableCxt);
  if (ret != TSDB_CODE_SUCCESS) {
X
Xiaoyu Wang 已提交
631
    uError("initTableColSubmitData error");
wmmhello's avatar
wmmhello 已提交
632 633 634
    goto end;
  }

wmmhello's avatar
wmmhello 已提交
635
  char* p = (char*)data;
X
Xiaoyu Wang 已提交
636 637
  // | version | total length | total rows | total columns | flag seg| block group id | column schema | each column
  // length |
wmmhello's avatar
wmmhello 已提交
638 639 640 641 642 643 644 645 646 647 648 649
  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 已提交
650
  int8_t* fields = p;
wmmhello's avatar
wmmhello 已提交
651 652 653 654 655 656 657
  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 已提交
658 659
  SSchema*       pSchema = getTableColumnSchema(pTableCxt->pMeta);
  SBoundColInfo* boundInfo = &pTableCxt->boundColsInfo;
wmmhello's avatar
wmmhello 已提交
660

X
Xiaoyu Wang 已提交
661
  if (boundInfo->numOfBound != numOfCols) {
wmmhello's avatar
wmmhello 已提交
662 663 664 665 666
    uError("boundInfo->numOfBound:%d != numOfCols:%d", boundInfo->numOfBound, numOfCols);
    ret = TSDB_CODE_INVALID_PARA;
    goto end;
  }
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
X
Xiaoyu Wang 已提交
667
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
wmmhello's avatar
wmmhello 已提交
668 669 670
    SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, c);

    if (*fields != pColSchema->type && *(int32_t*)(fields + sizeof(int8_t)) != pColSchema->bytes) {
X
Xiaoyu Wang 已提交
671
      uError("type or bytes not equal");
wmmhello's avatar
wmmhello 已提交
672 673 674 675 676 677 678 679 680 681
      ret = TSDB_CODE_INVALID_PARA;
      goto end;
    }

    int8_t* offset = pStart;
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
      pStart += numOfRows * sizeof(int32_t);
    } else {
      pStart += BitmapLen(numOfRows);
    }
X
Xiaoyu Wang 已提交
682
    char* pData = pStart;
wmmhello's avatar
wmmhello 已提交
683 684 685

    tColDataAddValueByDataBlock(pCol, pColSchema->type, pColSchema->bytes, numOfRows, offset, pData);
    fields += sizeof(int8_t) + sizeof(int32_t);
686
    if (needChangeLength) {
687
      pStart += htonl(colLength[c]);
688
    } else {
689 690
      pStart += colLength[c];
    }
wmmhello's avatar
wmmhello 已提交
691 692 693 694
  }

end:
  return ret;
695
}