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

taosdata / TDengine / #4937

23 Jan 2026 09:40AM UTC coverage: 66.794% (+0.05%) from 66.746%
#4937

push

travis-ci

web-flow
fix: case failuer caused by the modification of the error description (#34391)

204171 of 305671 relevant lines covered (66.79%)

127025939.33 hits per line

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

75.06
/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
static char* tableNameGetPosition(SToken* pToken, char target) {
644,547,251✔
31
  bool inEscape = false;
644,547,251✔
32
  bool inQuote = false;
644,547,251✔
33
  char quotaStr = 0;
644,547,251✔
34

35
  for (uint32_t i = 0; i < pToken->n; ++i) {
2,147,483,647✔
36
    if (*(pToken->z + i) == target && (!inEscape) && (!inQuote)) {
2,147,483,647✔
37
      return pToken->z + i;
236,298,305✔
38
    }
39

40
    if (*(pToken->z + i) == TS_ESCAPE_CHAR) {
2,147,483,647✔
41
      if (!inQuote) {
41,795,223✔
42
        inEscape = !inEscape;
41,789,818✔
43
      }
44
    }
45

46
    if (*(pToken->z + i) == '\'' || *(pToken->z + i) == '"') {
2,147,483,647✔
47
      if (!inEscape) {
6,182,842✔
48
        if (!inQuote) {
6,181,100✔
49
          quotaStr = *(pToken->z + i);
3,090,550✔
50
          inQuote = !inQuote;
3,090,550✔
51
        } else if (quotaStr == *(pToken->z + i)) {
3,090,550✔
52
          inQuote = !inQuote;
3,090,550✔
53
        }
54
      }
55
    }
56
  }
57

58
  return NULL;
408,268,494✔
59
}
60

61
int32_t insCreateSName(SName* pName, SToken* pTableName, int32_t acctId, const char* dbName, SMsgBuf* pMsgBuf) {
644,546,489✔
62
  const char* msg1 = "table name is too long";
644,546,489✔
63
  const char* msg2 = "invalid database name";
644,546,489✔
64
  const char* msg3 = "db is not specified";
644,546,489✔
65
  const char* msg4 = "invalid table name";
644,546,489✔
66
  const char* msg5 = "database name is too long";
644,546,489✔
67

68
  int32_t code = TSDB_CODE_SUCCESS;
644,546,489✔
69
  char*   p = tableNameGetPosition(pTableName, TS_PATH_DELIMITER[0]);
644,546,489✔
70

71
  if (p != NULL) {  // db has been specified in sql string so we ignore current db path
644,566,724✔
72
    // before dbname dequote
73
    int32_t dbLen = p - pTableName->z;
236,300,535✔
74
    if (dbLen <= 0) {
236,285,315✔
75
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg2);
×
76
    }
77
    if (dbLen >= TSDB_DB_FNAME_LEN + TSDB_NAME_QUOTE) {
236,285,315✔
78
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg5);
×
79
    }
80

81
    char name[TSDB_DB_FNAME_LEN + TSDB_NAME_QUOTE] = {0};
236,285,315✔
82
    strncpy(name, pTableName->z, dbLen);
236,287,590✔
83
    int32_t actualDbLen = strdequote(name);
236,287,019✔
84

85
    // after dbname dequote
86
    if (actualDbLen <= 0) {
236,286,756✔
87
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg2);
4,349✔
88
    }
89
    if (actualDbLen >= TSDB_DB_NAME_LEN) {
236,282,407✔
90
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg5);
×
91
    }
92

93
    code = tNameSetDbName(pName, acctId, name, actualDbLen);
236,282,407✔
94
    if (code != TSDB_CODE_SUCCESS) {
236,291,048✔
95
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg2);
×
96
    }
97

98
    // before tbname dequote
99
    int32_t tbLen = pTableName->n - dbLen - 1;
236,291,048✔
100
    if (tbLen <= 0) {
236,286,638✔
101
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
1,120✔
102
    }
103
    if (tbLen >= TSDB_TABLE_NAME_LEN + TSDB_NAME_QUOTE) {
236,288,139✔
104
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
118✔
105
    }
106

107
    char tbname[TSDB_TABLE_NAME_LEN + TSDB_NAME_QUOTE] = {0};
236,288,021✔
108
    strncpy(tbname, p + 1, tbLen);
236,288,539✔
109
    int32_t actualTbLen = strdequote(tbname);
236,283,426✔
110

111
    // after tbname dequote
112
    if (actualTbLen <= 0) {
236,287,608✔
113
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
×
114
    }
115
    if (actualTbLen >= TSDB_TABLE_NAME_LEN) {
236,291,448✔
116
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
118✔
117
    }
118

119
    code = tNameFromString(pName, tbname, T_NAME_TABLE);
236,291,330✔
120
    if (code != 0) {
236,296,163✔
121
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
4,828✔
122
    }
123
  } else {  // get current DB name first, and then set it into path
124
    // before tbname dequote
125
    int32_t tbLen = pTableName->n;
408,266,189✔
126
    if (tbLen <= 0) {
408,261,704✔
127
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
×
128
    }
129

130
    if (tbLen >= TSDB_TABLE_NAME_LEN + TSDB_NAME_QUOTE) {
408,265,217✔
131
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
118✔
132
    }
133

134
    char tbname[TSDB_TABLE_NAME_LEN + TSDB_NAME_QUOTE] = {0};
408,265,099✔
135
    strncpy(tbname, pTableName->z, tbLen);
408,262,153✔
136
    int32_t actualTbLen = strdequote(tbname);
408,263,763✔
137
    // after tbname dequote
138
    if (actualTbLen <= 0) {
408,261,809✔
139
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
3,884✔
140
    }
141
    if (actualTbLen >= TSDB_TABLE_NAME_LEN) {
408,258,021✔
142
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
118✔
143
    }
144

145
    if (dbName == NULL || strlen(dbName) == 0) {
408,257,903✔
146
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED, msg3);
×
147
    }
148

149
    code = tNameSetDbName(pName, acctId, dbName, strlen(dbName));
408,268,702✔
150
    if (code != TSDB_CODE_SUCCESS) {
408,263,687✔
151
      code = generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg2);
×
152
      return code;
×
153
    }
154

155
    code = tNameFromString(pName, tbname, T_NAME_TABLE);
408,263,687✔
156
    if (code != 0) {
408,258,522✔
157
      code = generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
×
158
    }
159
  }
160

161
  if (NULL != strchr(pName->tname, '.')) {
644,554,219✔
162
    code = generateSyntaxErrMsgExt(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, "The table name cannot contain '.'");
×
163
  }
164

165
  return code;
644,554,851✔
166
}
167

168
int16_t insFindCol(SToken* pColname, int16_t start, int16_t end, SSchema* pSchema) {
918,055,976✔
169
  while (start < end) {
2,147,483,647✔
170
    if (strlen(pSchema[start].name) == pColname->n && strncmp(pColname->z, pSchema[start].name, pColname->n) == 0) {
2,147,483,647✔
171
      return start;
912,398,611✔
172
    }
173
    ++start;
2,147,483,647✔
174
  }
175
  return -1;
5,655,897✔
176
}
177

178
int32_t insBuildCreateTbReq(SVCreateTbReq* pTbReq, const char* tname, STag* pTag, int64_t suid, const char* sname,
14,516,200✔
179
                            SArray* tagName, uint8_t tagNum, int32_t ttl) {
180
  pTbReq->type = TD_CHILD_TABLE;
14,516,200✔
181
  pTbReq->ctb.pTag = (uint8_t*)pTag;
14,525,008✔
182
  pTbReq->name = taosStrdup(tname);
14,518,041✔
183
  if (!pTbReq->name) return terrno;
14,525,698✔
184
  pTbReq->ctb.suid = suid;
14,510,503✔
185
  pTbReq->ctb.tagNum = tagNum;
14,518,424✔
186
  if (sname) {
14,514,260✔
187
    pTbReq->ctb.stbName = taosStrdup(sname);
13,882,849✔
188
    if (!pTbReq->ctb.stbName) return terrno;
13,889,177✔
189
  }
190
  pTbReq->ctb.tagName = taosArrayDup(tagName, NULL);
14,518,680✔
191
  if (!pTbReq->ctb.tagName) return terrno;
14,524,882✔
192
  pTbReq->ttl = ttl;
14,521,463✔
193
  pTbReq->commentLen = -1;
14,523,053✔
194

195
  return TSDB_CODE_SUCCESS;
14,519,579✔
196
}
197

198
static void initBoundCols(int32_t ncols, int16_t* pBoundCols) {
×
199
  for (int32_t i = 0; i < ncols; ++i) {
×
200
    pBoundCols[i] = i;
×
201
  }
202
}
×
203

204
static int32_t initColValues(STableMeta* pTableMeta, SArray* pValues) {
584,872,866✔
205
  SSchema* pSchemas = getTableColumnSchema(pTableMeta);
584,872,866✔
206
  int32_t  code = 0;
584,878,602✔
207
  for (int32_t i = 0; i < pTableMeta->tableInfo.numOfColumns; ++i) {
2,147,483,647✔
208
    SColVal val = COL_VAL_NONE(pSchemas[i].colId, pSchemas[i].type);
2,147,483,647✔
209
    if (NULL == taosArrayPush(pValues, &val)) {
2,147,483,647✔
210
      code = terrno;
×
211
      break;
×
212
    }
213
  }
214
  return code;
584,883,735✔
215
}
216

217
int32_t insInitColValues(STableMeta* pTableMeta, SArray* aColValues) { return initColValues(pTableMeta, aColValues); }
197,941✔
218

219
int32_t insInitBoundColsInfo(int32_t numOfBound, SBoundColInfo* pInfo) {
608,390,185✔
220
  pInfo->numOfCols = numOfBound;
608,390,185✔
221
  pInfo->numOfBound = numOfBound;
608,403,968✔
222
  pInfo->hasBoundCols = false;
608,398,972✔
223
  pInfo->mixTagsCols = false;
608,403,221✔
224
  pInfo->pColIndex = taosMemoryCalloc(numOfBound, sizeof(int16_t));
608,399,501✔
225
  if (NULL == pInfo->pColIndex) {
608,396,521✔
226
    return terrno;
×
227
  }
228
  for (int32_t i = 0; i < numOfBound; ++i) {
2,147,483,647✔
229
    pInfo->pColIndex[i] = i;
2,147,483,647✔
230
  }
231
  return TSDB_CODE_SUCCESS;
608,398,586✔
232
}
233

234
void insResetBoundColsInfo(SBoundColInfo* pInfo) {
2,864✔
235
  pInfo->numOfBound = pInfo->numOfCols;
2,864✔
236
  pInfo->hasBoundCols = false;
2,864✔
237
  pInfo->mixTagsCols = false;
2,864✔
238
  for (int32_t i = 0; i < pInfo->numOfCols; ++i) {
14,320✔
239
    pInfo->pColIndex[i] = i;
11,456✔
240
  }
241
}
2,864✔
242

243
void insCheckTableDataOrder(STableDataCxt* pTableCxt, SRowKey* rowKey) {
2,147,483,647✔
244
  // once the data block is disordered, we do NOT keep last timestamp any more
245
  if (!pTableCxt->ordered) {
2,147,483,647✔
246
    return;
24,794,285✔
247
  }
248

249
  if (tRowKeyCompare(rowKey, &pTableCxt->lastKey) < 0) {
2,147,483,647✔
250
    pTableCxt->ordered = false;
1,340,430✔
251
  }
252

253
  if (tRowKeyCompare(rowKey, &pTableCxt->lastKey) == 0) {
2,147,483,647✔
254
    pTableCxt->duplicateTs = true;
1,738,005✔
255
  }
256

257
  // TODO: for variable length data type, we need to copy it out
258
  pTableCxt->lastKey = *rowKey;
2,147,483,647✔
259
  return;
2,147,483,647✔
260
}
261

262
static int32_t createTableDataCxt(STableMeta* pTableMeta, SVCreateTbReq** pCreateTbReq, STableDataCxt** pOutput,
587,344,883✔
263
                                  bool colMode, bool ignoreColVals) {
264
  STableDataCxt* pTableCxt = taosMemoryCalloc(1, sizeof(STableDataCxt));
587,344,883✔
265
  if (NULL == pTableCxt) {
587,327,651✔
266
    *pOutput = NULL;
×
267
    return terrno;
×
268
  }
269

270
  int32_t code = TSDB_CODE_SUCCESS;
587,327,651✔
271

272
  pTableCxt->lastKey = (SRowKey){0};
587,327,651✔
273
  pTableCxt->ordered = true;
587,340,842✔
274
  pTableCxt->duplicateTs = false;
587,344,620✔
275

276
  pTableCxt->pMeta = tableMetaDup(pTableMeta);
587,346,570✔
277
  if (NULL == pTableCxt->pMeta) {
587,362,807✔
278
    code = TSDB_CODE_OUT_OF_MEMORY;
×
279
  }
280
  if (TSDB_CODE_SUCCESS == code) {
587,369,640✔
281
    pTableCxt->pSchema =
587,364,012✔
282
        tBuildTSchema(getTableColumnSchema(pTableMeta), pTableMeta->tableInfo.numOfColumns, pTableMeta->sversion);
587,369,026✔
283
    if (NULL == pTableCxt->pSchema) {
587,366,654✔
284
      code = TSDB_CODE_OUT_OF_MEMORY;
×
285
    }
286
  }
287
  pTableCxt->hasBlob = schemaHasBlob(pTableCxt->pSchema);
587,369,566✔
288

289
  if (TSDB_CODE_SUCCESS == code) {
587,356,660✔
290
    code = insInitBoundColsInfo(pTableMeta->tableInfo.numOfColumns, &pTableCxt->boundColsInfo);
587,355,412✔
291
  }
292
  if (TSDB_CODE_SUCCESS == code && !ignoreColVals) {
587,356,550✔
293
    pTableCxt->pValues = taosArrayInit(pTableMeta->tableInfo.numOfColumns, sizeof(SColVal));
584,671,010✔
294
    if (NULL == pTableCxt->pValues) {
584,666,342✔
295
      code = terrno;
×
296
    } else {
297
      code = initColValues(pTableMeta, pTableCxt->pValues);
584,670,939✔
298
    }
299
  }
300
  if (TSDB_CODE_SUCCESS == code) {
587,367,902✔
301
    pTableCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
587,370,067✔
302
    if (NULL == pTableCxt->pData) {
587,344,384✔
303
      code = terrno;
×
304
    } else {
305
      pTableCxt->pData->flags = (pCreateTbReq != NULL && NULL != *pCreateTbReq) ? SUBMIT_REQ_AUTO_CREATE_TABLE : 0;
587,351,646✔
306
      pTableCxt->pData->flags |= colMode ? SUBMIT_REQ_COLUMN_DATA_FORMAT : 0;
587,360,664✔
307
      pTableCxt->pData->suid = pTableMeta->suid;
587,365,851✔
308
      pTableCxt->pData->uid = pTableMeta->uid;
587,352,870✔
309
      pTableCxt->pData->sver = pTableMeta->sversion;
587,358,781✔
310
      pTableCxt->pData->pCreateTbReq = pCreateTbReq != NULL ? *pCreateTbReq : NULL;
587,359,638✔
311
      int8_t flag = pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
587,360,338✔
312
      if (pCreateTbReq != NULL) *pCreateTbReq = NULL;
587,360,862✔
313
      if (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
587,357,786✔
314
        pTableCxt->pData->aCol = taosArrayInit(128, sizeof(SColData));
6,417,144✔
315
        if (NULL == pTableCxt->pData->aCol) {
6,418,357✔
316
          code = terrno;
×
317
        }
318
      } else {
319
        pTableCxt->pData->aRowP = taosArrayInit(128, POINTER_BYTES);
580,932,981✔
320
        if (NULL == pTableCxt->pData->aRowP) {
580,932,018✔
321
          code = terrno;
×
322
        }
323
      }
324
    }
325
  }
326
  if (TSDB_CODE_SUCCESS == code) {
587,361,628✔
327
    *pOutput = pTableCxt;
587,361,628✔
328
    parserDebug("uid:%" PRId64 ", create table data context, code:%d, vgId:%d", pTableMeta->uid, code,
587,360,684✔
329
                pTableMeta->vgId);
330
  } else {
331
    insDestroyTableDataCxt(pTableCxt);
×
332
  }
333

334
  return code;
587,361,725✔
335
}
336

337
static int32_t rebuildTableData(SSubmitTbData* pSrc, SSubmitTbData** pDst, int8_t hasBlob) {
7,064,676✔
338
  int32_t        code = TSDB_CODE_SUCCESS;
7,064,676✔
339
  SSubmitTbData* pTmp = taosMemoryCalloc(1, sizeof(SSubmitTbData));
7,064,676✔
340
  if (NULL == pTmp) {
7,063,758✔
341
    code = terrno;
×
342
  } else {
343
    pTmp->flags = pSrc->flags;
7,063,758✔
344
    pTmp->suid = pSrc->suid;
7,064,060✔
345
    pTmp->uid = pSrc->uid;
7,066,254✔
346
    pTmp->sver = pSrc->sver;
7,067,486✔
347
    pTmp->pCreateTbReq = NULL;
7,067,192✔
348
    if (pTmp->flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
7,065,308✔
349
      if (pSrc->pCreateTbReq) {
6,923,344✔
350
        code = cloneSVreateTbReq(pSrc->pCreateTbReq, &pTmp->pCreateTbReq);
6,918,980✔
351
      } else {
352
        pTmp->flags &= ~SUBMIT_REQ_AUTO_CREATE_TABLE;
×
353
      }
354
    }
355
    if (TSDB_CODE_SUCCESS == code) {
7,063,734✔
356
      if (pTmp->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
7,063,734✔
357
        pTmp->aCol = taosArrayInit(128, sizeof(SColData));
6,427,819✔
358
        if (NULL == pTmp->aCol) {
6,427,033✔
359
          code = terrno;
×
360
          taosMemoryFree(pTmp);
×
361
        }
362
      } else {
363
        pTmp->aRowP = taosArrayInit(128, POINTER_BYTES);
638,419✔
364
        if (NULL == pTmp->aRowP) {
639,635✔
365
          code = terrno;
×
366
          taosMemoryFree(pTmp);
×
367
        }
368

369
        if (code != 0) {
639,635✔
370
          taosArrayDestroy(pTmp->aRowP);
×
371
          taosMemoryFree(pTmp);
×
372
        }
373
      }
374

375
    } else {
376
      taosMemoryFree(pTmp);
×
377
    }
378
  }
379

380
  taosMemoryFree(pSrc);
7,067,246✔
381
  if (TSDB_CODE_SUCCESS == code) {
7,068,740✔
382
    *pDst = pTmp;
7,067,458✔
383
  }
384

385
  return code;
7,068,740✔
386
}
387

388
static void resetColValues(SArray* pValues) {
32,404,730✔
389
  int32_t num = taosArrayGetSize(pValues);
32,404,730✔
390
  for (int32_t i = 0; i < num; ++i) {
364,861,406✔
391
    SColVal* pVal = taosArrayGet(pValues, i);
332,456,676✔
392
    pVal->flag = CV_FLAG_NONE;
332,456,676✔
393
  }
394
}
32,404,730✔
395

396
int32_t insGetTableDataCxt(SHashObj* pHash, void* id, int32_t idLen, STableMeta* pTableMeta,
1,627,287,664✔
397
                           SVCreateTbReq** pCreateTbReq, STableDataCxt** pTableCxt, bool colMode, bool ignoreColVals) {
398
  STableDataCxt** tmp = (STableDataCxt**)taosHashGet(pHash, id, idLen);
1,627,287,664✔
399
  if (NULL != tmp) {
1,627,312,647✔
400
    *pTableCxt = *tmp;
1,039,950,396✔
401
    if (!ignoreColVals) {
1,039,950,396✔
402
      resetColValues((*pTableCxt)->pValues);
32,404,730✔
403
    }
404
    return TSDB_CODE_SUCCESS;
1,039,950,396✔
405
  }
406
  int32_t code = createTableDataCxt(pTableMeta, pCreateTbReq, pTableCxt, colMode, ignoreColVals);
587,362,251✔
407
  if (TSDB_CODE_SUCCESS == code) {
587,359,525✔
408
    void* pData = *pTableCxt;  // deal scan coverity
587,356,603✔
409
    code = taosHashPut(pHash, id, idLen, &pData, POINTER_BYTES);
587,364,610✔
410
  }
411

412
  if (TSDB_CODE_SUCCESS != code) {
587,361,494✔
413
    insDestroyTableDataCxt(*pTableCxt);
×
414
  }
415
  return code;
587,365,028✔
416
}
417

418
void destroyColVal(void* p) {
2,147,483,647✔
419
  if (!p) return;
2,147,483,647✔
420

421
  SColVal* pVal = (SColVal*)p;
2,147,483,647✔
422

423
  if (IS_VAR_DATA_TYPE(pVal->value.type) || TSDB_DATA_TYPE_DECIMAL == pVal->value.type) {
2,147,483,647✔
424
    taosMemoryFreeClear(pVal->value.pData);
1,776,694,940✔
425
  }
426
}
427

428
void insDestroyTableDataCxt(STableDataCxt* pTableCxt) {
592,868,710✔
429
  if (NULL == pTableCxt) {
592,868,710✔
430
    return;
×
431
  }
432

433
  taosMemoryFreeClear(pTableCxt->pMeta);
592,868,710✔
434
  tDestroyTSchema(pTableCxt->pSchema);
592,865,159✔
435
  qDestroyBoundColInfo(&pTableCxt->boundColsInfo);
592,863,637✔
436
  taosArrayDestroyEx(pTableCxt->pValues, destroyColVal);
592,869,466✔
437
  if (pTableCxt->pData) {
592,866,814✔
438
    tDestroySubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
13,262,981✔
439
    taosMemoryFree(pTableCxt->pData);
13,262,965✔
440
  }
441
  taosMemoryFree(pTableCxt);
592,863,837✔
442
}
443

444
static void destroyColValSml(void* p) {
10,030,722✔
445
  SColVal* pVal = p;
10,030,722✔
446
  if (TSDB_DATA_TYPE_NCHAR == pVal->value.type || TSDB_DATA_TYPE_GEOMETRY == pVal->value.type ||
10,030,722✔
447
      TSDB_DATA_TYPE_VARBINARY == pVal->value.type) {
9,806,436✔
448
    taosMemoryFreeClear(pVal->value.pData);
229,068✔
449
  }
450
}
10,032,141✔
451

452
static void insDestroyTableDataCxtSml(STableDataCxt* pTableCxt) {
708,606✔
453
  if (NULL == pTableCxt) {
708,606✔
454
    return;
×
455
  }
456

457
  taosMemoryFreeClear(pTableCxt->pMeta);
708,606✔
458
  tDestroyTSchema(pTableCxt->pSchema);
708,606✔
459
  qDestroyBoundColInfo(&pTableCxt->boundColsInfo);
708,550✔
460
  taosArrayDestroyEx(pTableCxt->pValues, destroyColValSml);
708,606✔
461
  if (pTableCxt->pData) {
708,520✔
462
    tDestroySubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
708,520✔
463
    taosMemoryFree(pTableCxt->pData);
708,477✔
464
  }
465
  taosMemoryFree(pTableCxt);
708,563✔
466
}
467

468
void insDestroyVgroupDataCxt(SVgroupDataCxt* pVgCxt) {
561,929,260✔
469
  if (NULL == pVgCxt) {
561,929,260✔
470
    return;
×
471
  }
472

473
  tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
561,929,260✔
474
  taosMemoryFree(pVgCxt->pData);
561,927,345✔
475

476
  taosMemoryFree(pVgCxt);
561,931,108✔
477
}
478

479
void insDestroyVgroupDataCxtList(SArray* pVgCxtList) {
1,094,104,594✔
480
  if (NULL == pVgCxtList) {
1,094,104,594✔
481
    return;
547,108,174✔
482
  }
483

484
  size_t size = taosArrayGetSize(pVgCxtList);
546,996,420✔
485
  for (int32_t i = 0; i < size; i++) {
1,108,934,654✔
486
    void* p = taosArrayGetP(pVgCxtList, i);
561,937,584✔
487
    insDestroyVgroupDataCxt(p);
561,938,362✔
488
  }
489

490
  taosArrayDestroy(pVgCxtList);
546,997,070✔
491
}
492

493
void insDestroyVgroupDataCxtHashMap(SHashObj* pVgCxtHash) {
×
494
  if (NULL == pVgCxtHash) {
×
495
    return;
×
496
  }
497

498
  void** p = taosHashIterate(pVgCxtHash, NULL);
×
499
  while (p) {
×
500
    insDestroyVgroupDataCxt(*(SVgroupDataCxt**)p);
×
501

502
    p = taosHashIterate(pVgCxtHash, p);
×
503
  }
504

505
  taosHashCleanup(pVgCxtHash);
×
506
}
507

508
void insDestroyTableDataCxtHashMap(SHashObj* pTableCxtHash) {
546,888,922✔
509
  if (NULL == pTableCxtHash) {
546,888,922✔
510
    return;
6,398,583✔
511
  }
512

513
  void** p = taosHashIterate(pTableCxtHash, NULL);
540,490,339✔
514
  while (p) {
1,120,668,069✔
515
    insDestroyTableDataCxt(*(STableDataCxt**)p);
580,177,195✔
516

517
    p = taosHashIterate(pTableCxtHash, p);
580,172,289✔
518
  }
519

520
  taosHashCleanup(pTableCxtHash);
540,490,874✔
521
}
522

523
void insDestroyTableDataCxtHashMapSml(SHashObj* pTableCxtHash) {
963,813✔
524
  if (NULL == pTableCxtHash) {
963,813✔
525
    return;
×
526
  }
527

528
  void** p = taosHashIterate(pTableCxtHash, NULL);
963,813✔
529
  while (p) {
1,672,393✔
530
    insDestroyTableDataCxtSml(*(STableDataCxt**)p);
708,606✔
531

532
    p = taosHashIterate(pTableCxtHash, p);
708,606✔
533
  }
534

535
  taosHashCleanup(pTableCxtHash);
963,787✔
536
}
537

538
static int32_t fillVgroupDataCxt(STableDataCxt* pTableCxt, SVgroupDataCxt* pVgCxt, bool isRebuild, bool clear) {
588,588,451✔
539
  int32_t code = 0;
588,588,451✔
540
  if (NULL == pVgCxt->pData->aSubmitTbData) {
588,588,451✔
541
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
561,518,037✔
542
    if (pVgCxt->pData->aSubmitTbData == NULL) {
561,520,347✔
543
      return terrno;
×
544
    }
545
    if (pTableCxt->hasBlob) {
561,521,941✔
546
      pVgCxt->pData->aSubmitBlobData = taosArrayInit(128, sizeof(SBlobSet*));
21,429✔
547
      if (NULL == pVgCxt->pData->aSubmitBlobData) {
21,429✔
548
        return terrno;
×
549
      }
550
    }
551
  }
552

553
  // push data to submit, rebuild empty data for next submit
554
  if (!pTableCxt->hasBlob) pTableCxt->pData->pBlobSet = NULL;
588,598,700✔
555

556
  if (NULL == taosArrayPush(pVgCxt->pData->aSubmitTbData, pTableCxt->pData)) {
1,177,190,830✔
557
    return terrno;
×
558
  }
559

560
  if (pTableCxt->hasBlob) {
588,596,342✔
561
    parserDebug("blob row transfer %p, pData %p, %s", pTableCxt->pData->pBlobSet, pTableCxt->pData, __func__);
21,429✔
562
    if (NULL == taosArrayPush(pVgCxt->pData->aSubmitBlobData, &pTableCxt->pData->pBlobSet)) {
42,858✔
563
      return terrno;
×
564
    }
565
    pTableCxt->pData->pBlobSet = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
21,429✔
566
  }
567

568
  if (isRebuild) {
588,592,507✔
569
    code = rebuildTableData(pTableCxt->pData, &pTableCxt->pData, pTableCxt->hasBlob);
7,065,418✔
570
  } else if (clear) {
581,527,089✔
571
    taosMemoryFreeClear(pTableCxt->pData);
579,686,900✔
572
  }
573
  parserDebug("uid:%" PRId64 ", add table data context to vgId:%d", pTableCxt->pMeta->uid, pVgCxt->vgId);
588,594,483✔
574

575
  return code;
588,590,132✔
576
}
577

578
static int32_t createVgroupDataCxt(int32_t vgId, SHashObj* pVgroupHash, SArray* pVgroupList, SVgroupDataCxt** pOutput) {
562,030,249✔
579
  SVgroupDataCxt* pVgCxt = taosMemoryCalloc(1, sizeof(SVgroupDataCxt));
562,030,249✔
580
  if (NULL == pVgCxt) {
562,028,133✔
581
    return terrno;
×
582
  }
583
  pVgCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitReq2));
562,028,133✔
584
  if (NULL == pVgCxt->pData) {
562,031,506✔
585
    insDestroyVgroupDataCxt(pVgCxt);
×
586
    return terrno;
×
587
  }
588

589
  pVgCxt->vgId = vgId;
562,031,347✔
590
  int32_t code = taosHashPut(pVgroupHash, &pVgCxt->vgId, sizeof(pVgCxt->vgId), &pVgCxt, POINTER_BYTES);
562,029,780✔
591
  if (TSDB_CODE_SUCCESS == code) {
562,030,159✔
592
    if (NULL == taosArrayPush(pVgroupList, &pVgCxt)) {
562,032,646✔
593
      code = terrno;
×
594
      insDestroyVgroupDataCxt(pVgCxt);
×
595
      return code;
×
596
    }
597
    //    uDebug("td23101 2vgId:%d, uid:%" PRIu64, pVgCxt->vgId, pTableCxt->pMeta->uid);
598
    *pOutput = pVgCxt;
562,032,646✔
599
  } else {
600
    insDestroyVgroupDataCxt(pVgCxt);
×
601
  }
602
  return code;
562,031,388✔
603
}
604

605
int insColDataComp(const void* lp, const void* rp) {
27,336,976✔
606
  SColData* pLeft = (SColData*)lp;
27,336,976✔
607
  SColData* pRight = (SColData*)rp;
27,336,976✔
608
  if (pLeft->cid < pRight->cid) {
27,336,976✔
609
    return -1;
27,293,145✔
610
  } else if (pLeft->cid > pRight->cid) {
49,266✔
611
    return 1;
49,266✔
612
  }
613

614
  return 0;
×
615
}
616

617
int32_t insTryAddTableVgroupInfo(SHashObj* pAllVgHash, SStbInterlaceInfo* pBuildInfo, int32_t* vgId,
64,832✔
618
                                 STableColsData* pTbData, SName* sname) {
619
  if (*vgId >= 0 && taosHashGet(pAllVgHash, (const char*)vgId, sizeof(*vgId))) {
64,832✔
620
    return TSDB_CODE_SUCCESS;
53,790✔
621
  }
622

623
  SVgroupInfo      vgInfo = {0};
11,043✔
624
  SRequestConnInfo conn = {.pTrans = pBuildInfo->transport,
21,892✔
625
                           .requestId = pBuildInfo->requestId,
11,042✔
626
                           .requestObjRefId = pBuildInfo->requestSelf,
11,042✔
627
                           .mgmtEps = pBuildInfo->mgmtEpSet};
628

629
  int32_t code = catalogGetTableHashVgroup((SCatalog*)pBuildInfo->pCatalog, &conn, sname, &vgInfo);
11,042✔
630
  if (TSDB_CODE_SUCCESS != code) {
11,042✔
631
    return code;
×
632
  }
633

634
  code = taosHashPut(pAllVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
11,042✔
635
  if (TSDB_CODE_SUCCESS != code) {
11,042✔
636
    return code;
×
637
  }
638

639
  return TSDB_CODE_SUCCESS;
11,042✔
640
}
641

642
int32_t insGetStmtTableVgUid(SHashObj* pAllVgHash, SStbInterlaceInfo* pBuildInfo, STableColsData* pTbData,
2,745,981✔
643
                             uint64_t* uid, int32_t* vgId, uint64_t* suid) {
644
  STableVgUid* pTbInfo = NULL;
2,745,981✔
645
  int32_t      code = 0;
2,745,981✔
646

647
  if (pTbData->getFromHash) {
2,745,981✔
648
    pTbInfo = (STableVgUid*)tSimpleHashGet(pBuildInfo->pTableHash, pTbData->tbName, strlen(pTbData->tbName));
2,682,019✔
649
  }
650

651
  if (NULL == pTbInfo) {
2,746,292✔
652
    SName sname;
51,554✔
653
    code = qCreateSName2(&sname, pTbData->tbName, pBuildInfo->acctId, pBuildInfo->dbname, NULL, 0);
65,190✔
654
    if (TSDB_CODE_SUCCESS != code) {
65,233✔
655
      return code;
400✔
656
    }
657

658
    STableMeta*      pTableMeta = NULL;
65,233✔
659
    SRequestConnInfo conn = {.pTrans = pBuildInfo->transport,
116,830✔
660
                             .requestId = pBuildInfo->requestId,
65,190✔
661
                             .requestObjRefId = pBuildInfo->requestSelf,
65,189✔
662
                             .mgmtEps = pBuildInfo->mgmtEpSet};
663
    code = catalogGetTableMeta((SCatalog*)pBuildInfo->pCatalog, &conn, &sname, &pTableMeta);
65,233✔
664

665
    if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
65,231✔
666
      parserWarn("stmt2 async bind don't find table:%s.%s, try auto create table", sname.dbname, sname.tname);
400✔
667
      return code;
400✔
668
    }
669

670
    if (TSDB_CODE_SUCCESS != code) {
64,831✔
671
      parserError("stmt2 async get table meta:%s.%s failed, code:%d", sname.dbname, sname.tname, code);
×
672
      return code;
×
673
    }
674

675
    *uid = pTableMeta->uid;
64,831✔
676
    *vgId = pTableMeta->vgId;
64,831✔
677
    *suid = pTableMeta->suid;
64,787✔
678

679
    STableVgUid tbInfo = {.uid = *uid, .vgid = *vgId, .suid = *suid};
64,788✔
680
    code = tSimpleHashPut(pBuildInfo->pTableHash, pTbData->tbName, strlen(pTbData->tbName), &tbInfo, sizeof(tbInfo));
64,785✔
681
    if (TSDB_CODE_SUCCESS == code) {
64,789✔
682
      code = insTryAddTableVgroupInfo(pAllVgHash, pBuildInfo, vgId, pTbData, &sname);
64,790✔
683
    }
684

685
    taosMemoryFree(pTableMeta);
64,831✔
686
  } else {
687
    *uid = pTbInfo->uid;
2,681,102✔
688
    *vgId = pTbInfo->vgid;
2,681,471✔
689
    *suid = pTbInfo->suid;
2,680,907✔
690
  }
691

692
  return code;
2,745,810✔
693
}
694

695
int32_t qBuildStmtFinOutput1(SQuery* pQuery, SHashObj* pAllVgHash, SArray* pVgDataBlocks) {
×
696
  int32_t             code = TSDB_CODE_SUCCESS;
×
697
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
×
698

699
  if (TSDB_CODE_SUCCESS == code) {
×
700
    code = insBuildVgDataBlocks(pAllVgHash, pVgDataBlocks, &pStmt->pDataBlocks, true);
×
701
  }
702

703
  return code;
×
704
}
705

706
int32_t checkAndMergeSVgroupDataCxtByTbname(STableDataCxt* pTbCtx, SVgroupDataCxt* pVgCxt, SSHashObj* pTableNameHash,
906,432✔
707
                                            char* tbname) {
708
  if (NULL == pVgCxt->pData->aSubmitTbData) {
906,432✔
709
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
509,409✔
710
    if (NULL == pVgCxt->pData->aSubmitTbData) {
509,432✔
711
      return terrno;
×
712
    }
713
    if (pTbCtx->hasBlob) {
509,432✔
714
      pVgCxt->pData->aSubmitBlobData = taosArrayInit(128, sizeof(SBlobSet*));
×
715
      if (pVgCxt->pData->aSubmitBlobData == NULL) {
×
716
        return terrno;
×
717
      }
718
    }
719
  }
720

721
  int32_t  code = TSDB_CODE_SUCCESS;
906,415✔
722
  SArray** rowP = NULL;
906,415✔
723

724
  rowP = (SArray**)tSimpleHashGet(pTableNameHash, tbname, strlen(tbname));
906,415✔
725

726
  if (rowP != NULL && *rowP != NULL) {
906,634✔
727
    int32_t aRowPSize = taosArrayGetSize(pTbCtx->pData->aRowP);
×
728
    for (int32_t j = 0; j < aRowPSize; ++j) {
×
729
      SRow* pRow = (SRow*)taosArrayGetP(pTbCtx->pData->aRowP, j);
×
730
      if (pRow) {
×
731
        if (NULL == taosArrayPush(*rowP, &pRow)) {
×
732
          return terrno;
×
733
        }
734
      }
735
    }
736

737
    if (pTbCtx->hasBlob == 0) {
×
738
      code = tRowSort(*rowP);
×
739
      TAOS_CHECK_RETURN(code);
×
740

741
      code = tRowMerge(*rowP, pTbCtx->pSchema, 0);
×
742
      TAOS_CHECK_RETURN(code);
×
743
    } else {
744
      code = tRowSortWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet);
×
745
      TAOS_CHECK_RETURN(code);
×
746

747
      code = tRowMergeWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet, 0);
×
748
      TAOS_CHECK_RETURN(code);
×
749
    }
750
  
751
    parserDebug("merge same uid data: %" PRId64 ", vgId:%d", pTbCtx->pData->uid, pVgCxt->vgId);
×
752

753
    taosArrayDestroy(pTbCtx->pData->aRowP);
×
754
    if (pTbCtx->pData->pCreateTbReq != NULL) {
×
755
      tdDestroySVCreateTbReq(pTbCtx->pData->pCreateTbReq);
×
756
      taosMemoryFree(pTbCtx->pData->pCreateTbReq);
×
757
      pTbCtx->pData->pCreateTbReq = NULL;
×
758
    }
759
    return TSDB_CODE_SUCCESS;
×
760
  }
761

762
  if (pTbCtx->hasBlob == 0) {
906,634✔
763
    pTbCtx->pData->pBlobSet = NULL;  // if no blob, set it to NULL
906,588✔
764
  }
765

766
  if (NULL == taosArrayPush(pVgCxt->pData->aSubmitTbData, pTbCtx->pData)) {
1,813,222✔
767
    return terrno;
×
768
  }
769

770
  if (pTbCtx->hasBlob) {
906,628✔
771
    parserDebug("blob row transfer %p, pData %p, %s", pTbCtx->pData->pBlobSet, pTbCtx->pData, __func__);
×
772
    if (NULL == taosArrayPush(pVgCxt->pData->aSubmitBlobData, &pTbCtx->pData->pBlobSet)) {
×
773
      return terrno;
×
774
    }
775
    pTbCtx->pData->pBlobSet = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
×
776
  }
777

778
  code = tSimpleHashPut(pTableNameHash, tbname, strlen(tbname), &pTbCtx->pData->aRowP, sizeof(SArray*));
906,628✔
779

780
  if (code != TSDB_CODE_SUCCESS) {
906,446✔
781
    return code;
×
782
  }
783

784
  parserDebug("uid:%" PRId64 ", add table data context to vgId:%d", pTbCtx->pMeta->uid, pVgCxt->vgId);
906,446✔
785

786
  return TSDB_CODE_SUCCESS;
906,478✔
787
}
788

789
int32_t insAppendStmtTableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
1,838,328✔
790
                                  SStbInterlaceInfo* pBuildInfo) {
791
  int32_t  code = TSDB_CODE_SUCCESS;
1,838,328✔
792
  uint64_t uid;
1,825,666✔
793
  int32_t  vgId;
1,826,602✔
794
  uint64_t suid;
1,827,029✔
795

796
  pTbCtx->pData->aRowP = pTbData->aCol;
1,840,067✔
797

798
  code = insGetStmtTableVgUid(pAllVgHash, pBuildInfo, pTbData, &uid, &vgId, &suid);
1,840,721✔
799
  if (TSDB_CODE_SUCCESS != code) {
1,839,590✔
800
    return code;
×
801
  }
802

803
  pTbCtx->pMeta->vgId = vgId;
1,839,590✔
804
  pTbCtx->pMeta->uid = uid;
1,839,123✔
805
  pTbCtx->pData->uid = uid;
1,839,455✔
806

807
  if (!pTbCtx->ordered) {
1,839,874✔
808
    code = tRowSort(pTbCtx->pData->aRowP);
10✔
809
  }
810
  if (code == TSDB_CODE_SUCCESS && (!pTbCtx->ordered || pTbCtx->duplicateTs)) {
1,839,078✔
811
    code = tRowMerge(pTbCtx->pData->aRowP, pTbCtx->pSchema, 0);
339✔
812
  }
813

814
  if (TSDB_CODE_SUCCESS != code) {
1,838,792✔
815
    return code;
×
816
  }
817

818
  SVgroupDataCxt* pVgCxt = NULL;
1,838,792✔
819
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
1,839,074✔
820
  if (NULL == pp) {
1,837,476✔
821
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
314,330✔
822
    if (NULL == pp) {
314,246✔
823
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
314,246✔
824
    } else {
825
      pVgCxt = *(SVgroupDataCxt**)pp;
×
826
    }
827
  } else {
828
    pVgCxt = *(SVgroupDataCxt**)pp;
1,523,146✔
829
  }
830

831
  if (TSDB_CODE_SUCCESS == code) {
1,839,881✔
832
    code = fillVgroupDataCxt(pTbCtx, pVgCxt, false, false);
1,839,881✔
833
  }
834

835
  if (taosArrayGetSize(pVgCxt->pData->aSubmitTbData) >= 20000) {
1,839,545✔
836
    code = qBuildStmtFinOutput1((SQuery*)pBuildInfo->pQuery, pAllVgHash, pBuildInfo->pVgroupList);
×
837
    // taosArrayClear(pVgCxt->pData->aSubmitTbData);
838
    tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
×
839
    // insDestroyVgroupDataCxt(pVgCxt);
840
  }
841

842
  return code;
1,838,980✔
843
}
844

845
int32_t insAppendStmt2TableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
906,333✔
846
                                   SStbInterlaceInfo* pBuildInfo, SVCreateTbReq* ctbReq) {
847
  int32_t  code = TSDB_CODE_SUCCESS;
906,333✔
848
  uint64_t uid;
301,683✔
849
  int32_t  vgId;
301,683✔
850
  uint64_t suid;
301,683✔
851

852
  pTbCtx->pData->aRowP = pTbData->aCol;
906,333✔
853
  pTbCtx->pData->pBlobSet = pTbData->pBlobSet;
906,456✔
854

855
  code = insGetStmtTableVgUid(pAllVgHash, pBuildInfo, pTbData, &uid, &vgId, &suid);
906,416✔
856
  if (ctbReq != NULL && code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
906,560✔
857
    pTbCtx->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
400✔
858
    vgId = (int32_t)ctbReq->uid;
400✔
859
    uid = 0;
400✔
860
    pTbCtx->pMeta->vgId = (int32_t)ctbReq->uid;
400✔
861
    ctbReq->uid = 0;
400✔
862
    pTbCtx->pMeta->uid = 0;
400✔
863
    pTbCtx->pData->uid = 0;
400✔
864
    pTbCtx->pData->pCreateTbReq = ctbReq;
400✔
865
    code = TSDB_CODE_SUCCESS;
400✔
866
  } else {
867
    if (TSDB_CODE_SUCCESS != code) {
906,160✔
868
      return code;
×
869
    }
870
    if (pTbCtx->pData->suid != suid) {
906,160✔
871
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
×
872
    }
873

874
    pTbCtx->pMeta->vgId = vgId;
906,120✔
875
    pTbCtx->pMeta->uid = uid;
906,117✔
876
    pTbCtx->pData->uid = uid;
906,120✔
877
    pTbCtx->pData->pCreateTbReq = NULL;
906,077✔
878

879
    if (ctbReq != NULL) {
906,077✔
880
      tdDestroySVCreateTbReq(ctbReq);
881
      taosMemoryFree(ctbReq);
499,658✔
882
      ctbReq = NULL;
499,656✔
883
    }
884
  }
885

886
  if (pTbCtx->hasBlob == 0) {
906,621✔
887
    if (!pTbData->isOrdered) {
906,594✔
888
      code = tRowSort(pTbCtx->pData->aRowP);
×
889
    }
890
    if (code == TSDB_CODE_SUCCESS && (!pTbData->isOrdered || pTbData->isDuplicateTs)) {
906,634✔
891
      code = tRowMerge(pTbCtx->pData->aRowP, pTbCtx->pSchema, PREFER_NON_NULL);
40✔
892
    }
893
  } else {
894
    if (!pTbData->isOrdered) {
70✔
895
      code = tRowSortWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet);
×
896
    }
897
    if (code == TSDB_CODE_SUCCESS && (!pTbData->isOrdered || pTbData->isDuplicateTs)) {
70✔
898
      code = tRowMergeWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet, 0);
×
899
    }
900
  }
901

902
  if (TSDB_CODE_SUCCESS != code) {
906,494✔
903
    return code;
×
904
  }
905

906
  SVgroupDataCxt* pVgCxt = NULL;
906,494✔
907
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
906,494✔
908
  if (NULL == pp) {
906,413✔
909
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
509,313✔
910
    if (NULL == pp) {
509,502✔
911
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
509,502✔
912
    } else {
913
      pVgCxt = *(SVgroupDataCxt**)pp;
×
914
    }
915
  } else {
916
    pVgCxt = *(SVgroupDataCxt**)pp;
397,100✔
917
  }
918

919
  if (code == TSDB_CODE_SUCCESS) {
906,399✔
920
    code = checkAndMergeSVgroupDataCxtByTbname(pTbCtx, pVgCxt, pBuildInfo->pTableRowDataHash, pTbData->tbName);
906,377✔
921
  }
922

923
  if (taosArrayGetSize(pVgCxt->pData->aSubmitTbData) >= 20000) {
906,500✔
924
    code = qBuildStmtFinOutput1((SQuery*)pBuildInfo->pQuery, pAllVgHash, pBuildInfo->pVgroupList);
×
925
    // taosArrayClear(pVgCxt->pData->aSubmitTbData);
926
    tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
×
927
    // insDestroyVgroupDataCxt(pVgCxt);
928
  }
929

930
  return code;
906,462✔
931
}
932

933
/*
934
int32_t insMergeStmtTableDataCxt(STableDataCxt* pTableCxt, SArray* pTableList, SArray** pVgDataBlocks, bool isRebuild,
935
int32_t tbNum) { SHashObj* pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, false);
936
  SArray*   pVgroupList = taosArrayInit(8, POINTER_BYTES);
937
  if (NULL == pVgroupHash || NULL == pVgroupList) {
938
    taosHashCleanup(pVgroupHash);
939
    taosArrayDestroy(pVgroupList);
940
    return TSDB_CODE_OUT_OF_MEMORY;
941
  }
942

943
  int32_t code = TSDB_CODE_SUCCESS;
944

945
  for (int32_t i = 0; i < tbNum; ++i) {
946
    STableColsData *pTableCols = (STableColsData*)taosArrayGet(pTableList, i);
947
    pTableCxt->pMeta->vgId = pTableCols->vgId;
948
    pTableCxt->pMeta->uid = pTableCols->uid;
949
    pTableCxt->pData->uid = pTableCols->uid;
950
    pTableCxt->pData->aCol = pTableCols->aCol;
951

952
    SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
953
    if (pCol->nVal <= 0) {
954
      continue;
955
    }
956

957
    if (pTableCxt->pData->pCreateTbReq) {
958
      pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
959
    }
960

961
    taosArraySort(pTableCxt->pData->aCol, insColDataComp);
962

963
    tColDataSortMerge(pTableCxt->pData->aCol);
964

965
    if (TSDB_CODE_SUCCESS == code) {
966
      SVgroupDataCxt* pVgCxt = NULL;
967
      int32_t         vgId = pTableCxt->pMeta->vgId;
968
      void**          pp = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
969
      if (NULL == pp) {
970
        code = createVgroupDataCxt(pTableCxt, pVgroupHash, pVgroupList, &pVgCxt);
971
      } else {
972
        pVgCxt = *(SVgroupDataCxt**)pp;
973
      }
974
      if (TSDB_CODE_SUCCESS == code) {
975
        code = fillVgroupDataCxt(pTableCxt, pVgCxt, false, false);
976
      }
977
    }
978
  }
979

980
  taosHashCleanup(pVgroupHash);
981
  if (TSDB_CODE_SUCCESS == code) {
982
    *pVgDataBlocks = pVgroupList;
983
  } else {
984
    insDestroyVgroupDataCxtList(pVgroupList);
985
  }
986

987
  return code;
988
}
989
*/
990

991
static int8_t colDataHasBlob(SColData* pCol) {
×
992
  if (IS_STR_DATA_BLOB(pCol->type)) {
×
993
    return 1;
×
994
  }
995
  return 0;
×
996
}
997
int32_t insMergeTableDataCxt(SHashObj* pTableHash, SArray** pVgDataBlocks, bool isRebuild) {
546,335,731✔
998
  SHashObj* pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
546,335,731✔
999
  SArray*   pVgroupList = taosArrayInit(8, POINTER_BYTES);
546,345,962✔
1000
  if (NULL == pVgroupHash || NULL == pVgroupList) {
546,343,967✔
1001
    taosHashCleanup(pVgroupHash);
×
1002
    taosArrayDestroy(pVgroupList);
×
1003
    return terrno;
×
1004
  }
1005

1006
  int32_t code = TSDB_CODE_SUCCESS;
546,344,587✔
1007
  bool    colFormat = false;
546,344,587✔
1008

1009
  void* p = taosHashIterate(pTableHash, NULL);
546,344,587✔
1010
  if (p) {
546,351,062✔
1011
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
546,350,638✔
1012
    colFormat = (0 != (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT));
546,349,827✔
1013
  }
1014

1015
  while (TSDB_CODE_SUCCESS == code && NULL != p) {
1,133,109,860✔
1016
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
586,756,855✔
1017
    if (colFormat) {
586,756,863✔
1018
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
6,430,482✔
1019
      if (pCol && pCol->nVal <= 0) {
6,430,792✔
1020
        p = taosHashIterate(pTableHash, p);
117✔
1021
        continue;
117✔
1022
      }
1023

1024
      if (pTableCxt->pData->pCreateTbReq) {
6,430,365✔
1025
        pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
6,295,637✔
1026
      }
1027
      int8_t isBlob = IS_STR_DATA_BLOB(pCol->type) ? 1 : 0;
6,430,055✔
1028
      if (isBlob == 0) {
6,428,489✔
1029
        taosArraySort(pTableCxt->pData->aCol, insColDataComp);
6,429,729✔
1030
        code = tColDataSortMerge(&pTableCxt->pData->aCol);
6,427,869✔
1031
      } else {
1032
        taosArraySort(pTableCxt->pData->aCol, insColDataComp);
×
1033
        code = tColDataSortMergeWithBlob(&pTableCxt->pData->aCol, pTableCxt->pData->pBlobSet);
×
1034
      }
1035
    } else {
1036
      // skip the table has no data to insert
1037
      // eg: import a csv without valid data
1038
      // if (0 == taosArrayGetSize(pTableCxt->pData->aRowP)) {
1039
      //   parserWarn("no row in tableDataCxt uid:%" PRId64 " ", pTableCxt->pMeta->uid);
1040
      //   p = taosHashIterate(pTableHash, p);
1041
      //   continue;
1042
      // }
1043
      if (pTableCxt->hasBlob == 0) {
580,326,381✔
1044
        if (!pTableCxt->ordered) {
580,306,065✔
1045
          code = tRowSort(pTableCxt->pData->aRowP);
1,339,695✔
1046
        }
1047
        if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
580,305,882✔
1048
          code = tRowMerge(pTableCxt->pData->aRowP, pTableCxt->pSchema, PREFER_NON_NULL);
1,469,020✔
1049
        }
1050
      } else {
1051
        if (!pTableCxt->ordered) {
21,429✔
1052
          code = tRowSortWithBlob(pTableCxt->pData->aRowP, pTableCxt->pSchema, pTableCxt->pData->pBlobSet);
735✔
1053
        }
1054
        if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
21,429✔
1055
          code = tRowMergeWithBlob(pTableCxt->pData->aRowP, pTableCxt->pSchema, pTableCxt->pData->pBlobSet, 0);
735✔
1056
        }
1057
      }
1058
    }
1059

1060
    if (TSDB_CODE_SUCCESS == code) {
586,755,796✔
1061
      SVgroupDataCxt* pVgCxt = NULL;
586,755,796✔
1062
      int32_t         vgId = pTableCxt->pMeta->vgId;
586,756,289✔
1063
      void**          pp = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
586,753,989✔
1064
      if (NULL == pp) {
586,755,663✔
1065
        code = createVgroupDataCxt(vgId, pVgroupHash, pVgroupList, &pVgCxt);
561,209,076✔
1066
      } else {
1067
        pVgCxt = *(SVgroupDataCxt**)pp;
25,546,587✔
1068
      }
1069
      if (TSDB_CODE_SUCCESS == code) {
586,754,157✔
1070
        code = fillVgroupDataCxt(pTableCxt, pVgCxt, isRebuild, true);
586,753,405✔
1071
      }
1072
    }
1073
    if (TSDB_CODE_SUCCESS == code) {
586,752,258✔
1074
      p = taosHashIterate(pTableHash, p);
586,752,258✔
1075
    }
1076
  }
1077

1078
  taosHashCleanup(pVgroupHash);
546,353,005✔
1079
  if (TSDB_CODE_SUCCESS == code) {
546,345,538✔
1080
    *pVgDataBlocks = pVgroupList;
546,346,174✔
1081
  } else {
1082
    insDestroyVgroupDataCxtList(pVgroupList);
×
1083
  }
1084

1085
  return code;
546,346,615✔
1086
}
1087

1088
static int32_t buildSubmitReq(int32_t vgId, SSubmitReq2* pReq, void** pData, uint32_t* pLen) {
562,027,698✔
1089
  int32_t  code = TSDB_CODE_SUCCESS;
562,027,698✔
1090
  uint32_t len = 0;
562,027,698✔
1091
  void*    pBuf = NULL;
562,027,698✔
1092
  tEncodeSize(tEncodeSubmitReq, pReq, len, code);
562,027,698✔
1093
  if (TSDB_CODE_SUCCESS == code) {
562,026,940✔
1094
    SEncoder encoder;
559,091,997✔
1095
    len += sizeof(SSubmitReq2Msg);
562,013,558✔
1096
    pBuf = taosMemoryMalloc(len);
562,013,558✔
1097
    if (NULL == pBuf) {
562,013,085✔
1098
      return terrno;
×
1099
    }
1100
    ((SSubmitReq2Msg*)pBuf)->header.vgId = htonl(vgId);
562,013,085✔
1101
    ((SSubmitReq2Msg*)pBuf)->header.contLen = htonl(len);
562,021,402✔
1102
    ((SSubmitReq2Msg*)pBuf)->version = htobe64(1);
562,023,940✔
1103
    tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SSubmitReq2Msg)), len - sizeof(SSubmitReq2Msg));
562,032,408✔
1104
    code = tEncodeSubmitReq(&encoder, pReq);
562,027,388✔
1105
    tEncoderClear(&encoder);
562,033,633✔
1106
  }
1107

1108
  if (TSDB_CODE_SUCCESS == code) {
562,030,639✔
1109
    *pData = pBuf;
562,030,639✔
1110
    *pLen = len;
562,031,035✔
1111
  } else {
1112
    taosMemoryFree(pBuf);
×
1113
  }
1114
  return code;
562,020,263✔
1115
}
1116

1117
static void destroyVgDataBlocks(void* p) {
×
1118
  if (p == NULL) return;
×
1119
  SVgDataBlocks* pVg = p;
×
1120
  taosMemoryFree(pVg->pData);
×
1121
  taosMemoryFree(pVg);
×
1122
}
1123

1124
int32_t insResetBlob(SSubmitReq2* p) {
562,024,360✔
1125
  int32_t code = 0;
562,024,360✔
1126
  if (p->raw) {
562,024,360✔
1127
    return TSDB_CODE_SUCCESS;  // no blob data in raw mode
×
1128
  }
1129

1130
  if (p->aSubmitBlobData != NULL) {
562,028,054✔
1131
    for (int32_t i = 0; i < taosArrayGetSize(p->aSubmitTbData); i++) {
42,858✔
1132
      SSubmitTbData* pSubmitTbData = taosArrayGet(p->aSubmitTbData, i);
21,429✔
1133
      SBlobSet**     ppBlob = taosArrayGet(p->aSubmitBlobData, i);
21,429✔
1134
      SBlobSet*      pBlob = ppBlob ? *ppBlob : NULL;
21,429✔
1135
      int32_t        nrow = taosArrayGetSize(pSubmitTbData->aRowP);
21,429✔
1136
      int32_t        nblob = 0;
21,429✔
1137
      if (nrow > 0 && pBlob) {
21,429✔
1138
        nblob = taosArrayGetSize(pBlob->pSeqTable);
21,429✔
1139
      }
1140
      uTrace("blob %p row size %d, pData size %d", pBlob, nblob, nrow);
21,429✔
1141
      pSubmitTbData->pBlobSet = pBlob;
21,429✔
1142
      if (ppBlob != NULL) *ppBlob = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
21,429✔
1143
    }
1144
  } else {
1145
    for (int32_t i = 0; i < taosArrayGetSize(p->aSubmitTbData); i++) {
1,151,483,745✔
1146
      SSubmitTbData* pSubmitTbData = taosArrayGet(p->aSubmitTbData, i);
589,475,009✔
1147
      pSubmitTbData->pBlobSet = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
589,479,487✔
1148
    }
1149
  }
1150

1151
  return code;
562,032,545✔
1152
}
1153
int32_t insBuildVgDataBlocks(SHashObj* pVgroupsHashObj, SArray* pVgDataCxtList, SArray** pVgDataBlocks, bool append) {
547,098,153✔
1154
  size_t  numOfVg = taosArrayGetSize(pVgDataCxtList);
547,098,153✔
1155
  SArray* pDataBlocks = (append && *pVgDataBlocks) ? *pVgDataBlocks : taosArrayInit(numOfVg, POINTER_BYTES);
547,099,698✔
1156
  if (NULL == pDataBlocks) {
547,096,665✔
1157
    return TSDB_CODE_OUT_OF_MEMORY;
×
1158
  }
1159

1160
  int32_t code = TSDB_CODE_SUCCESS;
547,096,665✔
1161
  for (size_t i = 0; TSDB_CODE_SUCCESS == code && i < numOfVg; ++i) {
1,109,120,984✔
1162
    SVgroupDataCxt* src = taosArrayGetP(pVgDataCxtList, i);
562,030,244✔
1163
    if (taosArrayGetSize(src->pData->aSubmitTbData) <= 0) {
562,032,806✔
1164
      continue;
×
1165
    }
1166
    SVgDataBlocks* dst = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
562,031,036✔
1167
    if (NULL == dst) {
562,022,785✔
1168
      code = terrno;
×
1169
    }
1170

1171
    if (TSDB_CODE_SUCCESS == code) {
562,022,785✔
1172
      dst->numOfTables = taosArrayGetSize(src->pData->aSubmitTbData);
562,024,312✔
1173
      code = taosHashGetDup(pVgroupsHashObj, (const char*)&src->vgId, sizeof(src->vgId), &dst->vg);
562,025,304✔
1174
    }
1175
    if (TSDB_CODE_SUCCESS == code) {
562,035,506✔
1176
      code = insResetBlob(src->pData);
562,035,506✔
1177
    }
1178

1179
    if (TSDB_CODE_SUCCESS == code) {
562,025,674✔
1180
      code = buildSubmitReq(src->vgId, src->pData, &dst->pData, &dst->size);
562,025,662✔
1181
    }
1182
    if (TSDB_CODE_SUCCESS == code) {
562,017,988✔
1183
      code = (NULL == taosArrayPush(pDataBlocks, &dst) ? terrno : TSDB_CODE_SUCCESS);
562,026,567✔
1184
    }
1185
    if (TSDB_CODE_SUCCESS != code) {
562,026,700✔
1186
      destroyVgDataBlocks(dst);
×
1187
    }
1188
  }
1189

1190
  if (append) {
547,090,740✔
1191
    if (NULL == *pVgDataBlocks) {
752,376✔
1192
      *pVgDataBlocks = pDataBlocks;
752,362✔
1193
    }
1194
    return code;
752,376✔
1195
  }
1196

1197
  if (TSDB_CODE_SUCCESS == code) {
546,338,364✔
1198
    *pVgDataBlocks = pDataBlocks;
546,333,601✔
1199
  } else {
1200
    taosArrayDestroyP(pDataBlocks, destroyVgDataBlocks);
5,064✔
1201
  }
1202

1203
  return code;
546,342,553✔
1204
}
1205

1206
static bool findFileds(SSchema* pSchema, TAOS_FIELD* fields, int numFields) {
×
1207
  for (int i = 0; i < numFields; i++) {
×
1208
    if (strcmp(pSchema->name, fields[i].name) == 0) {
×
1209
      return true;
×
1210
    }
1211
  }
1212

1213
  return false;
×
1214
}
1215

1216
int32_t checkSchema(SSchema* pColSchema, SSchemaExt* pColExtSchema, int8_t* fields, char* errstr, int32_t errstrLen) {
157,687✔
1217
  if (*fields != pColSchema->type) {
157,687✔
1218
    if (errstr != NULL) {
273✔
1219
      snprintf(errstr, errstrLen, "column type not equal, name:%s, schema type:%s, data type:%s", pColSchema->name,
×
1220
               tDataTypes[pColSchema->type].name, tDataTypes[*fields].name);
×
1221
    } else {
1222
      char buf[512] = {0};
273✔
1223
      snprintf(buf, sizeof(buf), "column type not equal, name:%s, schema type:%s, data type:%s", pColSchema->name,
546✔
1224
               tDataTypes[pColSchema->type].name, tDataTypes[*fields].name);
546✔
1225
      uError("checkSchema %s", buf);
273✔
1226
    }
1227
    return TSDB_CODE_INVALID_PARA;
273✔
1228
  }
1229

1230
  if (IS_DECIMAL_TYPE(pColSchema->type)) {
157,414✔
1231
    uint8_t precision = 0, scale = 0;
273✔
1232
    decimalFromTypeMod(pColExtSchema->typeMod, &precision, &scale);
273✔
1233
    uint8_t precisionData = 0, scaleData = 0;
273✔
1234
    int32_t bytes = *(int32_t*)(fields + sizeof(int8_t));
273✔
1235
    extractDecimalTypeInfoFromBytes(&bytes, &precisionData, &scaleData);
273✔
1236
    if (precision != precisionData || scale != scaleData) {
273✔
1237
      if (errstr != NULL) {
×
1238
        snprintf(errstr, errstrLen,
×
1239
                 "column decimal type not equal, name:%s, schema type:%s, precision:%d, scale:%d, data type:%s, "
1240
                 "precision:%d, scale:%d",
1241
                 pColSchema->name, tDataTypes[pColSchema->type].name, precision, scale, tDataTypes[*fields].name,
×
1242
                 precisionData, scaleData);
1243
        return TSDB_CODE_INVALID_PARA;
×
1244
      } else {
1245
        char buf[512] = {0};
×
1246
        snprintf(buf, sizeof(buf),
×
1247
                 "column decimal type not equal, name:%s, schema type:%s, precision:%d, scale:%d, data type:%s, "
1248
                 "precision:%d, scale:%d",
1249
                 pColSchema->name, tDataTypes[pColSchema->type].name, precision, scale, tDataTypes[*fields].name,
×
1250
                 precisionData, scaleData);
1251
        uError("checkSchema %s", buf);
×
1252
        return TSDB_CODE_INVALID_PARA;
×
1253
      }
1254
    }
1255
    return 0;
273✔
1256
  }
1257

1258
  if (IS_VAR_DATA_TYPE(pColSchema->type)) {
157,141✔
1259
    int32_t bytes = *(int32_t*)(fields + sizeof(int8_t));
27,542✔
1260
    if (IS_STR_DATA_BLOB(pColSchema->type)) {
27,542✔
1261
      if (bytes >= TSDB_MAX_BLOB_LEN) {
×
1262
        uError("column blob data bytes exceed max limit, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
×
1263
               pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name, bytes);
1264
        return TSDB_CODE_INVALID_PARA;
×
1265
      }
1266
    } else {
1267
      if (bytes > pColSchema->bytes) {
27,542✔
1268
        if (errstr != NULL) {
273✔
1269
          snprintf(errstr, errstrLen,
×
1270
                   "column var data bytes error, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
1271
                   pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name,
×
1272
                   *(int32_t*)(fields + sizeof(int8_t)));
×
1273
        } else {
1274
          char buf[512] = {0};
273✔
1275
          snprintf(buf, sizeof(buf),
546✔
1276
                   "column var data bytes error, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
1277
                   pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name,
819✔
1278
                   *(int32_t*)(fields + sizeof(int8_t)));
273✔
1279
          uError("checkSchema %s", buf);
273✔
1280
        }
1281
        return TSDB_CODE_INVALID_PARA;
273✔
1282
      }
1283
    }
1284
  }
1285

1286
  if (!IS_VAR_DATA_TYPE(pColSchema->type) && *(int32_t*)(fields + sizeof(int8_t)) != pColSchema->bytes) {
156,868✔
1287
    if (errstr != NULL) {
×
1288
      snprintf(errstr, errstrLen,
×
1289
               "column normal data bytes not equal, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
1290
               pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name,
×
1291
               *(int32_t*)(fields + sizeof(int8_t)));
×
1292
    } else {
1293
      char buf[512] = {0};
×
1294
      snprintf(buf, sizeof(buf),
×
1295
               "column normal data bytes not equal, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
1296
               pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name,
×
1297
               *(int32_t*)(fields + sizeof(int8_t)));
×
1298
      uError("checkSchema %s", buf);
×
1299
    }
1300
    return TSDB_CODE_INVALID_PARA;
×
1301
  }
1302
  return 0;
156,868✔
1303
}
1304

1305
#define PRCESS_DATA(i, j)                                                                                          \
1306
  ret = checkSchema(pColSchema, pColExtSchema, fields, errstr, errstrLen);                                         \
1307
  if (ret != 0) {                                                                                                  \
1308
    goto end;                                                                                                      \
1309
  }                                                                                                                \
1310
                                                                                                                   \
1311
  if (pColSchema->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {                                                          \
1312
    hasTs = true;                                                                                                  \
1313
  }                                                                                                                \
1314
                                                                                                                   \
1315
  int8_t* offset = pStart;                                                                                         \
1316
  if (IS_VAR_DATA_TYPE(pColSchema->type)) {                                                                        \
1317
    pStart += numOfRows * sizeof(int32_t);                                                                         \
1318
  } else {                                                                                                         \
1319
    pStart += BitmapLen(numOfRows);                                                                                \
1320
  }                                                                                                                \
1321
  char* pData = pStart;                                                                                            \
1322
                                                                                                                   \
1323
  SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, j);                                                        \
1324
  if (hasBlob) {                                                                                                   \
1325
    ret = tColDataAddValueByDataBlockWithBlob(pCol, pColSchema->type, pColSchema->bytes, numOfRows, offset, pData, \
1326
                                              pBlobSet);                                                           \
1327
  } else {                                                                                                         \
1328
    ret = tColDataAddValueByDataBlock(pCol, pColSchema->type, pColSchema->bytes, numOfRows, offset, pData);        \
1329
  }                                                                                                                \
1330
  if (ret != 0) {                                                                                                  \
1331
    goto end;                                                                                                      \
1332
  }                                                                                                                \
1333
  fields += sizeof(int8_t) + sizeof(int32_t);                                                                      \
1334
  if (needChangeLength && version == BLOCK_VERSION_1) {                                                            \
1335
    pStart += htonl(colLength[i]);                                                                                 \
1336
  } else {                                                                                                         \
1337
    pStart += colLength[i];                                                                                        \
1338
  }                                                                                                                \
1339
  boundInfo->pColIndex[j] = -1;
1340

1341
int rawBlockBindData(SQuery* query, STableMeta* pTableMeta, void* data, SVCreateTbReq* pCreateTb, void* tFields,
34,259✔
1342
                     int numFields, bool needChangeLength, char* errstr, int32_t errstrLen, bool raw) {
1343
  int       ret = 0;
34,259✔
1344
  int8_t    hasBlob = 0;
34,259✔
1345
  SBlobSet* pBlobSet = NULL;
34,259✔
1346
  if (data == NULL) {
34,259✔
1347
    uError("rawBlockBindData, data is NULL");
×
1348
    return TSDB_CODE_APP_ERROR;
×
1349
  }
1350
  void* tmp =
1351
      taosHashGet(((SVnodeModifyOpStmt*)(query->pRoot))->pTableBlockHashObj, &pTableMeta->uid, sizeof(pTableMeta->uid));
34,259✔
1352
  SVCreateTbReq* pCreateReqTmp = NULL;
34,259✔
1353
  if (tmp == NULL && pCreateTb != NULL) {
34,259✔
1354
    ret = cloneSVreateTbReq(pCreateTb, &pCreateReqTmp);
3,268✔
1355
    if (ret != TSDB_CODE_SUCCESS) {
3,268✔
1356
      uError("cloneSVreateTbReq error");
×
1357
      goto end;
×
1358
    }
1359
  }
1360

1361
  STableDataCxt* pTableCxt = NULL;
34,259✔
1362
  ret = insGetTableDataCxt(((SVnodeModifyOpStmt*)(query->pRoot))->pTableBlockHashObj, &pTableMeta->uid,
34,259✔
1363
                           sizeof(pTableMeta->uid), pTableMeta, &pCreateReqTmp, &pTableCxt, true, false);
1364
  if (pCreateReqTmp != NULL) {
34,259✔
1365
    tdDestroySVCreateTbReq(pCreateReqTmp);
×
1366
    taosMemoryFree(pCreateReqTmp);
×
1367
  }
1368

1369
  hasBlob = pTableCxt->hasBlob;
34,259✔
1370
  if (hasBlob && pTableCxt->pData->pBlobSet == NULL) {
34,259✔
1371
    ret = tBlobSetCreate(512, 0, &pTableCxt->pData->pBlobSet);
×
1372
    if (pTableCxt->pData->pBlobSet == NULL) {
×
1373
      uError("create blob set failed");
×
1374
      ret = terrno;
×
1375
    }
1376
  }
1377

1378
  if (ret != TSDB_CODE_SUCCESS) {
34,259✔
1379
    uError("insGetTableDataCxt error");
×
1380
    goto end;
×
1381
  }
1382
  pBlobSet = pTableCxt->pData->pBlobSet;
34,259✔
1383

1384
  pTableCxt->pData->flags |= TD_REQ_FROM_TAOX;
34,259✔
1385
  if (tmp == NULL) {
34,259✔
1386
    ret = initTableColSubmitData(pTableCxt);
30,714✔
1387
    if (ret != TSDB_CODE_SUCCESS) {
30,714✔
1388
      uError("initTableColSubmitData error");
×
1389
      goto end;
×
1390
    }
1391
  }
1392

1393
  char* p = (char*)data;
34,259✔
1394
  // | version | total length | total rows | blankFill | total columns | flag seg| block group id | column schema | each
1395
  // column length |
1396
  int32_t version = *(int32_t*)data;
34,259✔
1397
  p += sizeof(int32_t);
34,259✔
1398
  p += sizeof(int32_t);
34,259✔
1399

1400
  int32_t numOfRows = *(int32_t*)p;
34,259✔
1401
  p += sizeof(int32_t);
34,259✔
1402

1403
  int32_t numOfCols = *(int32_t*)p;
34,259✔
1404
  p += sizeof(int32_t);
34,259✔
1405

1406
  p += sizeof(int32_t);
34,259✔
1407
  p += sizeof(uint64_t);
34,259✔
1408

1409
  int8_t* fields = (int8_t*)p;
34,259✔
1410
  if (*fields >= TSDB_DATA_TYPE_MAX || *fields < 0) {
34,259✔
1411
    uError("fields type error:%d", *fields);
×
1412
    ret = TSDB_CODE_INVALID_PARA;
×
1413
    goto end;
×
1414
  }
1415
  p += numOfCols * (sizeof(int8_t) + sizeof(int32_t));
34,259✔
1416

1417
  int32_t* colLength = (int32_t*)p;
34,259✔
1418
  p += sizeof(int32_t) * numOfCols;
34,259✔
1419

1420
  char* pStart = p;
34,259✔
1421

1422
  SSchema*       pSchema = getTableColumnSchema(pTableMeta);
34,259✔
1423
  SSchemaExt*    pExtSchemas = getTableColumnExtSchema(pTableMeta);
34,259✔
1424
  SBoundColInfo* boundInfo = &pTableCxt->boundColsInfo;
34,259✔
1425

1426
  if (tFields != NULL && numFields != numOfCols) {
34,259✔
1427
    if (errstr != NULL) snprintf(errstr, errstrLen, "numFields:%d not equal to data cols:%d", numFields, numOfCols);
×
1428
    ret = TSDB_CODE_INVALID_PARA;
×
1429
    goto end;
×
1430
  }
1431

1432
  bool hasTs = false;
34,259✔
1433
  if (tFields == NULL) {
34,259✔
1434
    int32_t len = TMIN(numOfCols, boundInfo->numOfBound);
1,638✔
1435
    for (int j = 0; j < len; j++) {
4,914✔
1436
      SSchema*    pColSchema = &pSchema[j];
3,822✔
1437
      SSchemaExt* pColExtSchema = &pExtSchemas[j];
3,822✔
1438
      PRCESS_DATA(j, j)
3,822✔
1439
    }
1440
  } else {
1441
    for (int i = 0; i < numFields; i++) {
176,112✔
1442
      for (int j = 0; j < boundInfo->numOfBound; j++) {
1,325,124✔
1443
        SSchema*    pColSchema = &pSchema[j];
1,325,124✔
1444
        SSchemaExt* pColExtSchema = &pExtSchemas[j];
1,325,124✔
1445
        char*       fieldName = NULL;
1,325,124✔
1446
        if (raw) {
1,325,124✔
1447
          fieldName = ((SSchemaWrapper*)tFields)->pSchema[i].name;
1,323,759✔
1448
        } else {
1449
          fieldName = ((TAOS_FIELD*)tFields)[i].name;
1,365✔
1450
        }
1451
        if (strcmp(pColSchema->name, fieldName) == 0) {
1,325,124✔
1452
          PRCESS_DATA(i, j)
143,491✔
1453
          break;
143,491✔
1454
        }
1455
      }
1456
    }
1457
  }
1458

1459
  if (!hasTs) {
33,713✔
1460
    if (errstr != NULL) snprintf(errstr, errstrLen, "timestamp column(primary key) not found in raw data");
×
1461
    ret = TSDB_CODE_INVALID_PARA;
×
1462
    goto end;
×
1463
  }
1464

1465
  // process NULL data
1466
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
183,749✔
1467
    if (boundInfo->pColIndex[c] != -1) {
150,036✔
1468
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, c);
3,542✔
1469
      ret = tColDataAddValueByDataBlock(pCol, 0, 0, numOfRows, NULL, NULL);
3,542✔
1470
      if (ret != 0) {
3,542✔
1471
        goto end;
×
1472
      }
1473
    } else {
1474
      boundInfo->pColIndex[c] = c;  // restore for next block
146,494✔
1475
    }
1476
  }
1477

1478
end:
34,259✔
1479
  return ret;
34,259✔
1480
}
1481

1482
int rawBlockBindRawData(SHashObj* pVgroupHash, SArray* pVgroupList, STableMeta* pTableMeta, void* data) {
×
1483
  int code = transformRawSSubmitTbData(data, pTableMeta->suid, pTableMeta->uid, pTableMeta->sversion);
×
1484
  if (code != 0) {
×
1485
    return code;
×
1486
  }
1487
  SVgroupDataCxt* pVgCxt = NULL;
×
1488
  void**          pp = taosHashGet(pVgroupHash, &pTableMeta->vgId, sizeof(pTableMeta->vgId));
×
1489
  if (NULL == pp) {
×
1490
    code = createVgroupDataCxt(pTableMeta->vgId, pVgroupHash, pVgroupList, &pVgCxt);
×
1491
    if (code != 0) {
×
1492
      return code;
×
1493
    }
1494
  } else {
1495
    pVgCxt = *(SVgroupDataCxt**)pp;
×
1496
  }
1497
  if (NULL == pVgCxt->pData->aSubmitTbData) {
×
1498
    pVgCxt->pData->aSubmitTbData = taosArrayInit(0, POINTER_BYTES);
×
1499
    pVgCxt->pData->raw = true;
×
1500
    if (NULL == pVgCxt->pData->aSubmitTbData) {
×
1501
      return terrno;
×
1502
    }
1503
  }
1504

1505
  // push data to submit, rebuild empty data for next submit
1506
  if (NULL == taosArrayPush(pVgCxt->pData->aSubmitTbData, &data)) {
×
1507
    return terrno;
×
1508
  }
1509

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

1512
  return 0;
×
1513
}
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