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

taosdata / TDengine / #4894

22 Dec 2025 09:33AM UTC coverage: 65.72% (+0.2%) from 65.57%
#4894

push

travis-ci

web-flow
Update README.md (#34007)

* Update README.md

* docs: update table of contents and improve installation instructions in README

* docs: adjust words

---------

Signed-off-by: WANG Xu <feici02@outlook.com>
Co-authored-by: WANG Xu <feici02@outlook.com>

184394 of 280577 relevant lines covered (65.72%)

111859687.16 hits per line

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

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

16
#include "parInsertUtil.h"
17

18
#include "catalog.h"
19
#include "parInt.h"
20
#include "parUtil.h"
21
#include "querynodes.h"
22
#include "tRealloc.h"
23
#include "taoserror.h"
24
#include "tarray.h"
25
#include "tdatablock.h"
26
#include "tdataformat.h"
27
#include "tmisce.h"
28
#include "ttypes.h"
29

30
void qDestroyBoundColInfo(void* pInfo) {
4,340,443✔
31
  if (NULL == pInfo) {
4,340,443✔
32
    return;
3,508,575✔
33
  }
34

35
  SBoundColInfo* pBoundInfo = (SBoundColInfo*)pInfo;
831,868✔
36

37
  taosMemoryFreeClear(pBoundInfo->pColIndex);
831,868✔
38
}
39

40
static char* tableNameGetPosition(SToken* pToken, char target) {
544,671,666✔
41
  bool inEscape = false;
544,671,666✔
42
  bool inQuote = false;
544,671,666✔
43
  char quotaStr = 0;
544,671,666✔
44

45
  for (uint32_t i = 0; i < pToken->n; ++i) {
2,147,483,647✔
46
    if (*(pToken->z + i) == target && (!inEscape) && (!inQuote)) {
2,147,483,647✔
47
      return pToken->z + i;
158,449,906✔
48
    }
49

50
    if (*(pToken->z + i) == TS_ESCAPE_CHAR) {
2,147,483,647✔
51
      if (!inQuote) {
9,682,579✔
52
        inEscape = !inEscape;
9,682,775✔
53
      }
54
    }
55

56
    if (*(pToken->z + i) == '\'' || *(pToken->z + i) == '"') {
2,147,483,647✔
57
      if (!inEscape) {
7,396,748✔
58
        if (!inQuote) {
7,392,350✔
59
          quotaStr = *(pToken->z + i);
3,696,175✔
60
          inQuote = !inQuote;
3,696,175✔
61
        } else if (quotaStr == *(pToken->z + i)) {
3,696,175✔
62
          inQuote = !inQuote;
3,696,175✔
63
        }
64
      }
65
    }
66
  }
67

68
  return NULL;
386,234,749✔
69
}
70

71
int32_t insCreateSName(SName* pName, SToken* pTableName, int32_t acctId, const char* dbName, SMsgBuf* pMsgBuf) {
544,672,156✔
72
  const char* msg1 = "name too long";
544,672,156✔
73
  const char* msg2 = "invalid database name";
544,672,156✔
74
  const char* msg3 = "db is not specified";
544,672,156✔
75
  const char* msg4 = "invalid table name";
544,672,156✔
76

77
  int32_t code = TSDB_CODE_SUCCESS;
544,672,156✔
78
  char*   p = tableNameGetPosition(pTableName, TS_PATH_DELIMITER[0]);
544,672,156✔
79

80
  if (p != NULL) {  // db has been specified in sql string so we ignore current db path
544,685,217✔
81
    int32_t dbLen = p - pTableName->z;
158,450,889✔
82
    if (dbLen <= 0) {
158,447,444✔
83
      return buildInvalidOperationMsg(pMsgBuf, msg2);
×
84
    }
85
    char name[TSDB_DB_FNAME_LEN] = {0};
158,447,444✔
86
    strncpy(name, pTableName->z, dbLen);
158,446,243✔
87
    int32_t actualDbLen = strdequote(name);
158,444,194✔
88

89
    code = tNameSetDbName(pName, acctId, name, actualDbLen);
158,438,744✔
90
    if (code != TSDB_CODE_SUCCESS) {
158,446,042✔
91
      return buildInvalidOperationMsg(pMsgBuf, msg1);
×
92
    }
93

94
    int32_t tbLen = pTableName->n - dbLen - 1;
158,446,042✔
95
    if (tbLen <= 0) {
158,447,744✔
96
      return buildInvalidOperationMsg(pMsgBuf, msg4);
×
97
    }
98

99
    char tbname[TSDB_TABLE_FNAME_LEN] = {0};
158,447,744✔
100
    strncpy(tbname, p + 1, tbLen);
158,442,603✔
101
    /*tbLen = */ (void)strdequote(tbname);
158,439,602✔
102

103
    code = tNameFromString(pName, tbname, T_NAME_TABLE);
158,449,314✔
104
    if (code != 0) {
158,438,510✔
105
      return buildInvalidOperationMsg(pMsgBuf, msg1);
×
106
    }
107
  } else {  // get current DB name first, and then set it into path
108
    char tbname[TSDB_TABLE_FNAME_LEN] = {0};
386,234,328✔
109
    strncpy(tbname, pTableName->z, pTableName->n);
386,233,070✔
110
    int32_t tbLen = strdequote(tbname);
386,233,944✔
111
    if (tbLen >= TSDB_TABLE_NAME_LEN) {
386,232,588✔
112
      return buildInvalidOperationMsg(pMsgBuf, msg1);
×
113
    }
114
    if (tbLen == 0) {
386,233,565✔
115
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, "invalid table name");
764✔
116
    }
117

118
    char name[TSDB_TABLE_FNAME_LEN] = {0};
386,232,801✔
119
    strncpy(name, pTableName->z, pTableName->n);
386,233,165✔
120
    (void)strdequote(name);
386,233,583✔
121

122
    if (dbName == NULL) {
386,234,779✔
123
      return buildInvalidOperationMsg(pMsgBuf, msg3);
2,506✔
124
    }
125
    if (name[0] == '\0') return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
386,232,375✔
126

127
    code = tNameSetDbName(pName, acctId, dbName, strlen(dbName));
386,232,375✔
128
    if (code != TSDB_CODE_SUCCESS) {
386,230,908✔
129
      code = buildInvalidOperationMsg(pMsgBuf, msg2);
×
130
      return code;
×
131
    }
132

133
    code = tNameFromString(pName, name, T_NAME_TABLE);
386,230,908✔
134
    if (code != 0) {
386,231,135✔
135
      code = buildInvalidOperationMsg(pMsgBuf, msg1);
×
136
    }
137
  }
138

139
  if (NULL != strchr(pName->tname, '.')) {
544,678,654✔
140
    code = generateSyntaxErrMsgExt(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, "The table name cannot contain '.'");
×
141
  }
142

143
  return code;
544,679,791✔
144
}
145

146
int16_t insFindCol(SToken* pColname, int16_t start, int16_t end, SSchema* pSchema) {
562,291,746✔
147
  while (start < end) {
2,147,483,647✔
148
    if (strlen(pSchema[start].name) == pColname->n && strncmp(pColname->z, pSchema[start].name, pColname->n) == 0) {
2,147,483,647✔
149
      return start;
555,119,751✔
150
    }
151
    ++start;
2,147,483,647✔
152
  }
153
  return -1;
7,174,903✔
154
}
155

156
int32_t insBuildCreateTbReq(SVCreateTbReq* pTbReq, const char* tname, STag* pTag, int64_t suid, const char* sname,
14,614,524✔
157
                            SArray* tagName, uint8_t tagNum, int32_t ttl) {
158
  pTbReq->type = TD_CHILD_TABLE;
14,614,524✔
159
  pTbReq->ctb.pTag = (uint8_t*)pTag;
14,614,818✔
160
  pTbReq->name = taosStrdup(tname);
14,614,769✔
161
  if (!pTbReq->name) return terrno;
14,615,390✔
162
  pTbReq->ctb.suid = suid;
14,615,076✔
163
  pTbReq->ctb.tagNum = tagNum;
14,615,027✔
164
  if (sname) {
14,614,782✔
165
    pTbReq->ctb.stbName = taosStrdup(sname);
13,580,399✔
166
    if (!pTbReq->ctb.stbName) return terrno;
13,580,595✔
167
  }
168
  pTbReq->ctb.tagName = taosArrayDup(tagName, NULL);
14,615,174✔
169
  if (!pTbReq->ctb.tagName) return terrno;
14,615,204✔
170
  pTbReq->ttl = ttl;
14,615,325✔
171
  pTbReq->commentLen = -1;
14,615,305✔
172

173
  return TSDB_CODE_SUCCESS;
14,615,523✔
174
}
175

176
static void initBoundCols(int32_t ncols, int16_t* pBoundCols) {
×
177
  for (int32_t i = 0; i < ncols; ++i) {
×
178
    pBoundCols[i] = i;
×
179
  }
180
}
×
181

182
static int32_t initColValues(STableMeta* pTableMeta, SArray* pValues) {
522,345,134✔
183
  SSchema* pSchemas = getTableColumnSchema(pTableMeta);
522,345,134✔
184
  int32_t  code = 0;
522,347,695✔
185
  for (int32_t i = 0; i < pTableMeta->tableInfo.numOfColumns; ++i) {
2,147,483,647✔
186
    SColVal val = COL_VAL_NONE(pSchemas[i].colId, pSchemas[i].type);
2,147,483,647✔
187
    if (NULL == taosArrayPush(pValues, &val)) {
2,147,483,647✔
188
      code = terrno;
×
189
      break;
×
190
    }
191
  }
192
  return code;
522,355,402✔
193
}
194

195
int32_t insInitColValues(STableMeta* pTableMeta, SArray* aColValues) { return initColValues(pTableMeta, aColValues); }
143,752✔
196

197
int32_t insInitBoundColsInfo(int32_t numOfBound, SBoundColInfo* pInfo) {
545,483,665✔
198
  pInfo->numOfCols = numOfBound;
545,483,665✔
199
  pInfo->numOfBound = numOfBound;
545,490,519✔
200
  pInfo->hasBoundCols = false;
545,489,597✔
201
  pInfo->mixTagsCols = false;
545,490,705✔
202
  pInfo->pColIndex = taosMemoryCalloc(numOfBound, sizeof(int16_t));
545,489,629✔
203
  if (NULL == pInfo->pColIndex) {
545,491,486✔
204
    return terrno;
×
205
  }
206
  for (int32_t i = 0; i < numOfBound; ++i) {
2,147,483,647✔
207
    pInfo->pColIndex[i] = i;
2,147,483,647✔
208
  }
209
  return TSDB_CODE_SUCCESS;
545,494,379✔
210
}
211

212
void insResetBoundColsInfo(SBoundColInfo* pInfo) {
2,884✔
213
  pInfo->numOfBound = pInfo->numOfCols;
2,884✔
214
  pInfo->hasBoundCols = false;
2,884✔
215
  pInfo->mixTagsCols = false;
2,884✔
216
  for (int32_t i = 0; i < pInfo->numOfCols; ++i) {
14,420✔
217
    pInfo->pColIndex[i] = i;
11,536✔
218
  }
219
}
2,884✔
220

221
void insCheckTableDataOrder(STableDataCxt* pTableCxt, SRowKey* rowKey) {
2,147,483,647✔
222
  // once the data block is disordered, we do NOT keep last timestamp any more
223
  if (!pTableCxt->ordered) {
2,147,483,647✔
224
    return;
108,721,966✔
225
  }
226

227
  if (tRowKeyCompare(rowKey, &pTableCxt->lastKey) < 0) {
2,147,483,647✔
228
    pTableCxt->ordered = false;
580,525✔
229
  }
230

231
  if (tRowKeyCompare(rowKey, &pTableCxt->lastKey) == 0) {
2,147,483,647✔
232
    pTableCxt->duplicateTs = true;
1,039,147✔
233
  }
234

235
  // TODO: for variable length data type, we need to copy it out
236
  pTableCxt->lastKey = *rowKey;
2,147,483,647✔
237
  return;
2,147,483,647✔
238
}
239

240
void insDestroyBoundColInfo(SBoundColInfo* pInfo) { taosMemoryFreeClear(pInfo->pColIndex); }
1,533,745,211✔
241

242
static int32_t createTableDataCxt(STableMeta* pTableMeta, SVCreateTbReq** pCreateTbReq, STableDataCxt** pOutput,
525,336,456✔
243
                                  bool colMode, bool ignoreColVals) {
244
  STableDataCxt* pTableCxt = taosMemoryCalloc(1, sizeof(STableDataCxt));
525,336,456✔
245
  if (NULL == pTableCxt) {
525,336,653✔
246
    *pOutput = NULL;
×
247
    return terrno;
×
248
  }
249

250
  int32_t code = TSDB_CODE_SUCCESS;
525,336,653✔
251

252
  pTableCxt->lastKey = (SRowKey){0};
525,336,653✔
253
  pTableCxt->ordered = true;
525,339,388✔
254
  pTableCxt->duplicateTs = false;
525,339,739✔
255

256
  pTableCxt->pMeta = tableMetaDup(pTableMeta);
525,340,409✔
257
  if (NULL == pTableCxt->pMeta) {
525,347,290✔
258
    code = TSDB_CODE_OUT_OF_MEMORY;
×
259
  }
260
  if (TSDB_CODE_SUCCESS == code) {
525,351,076✔
261
    pTableCxt->pSchema =
525,357,142✔
262
        tBuildTSchema(getTableColumnSchema(pTableMeta), pTableMeta->tableInfo.numOfColumns, pTableMeta->sversion);
525,349,521✔
263
    if (NULL == pTableCxt->pSchema) {
525,352,472✔
264
      code = TSDB_CODE_OUT_OF_MEMORY;
×
265
    }
266
  }
267
  pTableCxt->hasBlob = schemaHasBlob(pTableCxt->pSchema);
525,359,672✔
268

269
  if (TSDB_CODE_SUCCESS == code) {
525,357,256✔
270
    code = insInitBoundColsInfo(pTableMeta->tableInfo.numOfColumns, &pTableCxt->boundColsInfo);
525,357,766✔
271
  }
272
  if (TSDB_CODE_SUCCESS == code && !ignoreColVals) {
525,355,195✔
273
    pTableCxt->pValues = taosArrayInit(pTableMeta->tableInfo.numOfColumns, sizeof(SColVal));
522,203,837✔
274
    if (NULL == pTableCxt->pValues) {
522,208,289✔
275
      code = terrno;
×
276
    } else {
277
      code = initColValues(pTableMeta, pTableCxt->pValues);
522,205,676✔
278
    }
279
  }
280
  if (TSDB_CODE_SUCCESS == code) {
525,356,290✔
281
    pTableCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
525,357,412✔
282
    if (NULL == pTableCxt->pData) {
525,352,981✔
283
      code = terrno;
×
284
    } else {
285
      pTableCxt->pData->flags = (pCreateTbReq != NULL && NULL != *pCreateTbReq) ? SUBMIT_REQ_AUTO_CREATE_TABLE : 0;
525,354,977✔
286
      pTableCxt->pData->flags |= colMode ? SUBMIT_REQ_COLUMN_DATA_FORMAT : 0;
525,355,591✔
287
      pTableCxt->pData->suid = pTableMeta->suid;
525,352,174✔
288
      pTableCxt->pData->uid = pTableMeta->uid;
525,355,000✔
289
      pTableCxt->pData->sver = pTableMeta->sversion;
525,354,100✔
290
      pTableCxt->pData->pCreateTbReq = pCreateTbReq != NULL ? *pCreateTbReq : NULL;
525,349,689✔
291
      int8_t flag = pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
525,354,338✔
292
      if (pCreateTbReq != NULL) *pCreateTbReq = NULL;
525,350,755✔
293
      if (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
525,350,160✔
294
        pTableCxt->pData->aCol = taosArrayInit(128, sizeof(SColData));
839,095✔
295
        if (NULL == pTableCxt->pData->aCol) {
839,482✔
296
          code = terrno;
×
297
        }
298
      } else {
299
        pTableCxt->pData->aRowP = taosArrayInit(128, POINTER_BYTES);
524,508,049✔
300
        if (NULL == pTableCxt->pData->aRowP) {
524,515,862✔
301
          code = terrno;
×
302
        }
303
      }
304
    }
305
  }
306
  if (TSDB_CODE_SUCCESS == code) {
525,355,519✔
307
    *pOutput = pTableCxt;
525,355,519✔
308
    parserDebug("uid:%" PRId64 ", create table data context, code:%d, vgId:%d", pTableMeta->uid, code,
525,352,816✔
309
                pTableMeta->vgId);
310
  } else {
311
    insDestroyTableDataCxt(pTableCxt);
×
312
  }
313

314
  return code;
525,356,085✔
315
}
316

317
static int32_t rebuildTableData(SSubmitTbData* pSrc, SSubmitTbData** pDst, int8_t hasBlob) {
1,904,551✔
318
  int32_t        code = TSDB_CODE_SUCCESS;
1,904,551✔
319
  SSubmitTbData* pTmp = taosMemoryCalloc(1, sizeof(SSubmitTbData));
1,904,551✔
320
  if (NULL == pTmp) {
1,903,789✔
321
    code = terrno;
×
322
  } else {
323
    pTmp->flags = pSrc->flags;
1,903,789✔
324
    pTmp->suid = pSrc->suid;
1,903,838✔
325
    pTmp->uid = pSrc->uid;
1,904,395✔
326
    pTmp->sver = pSrc->sver;
1,904,493✔
327
    pTmp->pCreateTbReq = NULL;
1,904,444✔
328
    if (pTmp->flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
1,904,346✔
329
      if (pSrc->pCreateTbReq) {
1,544,684✔
330
        code = cloneSVreateTbReq(pSrc->pCreateTbReq, &pTmp->pCreateTbReq);
1,544,635✔
331
      } else {
332
        pTmp->flags &= ~SUBMIT_REQ_AUTO_CREATE_TABLE;
×
333
      }
334
    }
335
    if (TSDB_CODE_SUCCESS == code) {
1,904,263✔
336
      if (pTmp->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,904,263✔
337
        pTmp->aCol = taosArrayInit(128, sizeof(SColData));
844,349✔
338
        if (NULL == pTmp->aCol) {
844,439✔
339
          code = terrno;
×
340
          taosMemoryFree(pTmp);
×
341
        }
342
      } else {
343
        pTmp->aRowP = taosArrayInit(128, POINTER_BYTES);
1,060,179✔
344
        if (NULL == pTmp->aRowP) {
1,060,194✔
345
          code = terrno;
×
346
          taosMemoryFree(pTmp);
×
347
        }
348

349
        if (code != 0) {
1,060,194✔
350
          taosArrayDestroy(pTmp->aRowP);
×
351
          taosMemoryFree(pTmp);
×
352
        }
353
      }
354

355
    } else {
356
      taosMemoryFree(pTmp);
×
357
    }
358
  }
359

360
  taosMemoryFree(pSrc);
1,904,584✔
361
  if (TSDB_CODE_SUCCESS == code) {
1,904,520✔
362
    *pDst = pTmp;
1,904,520✔
363
  }
364

365
  return code;
1,904,569✔
366
}
367

368
static void resetColValues(SArray* pValues) {
1,549,955✔
369
  int32_t num = taosArrayGetSize(pValues);
1,549,955✔
370
  for (int32_t i = 0; i < num; ++i) {
23,253,424✔
371
    SColVal* pVal = taosArrayGet(pValues, i);
21,703,469✔
372
    pVal->flag = CV_FLAG_NONE;
21,703,469✔
373
  }
374
}
1,549,955✔
375

376
int32_t insGetTableDataCxt(SHashObj* pHash, void* id, int32_t idLen, STableMeta* pTableMeta,
2,147,483,647✔
377
                           SVCreateTbReq** pCreateTbReq, STableDataCxt** pTableCxt, bool colMode, bool ignoreColVals) {
378
  STableDataCxt** tmp = (STableDataCxt**)taosHashGet(pHash, id, idLen);
2,147,483,647✔
379
  if (NULL != tmp) {
2,147,483,647✔
380
    *pTableCxt = *tmp;
2,147,483,647✔
381
    if (!ignoreColVals) {
2,147,483,647✔
382
      resetColValues((*pTableCxt)->pValues);
1,549,955✔
383
    }
384
    return TSDB_CODE_SUCCESS;
2,147,483,647✔
385
  }
386
  int32_t code = createTableDataCxt(pTableMeta, pCreateTbReq, pTableCxt, colMode, ignoreColVals);
525,353,662✔
387
  if (TSDB_CODE_SUCCESS == code) {
525,355,280✔
388
    void* pData = *pTableCxt;  // deal scan coverity
525,355,703✔
389
    code = taosHashPut(pHash, id, idLen, &pData, POINTER_BYTES);
525,355,850✔
390
  }
391

392
  if (TSDB_CODE_SUCCESS != code) {
525,358,347✔
393
    insDestroyTableDataCxt(*pTableCxt);
×
394
  }
395
  return code;
525,358,076✔
396
}
397

398
static void destroyColVal(void* p) {
2,147,483,647✔
399
  SColVal* pVal = p;
2,147,483,647✔
400
  if (TSDB_DATA_TYPE_NCHAR == pVal->value.type || TSDB_DATA_TYPE_GEOMETRY == pVal->value.type ||
2,147,483,647✔
401
      TSDB_DATA_TYPE_VARBINARY == pVal->value.type || TSDB_DATA_TYPE_DECIMAL == pVal->value.type) {
2,147,483,647✔
402
    taosMemoryFreeClear(pVal->value.pData);
829,887,738✔
403
  }
404
}
2,147,483,647✔
405

406
void insDestroyTableDataCxt(STableDataCxt* pTableCxt) {
525,868,223✔
407
  if (NULL == pTableCxt) {
525,868,223✔
408
    return;
×
409
  }
410

411
  taosMemoryFreeClear(pTableCxt->pMeta);
525,868,223✔
412
  tDestroyTSchema(pTableCxt->pSchema);
525,867,642✔
413
  insDestroyBoundColInfo(&pTableCxt->boundColsInfo);
525,868,197✔
414
  taosArrayDestroyEx(pTableCxt->pValues, destroyColVal);
525,868,026✔
415
  if (pTableCxt->pData) {
525,868,320✔
416
    tDestroySubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
3,165,748✔
417
    taosMemoryFree(pTableCxt->pData);
3,165,895✔
418
  }
419
  taosMemoryFree(pTableCxt);
525,867,080✔
420
}
421

422
void insDestroyVgroupDataCxt(SVgroupDataCxt* pVgCxt) {
496,817,322✔
423
  if (NULL == pVgCxt) {
496,817,322✔
424
    return;
×
425
  }
426

427
  tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
496,817,322✔
428
  taosMemoryFree(pVgCxt->pData);
496,817,176✔
429

430
  taosMemoryFree(pVgCxt);
496,816,561✔
431
}
432

433
void insDestroyVgroupDataCxtList(SArray* pVgCxtList) {
949,365,296✔
434
  if (NULL == pVgCxtList) {
949,365,296✔
435
    return;
473,280,211✔
436
  }
437

438
  size_t size = taosArrayGetSize(pVgCxtList);
476,085,085✔
439
  for (int32_t i = 0; i < size; i++) {
972,911,782✔
440
    void* p = taosArrayGetP(pVgCxtList, i);
496,821,500✔
441
    insDestroyVgroupDataCxt(p);
496,821,472✔
442
  }
443

444
  taosArrayDestroy(pVgCxtList);
476,090,282✔
445
}
446

447
void insDestroyVgroupDataCxtHashMap(SHashObj* pVgCxtHash) {
×
448
  if (NULL == pVgCxtHash) {
×
449
    return;
×
450
  }
451

452
  void** p = taosHashIterate(pVgCxtHash, NULL);
×
453
  while (p) {
×
454
    insDestroyVgroupDataCxt(*(SVgroupDataCxt**)p);
×
455

456
    p = taosHashIterate(pVgCxtHash, p);
×
457
  }
458

459
  taosHashCleanup(pVgCxtHash);
×
460
}
461

462
void insDestroyTableDataCxtHashMap(SHashObj* pTableCxtHash) {
474,214,798✔
463
  if (NULL == pTableCxtHash) {
474,214,798✔
464
    return;
832,743✔
465
  }
466

467
  void** p = taosHashIterate(pTableCxtHash, NULL);
473,382,055✔
468
  while (p) {
997,885,856✔
469
    insDestroyTableDataCxt(*(STableDataCxt**)p);
524,502,004✔
470

471
    p = taosHashIterate(pTableCxtHash, p);
524,498,966✔
472
  }
473

474
  taosHashCleanup(pTableCxtHash);
473,383,852✔
475
}
476

477
static int32_t fillVgroupDataCxt(STableDataCxt* pTableCxt, SVgroupDataCxt* pVgCxt, bool isRebuild, bool clear) {
535,266,295✔
478
  int32_t code = 0;
535,266,295✔
479
  if (NULL == pVgCxt->pData->aSubmitTbData) {
535,266,295✔
480
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
494,515,365✔
481
    if (pVgCxt->pData->aSubmitTbData == NULL) {
494,519,215✔
482
      return terrno;
×
483
    }
484
    if (pTableCxt->hasBlob) {
494,518,432✔
485
      pVgCxt->pData->aSubmitBlobData = taosArrayInit(128, sizeof(SBlobSet*));
23,722✔
486
      if (NULL == pVgCxt->pData->aSubmitBlobData) {
23,722✔
487
        return terrno;
×
488
      }
489
    }
490
  }
491

492
  // push data to submit, rebuild empty data for next submit
493
  if (!pTableCxt->hasBlob) pTableCxt->pData->pBlobSet = NULL;
535,290,152✔
494

495
  if (NULL == taosArrayPush(pVgCxt->pData->aSubmitTbData, pTableCxt->pData)) {
1,070,570,527✔
496
    return terrno;
×
497
  }
498

499
  if (pTableCxt->hasBlob) {
535,282,450✔
500
    parserDebug("blob row transfer %p, pData %p, %s", pTableCxt->pData->pBlobSet, pTableCxt->pData, __func__);
23,722✔
501
    if (NULL == taosArrayPush(pVgCxt->pData->aSubmitBlobData, &pTableCxt->pData->pBlobSet)) {
47,444✔
502
      return terrno;
×
503
    }
504
    pTableCxt->pData->pBlobSet = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
23,722✔
505
  }
506

507
  if (isRebuild) {
535,273,515✔
508
    code = rebuildTableData(pTableCxt->pData, &pTableCxt->pData, pTableCxt->hasBlob);
1,904,551✔
509
  } else if (clear) {
533,368,964✔
510
    taosMemoryFreeClear(pTableCxt->pData);
522,685,856✔
511
  }
512
  parserDebug("uid:%" PRId64 ", add table data context to vgId:%d", pTableCxt->pMeta->uid, pVgCxt->vgId);
535,273,784✔
513

514
  return code;
535,273,962✔
515
}
516

517
static int32_t createVgroupDataCxt(int32_t vgId, SHashObj* pVgroupHash, SArray* pVgroupList, SVgroupDataCxt** pOutput) {
496,839,680✔
518
  SVgroupDataCxt* pVgCxt = taosMemoryCalloc(1, sizeof(SVgroupDataCxt));
496,839,680✔
519
  if (NULL == pVgCxt) {
496,843,904✔
520
    return terrno;
×
521
  }
522
  pVgCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitReq2));
496,843,904✔
523
  if (NULL == pVgCxt->pData) {
496,841,207✔
524
    insDestroyVgroupDataCxt(pVgCxt);
×
525
    return terrno;
×
526
  }
527

528
  pVgCxt->vgId = vgId;
496,840,836✔
529
  int32_t code = taosHashPut(pVgroupHash, &pVgCxt->vgId, sizeof(pVgCxt->vgId), &pVgCxt, POINTER_BYTES);
496,839,144✔
530
  if (TSDB_CODE_SUCCESS == code) {
496,842,798✔
531
    if (NULL == taosArrayPush(pVgroupList, &pVgCxt)) {
496,843,122✔
532
      code = terrno;
×
533
      insDestroyVgroupDataCxt(pVgCxt);
×
534
      return code;
×
535
    }
536
    //    uDebug("td23101 2vgId:%d, uid:%" PRIu64, pVgCxt->vgId, pTableCxt->pMeta->uid);
537
    *pOutput = pVgCxt;
496,843,122✔
538
  } else {
539
    insDestroyVgroupDataCxt(pVgCxt);
×
540
  }
541
  return code;
496,839,418✔
542
}
543

544
int insColDataComp(const void* lp, const void* rp) {
8,646,232✔
545
  SColData* pLeft = (SColData*)lp;
8,646,232✔
546
  SColData* pRight = (SColData*)rp;
8,646,232✔
547
  if (pLeft->cid < pRight->cid) {
8,646,232✔
548
    return -1;
8,272,586✔
549
  } else if (pLeft->cid > pRight->cid) {
373,842✔
550
    return 1;
373,842✔
551
  }
552

553
  return 0;
×
554
}
555

556
int32_t insTryAddTableVgroupInfo(SHashObj* pAllVgHash, SStbInterlaceInfo* pBuildInfo, int32_t* vgId,
266,772✔
557
                                 STableColsData* pTbData, SName* sname) {
558
  if (*vgId >= 0 && taosHashGet(pAllVgHash, (const char*)vgId, sizeof(*vgId))) {
266,772✔
559
    return TSDB_CODE_SUCCESS;
210,762✔
560
  }
561

562
  SVgroupInfo      vgInfo = {0};
56,495✔
563
  SRequestConnInfo conn = {.pTrans = pBuildInfo->transport,
111,855✔
564
                           .requestId = pBuildInfo->requestId,
56,236✔
565
                           .requestObjRefId = pBuildInfo->requestSelf,
56,236✔
566
                           .mgmtEps = pBuildInfo->mgmtEpSet};
567

568
  int32_t code = catalogGetTableHashVgroup((SCatalog*)pBuildInfo->pCatalog, &conn, sname, &vgInfo);
56,236✔
569
  if (TSDB_CODE_SUCCESS != code) {
56,270✔
570
    return code;
×
571
  }
572

573
  code = taosHashPut(pAllVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
56,270✔
574
  if (TSDB_CODE_SUCCESS != code) {
56,270✔
575
    return code;
×
576
  }
577

578
  return TSDB_CODE_SUCCESS;
56,270✔
579
}
580

581
int32_t insGetStmtTableVgUid(SHashObj* pAllVgHash, SStbInterlaceInfo* pBuildInfo, STableColsData* pTbData,
14,558,840✔
582
                             uint64_t* uid, int32_t* vgId, uint64_t* suid) {
583
  STableVgUid* pTbInfo = NULL;
14,558,840✔
584
  int32_t      code = 0;
14,558,840✔
585

586
  if (pTbData->getFromHash) {
14,558,840✔
587
    pTbInfo = (STableVgUid*)tSimpleHashGet(pBuildInfo->pTableHash, pTbData->tbName, strlen(pTbData->tbName));
14,301,169✔
588
  }
589

590
  if (NULL == pTbInfo) {
14,562,466✔
591
    SName sname;
225,132✔
592
    code = qCreateSName2(&sname, pTbData->tbName, pBuildInfo->acctId, pBuildInfo->dbname, NULL, 0);
267,812✔
593
    if (TSDB_CODE_SUCCESS != code) {
267,797✔
594
      return code;
1,300✔
595
    }
596

597
    STableMeta*      pTableMeta = NULL;
267,797✔
598
    SRequestConnInfo conn = {.pTrans = pBuildInfo->transport,
493,174✔
599
                             .requestId = pBuildInfo->requestId,
268,057✔
600
                             .requestObjRefId = pBuildInfo->requestSelf,
267,537✔
601
                             .mgmtEps = pBuildInfo->mgmtEpSet};
602
    code = catalogGetTableMeta((SCatalog*)pBuildInfo->pCatalog, &conn, &sname, &pTableMeta);
268,057✔
603

604
    if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
267,812✔
605
      parserWarn("stmt2 async bind don't find table:%s.%s, try auto create table", sname.dbname, sname.tname);
1,368✔
606
      return code;
1,300✔
607
    }
608

609
    if (TSDB_CODE_SUCCESS != code) {
266,444✔
610
      parserError("stmt2 async get table meta:%s.%s failed, code:%d", sname.dbname, sname.tname, code);
×
611
      return code;
×
612
    }
613

614
    *uid = pTableMeta->uid;
266,444✔
615
    *vgId = pTableMeta->vgId;
266,704✔
616
    *suid = pTableMeta->suid;
266,964✔
617

618
    STableVgUid tbInfo = {.uid = *uid, .vgid = *vgId, .suid = *suid};
266,184✔
619
    code = tSimpleHashPut(pBuildInfo->pTableHash, pTbData->tbName, strlen(pTbData->tbName), &tbInfo, sizeof(tbInfo));
266,444✔
620
    if (TSDB_CODE_SUCCESS == code) {
267,032✔
621
      code = insTryAddTableVgroupInfo(pAllVgHash, pBuildInfo, vgId, pTbData, &sname);
267,032✔
622
    }
623

624
    taosMemoryFree(pTableMeta);
267,032✔
625
  } else {
626
    *uid = pTbInfo->uid;
14,294,654✔
627
    *vgId = pTbInfo->vgid;
14,299,865✔
628
    *suid = pTbInfo->suid;
14,299,865✔
629
  }
630

631
  return code;
14,567,997✔
632
}
633

634
int32_t qBuildStmtFinOutput1(SQuery* pQuery, SHashObj* pAllVgHash, SArray* pVgDataBlocks) {
×
635
  int32_t             code = TSDB_CODE_SUCCESS;
×
636
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
×
637

638
  if (TSDB_CODE_SUCCESS == code) {
×
639
    code = insBuildVgDataBlocks(pAllVgHash, pVgDataBlocks, &pStmt->pDataBlocks, true);
×
640
  }
641

642
  return code;
×
643
}
644

645
int32_t checkAndMergeSVgroupDataCxtByTbname(STableDataCxt* pTbCtx, SVgroupDataCxt* pVgCxt, SSHashObj* pTableNameHash,
3,875,684✔
646
                                            char* tbname) {
647
  if (NULL == pVgCxt->pData->aSubmitTbData) {
3,875,684✔
648
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
2,323,082✔
649
    if (NULL == pVgCxt->pData->aSubmitTbData) {
2,324,602✔
650
      return terrno;
×
651
    }
652
    if (pTbCtx->hasBlob) {
2,324,343✔
653
      pVgCxt->pData->aSubmitBlobData = taosArrayInit(128, sizeof(SBlobSet*));
×
654
      if (pVgCxt->pData->aSubmitBlobData == NULL) {
×
655
        return terrno;
×
656
      }
657
    }
658
  }
659

660
  int32_t  code = TSDB_CODE_SUCCESS;
3,877,723✔
661
  SArray** rowP = NULL;
3,877,723✔
662

663
  rowP = (SArray**)tSimpleHashGet(pTableNameHash, tbname, strlen(tbname));
3,877,723✔
664

665
  if (rowP != NULL && *rowP != NULL) {
3,877,354✔
666
    for (int32_t j = 0; j < taosArrayGetSize(*rowP); ++j) {
×
667
      SRow* pRow = (SRow*)taosArrayGetP(pTbCtx->pData->aRowP, j);
×
668
      if (pRow) {
×
669
        if (NULL == taosArrayPush(*rowP, &pRow)) {
×
670
          return terrno;
×
671
        }
672
      }
673

674
      if (pTbCtx->hasBlob == 0) {
×
675
        code = tRowSort(*rowP);
×
676
        TAOS_CHECK_RETURN(code);
×
677

678
        code = tRowMerge(*rowP, pTbCtx->pSchema, 0);
×
679
        TAOS_CHECK_RETURN(code);
×
680
      } else {
681
        code = tRowSortWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet);
×
682
        TAOS_CHECK_RETURN(code);
×
683

684
        code = tRowMergeWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet, 0);
×
685
        TAOS_CHECK_RETURN(code);
×
686
      }
687
    }
688

689
    parserDebug("merge same uid data: %" PRId64 ", vgId:%d", pTbCtx->pData->uid, pVgCxt->vgId);
×
690

691
    if (pTbCtx->pData->pCreateTbReq != NULL) {
×
692
      tdDestroySVCreateTbReq(pTbCtx->pData->pCreateTbReq);
×
693
      taosMemoryFree(pTbCtx->pData->pCreateTbReq);
×
694
      pTbCtx->pData->pCreateTbReq = NULL;
×
695
    }
696
    return TSDB_CODE_SUCCESS;
×
697
  }
698

699
  if (pTbCtx->hasBlob == 0) {
3,877,354✔
700
    pTbCtx->pData->pBlobSet = NULL;  // if no blob, set it to NULL
3,877,184✔
701
  }
702

703
  if (NULL == taosArrayPush(pVgCxt->pData->aSubmitTbData, pTbCtx->pData)) {
7,753,556✔
704
    return terrno;
×
705
  }
706

707
  if (pTbCtx->hasBlob) {
3,876,992✔
708
    parserDebug("blob row transfer %p, pData %p, %s", pTbCtx->pData->pBlobSet, pTbCtx->pData, __func__);
×
709
    if (NULL == taosArrayPush(pVgCxt->pData->aSubmitBlobData, &pTbCtx->pData->pBlobSet)) {
×
710
      return terrno;
×
711
    }
712
    pTbCtx->pData->pBlobSet = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
×
713
  }
714

715
  code = tSimpleHashPut(pTableNameHash, tbname, strlen(tbname), &pTbCtx->pData->aRowP, sizeof(SArray*));
3,876,992✔
716

717
  if (code != TSDB_CODE_SUCCESS) {
3,875,370✔
718
    return code;
×
719
  }
720

721
  parserDebug("uid:%" PRId64 ", add table data context to vgId:%d", pTbCtx->pMeta->uid, pVgCxt->vgId);
3,875,370✔
722

723
  return TSDB_CODE_SUCCESS;
3,876,542✔
724
}
725

726
int32_t insAppendStmtTableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
10,681,472✔
727
                                  SStbInterlaceInfo* pBuildInfo) {
728
  int32_t  code = TSDB_CODE_SUCCESS;
10,681,472✔
729
  uint64_t uid;
10,641,860✔
730
  int32_t  vgId;
10,647,086✔
731
  uint64_t suid;
10,647,911✔
732

733
  pTbCtx->pData->aRowP = pTbData->aCol;
10,688,073✔
734

735
  code = insGetStmtTableVgUid(pAllVgHash, pBuildInfo, pTbData, &uid, &vgId, &suid);
10,695,223✔
736
  if (TSDB_CODE_SUCCESS != code) {
10,690,514✔
737
    return code;
×
738
  }
739

740
  pTbCtx->pMeta->vgId = vgId;
10,690,514✔
741
  pTbCtx->pMeta->uid = uid;
10,691,339✔
742
  pTbCtx->pData->uid = uid;
10,691,889✔
743

744
  if (!pTbCtx->ordered) {
10,690,789✔
745
    code = tRowSort(pTbCtx->pData->aRowP);
34✔
746
  }
747
  if (code == TSDB_CODE_SUCCESS && (!pTbCtx->ordered || pTbCtx->duplicateTs)) {
10,683,928✔
748
    code = tRowMerge(pTbCtx->pData->aRowP, pTbCtx->pSchema, 0);
34✔
749
  }
750

751
  if (TSDB_CODE_SUCCESS != code) {
10,680,381✔
752
    return code;
×
753
  }
754

755
  SVgroupDataCxt* pVgCxt = NULL;
10,680,381✔
756
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
10,681,481✔
757
  if (NULL == pp) {
10,692,164✔
758
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
1,763,726✔
759
    if (NULL == pp) {
1,764,537✔
760
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
1,764,537✔
761
    } else {
762
      pVgCxt = *(SVgroupDataCxt**)pp;
×
763
    }
764
  } else {
765
    pVgCxt = *(SVgroupDataCxt**)pp;
8,928,438✔
766
  }
767

768
  if (TSDB_CODE_SUCCESS == code) {
10,690,528✔
769
    code = fillVgroupDataCxt(pTbCtx, pVgCxt, false, false);
10,690,528✔
770
  }
771

772
  if (taosArrayGetSize(pVgCxt->pData->aSubmitTbData) >= 20000) {
10,682,011✔
773
    code = qBuildStmtFinOutput1((SQuery*)pBuildInfo->pQuery, pAllVgHash, pBuildInfo->pVgroupList);
×
774
    // taosArrayClear(pVgCxt->pData->aSubmitTbData);
775
    tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
×
776
    // insDestroyVgroupDataCxt(pVgCxt);
777
  }
778

779
  return code;
10,675,686✔
780
}
781

782
int32_t insAppendStmt2TableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
3,875,682✔
783
                                   SStbInterlaceInfo* pBuildInfo, SVCreateTbReq* ctbReq) {
784
  int32_t  code = TSDB_CODE_SUCCESS;
3,875,682✔
785
  uint64_t uid;
1,928,528✔
786
  int32_t  vgId;
1,929,047✔
787
  uint64_t suid;
1,929,307✔
788

789
  pTbCtx->pData->aRowP = pTbData->aCol;
3,876,461✔
790
  pTbCtx->pData->pBlobSet = pTbData->pBlobSet;
3,876,201✔
791

792
  code = insGetStmtTableVgUid(pAllVgHash, pBuildInfo, pTbData, &uid, &vgId, &suid);
3,876,201✔
793
  if (ctbReq != NULL && code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
3,877,162✔
794
    pTbCtx->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
1,300✔
795
    vgId = (int32_t)ctbReq->uid;
1,300✔
796
    uid = 0;
1,300✔
797
    pTbCtx->pMeta->vgId = (int32_t)ctbReq->uid;
1,300✔
798
    ctbReq->uid = 0;
1,300✔
799
    pTbCtx->pMeta->uid = 0;
1,300✔
800
    pTbCtx->pData->uid = 0;
1,300✔
801
    pTbCtx->pData->pCreateTbReq = ctbReq;
1,300✔
802
    code = TSDB_CODE_SUCCESS;
1,300✔
803
  } else {
804
    if (TSDB_CODE_SUCCESS != code) {
3,875,862✔
805
      return code;
×
806
    }
807
    if (pTbCtx->pData->suid != suid) {
3,875,862✔
808
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
×
809
    }
810

811
    pTbCtx->pMeta->vgId = vgId;
3,875,862✔
812
    pTbCtx->pMeta->uid = uid;
3,875,863✔
813
    pTbCtx->pData->uid = uid;
3,875,604✔
814
    pTbCtx->pData->pCreateTbReq = NULL;
3,876,122✔
815

816
    if (ctbReq != NULL) {
3,874,824✔
817
      tdDestroySVCreateTbReq(ctbReq);
818
      taosMemoryFree(ctbReq);
1,644,444✔
819
      ctbReq = NULL;
1,644,478✔
820
    }
821
  }
822

823
  if (pTbCtx->hasBlob == 0) {
3,876,464✔
824
    if (!pTbData->isOrdered) {
3,876,745✔
825
      code = tRowSort(pTbCtx->pData->aRowP);
×
826
    }
827
    if (code == TSDB_CODE_SUCCESS && (!pTbData->isOrdered || pTbData->isDuplicateTs)) {
3,875,709✔
828
      code = tRowMerge(pTbCtx->pData->aRowP, pTbCtx->pSchema, PREFER_NON_NULL);
272✔
829
    }
830
  } else {
831
    if (!pTbData->isOrdered) {
238✔
832
      code = tRowSortWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet);
×
833
    }
834
    if (code == TSDB_CODE_SUCCESS && (!pTbData->isOrdered || pTbData->isDuplicateTs)) {
238✔
835
      code = tRowMergeWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet, 0);
×
836
    }
837
  }
838

839
  if (TSDB_CODE_SUCCESS != code) {
3,875,899✔
840
    return code;
×
841
  }
842

843
  SVgroupDataCxt* pVgCxt = NULL;
3,875,899✔
844
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
3,876,418✔
845
  if (NULL == pp) {
3,876,904✔
846
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
2,324,118✔
847
    if (NULL == pp) {
2,324,704✔
848
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
2,324,704✔
849
    } else {
850
      pVgCxt = *(SVgroupDataCxt**)pp;
×
851
    }
852
  } else {
853
    pVgCxt = *(SVgroupDataCxt**)pp;
1,552,786✔
854
  }
855

856
  if (code == TSDB_CODE_SUCCESS) {
3,876,420✔
857
    code = checkAndMergeSVgroupDataCxtByTbname(pTbCtx, pVgCxt, pBuildInfo->pTableRowDataHash, pTbData->tbName);
3,876,284✔
858
  }
859

860
  if (taosArrayGetSize(pVgCxt->pData->aSubmitTbData) >= 20000) {
3,876,938✔
861
    code = qBuildStmtFinOutput1((SQuery*)pBuildInfo->pQuery, pAllVgHash, pBuildInfo->pVgroupList);
×
862
    // taosArrayClear(pVgCxt->pData->aSubmitTbData);
863
    tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
×
864
    // insDestroyVgroupDataCxt(pVgCxt);
865
  }
866

867
  return code;
3,876,372✔
868
}
869

870
/*
871
int32_t insMergeStmtTableDataCxt(STableDataCxt* pTableCxt, SArray* pTableList, SArray** pVgDataBlocks, bool isRebuild,
872
int32_t tbNum) { SHashObj* pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, false);
873
  SArray*   pVgroupList = taosArrayInit(8, POINTER_BYTES);
874
  if (NULL == pVgroupHash || NULL == pVgroupList) {
875
    taosHashCleanup(pVgroupHash);
876
    taosArrayDestroy(pVgroupList);
877
    return TSDB_CODE_OUT_OF_MEMORY;
878
  }
879

880
  int32_t code = TSDB_CODE_SUCCESS;
881

882
  for (int32_t i = 0; i < tbNum; ++i) {
883
    STableColsData *pTableCols = (STableColsData*)taosArrayGet(pTableList, i);
884
    pTableCxt->pMeta->vgId = pTableCols->vgId;
885
    pTableCxt->pMeta->uid = pTableCols->uid;
886
    pTableCxt->pData->uid = pTableCols->uid;
887
    pTableCxt->pData->aCol = pTableCols->aCol;
888

889
    SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
890
    if (pCol->nVal <= 0) {
891
      continue;
892
    }
893

894
    if (pTableCxt->pData->pCreateTbReq) {
895
      pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
896
    }
897

898
    taosArraySort(pTableCxt->pData->aCol, insColDataComp);
899

900
    tColDataSortMerge(pTableCxt->pData->aCol);
901

902
    if (TSDB_CODE_SUCCESS == code) {
903
      SVgroupDataCxt* pVgCxt = NULL;
904
      int32_t         vgId = pTableCxt->pMeta->vgId;
905
      void**          pp = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
906
      if (NULL == pp) {
907
        code = createVgroupDataCxt(pTableCxt, pVgroupHash, pVgroupList, &pVgCxt);
908
      } else {
909
        pVgCxt = *(SVgroupDataCxt**)pp;
910
      }
911
      if (TSDB_CODE_SUCCESS == code) {
912
        code = fillVgroupDataCxt(pTableCxt, pVgCxt, false, false);
913
      }
914
    }
915
  }
916

917
  taosHashCleanup(pVgroupHash);
918
  if (TSDB_CODE_SUCCESS == code) {
919
    *pVgDataBlocks = pVgroupList;
920
  } else {
921
    insDestroyVgroupDataCxtList(pVgroupList);
922
  }
923

924
  return code;
925
}
926
*/
927

928
static int8_t colDataHasBlob(SColData* pCol) {
×
929
  if (IS_STR_DATA_BLOB(pCol->type)) {
×
930
    return 1;
×
931
  }
932
  return 0;
×
933
}
934
int32_t insMergeTableDataCxt(SHashObj* pTableHash, SArray** pVgDataBlocks, bool isRebuild) {
472,460,139✔
935
  SHashObj* pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
472,460,139✔
936
  SArray*   pVgroupList = taosArrayInit(8, POINTER_BYTES);
472,464,124✔
937
  if (NULL == pVgroupHash || NULL == pVgroupList) {
472,463,902✔
938
    taosHashCleanup(pVgroupHash);
222✔
939
    taosArrayDestroy(pVgroupList);
×
940
    return terrno;
×
941
  }
942

943
  int32_t code = TSDB_CODE_SUCCESS;
472,463,912✔
944
  bool    colFormat = false;
472,463,912✔
945

946
  void* p = taosHashIterate(pTableHash, NULL);
472,463,912✔
947
  if (p) {
472,466,642✔
948
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
472,466,642✔
949
    colFormat = (0 != (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT));
472,466,587✔
950
  }
951

952
  while (TSDB_CODE_SUCCESS == code && NULL != p) {
997,063,315✔
953
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
524,593,028✔
954
    if (colFormat) {
524,594,058✔
955
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
845,048✔
956
      if (pCol && pCol->nVal <= 0) {
845,048✔
957
        p = taosHashIterate(pTableHash, p);
495✔
958
        continue;
495✔
959
      }
960

961
      if (pTableCxt->pData->pCreateTbReq) {
844,553✔
962
        pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
509,034✔
963
      }
964
      int8_t isBlob = IS_STR_DATA_BLOB(pCol->type) ? 1 : 0;
844,553✔
965
      if (isBlob == 0) {
844,553✔
966
        taosArraySort(pTableCxt->pData->aCol, insColDataComp);
844,553✔
967
        code = tColDataSortMerge(&pTableCxt->pData->aCol);
844,553✔
968
      } else {
969
        taosArraySort(pTableCxt->pData->aCol, insColDataComp);
×
970
        code = tColDataSortMergeWithBlob(&pTableCxt->pData->aCol, pTableCxt->pData->pBlobSet);
×
971
      }
972
    } else {
973
      // skip the table has no data to insert
974
      // eg: import a csv without valid data
975
      // if (0 == taosArrayGetSize(pTableCxt->pData->aRowP)) {
976
      //   parserWarn("no row in tableDataCxt uid:%" PRId64 " ", pTableCxt->pMeta->uid);
977
      //   p = taosHashIterate(pTableHash, p);
978
      //   continue;
979
      // }
980
      if (pTableCxt->hasBlob == 0) {
523,749,010✔
981
        if (!pTableCxt->ordered) {
523,725,367✔
982
          code = tRowSort(pTableCxt->pData->aRowP);
579,717✔
983
        }
984
        if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
523,723,725✔
985
          code = tRowMerge(pTableCxt->pData->aRowP, pTableCxt->pSchema, PREFER_NON_NULL);
605,915✔
986
        }
987
      } else {
988
        if (!pTableCxt->ordered) {
23,722✔
989
          code = tRowSortWithBlob(pTableCxt->pData->aRowP, pTableCxt->pSchema, pTableCxt->pData->pBlobSet);
808✔
990
        }
991
        if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
23,722✔
992
          code = tRowMergeWithBlob(pTableCxt->pData->aRowP, pTableCxt->pSchema, pTableCxt->pData->pBlobSet, 0);
808✔
993
        }
994
      }
995
    }
996

997
    if (TSDB_CODE_SUCCESS == code) {
524,590,464✔
998
      SVgroupDataCxt* pVgCxt = NULL;
524,590,464✔
999
      int32_t         vgId = pTableCxt->pMeta->vgId;
524,593,100✔
1000
      void**          pp = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
524,595,780✔
1001
      if (NULL == pp) {
524,592,683✔
1002
        code = createVgroupDataCxt(vgId, pVgroupHash, pVgroupList, &pVgCxt);
492,753,293✔
1003
      } else {
1004
        pVgCxt = *(SVgroupDataCxt**)pp;
31,839,390✔
1005
      }
1006
      if (TSDB_CODE_SUCCESS == code) {
524,592,111✔
1007
        code = fillVgroupDataCxt(pTableCxt, pVgCxt, isRebuild, true);
524,593,168✔
1008
      }
1009
    }
1010
    if (TSDB_CODE_SUCCESS == code) {
524,594,168✔
1011
      p = taosHashIterate(pTableHash, p);
524,594,168✔
1012
    }
1013
  }
1014

1015
  taosHashCleanup(pVgroupHash);
472,470,287✔
1016
  if (TSDB_CODE_SUCCESS == code) {
472,463,448✔
1017
    *pVgDataBlocks = pVgroupList;
472,464,496✔
1018
  } else {
1019
    insDestroyVgroupDataCxtList(pVgroupList);
68✔
1020
  }
1021

1022
  return code;
472,464,788✔
1023
}
1024

1025
static int32_t buildSubmitReq(int32_t vgId, SSubmitReq2* pReq, void** pData, uint32_t* pLen) {
496,838,093✔
1026
  int32_t  code = TSDB_CODE_SUCCESS;
496,838,093✔
1027
  uint32_t len = 0;
496,838,093✔
1028
  void*    pBuf = NULL;
496,838,093✔
1029
  tEncodeSize(tEncodeSubmitReq, pReq, len, code);
496,838,093✔
1030
  if (TSDB_CODE_SUCCESS == code) {
496,839,867✔
1031
    SEncoder encoder;
493,528,035✔
1032
    len += sizeof(SSubmitReq2Msg);
496,837,236✔
1033
    pBuf = taosMemoryMalloc(len);
496,837,236✔
1034
    if (NULL == pBuf) {
496,835,577✔
1035
      return terrno;
×
1036
    }
1037
    ((SSubmitReq2Msg*)pBuf)->header.vgId = htonl(vgId);
496,835,577✔
1038
    ((SSubmitReq2Msg*)pBuf)->header.contLen = htonl(len);
496,838,850✔
1039
    ((SSubmitReq2Msg*)pBuf)->version = htobe64(1);
496,841,996✔
1040
    tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SSubmitReq2Msg)), len - sizeof(SSubmitReq2Msg));
496,841,249✔
1041
    code = tEncodeSubmitReq(&encoder, pReq);
496,844,702✔
1042
    tEncoderClear(&encoder);
496,845,276✔
1043
  }
1044

1045
  if (TSDB_CODE_SUCCESS == code) {
496,844,077✔
1046
    *pData = pBuf;
496,844,077✔
1047
    *pLen = len;
496,844,869✔
1048
  } else {
1049
    taosMemoryFree(pBuf);
×
1050
  }
1051
  return code;
496,837,863✔
1052
}
1053

1054
static void destroyVgDataBlocks(void* p) {
×
1055
  if (p == NULL) return;
×
1056
  SVgDataBlocks* pVg = p;
×
1057
  taosMemoryFree(pVg->pData);
×
1058
  taosMemoryFree(pVg);
×
1059
}
1060

1061
int32_t insResetBlob(SSubmitReq2* p) {
496,838,213✔
1062
  int32_t code = 0;
496,838,213✔
1063
  if (p->raw) {
496,838,213✔
1064
    return TSDB_CODE_SUCCESS;  // no blob data in raw mode
147✔
1065
  }
1066

1067
  if (p->aSubmitBlobData != NULL) {
496,839,409✔
1068
    for (int32_t i = 0; i < taosArrayGetSize(p->aSubmitTbData); i++) {
47,444✔
1069
      SSubmitTbData* pSubmitTbData = taosArrayGet(p->aSubmitTbData, i);
23,722✔
1070
      SBlobSet**     ppBlob = taosArrayGet(p->aSubmitBlobData, i);
23,722✔
1071
      SBlobSet*      pBlob = ppBlob ? *ppBlob : NULL;
23,722✔
1072
      int32_t        nrow = taosArrayGetSize(pSubmitTbData->aRowP);
23,722✔
1073
      int32_t        nblob = 0;
23,722✔
1074
      if (nrow > 0 && pBlob) {
23,722✔
1075
        nblob = taosArrayGetSize(pBlob->pSeqTable);
23,722✔
1076
      }
1077
      uTrace("blob %p row size %d, pData size %d", pBlob, nblob, nrow);
23,722✔
1078
      pSubmitTbData->pBlobSet = pBlob;
23,722✔
1079
      if (ppBlob != NULL) *ppBlob = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
23,722✔
1080
    }
1081
  } else {
1082
    for (int32_t i = 0; i < taosArrayGetSize(p->aSubmitTbData); i++) {
1,035,957,598✔
1083
      SSubmitTbData* pSubmitTbData = taosArrayGet(p->aSubmitTbData, i);
539,142,501✔
1084
      pSubmitTbData->pBlobSet = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
539,142,649✔
1085
    }
1086
  }
1087

1088
  return code;
496,841,066✔
1089
}
1090
int32_t insBuildVgDataBlocks(SHashObj* pVgroupsHashObj, SArray* pVgDataCxtList, SArray** pVgDataBlocks, bool append) {
476,110,925✔
1091
  size_t  numOfVg = taosArrayGetSize(pVgDataCxtList);
476,110,925✔
1092
  SArray* pDataBlocks = (append && *pVgDataBlocks) ? *pVgDataBlocks : taosArrayInit(numOfVg, POINTER_BYTES);
476,113,736✔
1093
  if (NULL == pDataBlocks) {
476,112,662✔
1094
    return TSDB_CODE_OUT_OF_MEMORY;
×
1095
  }
1096

1097
  int32_t code = TSDB_CODE_SUCCESS;
476,112,662✔
1098
  for (size_t i = 0; TSDB_CODE_SUCCESS == code && i < numOfVg; ++i) {
972,954,368✔
1099
    SVgroupDataCxt* src = taosArrayGetP(pVgDataCxtList, i);
496,838,428✔
1100
    if (taosArrayGetSize(src->pData->aSubmitTbData) <= 0) {
496,844,079✔
1101
      continue;
×
1102
    }
1103
    SVgDataBlocks* dst = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
496,839,134✔
1104
    if (NULL == dst) {
496,838,938✔
1105
      code = terrno;
×
1106
    }
1107

1108
    if (TSDB_CODE_SUCCESS == code) {
496,838,938✔
1109
      dst->numOfTables = taosArrayGetSize(src->pData->aSubmitTbData);
496,839,304✔
1110
      code = taosHashGetDup(pVgroupsHashObj, (const char*)&src->vgId, sizeof(src->vgId), &dst->vg);
496,840,465✔
1111
    }
1112
    if (TSDB_CODE_SUCCESS == code) {
496,845,687✔
1113
      code = insResetBlob(src->pData);
496,845,687✔
1114
    }
1115

1116
    if (TSDB_CODE_SUCCESS == code) {
496,838,464✔
1117
      code = buildSubmitReq(src->vgId, src->pData, &dst->pData, &dst->size);
496,838,532✔
1118
    }
1119
    if (TSDB_CODE_SUCCESS == code) {
496,838,929✔
1120
      code = (NULL == taosArrayPush(pDataBlocks, &dst) ? terrno : TSDB_CODE_SUCCESS);
496,842,001✔
1121
    }
1122
    if (TSDB_CODE_SUCCESS != code) {
496,842,191✔
1123
      destroyVgDataBlocks(dst);
×
1124
    }
1125
  }
1126

1127
  if (append) {
476,115,940✔
1128
    if (NULL == *pVgDataBlocks) {
3,649,415✔
1129
      *pVgDataBlocks = pDataBlocks;
3,648,897✔
1130
    }
1131
    return code;
3,649,156✔
1132
  }
1133

1134
  if (TSDB_CODE_SUCCESS == code) {
472,466,525✔
1135
    *pVgDataBlocks = pDataBlocks;
472,462,851✔
1136
  } else {
1137
    taosArrayDestroyP(pDataBlocks, destroyVgDataBlocks);
3,674✔
1138
  }
1139

1140
  return code;
472,464,107✔
1141
}
1142

1143
static bool findFileds(SSchema* pSchema, TAOS_FIELD* fields, int numFields) {
×
1144
  for (int i = 0; i < numFields; i++) {
×
1145
    if (strcmp(pSchema->name, fields[i].name) == 0) {
×
1146
      return true;
×
1147
    }
1148
  }
1149

1150
  return false;
×
1151
}
1152

1153
int32_t checkSchema(SSchema* pColSchema, SSchemaExt* pColExtSchema, int8_t* fields, char* errstr, int32_t errstrLen) {
40,068✔
1154
  if (*fields != pColSchema->type) {
40,068✔
1155
    if (errstr != NULL) {
46✔
1156
      snprintf(errstr, errstrLen, "column type not equal, name:%s, schema type:%s, data type:%s", pColSchema->name,
×
1157
               tDataTypes[pColSchema->type].name, tDataTypes[*fields].name);
×
1158
    } else {
1159
      char buf[512] = {0};
46✔
1160
      snprintf(buf, sizeof(buf), "column type not equal, name:%s, schema type:%s, data type:%s", pColSchema->name,
92✔
1161
               tDataTypes[pColSchema->type].name, tDataTypes[*fields].name);
92✔
1162
      uError("checkSchema %s", buf);
46✔
1163
    }
1164
    return TSDB_CODE_INVALID_PARA;
46✔
1165
  }
1166

1167
  if (IS_DECIMAL_TYPE(pColSchema->type)) {
40,022✔
1168
    uint8_t precision = 0, scale = 0;
651✔
1169
    decimalFromTypeMod(pColExtSchema->typeMod, &precision, &scale);
651✔
1170
    uint8_t precisionData = 0, scaleData = 0;
651✔
1171
    int32_t bytes = *(int32_t*)(fields + sizeof(int8_t));
651✔
1172
    extractDecimalTypeInfoFromBytes(&bytes, &precisionData, &scaleData);
651✔
1173
    if (precision != precisionData || scale != scaleData) {
651✔
1174
      if (errstr != NULL) {
×
1175
        snprintf(errstr, errstrLen,
×
1176
                 "column decimal type not equal, name:%s, schema type:%s, precision:%d, scale:%d, data type:%s, "
1177
                 "precision:%d, scale:%d",
1178
                 pColSchema->name, tDataTypes[pColSchema->type].name, precision, scale, tDataTypes[*fields].name,
×
1179
                 precisionData, scaleData);
1180
        return TSDB_CODE_INVALID_PARA;
×
1181
      } else {
1182
        char buf[512] = {0};
×
1183
        snprintf(buf, sizeof(buf),
×
1184
                 "column decimal type not equal, name:%s, schema type:%s, precision:%d, scale:%d, data type:%s, "
1185
                 "precision:%d, scale:%d",
1186
                 pColSchema->name, tDataTypes[pColSchema->type].name, precision, scale, tDataTypes[*fields].name,
×
1187
                 precisionData, scaleData);
1188
        uError("checkSchema %s", buf);
×
1189
        return TSDB_CODE_INVALID_PARA;
×
1190
      }
1191
    }
1192
    return 0;
651✔
1193
  }
1194

1195
  if (IS_VAR_DATA_TYPE(pColSchema->type)) {
39,371✔
1196
    int32_t bytes = *(int32_t*)(fields + sizeof(int8_t));
7,314✔
1197
    if (IS_STR_DATA_BLOB(pColSchema->type)) {
7,314✔
1198
      if (bytes >= TSDB_MAX_BLOB_LEN) {
×
1199
        uError("column blob data bytes exceed max limit, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
×
1200
               pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name, bytes);
1201
        return TSDB_CODE_INVALID_PARA;
×
1202
      }
1203
    } else {
1204
      if (bytes > pColSchema->bytes) {
7,314✔
1205
        if (errstr != NULL) {
46✔
1206
          snprintf(errstr, errstrLen,
×
1207
                   "column var data bytes error, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
1208
                   pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name,
×
1209
                   *(int32_t*)(fields + sizeof(int8_t)));
×
1210
        } else {
1211
          char buf[512] = {0};
46✔
1212
          snprintf(buf, sizeof(buf),
92✔
1213
                   "column var data bytes error, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
1214
                   pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name,
138✔
1215
                   *(int32_t*)(fields + sizeof(int8_t)));
46✔
1216
          uError("checkSchema %s", buf);
46✔
1217
        }
1218
        return TSDB_CODE_INVALID_PARA;
46✔
1219
      }
1220
    }
1221
  }
1222

1223
  if (!IS_VAR_DATA_TYPE(pColSchema->type) && *(int32_t*)(fields + sizeof(int8_t)) != pColSchema->bytes) {
39,325✔
1224
    if (errstr != NULL) {
×
1225
      snprintf(errstr, errstrLen,
×
1226
               "column normal data bytes not equal, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
1227
               pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name,
×
1228
               *(int32_t*)(fields + sizeof(int8_t)));
×
1229
    } else {
1230
      char buf[512] = {0};
×
1231
      snprintf(buf, sizeof(buf),
×
1232
               "column normal data bytes not equal, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
1233
               pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name,
×
1234
               *(int32_t*)(fields + sizeof(int8_t)));
×
1235
      uError("checkSchema %s", buf);
×
1236
    }
1237
    return TSDB_CODE_INVALID_PARA;
×
1238
  }
1239
  return 0;
39,325✔
1240
}
1241

1242
#define PRCESS_DATA(i, j)                                                                                          \
1243
  ret = checkSchema(pColSchema, pColExtSchema, fields, errstr, errstrLen);                                         \
1244
  if (ret != 0) {                                                                                                  \
1245
    goto end;                                                                                                      \
1246
  }                                                                                                                \
1247
                                                                                                                   \
1248
  if (pColSchema->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {                                                          \
1249
    hasTs = true;                                                                                                  \
1250
  }                                                                                                                \
1251
                                                                                                                   \
1252
  int8_t* offset = pStart;                                                                                         \
1253
  if (IS_VAR_DATA_TYPE(pColSchema->type)) {                                                                        \
1254
    pStart += numOfRows * sizeof(int32_t);                                                                         \
1255
  } else {                                                                                                         \
1256
    pStart += BitmapLen(numOfRows);                                                                                \
1257
  }                                                                                                                \
1258
  char* pData = pStart;                                                                                            \
1259
                                                                                                                   \
1260
  SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, j);                                                        \
1261
  if (hasBlob) {                                                                                                   \
1262
    ret = tColDataAddValueByDataBlockWithBlob(pCol, pColSchema->type, pColSchema->bytes, numOfRows, offset, pData, \
1263
                                              pBlobSet);                                                           \
1264
  } else {                                                                                                         \
1265
    ret = tColDataAddValueByDataBlock(pCol, pColSchema->type, pColSchema->bytes, numOfRows, offset, pData);        \
1266
  }                                                                                                                \
1267
  if (ret != 0) {                                                                                                  \
1268
    goto end;                                                                                                      \
1269
  }                                                                                                                \
1270
  fields += sizeof(int8_t) + sizeof(int32_t);                                                                      \
1271
  if (needChangeLength && version == BLOCK_VERSION_1) {                                                            \
1272
    pStart += htonl(colLength[i]);                                                                                 \
1273
  } else {                                                                                                         \
1274
    pStart += colLength[i];                                                                                        \
1275
  }                                                                                                                \
1276
  boundInfo->pColIndex[j] = -1;
1277

1278
int rawBlockBindData(SQuery* query, STableMeta* pTableMeta, void* data, SVCreateTbReq* pCreateTb, void* tFields,
8,707✔
1279
                     int numFields, bool needChangeLength, char* errstr, int32_t errstrLen, bool raw) {
1280
  int       ret = 0;
8,707✔
1281
  int8_t    hasBlob = 0;
8,707✔
1282
  SBlobSet* pBlobSet = NULL;
8,707✔
1283
  if (data == NULL) {
8,707✔
1284
    uError("rawBlockBindData, data is NULL");
×
1285
    return TSDB_CODE_APP_ERROR;
×
1286
  }
1287
  void* tmp =
1288
      taosHashGet(((SVnodeModifyOpStmt*)(query->pRoot))->pTableBlockHashObj, &pTableMeta->uid, sizeof(pTableMeta->uid));
8,707✔
1289
  SVCreateTbReq* pCreateReqTmp = NULL;
8,707✔
1290
  if (tmp == NULL && pCreateTb != NULL) {
8,707✔
1291
    ret = cloneSVreateTbReq(pCreateTb, &pCreateReqTmp);
1,320✔
1292
    if (ret != TSDB_CODE_SUCCESS) {
1,320✔
1293
      uError("cloneSVreateTbReq error");
×
1294
      goto end;
×
1295
    }
1296
  }
1297

1298
  STableDataCxt* pTableCxt = NULL;
8,707✔
1299
  ret = insGetTableDataCxt(((SVnodeModifyOpStmt*)(query->pRoot))->pTableBlockHashObj, &pTableMeta->uid,
8,707✔
1300
                           sizeof(pTableMeta->uid), pTableMeta, &pCreateReqTmp, &pTableCxt, true, false);
1301
  if (pCreateReqTmp != NULL) {
8,707✔
1302
    tdDestroySVCreateTbReq(pCreateReqTmp);
×
1303
    taosMemoryFree(pCreateReqTmp);
×
1304
  }
1305

1306
  hasBlob = pTableCxt->hasBlob;
8,707✔
1307
  if (hasBlob && pTableCxt->pData->pBlobSet == NULL) {
8,707✔
1308
    ret = tBlobSetCreate(512, 0, &pTableCxt->pData->pBlobSet);
×
1309
    if (pTableCxt->pData->pBlobSet == NULL) {
×
1310
      uError("create blob set failed");
×
1311
      ret = terrno;
×
1312
    }
1313
  }
1314

1315
  if (ret != TSDB_CODE_SUCCESS) {
8,707✔
1316
    uError("insGetTableDataCxt error");
×
1317
    goto end;
×
1318
  }
1319
  pBlobSet = pTableCxt->pData->pBlobSet;
8,707✔
1320

1321
  pTableCxt->pData->flags |= TD_REQ_FROM_TAOX;
8,707✔
1322
  if (tmp == NULL) {
8,707✔
1323
    ret = initTableColSubmitData(pTableCxt);
7,912✔
1324
    if (ret != TSDB_CODE_SUCCESS) {
7,912✔
1325
      uError("initTableColSubmitData error");
×
1326
      goto end;
×
1327
    }
1328
  }
1329

1330
  char* p = (char*)data;
8,707✔
1331
  // | version | total length | total rows | blankFill | total columns | flag seg| block group id | column schema | each
1332
  // column length |
1333
  int32_t version = *(int32_t*)data;
8,707✔
1334
  p += sizeof(int32_t);
8,707✔
1335
  p += sizeof(int32_t);
8,707✔
1336

1337
  int32_t numOfRows = *(int32_t*)p;
8,707✔
1338
  p += sizeof(int32_t);
8,707✔
1339

1340
  int32_t numOfCols = *(int32_t*)p;
8,707✔
1341
  p += sizeof(int32_t);
8,707✔
1342

1343
  p += sizeof(int32_t);
8,707✔
1344
  p += sizeof(uint64_t);
8,707✔
1345

1346
  int8_t* fields = (int8_t*)p;
8,707✔
1347
  if (*fields >= TSDB_DATA_TYPE_MAX || *fields < 0) {
8,707✔
1348
    uError("fields type error:%d", *fields);
×
1349
    ret = TSDB_CODE_INVALID_PARA;
×
1350
    goto end;
×
1351
  }
1352
  p += numOfCols * (sizeof(int8_t) + sizeof(int32_t));
8,707✔
1353

1354
  int32_t* colLength = (int32_t*)p;
8,707✔
1355
  p += sizeof(int32_t) * numOfCols;
8,707✔
1356

1357
  char* pStart = p;
8,707✔
1358

1359
  SSchema*       pSchema = getTableColumnSchema(pTableMeta);
8,707✔
1360
  SSchemaExt*    pExtSchemas = getTableColumnExtSchema(pTableMeta);
8,707✔
1361
  SBoundColInfo* boundInfo = &pTableCxt->boundColsInfo;
8,707✔
1362

1363
  if (tFields != NULL && numFields != numOfCols) {
8,707✔
1364
    if (errstr != NULL) snprintf(errstr, errstrLen, "numFields:%d not equal to data cols:%d", numFields, numOfCols);
×
1365
    ret = TSDB_CODE_INVALID_PARA;
×
1366
    goto end;
×
1367
  }
1368

1369
  bool hasTs = false;
8,707✔
1370
  if (tFields == NULL) {
8,707✔
1371
    int32_t len = TMIN(numOfCols, boundInfo->numOfBound);
276✔
1372
    for (int j = 0; j < len; j++) {
828✔
1373
      SSchema*    pColSchema = &pSchema[j];
644✔
1374
      SSchemaExt* pColExtSchema = &pExtSchemas[j];
644✔
1375
      PRCESS_DATA(j, j)
644✔
1376
    }
1377
  } else {
1378
    for (int i = 0; i < numFields; i++) {
45,515✔
1379
      for (int j = 0; j < boundInfo->numOfBound; j++) {
303,106✔
1380
        SSchema*    pColSchema = &pSchema[j];
303,106✔
1381
        SSchemaExt* pColExtSchema = &pExtSchemas[j];
303,106✔
1382
        char*       fieldName = NULL;
303,106✔
1383
        if (raw) {
303,106✔
1384
          fieldName = ((SSchemaWrapper*)tFields)->pSchema[i].name;
302,876✔
1385
        } else {
1386
          fieldName = ((TAOS_FIELD*)tFields)[i].name;
230✔
1387
        }
1388
        if (strcmp(pColSchema->name, fieldName) == 0) {
303,106✔
1389
          PRCESS_DATA(i, j)
37,084✔
1390
          break;
37,084✔
1391
        }
1392
      }
1393
    }
1394
  }
1395

1396
  if (!hasTs) {
8,615✔
1397
    if (errstr != NULL) snprintf(errstr, errstrLen, "timestamp column(primary key) not found in raw data");
×
1398
    ret = TSDB_CODE_INVALID_PARA;
×
1399
    goto end;
×
1400
  }
1401

1402
  // process NULL data
1403
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
46,932✔
1404
    if (boundInfo->pColIndex[c] != -1) {
38,317✔
1405
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, c);
727✔
1406
      ret = tColDataAddValueByDataBlock(pCol, 0, 0, numOfRows, NULL, NULL);
727✔
1407
      if (ret != 0) {
727✔
1408
        goto end;
×
1409
      }
1410
    } else {
1411
      boundInfo->pColIndex[c] = c;  // restore for next block
37,590✔
1412
    }
1413
  }
1414

1415
end:
8,707✔
1416
  return ret;
8,707✔
1417
}
1418

1419
int rawBlockBindRawData(SHashObj* pVgroupHash, SArray* pVgroupList, STableMeta* pTableMeta, void* data) {
147✔
1420
  int code = transformRawSSubmitTbData(data, pTableMeta->suid, pTableMeta->uid, pTableMeta->sversion);
147✔
1421
  if (code != 0) {
147✔
1422
    return code;
×
1423
  }
1424
  SVgroupDataCxt* pVgCxt = NULL;
147✔
1425
  void**          pp = taosHashGet(pVgroupHash, &pTableMeta->vgId, sizeof(pTableMeta->vgId));
147✔
1426
  if (NULL == pp) {
147✔
1427
    code = createVgroupDataCxt(pTableMeta->vgId, pVgroupHash, pVgroupList, &pVgCxt);
147✔
1428
    if (code != 0) {
147✔
1429
      return code;
×
1430
    }
1431
  } else {
1432
    pVgCxt = *(SVgroupDataCxt**)pp;
×
1433
  }
1434
  if (NULL == pVgCxt->pData->aSubmitTbData) {
147✔
1435
    pVgCxt->pData->aSubmitTbData = taosArrayInit(0, POINTER_BYTES);
147✔
1436
    pVgCxt->pData->raw = true;
147✔
1437
    if (NULL == pVgCxt->pData->aSubmitTbData) {
147✔
1438
      return terrno;
×
1439
    }
1440
  }
1441

1442
  // push data to submit, rebuild empty data for next submit
1443
  if (NULL == taosArrayPush(pVgCxt->pData->aSubmitTbData, &data)) {
294✔
1444
    return terrno;
×
1445
  }
1446

1447
  uTrace("add raw data to vgId:%d, len:%d", pTableMeta->vgId, *(int32_t*)data);
147✔
1448

1449
  return 0;
147✔
1450
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc