parInsertUtil.c 20.4 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 147 148 149 150 151
  pTbReq->type = TD_CHILD_TABLE;
  pTbReq->name = strdup(tname);
  pTbReq->ctb.suid = suid;
  pTbReq->ctb.tagNum = tagNum;
  if (sname) pTbReq->ctb.stbName = strdup(sname);
  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 293 294 295 296 297
  if (TSDB_CODE_SUCCESS == code) {
    code = taosHashPut(pHash, id, idLen, pTableCxt, POINTER_BYTES);
  }
  return code;
}

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

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

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

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

H
Hongze Cheng 已提交
326
  tDestroySSubmitReq2(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
D
dapan1121 已提交
327
  taosMemoryFree(pVgCxt->pData);
X
Xiaoyu Wang 已提交
328
  taosMemoryFree(pVgCxt);
X
Xiaoyu Wang 已提交
329 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
}

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 已提交
376 377 378
  if (NULL == pVgCxt->pData->aSubmitTbData) {
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
    if (NULL == pVgCxt->pData->aSubmitTbData) {
X
Xiaoyu Wang 已提交
379 380 381
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
X
Xiaoyu Wang 已提交
382
  taosArrayPush(pVgCxt->pData->aSubmitTbData, pTableCxt->pData);
X
Xiaoyu Wang 已提交
383
  taosMemoryFreeClear(pTableCxt->pData);
X
Xiaoyu Wang 已提交
384

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

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

X
Xiaoyu Wang 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
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 已提交
413 414 415 416 417 418 419 420 421 422 423 424
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 已提交
425 426 427 428 429 430
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 已提交
431
    return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
432 433 434
  }

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

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

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

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

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

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

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

  return code;
}

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

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

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

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

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

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

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

X
Xiaoyu Wang 已提交
565
static int bindFileds(SBoundColInfo* pBoundInfo, SSchema* pSchema, TAOS_FIELD* fields, int numFields) {
wmmhello's avatar
wmmhello 已提交
566 567 568 569 570 571 572 573 574
  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 已提交
575
  for (int i = 0; i < numFields; i++) {
wmmhello's avatar
wmmhello 已提交
576 577 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
    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 已提交
610 611
int rawBlockBindData(SQuery* query, STableMeta* pTableMeta, void* data, SVCreateTbReq* pCreateTb, TAOS_FIELD* tFields,
                     int numFields) {
wmmhello's avatar
wmmhello 已提交
612
  STableDataCxt* pTableCxt = NULL;
X
Xiaoyu Wang 已提交
613 614
  int            ret = insGetTableDataCxt(((SVnodeModifyOpStmt*)(query->pRoot))->pTableBlockHashObj, &pTableMeta->uid,
                                          sizeof(pTableMeta->uid), pTableMeta, &pCreateTb, &pTableCxt, true);
wmmhello's avatar
wmmhello 已提交
615 616 617 618
  if (ret != TSDB_CODE_SUCCESS) {
    uError("insGetTableDataCxt error");
    goto end;
  }
X
Xiaoyu Wang 已提交
619
  if (tFields != NULL) {
wmmhello's avatar
wmmhello 已提交
620 621 622 623 624 625
    ret = bindFileds(&pTableCxt->boundColsInfo, getTableColumnSchema(pTableMeta), tFields, numFields);
    if (ret != TSDB_CODE_SUCCESS) {
      uError("bindFileds error");
      goto end;
    }
  }
wmmhello's avatar
wmmhello 已提交
626 627 628
  // no need to bind, because select * get all fields
  ret = initTableColSubmitData(pTableCxt);
  if (ret != TSDB_CODE_SUCCESS) {
X
Xiaoyu Wang 已提交
629
    uError("initTableColSubmitData error");
wmmhello's avatar
wmmhello 已提交
630 631 632
    goto end;
  }

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

X
Xiaoyu Wang 已提交
659
  if (boundInfo->numOfBound != numOfCols) {
wmmhello's avatar
wmmhello 已提交
660 661 662 663 664
    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 已提交
665
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
wmmhello's avatar
wmmhello 已提交
666 667 668
    SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, c);

    if (*fields != pColSchema->type && *(int32_t*)(fields + sizeof(int8_t)) != pColSchema->bytes) {
X
Xiaoyu Wang 已提交
669
      uError("type or bytes not equal");
wmmhello's avatar
wmmhello 已提交
670 671 672 673 674 675 676 677 678 679
      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 已提交
680
    char* pData = pStart;
wmmhello's avatar
wmmhello 已提交
681
    uError("rawBlockBindData col bytes:%d, type:%d, size:%d, htonl size:%d", pColSchema->bytes, pColSchema->type, colLength[c], htonl(colLength[c]));
wmmhello's avatar
wmmhello 已提交
682 683 684

    tColDataAddValueByDataBlock(pCol, pColSchema->type, pColSchema->bytes, numOfRows, offset, pData);
    fields += sizeof(int8_t) + sizeof(int32_t);
wmmhello's avatar
wmmhello 已提交
685
    pStart += htonl(colLength[c]);
wmmhello's avatar
wmmhello 已提交
686 687 688 689
  }

end:
  return ret;
690
}