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

taosdata / TDengine / #4857

17 Nov 2025 09:53AM UTC coverage: 64.135% (-0.2%) from 64.286%
#4857

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

218 of 311 new or added lines in 32 files covered. (70.1%)

5044 existing lines in 121 files now uncovered.

151302 of 235910 relevant lines covered (64.14%)

116627960.99 hits per line

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

75.24
/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) {
8,245,284✔
31
  if (NULL == pInfo) {
8,245,284✔
32
    return;
7,351,025✔
33
  }
34

35
  SBoundColInfo* pBoundInfo = (SBoundColInfo*)pInfo;
894,259✔
36

37
  taosMemoryFreeClear(pBoundInfo->pColIndex);
894,259✔
38
}
39

40
static char* tableNameGetPosition(SToken* pToken, char target) {
538,302,952✔
41
  bool inEscape = false;
538,302,952✔
42
  bool inQuote = false;
538,302,952✔
43
  char quotaStr = 0;
538,302,952✔
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;
168,683,730✔
48
    }
49

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

56
    if (*(pToken->z + i) == '\'' || *(pToken->z + i) == '"') {
2,147,483,647✔
57
      if (!inEscape) {
6,845,408✔
58
        if (!inQuote) {
6,839,800✔
59
          quotaStr = *(pToken->z + i);
3,419,900✔
60
          inQuote = !inQuote;
3,419,900✔
61
        } else if (quotaStr == *(pToken->z + i)) {
3,419,900✔
62
          inQuote = !inQuote;
3,419,900✔
63
        }
64
      }
65
    }
66
  }
67

68
  return NULL;
369,620,761✔
69
}
70

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

77
  int32_t code = TSDB_CODE_SUCCESS;
538,299,904✔
78
  char*   p = tableNameGetPosition(pTableName, TS_PATH_DELIMITER[0]);
538,299,904✔
79

80
  if (p != NULL) {  // db has been specified in sql string so we ignore current db path
538,306,772✔
81
    int32_t dbLen = p - pTableName->z;
168,683,490✔
82
    if (dbLen <= 0) {
168,681,798✔
83
      return buildInvalidOperationMsg(pMsgBuf, msg2);
×
84
    }
85
    char name[TSDB_DB_FNAME_LEN] = {0};
168,681,798✔
86
    strncpy(name, pTableName->z, dbLen);
168,682,749✔
87
    int32_t actualDbLen = strdequote(name);
168,681,436✔
88

89
    code = tNameSetDbName(pName, acctId, name, actualDbLen);
168,682,771✔
90
    if (code != TSDB_CODE_SUCCESS) {
168,682,591✔
91
      return buildInvalidOperationMsg(pMsgBuf, msg1);
×
92
    }
93

94
    int32_t tbLen = pTableName->n - dbLen - 1;
168,682,591✔
95
    if (tbLen <= 0) {
168,683,029✔
96
      return buildInvalidOperationMsg(pMsgBuf, msg4);
×
97
    }
98

99
    char tbname[TSDB_TABLE_FNAME_LEN] = {0};
168,683,029✔
100
    strncpy(tbname, p + 1, tbLen);
168,682,383✔
101
    /*tbLen = */ (void)strdequote(tbname);
168,682,355✔
102

103
    code = tNameFromString(pName, tbname, T_NAME_TABLE);
168,682,931✔
104
    if (code != 0) {
168,682,250✔
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};
369,623,282✔
109
    strncpy(tbname, pTableName->z, pTableName->n);
369,621,600✔
110
    int32_t tbLen = strdequote(tbname);
369,621,910✔
111
    if (tbLen >= TSDB_TABLE_NAME_LEN) {
369,622,729✔
112
      return buildInvalidOperationMsg(pMsgBuf, msg1);
1,640✔
113
    }
114
    if (tbLen == 0) {
369,621,487✔
115
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, "invalid table name");
736✔
116
    }
117

118
    char name[TSDB_TABLE_FNAME_LEN] = {0};
369,620,751✔
119
    strncpy(name, pTableName->z, pTableName->n);
369,621,618✔
120
    (void)strdequote(name);
369,621,672✔
121

122
    if (dbName == NULL) {
369,626,177✔
123
      return buildInvalidOperationMsg(pMsgBuf, msg3);
3,840✔
124
    }
125
    if (name[0] == '\0') return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
369,622,337✔
126

127
    code = tNameSetDbName(pName, acctId, dbName, strlen(dbName));
369,622,337✔
128
    if (code != TSDB_CODE_SUCCESS) {
369,618,642✔
129
      code = buildInvalidOperationMsg(pMsgBuf, msg2);
×
130
      return code;
×
131
    }
132

133
    code = tNameFromString(pName, name, T_NAME_TABLE);
369,618,642✔
134
    if (code != 0) {
369,614,766✔
135
      code = buildInvalidOperationMsg(pMsgBuf, msg1);
×
136
    }
137
  }
138

139
  if (NULL != strchr(pName->tname, '.')) {
538,301,893✔
140
    code = generateSyntaxErrMsgExt(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, "The table name cannot contain '.'");
6,608✔
141
  }
142

143
  return code;
538,303,792✔
144
}
145

146
int16_t insFindCol(SToken* pColname, int16_t start, int16_t end, SSchema* pSchema) {
610,958,296✔
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;
586,788,296✔
150
    }
151
    ++start;
2,147,483,647✔
152
  }
153
  return -1;
24,170,486✔
154
}
155

156
int32_t insBuildCreateTbReq(SVCreateTbReq* pTbReq, const char* tname, STag* pTag, int64_t suid, const char* sname,
22,066,495✔
157
                            SArray* tagName, uint8_t tagNum, int32_t ttl) {
158
  pTbReq->type = TD_CHILD_TABLE;
22,066,495✔
159
  pTbReq->ctb.pTag = (uint8_t*)pTag;
22,067,617✔
160
  pTbReq->name = taosStrdup(tname);
22,067,185✔
161
  if (!pTbReq->name) return terrno;
22,066,531✔
162
  pTbReq->ctb.suid = suid;
22,065,478✔
163
  pTbReq->ctb.tagNum = tagNum;
22,065,841✔
164
  if (sname) {
22,066,387✔
165
    pTbReq->ctb.stbName = taosStrdup(sname);
20,878,654✔
166
    if (!pTbReq->ctb.stbName) return terrno;
20,878,867✔
167
  }
168
  pTbReq->ctb.tagName = taosArrayDup(tagName, NULL);
22,066,816✔
169
  if (!pTbReq->ctb.tagName) return terrno;
22,071,258✔
170
  pTbReq->ttl = ttl;
22,071,219✔
171
  pTbReq->commentLen = -1;
22,071,258✔
172

173
  return TSDB_CODE_SUCCESS;
22,071,546✔
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) {
502,702,579✔
183
  SSchema* pSchemas = getTableColumnSchema(pTableMeta);
502,702,579✔
184
  int32_t  code = 0;
502,706,046✔
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;
502,713,023✔
193
}
194

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

197
int32_t insInitBoundColsInfo(int32_t numOfBound, SBoundColInfo* pInfo) {
527,477,323✔
198
  pInfo->numOfCols = numOfBound;
527,477,323✔
199
  pInfo->numOfBound = numOfBound;
527,482,927✔
200
  pInfo->hasBoundCols = false;
527,481,926✔
201
  pInfo->mixTagsCols = false;
527,484,440✔
202
  pInfo->pColIndex = taosMemoryCalloc(numOfBound, sizeof(int16_t));
527,484,127✔
203
  if (NULL == pInfo->pColIndex) {
527,488,534✔
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;
527,484,908✔
210
}
211

212
void insResetBoundColsInfo(SBoundColInfo* pInfo) {
7,866✔
213
  pInfo->numOfBound = pInfo->numOfCols;
7,866✔
214
  pInfo->hasBoundCols = false;
7,866✔
215
  pInfo->mixTagsCols = false;
7,866✔
216
  for (int32_t i = 0; i < pInfo->numOfCols; ++i) {
39,330✔
217
    pInfo->pColIndex[i] = i;
31,464✔
218
  }
219
}
7,866✔
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;
106,326,537✔
225
  }
226

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

231
  if (tRowKeyCompare(rowKey, &pTableCxt->lastKey) == 0) {
2,147,483,647✔
232
    pTableCxt->duplicateTs = true;
972,491✔
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,469,100,052✔
241

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

250
  int32_t code = TSDB_CODE_SUCCESS;
505,632,629✔
251

252
  pTableCxt->lastKey = (SRowKey){0};
505,632,629✔
253
  pTableCxt->ordered = true;
505,639,278✔
254
  pTableCxt->duplicateTs = false;
505,639,643✔
255

256
  pTableCxt->pMeta = tableMetaDup(pTableMeta);
505,644,463✔
257
  if (NULL == pTableCxt->pMeta) {
505,652,348✔
258
    code = TSDB_CODE_OUT_OF_MEMORY;
×
259
  }
260
  if (TSDB_CODE_SUCCESS == code) {
505,652,545✔
261
    pTableCxt->pSchema =
505,655,836✔
262
        tBuildTSchema(getTableColumnSchema(pTableMeta), pTableMeta->tableInfo.numOfColumns, pTableMeta->sversion);
505,651,135✔
263
    if (NULL == pTableCxt->pSchema) {
505,657,849✔
264
      code = TSDB_CODE_OUT_OF_MEMORY;
×
265
    }
266
  }
267
  pTableCxt->hasBlob = schemaHasBlob(pTableCxt->pSchema);
505,658,156✔
268

269
  if (TSDB_CODE_SUCCESS == code) {
505,650,338✔
270
    code = insInitBoundColsInfo(pTableMeta->tableInfo.numOfColumns, &pTableCxt->boundColsInfo);
505,650,149✔
271
  }
272
  if (TSDB_CODE_SUCCESS == code && !ignoreColVals) {
505,647,999✔
273
    pTableCxt->pValues = taosArrayInit(pTableMeta->tableInfo.numOfColumns, sizeof(SColVal));
502,577,570✔
274
    if (NULL == pTableCxt->pValues) {
502,579,831✔
275
      code = terrno;
×
276
    } else {
277
      code = initColValues(pTableMeta, pTableCxt->pValues);
502,580,179✔
278
    }
279
  }
280
  if (TSDB_CODE_SUCCESS == code) {
505,660,115✔
281
    pTableCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
505,659,534✔
282
    if (NULL == pTableCxt->pData) {
505,647,354✔
283
      code = terrno;
×
284
    } else {
285
      pTableCxt->pData->flags = (pCreateTbReq != NULL && NULL != *pCreateTbReq) ? SUBMIT_REQ_AUTO_CREATE_TABLE : 0;
505,657,904✔
286
      pTableCxt->pData->flags |= colMode ? SUBMIT_REQ_COLUMN_DATA_FORMAT : 0;
505,654,631✔
287
      pTableCxt->pData->suid = pTableMeta->suid;
505,651,905✔
288
      pTableCxt->pData->uid = pTableMeta->uid;
505,645,148✔
289
      pTableCxt->pData->sver = pTableMeta->sversion;
505,654,095✔
290
      pTableCxt->pData->pCreateTbReq = pCreateTbReq != NULL ? *pCreateTbReq : NULL;
505,650,462✔
291
      int8_t flag = pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
505,647,885✔
292
      if (pCreateTbReq != NULL) *pCreateTbReq = NULL;
505,646,448✔
293
      if (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
505,643,257✔
294
        pTableCxt->pData->aCol = taosArrayInit(128, sizeof(SColData));
898,508✔
295
        if (NULL == pTableCxt->pData->aCol) {
898,746✔
296
          code = terrno;
×
297
        }
298
      } else {
299
        pTableCxt->pData->aRowP = taosArrayInit(128, POINTER_BYTES);
504,743,210✔
300
        if (NULL == pTableCxt->pData->aRowP) {
504,754,989✔
301
          code = terrno;
×
302
        }
303
      }
304
    }
305
  }
306
  if (TSDB_CODE_SUCCESS == code) {
505,656,059✔
307
    *pOutput = pTableCxt;
505,656,059✔
308
    parserDebug("uid:%" PRId64 ", create table data context, code:%d, vgId:%d", pTableMeta->uid, code,
505,654,910✔
309
                pTableMeta->vgId);
310
  } else {
311
    insDestroyTableDataCxt(pTableCxt);
×
312
  }
313

314
  return code;
505,654,411✔
315
}
316

317
static int32_t rebuildTableData(SSubmitTbData* pSrc, SSubmitTbData** pDst, int8_t hasBlob) {
2,098,911✔
318
  int32_t        code = TSDB_CODE_SUCCESS;
2,098,911✔
319
  SSubmitTbData* pTmp = taosMemoryCalloc(1, sizeof(SSubmitTbData));
2,098,911✔
320
  if (NULL == pTmp) {
2,098,865✔
321
    code = terrno;
×
322
  } else {
323
    pTmp->flags = pSrc->flags;
2,098,865✔
324
    pTmp->suid = pSrc->suid;
2,099,188✔
325
    pTmp->uid = pSrc->uid;
2,099,081✔
326
    pTmp->sver = pSrc->sver;
2,099,117✔
327
    pTmp->pCreateTbReq = NULL;
2,099,081✔
328
    if (pTmp->flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
2,099,224✔
329
      if (pSrc->pCreateTbReq) {
1,592,104✔
330
        code = cloneSVreateTbReq(pSrc->pCreateTbReq, &pTmp->pCreateTbReq);
1,591,960✔
331
      } else {
332
        pTmp->flags &= ~SUBMIT_REQ_AUTO_CREATE_TABLE;
×
333
      }
334
    }
335
    if (TSDB_CODE_SUCCESS == code) {
2,099,343✔
336
      if (pTmp->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
2,099,343✔
337
        pTmp->aCol = taosArrayInit(128, sizeof(SColData));
899,415✔
338
        if (NULL == pTmp->aCol) {
899,666✔
339
          code = terrno;
×
340
          taosMemoryFree(pTmp);
×
341
        }
342
      } else {
343
        pTmp->aRowP = taosArrayInit(128, POINTER_BYTES);
1,200,000✔
344
        if (NULL == pTmp->aRowP) {
1,199,625✔
345
          code = terrno;
×
346
          taosMemoryFree(pTmp);
×
347
        }
348

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

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

360
  taosMemoryFree(pSrc);
2,099,394✔
361
  if (TSDB_CODE_SUCCESS == code) {
2,099,925✔
362
    *pDst = pTmp;
2,099,567✔
363
  }
364

365
  return code;
2,099,564✔
366
}
367

368
static void resetColValues(SArray* pValues) {
1,810,233✔
369
  int32_t num = taosArrayGetSize(pValues);
1,810,233✔
370
  for (int32_t i = 0; i < num; ++i) {
31,297,596✔
371
    SColVal* pVal = taosArrayGet(pValues, i);
29,487,363✔
372
    pVal->flag = CV_FLAG_NONE;
29,487,363✔
373
  }
374
}
1,810,233✔
375

376
int32_t insGetTableDataCxt(SHashObj* pHash, void* id, int32_t idLen, STableMeta* pTableMeta,
1,518,666,969✔
377
                           SVCreateTbReq** pCreateTbReq, STableDataCxt** pTableCxt, bool colMode, bool ignoreColVals) {
378
  STableDataCxt** tmp = (STableDataCxt**)taosHashGet(pHash, id, idLen);
1,518,666,969✔
379
  if (NULL != tmp) {
1,518,671,374✔
380
    *pTableCxt = *tmp;
1,013,019,543✔
381
    if (!ignoreColVals) {
1,013,019,543✔
382
      resetColValues((*pTableCxt)->pValues);
1,810,233✔
383
    }
384
    return TSDB_CODE_SUCCESS;
1,013,019,543✔
385
  }
386
  int32_t code = createTableDataCxt(pTableMeta, pCreateTbReq, pTableCxt, colMode, ignoreColVals);
505,651,831✔
387
  if (TSDB_CODE_SUCCESS == code) {
505,654,754✔
388
    void* pData = *pTableCxt;  // deal scan coverity
505,656,111✔
389
    code = taosHashPut(pHash, id, idLen, &pData, POINTER_BYTES);
505,657,838✔
390
  }
391

392
  if (TSDB_CODE_SUCCESS != code) {
505,659,148✔
393
    insDestroyTableDataCxt(*pTableCxt);
×
394
  }
395
  return code;
505,659,887✔
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);
794,140,420✔
403
  }
404
}
2,147,483,647✔
405

406
void insDestroyTableDataCxt(STableDataCxt* pTableCxt) {
506,064,826✔
407
  if (NULL == pTableCxt) {
506,064,826✔
408
    return;
×
409
  }
410

411
  taosMemoryFreeClear(pTableCxt->pMeta);
506,064,826✔
412
  tDestroyTSchema(pTableCxt->pSchema);
506,063,582✔
413
  insDestroyBoundColInfo(&pTableCxt->boundColsInfo);
506,064,837✔
414
  taosArrayDestroyEx(pTableCxt->pValues, destroyColVal);
506,064,358✔
415
  if (pTableCxt->pData) {
506,063,818✔
416
    tDestroySubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
3,234,720✔
417
    taosMemoryFree(pTableCxt->pData);
3,235,197✔
418
  }
419
  taosMemoryFree(pTableCxt);
506,063,649✔
420
}
421

422
void insDestroyVgroupDataCxt(SVgroupDataCxt* pVgCxt) {
478,792,966✔
423
  if (NULL == pVgCxt) {
478,792,966✔
424
    return;
×
425
  }
426

427
  tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
478,792,966✔
428
  taosMemoryFree(pVgCxt->pData);
478,795,167✔
429

430
  taosMemoryFree(pVgCxt);
478,794,597✔
431
}
432

433
void insDestroyVgroupDataCxtList(SArray* pVgCxtList) {
906,353,783✔
434
  if (NULL == pVgCxtList) {
906,353,783✔
435
    return;
448,242,942✔
436
  }
437

438
  size_t size = taosArrayGetSize(pVgCxtList);
458,110,841✔
439
  for (int32_t i = 0; i < size; i++) {
936,916,497✔
440
    void* p = taosArrayGetP(pVgCxtList, i);
478,796,084✔
441
    insDestroyVgroupDataCxt(p);
478,797,108✔
442
  }
443

444
  taosArrayDestroy(pVgCxtList);
458,120,413✔
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) {
449,364,991✔
463
  if (NULL == pTableCxtHash) {
449,364,991✔
464
    return;
895,892✔
465
  }
466

467
  void** p = taosHashIterate(pTableCxtHash, NULL);
448,469,099✔
468
  while (p) {
953,204,416✔
469
    insDestroyTableDataCxt(*(STableDataCxt**)p);
504,734,210✔
470

471
    p = taosHashIterate(pTableCxtHash, p);
504,731,360✔
472
  }
473

474
  taosHashCleanup(pTableCxtHash);
448,470,206✔
475
}
476

477
static int32_t fillVgroupDataCxt(STableDataCxt* pTableCxt, SVgroupDataCxt* pVgCxt, bool isRebuild, bool clear) {
517,456,319✔
478
  int32_t code = 0;
517,456,319✔
479
  if (NULL == pVgCxt->pData->aSubmitTbData) {
517,456,319✔
480
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
471,515,476✔
481
    if (pVgCxt->pData->aSubmitTbData == NULL) {
471,516,895✔
482
      return terrno;
×
483
    }
484
    if (pTableCxt->hasBlob) {
471,517,673✔
485
      pVgCxt->pData->aSubmitBlobData = taosArrayInit(128, sizeof(SBlobSet*));
35,696✔
486
      if (NULL == pVgCxt->pData->aSubmitBlobData) {
35,696✔
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;
517,467,778✔
494

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

499
  if (pTableCxt->hasBlob) {
517,468,171✔
500
    parserDebug("blob row transfer %p, pData %p, %s", pTableCxt->pData->pBlobSet, pTableCxt->pData, __func__);
35,696✔
501
    if (NULL == taosArrayPush(pVgCxt->pData->aSubmitBlobData, &pTableCxt->pData->pBlobSet)) {
71,392✔
502
      return terrno;
×
503
    }
504
    pTableCxt->pData->pBlobSet = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
35,696✔
505
  }
506

507
  if (isRebuild) {
517,459,520✔
508
    code = rebuildTableData(pTableCxt->pData, &pTableCxt->pData, pTableCxt->hasBlob);
2,098,857✔
509
  } else if (clear) {
515,360,663✔
510
    taosMemoryFreeClear(pTableCxt->pData);
502,805,098✔
511
  }
512
  parserDebug("uid:%" PRId64 ", add table data context to vgId:%d", pTableCxt->pMeta->uid, pVgCxt->vgId);
517,462,707✔
513

514
  return code;
517,463,961✔
515
}
516

517
static int32_t createVgroupDataCxt(int32_t vgId, SHashObj* pVgroupHash, SArray* pVgroupList, SVgroupDataCxt** pOutput) {
478,817,305✔
518
  SVgroupDataCxt* pVgCxt = taosMemoryCalloc(1, sizeof(SVgroupDataCxt));
478,817,305✔
519
  if (NULL == pVgCxt) {
478,822,264✔
520
    return terrno;
×
521
  }
522
  pVgCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitReq2));
478,822,264✔
523
  if (NULL == pVgCxt->pData) {
478,819,627✔
524
    insDestroyVgroupDataCxt(pVgCxt);
×
525
    return terrno;
×
526
  }
527

528
  pVgCxt->vgId = vgId;
478,821,005✔
529
  int32_t code = taosHashPut(pVgroupHash, &pVgCxt->vgId, sizeof(pVgCxt->vgId), &pVgCxt, POINTER_BYTES);
478,823,374✔
530
  if (TSDB_CODE_SUCCESS == code) {
478,822,568✔
531
    if (NULL == taosArrayPush(pVgroupList, &pVgCxt)) {
478,822,406✔
532
      code = terrno;
×
533
      insDestroyVgroupDataCxt(pVgCxt);
×
534
      return code;
310✔
535
    }
536
    //    uDebug("td23101 2vgId:%d, uid:%" PRIu64, pVgCxt->vgId, pTableCxt->pMeta->uid);
537
    *pOutput = pVgCxt;
478,822,406✔
538
  } else {
539
    insDestroyVgroupDataCxt(pVgCxt);
×
540
  }
541
  return code;
478,823,648✔
542
}
543

544
int insColDataComp(const void* lp, const void* rp) {
10,104,318✔
545
  SColData* pLeft = (SColData*)lp;
10,104,318✔
546
  SColData* pRight = (SColData*)rp;
10,104,318✔
547
  if (pLeft->cid < pRight->cid) {
10,104,318✔
548
    return -1;
9,749,134✔
549
  } else if (pLeft->cid > pRight->cid) {
356,682✔
550
    return 1;
356,682✔
551
  }
552

553
  return 0;
×
554
}
555

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

562
  SVgroupInfo      vgInfo = {0};
60,479✔
563
  SRequestConnInfo conn = {.pTrans = pBuildInfo->transport,
116,565✔
564
                           .requestId = pBuildInfo->requestId,
60,479✔
565
                           .requestObjRefId = pBuildInfo->requestSelf,
60,479✔
566
                           .mgmtEps = pBuildInfo->mgmtEpSet};
567

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

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

578
  return TSDB_CODE_SUCCESS;
60,479✔
579
}
580

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

586
  if (pTbData->getFromHash) {
27,876,789✔
587
    pTbInfo = (STableVgUid*)tSimpleHashGet(pBuildInfo->pTableHash, pTbData->tbName, strlen(pTbData->tbName));
25,685,603✔
588
  }
589

590
  if (NULL == pTbInfo) {
27,888,749✔
591
    SName sname;
2,148,927✔
592
    code = qCreateSName2(&sname, pTbData->tbName, pBuildInfo->acctId, pBuildInfo->dbname, NULL, 0);
2,211,572✔
593
    if (TSDB_CODE_SUCCESS != code) {
2,211,572✔
594
      return code;
9,340✔
595
    }
596

597
    STableMeta*      pTableMeta = NULL;
2,211,572✔
598
    SRequestConnInfo conn = {.pTrans = pBuildInfo->transport,
4,360,499✔
599
                             .requestId = pBuildInfo->requestId,
2,211,572✔
600
                             .requestObjRefId = pBuildInfo->requestSelf,
2,211,572✔
601
                             .mgmtEps = pBuildInfo->mgmtEpSet};
602
    code = catalogGetTableMeta((SCatalog*)pBuildInfo->pCatalog, &conn, &sname, &pTableMeta);
2,211,572✔
603

604
    if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
2,211,323✔
605
      parserWarn("stmt2 async bind don't find table:%s.%s, try auto create table", sname.dbname, sname.tname);
9,340✔
606
      return code;
9,340✔
607
    }
608

609
    if (TSDB_CODE_SUCCESS != code) {
2,201,983✔
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;
2,201,983✔
615
    *vgId = pTableMeta->vgId;
2,201,647✔
616
    *suid = pTableMeta->suid;
2,201,305✔
617

618
    STableVgUid tbInfo = {.uid = *uid, .vgid = *vgId, .suid = *suid};
2,201,392✔
619
    code = tSimpleHashPut(pBuildInfo->pTableHash, pTbData->tbName, strlen(pTbData->tbName), &tbInfo, sizeof(tbInfo));
2,200,390✔
620
    if (TSDB_CODE_SUCCESS == code) {
2,202,232✔
621
      code = insTryAddTableVgroupInfo(pAllVgHash, pBuildInfo, vgId, pTbData, &sname);
2,202,232✔
622
    }
623

624
    taosMemoryFree(pTableMeta);
2,202,064✔
625
  } else {
626
    *uid = pTbInfo->uid;
25,677,177✔
627
    *vgId = pTbInfo->vgid;
25,677,985✔
628
    *suid = pTbInfo->suid;
25,678,520✔
629
  }
630

631
  return code;
27,881,007✔
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,
15,322,844✔
646
                                            char* tbname) {
647
  if (NULL == pVgCxt->pData->aSubmitTbData) {
15,322,844✔
648
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
7,304,193✔
649
    if (NULL == pVgCxt->pData->aSubmitTbData) {
7,305,098✔
650
      return terrno;
×
651
    }
652
    if (pTbCtx->hasBlob) {
7,305,353✔
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;
15,324,514✔
661
  SArray** rowP = NULL;
15,324,514✔
662

663
  rowP = (SArray**)tSimpleHashGet(pTableNameHash, tbname, strlen(tbname));
15,324,514✔
664

665
  if (rowP != NULL && *rowP != NULL) {
15,325,652✔
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) {
15,325,652✔
700
    pTbCtx->pData->pBlobSet = NULL;  // if no blob, set it to NULL
15,324,869✔
701
  }
702

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

707
  if (pTbCtx->hasBlob) {
15,325,885✔
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*));
15,325,885✔
716

717
  if (code != TSDB_CODE_SUCCESS) {
15,325,351✔
718
    return code;
×
719
  }
720

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

723
  return TSDB_CODE_SUCCESS;
15,325,984✔
724
}
725

726
int32_t insAppendStmtTableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
12,549,696✔
727
                                  SStbInterlaceInfo* pBuildInfo) {
728
  int32_t  code = TSDB_CODE_SUCCESS;
12,549,696✔
729
  uint64_t uid;
12,508,794✔
730
  int32_t  vgId;
12,512,605✔
731
  uint64_t suid;
12,513,970✔
732

733
  pTbCtx->pData->aRowP = pTbData->aCol;
12,557,329✔
734

735
  code = insGetStmtTableVgUid(pAllVgHash, pBuildInfo, pTbData, &uid, &vgId, &suid);
12,561,843✔
736
  if (TSDB_CODE_SUCCESS != code) {
12,563,335✔
737
    return code;
×
738
  }
739

740
  pTbCtx->pMeta->vgId = vgId;
12,563,335✔
741
  pTbCtx->pMeta->uid = uid;
12,562,488✔
742
  pTbCtx->pData->uid = uid;
12,564,521✔
743

744
  if (!pTbCtx->ordered) {
12,563,346✔
745
    code = tRowSort(pTbCtx->pData->aRowP);
233✔
746
  }
747
  if (code == TSDB_CODE_SUCCESS && (!pTbCtx->ordered || pTbCtx->duplicateTs)) {
12,558,810✔
748
    code = tRowMerge(pTbCtx->pData->aRowP, pTbCtx->pSchema, 0);
233✔
749
  }
750

751
  if (TSDB_CODE_SUCCESS != code) {
12,556,808✔
752
    return code;
×
753
  }
754

755
  SVgroupDataCxt* pVgCxt = NULL;
12,556,808✔
756
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
12,556,248✔
757
  if (NULL == pp) {
12,557,751✔
758
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
3,693,426✔
759
    if (NULL == pp) {
3,694,022✔
760
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
3,694,022✔
761
    } else {
762
      pVgCxt = *(SVgroupDataCxt**)pp;
×
763
    }
764
  } else {
765
    pVgCxt = *(SVgroupDataCxt**)pp;
8,864,325✔
766
  }
767

768
  if (TSDB_CODE_SUCCESS == code) {
12,558,548✔
769
    code = fillVgroupDataCxt(pTbCtx, pVgCxt, false, false);
12,558,548✔
770
  }
771

772
  if (taosArrayGetSize(pVgCxt->pData->aSubmitTbData) >= 20000) {
12,556,478✔
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;
12,554,084✔
780
}
781

782
int32_t insAppendStmt2TableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
15,323,382✔
783
                                   SStbInterlaceInfo* pBuildInfo, SVCreateTbReq* ctbReq) {
784
  int32_t  code = TSDB_CODE_SUCCESS;
15,323,382✔
785
  uint64_t uid;
1,873,618✔
786
  int32_t  vgId;
1,873,618✔
787
  uint64_t suid;
1,873,618✔
788

789
  pTbCtx->pData->aRowP = pTbData->aCol;
15,323,637✔
790
  pTbCtx->pData->pBlobSet = pTbData->pBlobSet;
15,324,147✔
791

792
  code = insGetStmtTableVgUid(pAllVgHash, pBuildInfo, pTbData, &uid, &vgId, &suid);
15,324,147✔
793
  if (ctbReq != NULL && code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
15,325,565✔
794
    pTbCtx->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
9,340✔
795
    vgId = (int32_t)ctbReq->uid;
9,340✔
796
    uid = 0;
9,340✔
797
    pTbCtx->pMeta->vgId = (int32_t)ctbReq->uid;
9,340✔
798
    ctbReq->uid = 0;
9,340✔
799
    pTbCtx->pMeta->uid = 0;
9,340✔
800
    pTbCtx->pData->uid = 0;
9,340✔
801
    pTbCtx->pData->pCreateTbReq = ctbReq;
9,340✔
802
    code = TSDB_CODE_SUCCESS;
9,340✔
803
  } else {
804
    if (TSDB_CODE_SUCCESS != code) {
15,316,225✔
805
      return code;
×
806
    }
807
    if (pTbCtx->pData->suid != suid) {
15,316,225✔
808
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
×
809
    }
810

811
    pTbCtx->pMeta->vgId = vgId;
15,316,480✔
812
    pTbCtx->pMeta->uid = uid;
15,316,735✔
813
    pTbCtx->pData->uid = uid;
15,316,480✔
814
    pTbCtx->pData->pCreateTbReq = NULL;
15,316,735✔
815

816
    if (ctbReq != NULL) {
15,316,735✔
817
      tdDestroySVCreateTbReq(ctbReq);
818
      taosMemoryFree(ctbReq);
11,348,816✔
819
      ctbReq = NULL;
11,349,679✔
820
    }
821
  }
822

823
  if (pTbCtx->hasBlob == 0) {
15,327,556✔
824
    if (!pTbData->isOrdered) {
15,326,854✔
825
      code = tRowSort(pTbCtx->pData->aRowP);
×
826
    }
827
    if (code == TSDB_CODE_SUCCESS && (!pTbData->isOrdered || pTbData->isDuplicateTs)) {
15,326,854✔
828
      code = tRowMerge(pTbCtx->pData->aRowP, pTbCtx->pSchema, PREFER_NON_NULL);
×
829
    }
830
  } else {
831
    if (!pTbData->isOrdered) {
702✔
832
      code = tRowSortWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet);
×
833
    }
834
    if (code == TSDB_CODE_SUCCESS && (!pTbData->isOrdered || pTbData->isDuplicateTs)) {
702✔
835
      code = tRowMergeWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet, 0);
×
836
    }
837
  }
838

839
  if (TSDB_CODE_SUCCESS != code) {
15,325,462✔
840
    return code;
×
841
  }
842

843
  SVgroupDataCxt* pVgCxt = NULL;
15,325,462✔
844
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
15,325,462✔
845
  if (NULL == pp) {
15,325,650✔
846
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
7,305,823✔
847
    if (NULL == pp) {
7,306,606✔
848
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
7,306,606✔
849
    } else {
850
      pVgCxt = *(SVgroupDataCxt**)pp;
×
851
    }
852
  } else {
853
    pVgCxt = *(SVgroupDataCxt**)pp;
8,019,827✔
854
  }
855

856
  if (code == TSDB_CODE_SUCCESS) {
15,324,644✔
857
    code = checkAndMergeSVgroupDataCxtByTbname(pTbCtx, pVgCxt, pBuildInfo->pTableRowDataHash, pTbData->tbName);
15,323,547✔
858
  }
859

860
  if (taosArrayGetSize(pVgCxt->pData->aSubmitTbData) >= 20000) {
15,326,769✔
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;
15,324,294✔
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) {
447,587,530✔
935
  SHashObj* pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
447,587,530✔
936
  SArray*   pVgroupList = taosArrayInit(8, POINTER_BYTES);
447,594,267✔
937
  if (NULL == pVgroupHash || NULL == pVgroupList) {
447,594,291✔
938
    taosHashCleanup(pVgroupHash);
502✔
939
    taosArrayDestroy(pVgroupList);
×
940
    return terrno;
×
941
  }
942

943
  int32_t code = TSDB_CODE_SUCCESS;
447,593,789✔
944
  bool    colFormat = false;
447,593,789✔
945

946
  void* p = taosHashIterate(pTableHash, NULL);
447,593,789✔
947
  if (p) {
447,595,559✔
948
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
447,595,559✔
949
    colFormat = (0 != (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT));
447,597,007✔
950
  }
951

952
  while (TSDB_CODE_SUCCESS == code && NULL != p) {
952,510,835✔
953
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
504,913,474✔
954
    if (colFormat) {
504,913,474✔
955
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
903,716✔
956
      if (pCol && pCol->nVal <= 0) {
903,608✔
957
        p = taosHashIterate(pTableHash, p);
3,942✔
958
        continue;
3,942✔
959
      }
960

961
      if (pTableCxt->pData->pCreateTbReq) {
899,666✔
962
        pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
403,573✔
963
      }
964
      int8_t isBlob = IS_STR_DATA_BLOB(pCol->type) ? 1 : 0;
899,594✔
965
      if (isBlob == 0) {
899,630✔
966
        taosArraySort(pTableCxt->pData->aCol, insColDataComp);
899,666✔
967
        code = tColDataSortMerge(&pTableCxt->pData->aCol);
899,452✔
968
      } else {
UNCOV
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) {
504,009,758✔
981
        if (!pTableCxt->ordered) {
503,973,969✔
982
          code = tRowSort(pTableCxt->pData->aRowP);
934,696✔
983
        }
984
        if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
503,972,741✔
985
          code = tRowMerge(pTableCxt->pData->aRowP, pTableCxt->pSchema, PREFER_NON_NULL);
959,859✔
986
        }
987
      } else {
988
        if (!pTableCxt->ordered) {
35,696✔
989
          code = tRowSortWithBlob(pTableCxt->pData->aRowP, pTableCxt->pSchema, pTableCxt->pData->pBlobSet);
1,552✔
990
        }
991
        if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
35,696✔
992
          code = tRowMergeWithBlob(pTableCxt->pData->aRowP, pTableCxt->pSchema, pTableCxt->pData->pBlobSet, 0);
1,552✔
993
        }
994
      }
995
    }
996

997
    if (TSDB_CODE_SUCCESS == code) {
504,907,344✔
998
      SVgroupDataCxt* pVgCxt = NULL;
504,907,344✔
999
      int32_t         vgId = pTableCxt->pMeta->vgId;
504,907,382✔
1000
      void**          pp = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
504,908,122✔
1001
      if (NULL == pp) {
504,907,896✔
1002
        code = createVgroupDataCxt(vgId, pVgroupHash, pVgroupList, &pVgCxt);
467,822,559✔
1003
      } else {
1004
        pVgCxt = *(SVgroupDataCxt**)pp;
37,085,337✔
1005
      }
1006
      if (TSDB_CODE_SUCCESS == code) {
504,908,989✔
1007
        code = fillVgroupDataCxt(pTableCxt, pVgCxt, isRebuild, true);
504,908,250✔
1008
      }
1009
    }
1010
    if (TSDB_CODE_SUCCESS == code) {
504,907,452✔
1011
      p = taosHashIterate(pTableHash, p);
504,907,452✔
1012
    }
1013
  }
1014

1015
  taosHashCleanup(pVgroupHash);
447,597,361✔
1016
  if (TSDB_CODE_SUCCESS == code) {
447,592,688✔
1017
    *pVgDataBlocks = pVgroupList;
447,593,565✔
1018
  } else {
1019
    insDestroyVgroupDataCxtList(pVgroupList);
157✔
1020
  }
1021

1022
  return code;
447,593,632✔
1023
}
1024

1025
static int32_t buildSubmitReq(int32_t vgId, SSubmitReq2* pReq, void** pData, uint32_t* pLen) {
478,813,477✔
1026
  int32_t  code = TSDB_CODE_SUCCESS;
478,813,477✔
1027
  uint32_t len = 0;
478,813,477✔
1028
  void*    pBuf = NULL;
478,813,477✔
1029
  tEncodeSize(tEncodeSubmitReq, pReq, len, code);
478,813,477✔
1030
  if (TSDB_CODE_SUCCESS == code) {
478,815,528✔
1031
    SEncoder encoder;
454,555,293✔
1032
    len += sizeof(SSubmitReq2Msg);
478,819,229✔
1033
    pBuf = taosMemoryMalloc(len);
478,819,229✔
1034
    if (NULL == pBuf) {
478,812,533✔
1035
      return terrno;
×
1036
    }
1037
    ((SSubmitReq2Msg*)pBuf)->header.vgId = htonl(vgId);
478,812,533✔
1038
    ((SSubmitReq2Msg*)pBuf)->header.contLen = htonl(len);
478,818,661✔
1039
    ((SSubmitReq2Msg*)pBuf)->version = htobe64(1);
478,823,616✔
1040
    tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SSubmitReq2Msg)), len - sizeof(SSubmitReq2Msg));
478,822,784✔
1041
    code = tEncodeSubmitReq(&encoder, pReq);
478,824,343✔
1042
    tEncoderClear(&encoder);
478,826,168✔
1043
  }
1044

1045
  if (TSDB_CODE_SUCCESS == code) {
478,821,458✔
1046
    *pData = pBuf;
478,821,458✔
1047
    *pLen = len;
478,822,751✔
1048
  } else {
1049
    taosMemoryFree(pBuf);
×
1050
  }
1051
  return code;
478,816,537✔
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) {
478,813,674✔
1062
  int32_t code = 0;
478,813,674✔
1063
  if (p->raw) {
478,813,674✔
1064
    return TSDB_CODE_SUCCESS;  // no blob data in raw mode
×
1065
  }
1066

1067
  if (p->aSubmitBlobData != NULL) {
478,818,384✔
1068
    for (int32_t i = 0; i < taosArrayGetSize(p->aSubmitTbData); i++) {
71,392✔
1069
      SSubmitTbData* pSubmitTbData = taosArrayGet(p->aSubmitTbData, i);
35,696✔
1070
      SBlobSet**     ppBlob = taosArrayGet(p->aSubmitBlobData, i);
35,696✔
1071
      SBlobSet*      pBlob = ppBlob ? *ppBlob : NULL;
35,696✔
1072
      int32_t        nrow = taosArrayGetSize(pSubmitTbData->aRowP);
35,696✔
1073
      int32_t        nblob = 0;
35,696✔
1074
      if (nrow > 0 && pBlob) {
35,696✔
1075
        nblob = taosArrayGetSize(pBlob->pSeqTable);
35,696✔
1076
      }
1077
      uTrace("blob %p row size %d, pData size %d", pBlob, nblob, nrow);
35,696✔
1078
      pSubmitTbData->pBlobSet = pBlob;
35,696✔
1079
      if (ppBlob != NULL) *ppBlob = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
35,696✔
1080
    }
1081
  } else {
1082
    for (int32_t i = 0; i < taosArrayGetSize(p->aSubmitTbData); i++) {
1,011,531,935✔
1083
      SSubmitTbData* pSubmitTbData = taosArrayGet(p->aSubmitTbData, i);
532,746,311✔
1084
      pSubmitTbData->pBlobSet = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
532,754,772✔
1085
    }
1086
  }
1087

1088
  return code;
478,823,612✔
1089
}
1090
int32_t insBuildVgDataBlocks(SHashObj* pVgroupsHashObj, SArray* pVgDataCxtList, SArray** pVgDataBlocks, bool append) {
458,143,471✔
1091
  size_t  numOfVg = taosArrayGetSize(pVgDataCxtList);
458,143,471✔
1092
  SArray* pDataBlocks = (append && *pVgDataBlocks) ? *pVgDataBlocks : taosArrayInit(numOfVg, POINTER_BYTES);
458,147,899✔
1093
  if (NULL == pDataBlocks) {
458,147,105✔
1094
    return TSDB_CODE_OUT_OF_MEMORY;
×
1095
  }
1096

1097
  int32_t code = TSDB_CODE_SUCCESS;
458,147,105✔
1098
  for (size_t i = 0; TSDB_CODE_SUCCESS == code && i < numOfVg; ++i) {
936,968,627✔
1099
    SVgroupDataCxt* src = taosArrayGetP(pVgDataCxtList, i);
478,820,702✔
1100
    if (taosArrayGetSize(src->pData->aSubmitTbData) <= 0) {
478,822,723✔
1101
      continue;
×
1102
    }
1103
    SVgDataBlocks* dst = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
478,823,420✔
1104
    if (NULL == dst) {
478,817,956✔
1105
      code = terrno;
×
1106
    }
1107

1108
    if (TSDB_CODE_SUCCESS == code) {
478,817,956✔
1109
      dst->numOfTables = taosArrayGetSize(src->pData->aSubmitTbData);
478,819,846✔
1110
      code = taosHashGetDup(pVgroupsHashObj, (const char*)&src->vgId, sizeof(src->vgId), &dst->vg);
478,821,094✔
1111
    }
1112
    if (TSDB_CODE_SUCCESS == code) {
478,823,272✔
1113
      code = insResetBlob(src->pData);
478,823,272✔
1114
    }
1115

1116
    if (TSDB_CODE_SUCCESS == code) {
478,823,532✔
1117
      code = buildSubmitReq(src->vgId, src->pData, &dst->pData, &dst->size);
478,823,610✔
1118
    }
1119
    if (TSDB_CODE_SUCCESS == code) {
478,816,148✔
1120
      code = (NULL == taosArrayPush(pDataBlocks, &dst) ? terrno : TSDB_CODE_SUCCESS);
478,820,998✔
1121
    }
1122
    if (TSDB_CODE_SUCCESS != code) {
478,820,496✔
1123
      destroyVgDataBlocks(dst);
×
1124
    }
1125
  }
1126

1127
  if (append) {
458,147,925✔
1128
    if (NULL == *pVgDataBlocks) {
10,554,089✔
1129
      *pVgDataBlocks = pDataBlocks;
10,554,506✔
1130
    }
1131
    return code;
10,554,868✔
1132
  }
1133

1134
  if (TSDB_CODE_SUCCESS == code) {
447,593,836✔
1135
    *pVgDataBlocks = pDataBlocks;
447,588,237✔
1136
  } else {
1137
    taosArrayDestroyP(pDataBlocks, destroyVgDataBlocks);
5,887✔
1138
  }
1139

1140
  return code;
447,591,949✔
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) {
21,403✔
1154
  if (*fields != pColSchema->type) {
21,403✔
1155
    if (errstr != NULL) {
33✔
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};
33✔
1160
      snprintf(buf, sizeof(buf), "column type not equal, name:%s, schema type:%s, data type:%s", pColSchema->name,
66✔
1161
               tDataTypes[pColSchema->type].name, tDataTypes[*fields].name);
66✔
1162
      uError("checkSchema %s", buf);
33✔
1163
    }
1164
    return TSDB_CODE_INVALID_PARA;
33✔
1165
  }
1166

1167
  if (IS_DECIMAL_TYPE(pColSchema->type)) {
21,370✔
1168
    uint8_t precision = 0, scale = 0;
90✔
1169
    decimalFromTypeMod(pColExtSchema->typeMod, &precision, &scale);
90✔
1170
    uint8_t precisionData = 0, scaleData = 0;
90✔
1171
    int32_t bytes = *(int32_t*)(fields + sizeof(int8_t));
90✔
1172
    extractDecimalTypeInfoFromBytes(&bytes, &precisionData, &scaleData);
90✔
1173
    if (precision != precisionData || scale != scaleData) {
90✔
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;
90✔
1193
  }
1194

1195
  if (IS_VAR_DATA_TYPE(pColSchema->type)) {
21,280✔
1196
    int32_t bytes = *(int32_t*)(fields + sizeof(int8_t));
3,422✔
1197
    if (IS_STR_DATA_BLOB(pColSchema->type)) {
3,422✔
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) {
3,422✔
1205
        if (errstr != NULL) {
33✔
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};
33✔
1212
          snprintf(buf, sizeof(buf),
66✔
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,
99✔
1215
                   *(int32_t*)(fields + sizeof(int8_t)));
33✔
1216
          uError("checkSchema %s", buf);
33✔
1217
        }
1218
        return TSDB_CODE_INVALID_PARA;
33✔
1219
      }
1220
    }
1221
  }
1222

1223
  if (!IS_VAR_DATA_TYPE(pColSchema->type) && *(int32_t*)(fields + sizeof(int8_t)) != pColSchema->bytes) {
21,247✔
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;
21,247✔
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,
4,221✔
1279
                     int numFields, bool needChangeLength, char* errstr, int32_t errstrLen, bool raw) {
1280
  int       ret = 0;
4,221✔
1281
  int8_t    hasBlob = 0;
4,221✔
1282
  SBlobSet* pBlobSet = NULL;
4,221✔
1283
  if (data == NULL) {
4,221✔
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));
4,221✔
1289
  SVCreateTbReq* pCreateReqTmp = NULL;
4,221✔
1290
  if (tmp == NULL && pCreateTb != NULL) {
4,221✔
1291
    ret = cloneSVreateTbReq(pCreateTb, &pCreateReqTmp);
440✔
1292
    if (ret != TSDB_CODE_SUCCESS) {
440✔
1293
      uError("cloneSVreateTbReq error");
×
1294
      goto end;
×
1295
    }
1296
  }
1297

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

1306
  hasBlob = pTableCxt->hasBlob;
4,221✔
1307
  if (hasBlob && pTableCxt->pData->pBlobSet == NULL) {
4,221✔
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) {
4,221✔
1316
    uError("insGetTableDataCxt error");
×
1317
    goto end;
×
1318
  }
1319
  pBlobSet = pTableCxt->pData->pBlobSet;
4,221✔
1320

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

1330
  char* p = (char*)data;
4,221✔
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;
4,221✔
1334
  p += sizeof(int32_t);
4,221✔
1335
  p += sizeof(int32_t);
4,221✔
1336

1337
  int32_t numOfRows = *(int32_t*)p;
4,221✔
1338
  p += sizeof(int32_t);
4,221✔
1339

1340
  int32_t numOfCols = *(int32_t*)p;
4,221✔
1341
  p += sizeof(int32_t);
4,221✔
1342

1343
  p += sizeof(int32_t);
4,221✔
1344
  p += sizeof(uint64_t);
4,221✔
1345

1346
  int8_t* fields = p;
4,221✔
1347
  if (*fields >= TSDB_DATA_TYPE_MAX || *fields < 0) {
4,221✔
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));
4,221✔
1353

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

1357
  char* pStart = p;
4,221✔
1358

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

1363
  if (tFields != NULL && numFields != numOfCols) {
4,221✔
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;
4,221✔
1370
  if (tFields == NULL) {
4,221✔
1371
    int32_t len = TMIN(numOfCols, boundInfo->numOfBound);
198✔
1372
    for (int j = 0; j < len; j++) {
594✔
1373
      SSchema*    pColSchema = &pSchema[j];
462✔
1374
      SSchemaExt* pColExtSchema = &pExtSchemas[j];
462✔
1375
      PRCESS_DATA(j, j)
462✔
1376
    }
1377
  } else {
1378
    for (int i = 0; i < numFields; i++) {
23,532✔
1379
      for (int j = 0; j < boundInfo->numOfBound; j++) {
239,944✔
1380
        SSchema*    pColSchema = &pSchema[j];
239,944✔
1381
        SSchemaExt* pColExtSchema = &pExtSchemas[j];
239,944✔
1382
        char*       fieldName = NULL;
239,944✔
1383
        if (raw) {
239,944✔
1384
          fieldName = ((SSchemaWrapper*)tFields)->pSchema[i].name;
239,779✔
1385
        } else {
1386
          fieldName = ((TAOS_FIELD*)tFields)[i].name;
165✔
1387
        }
1388
        if (strcmp(pColSchema->name, fieldName) == 0) {
239,944✔
1389
          PRCESS_DATA(i, j)
19,509✔
1390
          break;
19,509✔
1391
        }
1392
      }
1393
    }
1394
  }
1395

1396
  if (!hasTs) {
4,155✔
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) {
24,478✔
1404
    if (boundInfo->pColIndex[c] != -1) {
20,323✔
1405
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, c);
451✔
1406
      ret = tColDataAddValueByDataBlock(pCol, 0, 0, numOfRows, NULL, NULL);
451✔
1407
      if (ret != 0) {
451✔
1408
        goto end;
×
1409
      }
1410
    } else {
1411
      boundInfo->pColIndex[c] = c;  // restore for next block
19,872✔
1412
    }
1413
  }
1414

1415
end:
4,221✔
1416
  return ret;
4,221✔
1417
}
1418

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

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

1449
  return 0;
×
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