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

taosdata / TDengine / #4933

20 Jan 2026 10:44AM UTC coverage: 66.671% (+0.03%) from 66.646%
#4933

push

travis-ci

web-flow
merge: from main to 3.0 #34340

73 of 178 new or added lines in 9 files covered. (41.01%)

1199 existing lines in 124 files now uncovered.

203121 of 304663 relevant lines covered (66.67%)

132228377.94 hits per line

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

75.03
/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) {
641,920,407✔
31
  bool inEscape = false;
641,920,407✔
32
  bool inQuote = false;
641,920,407✔
33
  char quotaStr = 0;
641,920,407✔
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;
231,824,476✔
38
    }
39

40
    if (*(pToken->z + i) == TS_ESCAPE_CHAR) {
2,147,483,647✔
41
      if (!inQuote) {
24,998,763✔
42
        inEscape = !inEscape;
24,999,138✔
43
      }
44
    }
45

46
    if (*(pToken->z + i) == '\'' || *(pToken->z + i) == '"') {
2,147,483,647✔
47
      if (!inEscape) {
6,374,920✔
48
        if (!inQuote) {
6,373,300✔
49
          quotaStr = *(pToken->z + i);
3,186,650✔
50
          inQuote = !inQuote;
3,186,650✔
51
        } else if (quotaStr == *(pToken->z + i)) {
3,186,650✔
52
          inQuote = !inQuote;
3,186,650✔
53
        }
54
      }
55
    }
56
  }
57

58
  return NULL;
410,104,248✔
59
}
60

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

68
  int32_t code = TSDB_CODE_SUCCESS;
641,920,781✔
69
  char*   p = tableNameGetPosition(pTableName, TS_PATH_DELIMITER[0]);
641,920,781✔
70

71
  if (p != NULL) {  // db has been specified in sql string so we ignore current db path
641,928,763✔
72
    // before dbname dequote
73
    int32_t dbLen = p - pTableName->z;
231,824,301✔
74
    if (dbLen <= 0) {
231,820,691✔
75
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg2);
×
76
    }
77
    if (dbLen >= TSDB_DB_FNAME_LEN + TSDB_NAME_QUOTE) {
231,820,691✔
78
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg5);
×
79
    }
80

81
    char name[TSDB_DB_FNAME_LEN + TSDB_NAME_QUOTE] = {0};
231,820,691✔
82
    strncpy(name, pTableName->z, dbLen);
231,818,036✔
83
    int32_t actualDbLen = strdequote(name);
231,820,628✔
84

85
    // after dbname dequote
86
    if (actualDbLen <= 0) {
231,817,527✔
87
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg2);
850✔
88
    }
89
    if (actualDbLen >= TSDB_DB_NAME_LEN) {
231,817,077✔
90
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg5);
×
91
    }
92

93
    code = tNameSetDbName(pName, acctId, name, actualDbLen);
231,817,077✔
94
    if (code != TSDB_CODE_SUCCESS) {
231,819,849✔
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;
231,819,885✔
100
    if (tbLen <= 0) {
231,821,497✔
101
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
2,707✔
102
    }
103
    if (tbLen >= TSDB_TABLE_NAME_LEN + TSDB_NAME_QUOTE) {
231,818,790✔
104
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
123✔
105
    }
106

107
    char tbname[TSDB_TABLE_NAME_LEN + TSDB_NAME_QUOTE] = {0};
231,818,667✔
108
    strncpy(tbname, p + 1, tbLen);
231,820,731✔
109
    int32_t actualTbLen = strdequote(tbname);
231,815,712✔
110

111
    // after tbname dequote
112
    if (actualTbLen <= 0) {
231,820,164✔
113
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
4,845✔
114
    }
115
    if (actualTbLen >= TSDB_TABLE_NAME_LEN) {
231,815,983✔
116
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
123✔
117
    }
118

119
    code = tNameFromString(pName, tbname, T_NAME_TABLE);
231,815,860✔
120
    if (code != 0) {
231,822,526✔
UNCOV
121
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
×
122
    }
123
  } else {  // get current DB name first, and then set it into path
124
    // before tbname dequote
125
    int32_t tbLen = pTableName->n;
410,104,462✔
126
    if (tbLen <= 0) {
410,100,528✔
127
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
×
128
    }
129

130
    if (tbLen >= TSDB_TABLE_NAME_LEN + TSDB_NAME_QUOTE) {
410,102,702✔
131
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
123✔
132
    }
133

134
    char tbname[TSDB_TABLE_NAME_LEN + TSDB_NAME_QUOTE] = {0};
410,102,579✔
135
    strncpy(tbname, pTableName->z, tbLen);
410,103,415✔
136
    int32_t actualTbLen = strdequote(tbname);
410,103,313✔
137
    // after tbname dequote
138
    if (actualTbLen <= 0) {
410,101,333✔
139
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
1✔
140
    }
141
    if (actualTbLen >= TSDB_TABLE_NAME_LEN) {
410,101,752✔
142
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
123✔
143
    }
144

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

149
    code = tNameSetDbName(pName, acctId, dbName, strlen(dbName));
410,104,269✔
150
    if (code != TSDB_CODE_SUCCESS) {
410,102,526✔
151
      code = generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg2);
×
152
      return code;
×
153
    }
154

155
    code = tNameFromString(pName, tbname, T_NAME_TABLE);
410,102,526✔
156
    if (code != 0) {
410,101,341✔
157
      code = generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
×
158
    }
159
  }
160

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

165
  return code;
641,925,934✔
166
}
167

168
int16_t insFindCol(SToken* pColname, int16_t start, int16_t end, SSchema* pSchema) {
936,968,239✔
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;
931,254,266✔
172
    }
173
    ++start;
2,147,483,647✔
174
  }
175
  return -1;
5,714,297✔
176
}
177

178
int32_t insBuildCreateTbReq(SVCreateTbReq* pTbReq, const char* tname, STag* pTag, int64_t suid, const char* sname,
12,217,598✔
179
                            SArray* tagName, uint8_t tagNum, int32_t ttl) {
180
  pTbReq->type = TD_CHILD_TABLE;
12,217,598✔
181
  pTbReq->ctb.pTag = (uint8_t*)pTag;
12,219,966✔
182
  pTbReq->name = taosStrdup(tname);
12,214,942✔
183
  if (!pTbReq->name) return terrno;
12,215,330✔
184
  pTbReq->ctb.suid = suid;
12,214,998✔
185
  pTbReq->ctb.tagNum = tagNum;
12,215,330✔
186
  if (sname) {
12,215,330✔
187
    pTbReq->ctb.stbName = taosStrdup(sname);
11,576,752✔
188
    if (!pTbReq->ctb.stbName) return terrno;
11,575,092✔
189
  }
190
  pTbReq->ctb.tagName = taosArrayDup(tagName, NULL);
12,214,998✔
191
  if (!pTbReq->ctb.tagName) return terrno;
12,216,446✔
192
  pTbReq->ttl = ttl;
12,214,466✔
193
  pTbReq->commentLen = -1;
12,215,462✔
194

195
  return TSDB_CODE_SUCCESS;
12,218,118✔
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) {
586,562,956✔
205
  SSchema* pSchemas = getTableColumnSchema(pTableMeta);
586,562,956✔
206
  int32_t  code = 0;
586,565,155✔
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;
586,570,331✔
215
}
216

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

219
int32_t insInitBoundColsInfo(int32_t numOfBound, SBoundColInfo* pInfo) {
607,842,150✔
220
  pInfo->numOfCols = numOfBound;
607,842,150✔
221
  pInfo->numOfBound = numOfBound;
607,846,610✔
222
  pInfo->hasBoundCols = false;
607,841,864✔
223
  pInfo->mixTagsCols = false;
607,843,618✔
224
  pInfo->pColIndex = taosMemoryCalloc(numOfBound, sizeof(int16_t));
607,833,800✔
225
  if (NULL == pInfo->pColIndex) {
607,832,851✔
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;
607,841,332✔
232
}
233

234
void insResetBoundColsInfo(SBoundColInfo* pInfo) {
2,436✔
235
  pInfo->numOfBound = pInfo->numOfCols;
2,436✔
236
  pInfo->hasBoundCols = false;
2,436✔
237
  pInfo->mixTagsCols = false;
2,436✔
238
  for (int32_t i = 0; i < pInfo->numOfCols; ++i) {
12,180✔
239
    pInfo->pColIndex[i] = i;
9,744✔
240
  }
241
}
2,436✔
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;
28,064,205✔
247
  }
248

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

253
  if (tRowKeyCompare(rowKey, &pTableCxt->lastKey) == 0) {
2,147,483,647✔
254
    pTableCxt->duplicateTs = true;
1,775,595✔
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,
589,115,810✔
263
                                  bool colMode, bool ignoreColVals) {
264
  STableDataCxt* pTableCxt = taosMemoryCalloc(1, sizeof(STableDataCxt));
589,115,810✔
265
  if (NULL == pTableCxt) {
589,098,909✔
266
    *pOutput = NULL;
×
267
    return terrno;
×
268
  }
269

270
  int32_t code = TSDB_CODE_SUCCESS;
589,098,909✔
271

272
  pTableCxt->lastKey = (SRowKey){0};
589,098,909✔
273
  pTableCxt->ordered = true;
589,100,006✔
274
  pTableCxt->duplicateTs = false;
589,105,028✔
275

276
  pTableCxt->pMeta = tableMetaDup(pTableMeta);
589,108,895✔
277
  if (NULL == pTableCxt->pMeta) {
589,115,751✔
278
    code = TSDB_CODE_OUT_OF_MEMORY;
×
279
  }
280
  if (TSDB_CODE_SUCCESS == code) {
589,115,416✔
281
    pTableCxt->pSchema =
589,116,293✔
282
        tBuildTSchema(getTableColumnSchema(pTableMeta), pTableMeta->tableInfo.numOfColumns, pTableMeta->sversion);
589,114,548✔
283
    if (NULL == pTableCxt->pSchema) {
589,117,378✔
284
      code = TSDB_CODE_OUT_OF_MEMORY;
×
285
    }
286
  }
287
  pTableCxt->hasBlob = schemaHasBlob(pTableCxt->pSchema);
589,124,506✔
288

289
  if (TSDB_CODE_SUCCESS == code) {
589,110,871✔
290
    code = insInitBoundColsInfo(pTableMeta->tableInfo.numOfColumns, &pTableCxt->boundColsInfo);
589,116,496✔
291
  }
292
  if (TSDB_CODE_SUCCESS == code && !ignoreColVals) {
589,119,090✔
293
    pTableCxt->pValues = taosArrayInit(pTableMeta->tableInfo.numOfColumns, sizeof(SColVal));
586,358,633✔
294
    if (NULL == pTableCxt->pValues) {
586,358,763✔
295
      code = terrno;
×
296
    } else {
297
      code = initColValues(pTableMeta, pTableCxt->pValues);
586,361,844✔
298
    }
299
  }
300
  if (TSDB_CODE_SUCCESS == code) {
589,122,719✔
301
    pTableCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
589,126,046✔
302
    if (NULL == pTableCxt->pData) {
589,110,353✔
303
      code = terrno;
×
304
    } else {
305
      pTableCxt->pData->flags = (pCreateTbReq != NULL && NULL != *pCreateTbReq) ? SUBMIT_REQ_AUTO_CREATE_TABLE : 0;
589,115,460✔
306
      pTableCxt->pData->flags |= colMode ? SUBMIT_REQ_COLUMN_DATA_FORMAT : 0;
589,122,128✔
307
      pTableCxt->pData->suid = pTableMeta->suid;
589,117,874✔
308
      pTableCxt->pData->uid = pTableMeta->uid;
589,117,886✔
309
      pTableCxt->pData->sver = pTableMeta->sversion;
589,119,246✔
310
      pTableCxt->pData->pCreateTbReq = pCreateTbReq != NULL ? *pCreateTbReq : NULL;
589,114,303✔
311
      int8_t flag = pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
589,119,373✔
312
      if (pCreateTbReq != NULL) *pCreateTbReq = NULL;
589,112,648✔
313
      if (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
589,115,807✔
314
        pTableCxt->pData->aCol = taosArrayInit(128, sizeof(SColData));
3,469,129✔
315
        if (NULL == pTableCxt->pData->aCol) {
3,469,470✔
316
          code = terrno;
×
317
        }
318
      } else {
319
        pTableCxt->pData->aRowP = taosArrayInit(128, POINTER_BYTES);
585,644,393✔
320
        if (NULL == pTableCxt->pData->aRowP) {
585,647,313✔
321
          code = terrno;
×
322
        }
323
      }
324
    }
325
  }
326
  if (TSDB_CODE_SUCCESS == code) {
589,119,640✔
327
    *pOutput = pTableCxt;
589,119,640✔
328
    parserDebug("uid:%" PRId64 ", create table data context, code:%d, vgId:%d", pTableMeta->uid, code,
589,121,727✔
329
                pTableMeta->vgId);
330
  } else {
331
    insDestroyTableDataCxt(pTableCxt);
×
332
  }
333

334
  return code;
589,118,927✔
335
}
336

337
static int32_t rebuildTableData(SSubmitTbData* pSrc, SSubmitTbData** pDst, int8_t hasBlob) {
4,120,290✔
338
  int32_t        code = TSDB_CODE_SUCCESS;
4,120,290✔
339
  SSubmitTbData* pTmp = taosMemoryCalloc(1, sizeof(SSubmitTbData));
4,120,290✔
340
  if (NULL == pTmp) {
4,120,232✔
341
    code = terrno;
×
342
  } else {
343
    pTmp->flags = pSrc->flags;
4,120,232✔
344
    pTmp->suid = pSrc->suid;
4,120,564✔
345
    pTmp->uid = pSrc->uid;
4,120,947✔
346
    pTmp->sver = pSrc->sver;
4,120,990✔
347
    pTmp->pCreateTbReq = NULL;
4,120,734✔
348
    if (pTmp->flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
4,120,734✔
349
      if (pSrc->pCreateTbReq) {
3,970,308✔
350
        code = cloneSVreateTbReq(pSrc->pCreateTbReq, &pTmp->pCreateTbReq);
3,969,601✔
351
      } else {
352
        pTmp->flags &= ~SUBMIT_REQ_AUTO_CREATE_TABLE;
×
353
      }
354
    }
355
    if (TSDB_CODE_SUCCESS == code) {
4,121,066✔
356
      if (pTmp->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
4,121,066✔
357
        pTmp->aCol = taosArrayInit(128, sizeof(SColData));
3,475,051✔
358
        if (NULL == pTmp->aCol) {
3,475,933✔
359
          code = terrno;
×
360
          taosMemoryFree(pTmp);
×
361
        }
362
      } else {
363
        pTmp->aRowP = taosArrayInit(128, POINTER_BYTES);
646,015✔
364
        if (NULL == pTmp->aRowP) {
645,353✔
365
          code = terrno;
×
366
          taosMemoryFree(pTmp);
×
367
        }
368

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

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

380
  taosMemoryFree(pSrc);
4,121,286✔
381
  if (TSDB_CODE_SUCCESS == code) {
4,121,986✔
382
    *pDst = pTmp;
4,121,322✔
383
  }
384

385
  return code;
4,121,986✔
386
}
387

388
static void resetColValues(SArray* pValues) {
33,244,523✔
389
  int32_t num = taosArrayGetSize(pValues);
33,244,523✔
390
  for (int32_t i = 0; i < num; ++i) {
374,199,262✔
391
    SColVal* pVal = taosArrayGet(pValues, i);
340,954,739✔
392
    pVal->flag = CV_FLAG_NONE;
340,954,739✔
393
  }
394
}
33,244,523✔
395

396
int32_t insGetTableDataCxt(SHashObj* pHash, void* id, int32_t idLen, STableMeta* pTableMeta,
1,664,084,577✔
397
                           SVCreateTbReq** pCreateTbReq, STableDataCxt** pTableCxt, bool colMode, bool ignoreColVals) {
398
  STableDataCxt** tmp = (STableDataCxt**)taosHashGet(pHash, id, idLen);
1,664,084,577✔
399
  if (NULL != tmp) {
1,664,097,079✔
400
    *pTableCxt = *tmp;
1,074,974,790✔
401
    if (!ignoreColVals) {
1,074,974,790✔
402
      resetColValues((*pTableCxt)->pValues);
33,244,523✔
403
    }
404
    return TSDB_CODE_SUCCESS;
1,074,974,790✔
405
  }
406
  int32_t code = createTableDataCxt(pTableMeta, pCreateTbReq, pTableCxt, colMode, ignoreColVals);
589,122,289✔
407
  if (TSDB_CODE_SUCCESS == code) {
589,118,615✔
408
    void* pData = *pTableCxt;  // deal scan coverity
589,120,100✔
409
    code = taosHashPut(pHash, id, idLen, &pData, POINTER_BYTES);
589,120,522✔
410
  }
411

412
  if (TSDB_CODE_SUCCESS != code) {
589,124,696✔
413
    insDestroyTableDataCxt(*pTableCxt);
×
414
  }
415
  return code;
589,124,539✔
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,978,768,134✔
425
  }
426
}
427

428
void insDestroyTableDataCxt(STableDataCxt* pTableCxt) {
591,650,044✔
429
  if (NULL == pTableCxt) {
591,650,044✔
430
    return;
×
431
  }
432

433
  taosMemoryFreeClear(pTableCxt->pMeta);
591,650,044✔
434
  tDestroyTSchema(pTableCxt->pSchema);
591,648,609✔
435
  qDestroyBoundColInfo(&pTableCxt->boundColsInfo);
591,649,882✔
436
  taosArrayDestroyEx(pTableCxt->pValues, destroyColVal);
591,650,783✔
437
  if (pTableCxt->pData) {
591,649,543✔
438
    tDestroySubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
7,352,295✔
439
    taosMemoryFree(pTableCxt->pData);
7,351,299✔
440
  }
441
  taosMemoryFree(pTableCxt);
591,648,870✔
442
}
443

444
static void destroyColValSml(void* p) {
10,159,435✔
445
  SColVal* pVal = p;
10,159,435✔
446
  if (TSDB_DATA_TYPE_NCHAR == pVal->value.type || TSDB_DATA_TYPE_GEOMETRY == pVal->value.type ||
10,159,435✔
447
      TSDB_DATA_TYPE_VARBINARY == pVal->value.type) {
9,930,839✔
448
    taosMemoryFreeClear(pVal->value.pData);
231,219✔
449
  }
450
}
10,159,830✔
451

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

457
  taosMemoryFreeClear(pTableCxt->pMeta);
714,970✔
458
  tDestroyTSchema(pTableCxt->pSchema);
714,926✔
459
  qDestroyBoundColInfo(&pTableCxt->boundColsInfo);
714,926✔
460
  taosArrayDestroyEx(pTableCxt->pValues, destroyColValSml);
714,970✔
461
  if (pTableCxt->pData) {
715,013✔
462
    tDestroySubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
715,013✔
463
    taosMemoryFree(pTableCxt->pData);
714,970✔
464
  }
465
  taosMemoryFree(pTableCxt);
714,970✔
466
}
467

468
void insDestroyVgroupDataCxt(SVgroupDataCxt* pVgCxt) {
563,137,425✔
469
  if (NULL == pVgCxt) {
563,137,425✔
470
    return;
×
471
  }
472

473
  tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
563,137,425✔
474
  taosMemoryFree(pVgCxt->pData);
563,136,447✔
475

476
  taosMemoryFree(pVgCxt);
563,136,869✔
477
}
478

479
void insDestroyVgroupDataCxtList(SArray* pVgCxtList) {
1,095,881,836✔
480
  if (NULL == pVgCxtList) {
1,095,881,836✔
481
    return;
547,978,825✔
482
  }
483

484
  size_t size = taosArrayGetSize(pVgCxtList);
547,903,011✔
485
  for (int32_t i = 0; i < size; i++) {
1,111,038,084✔
486
    void* p = taosArrayGetP(pVgCxtList, i);
563,137,640✔
487
    insDestroyVgroupDataCxt(p);
563,137,847✔
488
  }
489

490
  taosArrayDestroy(pVgCxtList);
547,900,444✔
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) {
547,755,527✔
509
  if (NULL == pTableCxtHash) {
547,755,527✔
510
    return;
3,442,363✔
511
  }
512

513
  void** p = taosHashIterate(pTableCxtHash, NULL);
544,313,164✔
514
  while (p) {
1,129,188,796✔
515
    insDestroyTableDataCxt(*(STableDataCxt**)p);
584,874,758✔
516

517
    p = taosHashIterate(pTableCxtHash, p);
584,873,837✔
518
  }
519

520
  taosHashCleanup(pTableCxtHash);
544,314,038✔
521
}
522

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

528
  void** p = taosHashIterate(pTableCxtHash, NULL);
966,095✔
529
  while (p) {
1,681,108✔
530
    insDestroyTableDataCxtSml(*(STableDataCxt**)p);
714,970✔
531

532
    p = taosHashIterate(pTableCxtHash, p);
714,970✔
533
  }
534

535
  taosHashCleanup(pTableCxtHash);
966,138✔
536
}
537

538
static int32_t fillVgroupDataCxt(STableDataCxt* pTableCxt, SVgroupDataCxt* pVgCxt, bool isRebuild, bool clear) {
590,362,094✔
539
  int32_t code = 0;
590,362,094✔
540
  if (NULL == pVgCxt->pData->aSubmitTbData) {
590,362,094✔
541
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
562,688,871✔
542
    if (pVgCxt->pData->aSubmitTbData == NULL) {
562,687,198✔
543
      return terrno;
×
544
    }
545
    if (pTableCxt->hasBlob) {
562,688,274✔
546
      pVgCxt->pData->aSubmitBlobData = taosArrayInit(128, sizeof(SBlobSet*));
21,608✔
547
      if (NULL == pVgCxt->pData->aSubmitBlobData) {
21,608✔
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;
590,363,351✔
555

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

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

568
  if (isRebuild) {
590,360,945✔
569
    code = rebuildTableData(pTableCxt->pData, &pTableCxt->pData, pTableCxt->hasBlob);
4,120,741✔
570
  } else if (clear) {
586,240,204✔
571
    taosMemoryFreeClear(pTableCxt->pData);
584,383,785✔
572
  }
573
  parserDebug("uid:%" PRId64 ", add table data context to vgId:%d", pTableCxt->pMeta->uid, pVgCxt->vgId);
590,359,206✔
574

575
  return code;
590,360,943✔
576
}
577

578
static int32_t createVgroupDataCxt(int32_t vgId, SHashObj* pVgroupHash, SArray* pVgroupList, SVgroupDataCxt** pOutput) {
563,231,611✔
579
  SVgroupDataCxt* pVgCxt = taosMemoryCalloc(1, sizeof(SVgroupDataCxt));
563,231,611✔
580
  if (NULL == pVgCxt) {
563,233,726✔
581
    return terrno;
×
582
  }
583
  pVgCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitReq2));
563,233,726✔
584
  if (NULL == pVgCxt->pData) {
563,231,002✔
585
    insDestroyVgroupDataCxt(pVgCxt);
×
586
    return terrno;
×
587
  }
588

589
  pVgCxt->vgId = vgId;
563,232,172✔
590
  int32_t code = taosHashPut(pVgroupHash, &pVgCxt->vgId, sizeof(pVgCxt->vgId), &pVgCxt, POINTER_BYTES);
563,232,586✔
591
  if (TSDB_CODE_SUCCESS == code) {
563,233,306✔
592
    if (NULL == taosArrayPush(pVgroupList, &pVgCxt)) {
563,233,620✔
593
      code = terrno;
×
594
      insDestroyVgroupDataCxt(pVgCxt);
×
UNCOV
595
      return code;
×
596
    }
597
    //    uDebug("td23101 2vgId:%d, uid:%" PRIu64, pVgCxt->vgId, pTableCxt->pMeta->uid);
598
    *pOutput = pVgCxt;
563,233,620✔
599
  } else {
600
    insDestroyVgroupDataCxt(pVgCxt);
×
601
  }
602
  return code;
563,233,372✔
603
}
604

605
int insColDataComp(const void* lp, const void* rp) {
15,614,626✔
606
  SColData* pLeft = (SColData*)lp;
15,614,626✔
607
  SColData* pRight = (SColData*)rp;
15,614,626✔
608
  if (pLeft->cid < pRight->cid) {
15,614,626✔
609
    return -1;
15,561,655✔
610
  } else if (pLeft->cid > pRight->cid) {
53,613✔
611
    return 1;
53,613✔
612
  }
613

614
  return 0;
×
615
}
616

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

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

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

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

639
  return TSDB_CODE_SUCCESS;
10,976✔
640
}
641

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

647
  if (pTbData->getFromHash) {
2,863,203✔
648
    pTbInfo = (STableVgUid*)tSimpleHashGet(pBuildInfo->pTableHash, pTbData->tbName, strlen(pTbData->tbName));
2,787,931✔
649
  }
650

651
  if (NULL == pTbInfo) {
2,863,608✔
652
    SName sname;
61,364✔
653
    code = qCreateSName2(&sname, pTbData->tbName, pBuildInfo->acctId, pBuildInfo->dbname, NULL, 0);
76,427✔
654
    if (TSDB_CODE_SUCCESS != code) {
76,425✔
655
      return code;
450✔
656
    }
657

658
    STableMeta*      pTableMeta = NULL;
76,425✔
659
    SRequestConnInfo conn = {.pTrans = pBuildInfo->transport,
137,787✔
660
                             .requestId = pBuildInfo->requestId,
76,427✔
661
                             .requestObjRefId = pBuildInfo->requestSelf,
76,425✔
662
                             .mgmtEps = pBuildInfo->mgmtEpSet};
663
    code = catalogGetTableMeta((SCatalog*)pBuildInfo->pCatalog, &conn, &sname, &pTableMeta);
76,425✔
664

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

670
    if (TSDB_CODE_SUCCESS != code) {
75,975✔
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;
75,975✔
676
    *vgId = pTableMeta->vgId;
75,971✔
677
    *suid = pTableMeta->suid;
75,973✔
678

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

685
    taosMemoryFree(pTableMeta);
75,975✔
686
  } else {
687
    *uid = pTbInfo->uid;
2,787,181✔
688
    *vgId = pTbInfo->vgid;
2,787,268✔
689
    *suid = pTbInfo->suid;
2,787,315✔
690
  }
691

692
  return code;
2,863,294✔
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,
1,006,801✔
707
                                            char* tbname) {
708
  if (NULL == pVgCxt->pData->aSubmitTbData) {
1,006,801✔
709
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
543,944✔
710
    if (NULL == pVgCxt->pData->aSubmitTbData) {
544,117✔
711
      return terrno;
×
712
    }
713
    if (pTbCtx->hasBlob) {
544,117✔
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;
1,007,059✔
722
  SArray** rowP = NULL;
1,007,059✔
723

724
  rowP = (SArray**)tSimpleHashGet(pTableNameHash, tbname, strlen(tbname));
1,007,059✔
725

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

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

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

745
        code = tRowMergeWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet, 0);
×
746
        TAOS_CHECK_RETURN(code);
×
747
      }
748
    }
749

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

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

761
  if (pTbCtx->hasBlob == 0) {
1,007,096✔
762
    pTbCtx->pData->pBlobSet = NULL;  // if no blob, set it to NULL
1,007,072✔
763
  }
764

765
  if (NULL == taosArrayPush(pVgCxt->pData->aSubmitTbData, pTbCtx->pData)) {
2,014,077✔
766
    return terrno;
×
767
  }
768

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

777
  code = tSimpleHashPut(pTableNameHash, tbname, strlen(tbname), &pTbCtx->pData->aRowP, sizeof(SArray*));
1,007,023✔
778

779
  if (code != TSDB_CODE_SUCCESS) {
1,007,017✔
780
    return code;
×
781
  }
782

783
  parserDebug("uid:%" PRId64 ", add table data context to vgId:%d", pTbCtx->pMeta->uid, pVgCxt->vgId);
1,007,017✔
784

785
  return TSDB_CODE_SUCCESS;
1,007,017✔
786
}
787

788
int32_t insAppendStmtTableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
1,856,413✔
789
                                  SStbInterlaceInfo* pBuildInfo) {
790
  int32_t  code = TSDB_CODE_SUCCESS;
1,856,413✔
791
  uint64_t uid;
1,842,448✔
792
  int32_t  vgId;
1,842,821✔
793
  uint64_t suid;
1,842,821✔
794

795
  pTbCtx->pData->aRowP = pTbData->aCol;
1,856,880✔
796

797
  code = insGetStmtTableVgUid(pAllVgHash, pBuildInfo, pTbData, &uid, &vgId, &suid);
1,856,548✔
798
  if (TSDB_CODE_SUCCESS != code) {
1,856,637✔
799
    return code;
×
800
  }
801

802
  pTbCtx->pMeta->vgId = vgId;
1,856,637✔
803
  pTbCtx->pMeta->uid = uid;
1,856,682✔
804
  pTbCtx->pData->uid = uid;
1,856,545✔
805

806
  if (!pTbCtx->ordered) {
1,856,780✔
807
    code = tRowSort(pTbCtx->pData->aRowP);
12✔
808
  }
809
  if (code == TSDB_CODE_SUCCESS && (!pTbCtx->ordered || pTbCtx->duplicateTs)) {
1,856,361✔
810
    code = tRowMerge(pTbCtx->pData->aRowP, pTbCtx->pSchema, 0);
59✔
811
  }
812

813
  if (TSDB_CODE_SUCCESS != code) {
1,856,591✔
814
    return code;
×
815
  }
816

817
  SVgroupDataCxt* pVgCxt = NULL;
1,856,591✔
818
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
1,856,776✔
819
  if (NULL == pp) {
1,856,507✔
820
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
329,994✔
821
    if (NULL == pp) {
329,992✔
822
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
329,992✔
823
    } else {
824
      pVgCxt = *(SVgroupDataCxt**)pp;
×
825
    }
826
  } else {
827
    pVgCxt = *(SVgroupDataCxt**)pp;
1,526,513✔
828
  }
829

830
  if (TSDB_CODE_SUCCESS == code) {
1,856,470✔
831
    code = fillVgroupDataCxt(pTbCtx, pVgCxt, false, false);
1,856,470✔
832
  }
833

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

841
  return code;
1,855,841✔
842
}
843

844
int32_t insAppendStmt2TableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
1,006,769✔
845
                                   SStbInterlaceInfo* pBuildInfo, SVCreateTbReq* ctbReq) {
846
  int32_t  code = TSDB_CODE_SUCCESS;
1,006,769✔
847
  uint64_t uid;
311,430✔
848
  int32_t  vgId;
311,480✔
849
  uint64_t suid;
311,480✔
850

851
  pTbCtx->pData->aRowP = pTbData->aCol;
1,006,819✔
852
  pTbCtx->pData->pBlobSet = pTbData->pBlobSet;
1,006,991✔
853

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

873
    pTbCtx->pMeta->vgId = vgId;
1,006,543✔
874
    pTbCtx->pMeta->uid = uid;
1,006,543✔
875
    pTbCtx->pData->uid = uid;
1,006,586✔
876
    pTbCtx->pData->pCreateTbReq = NULL;
1,006,586✔
877

878
    if (ctbReq != NULL) {
1,006,586✔
879
      tdDestroySVCreateTbReq(ctbReq);
880
      taosMemoryFree(ctbReq);
588,369✔
881
      ctbReq = NULL;
588,380✔
882
    }
883
  }
884

885
  if (pTbCtx->hasBlob == 0) {
1,007,047✔
886
    if (!pTbData->isOrdered) {
1,007,097✔
887
      code = tRowSort(pTbCtx->pData->aRowP);
×
888
    }
889
    if (code == TSDB_CODE_SUCCESS && (!pTbData->isOrdered || pTbData->isDuplicateTs)) {
1,007,097✔
890
      code = tRowMerge(pTbCtx->pData->aRowP, pTbCtx->pSchema, PREFER_NON_NULL);
×
891
    }
892
  } else {
UNCOV
893
    if (!pTbData->isOrdered) {
×
894
      code = tRowSortWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet);
×
895
    }
UNCOV
896
    if (code == TSDB_CODE_SUCCESS && (!pTbData->isOrdered || pTbData->isDuplicateTs)) {
×
897
      code = tRowMergeWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet, 0);
×
898
    }
899
  }
900

901
  if (TSDB_CODE_SUCCESS != code) {
1,006,906✔
902
    return code;
×
903
  }
904

905
  SVgroupDataCxt* pVgCxt = NULL;
1,006,906✔
906
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
1,006,949✔
907
  if (NULL == pp) {
1,007,015✔
908
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
544,135✔
909
    if (NULL == pp) {
544,228✔
910
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
544,228✔
911
    } else {
912
      pVgCxt = *(SVgroupDataCxt**)pp;
×
913
    }
914
  } else {
915
    pVgCxt = *(SVgroupDataCxt**)pp;
462,880✔
916
  }
917

918
  if (code == TSDB_CODE_SUCCESS) {
1,006,862✔
919
    code = checkAndMergeSVgroupDataCxtByTbname(pTbCtx, pVgCxt, pBuildInfo->pTableRowDataHash, pTbData->tbName);
1,006,825✔
920
  }
921

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

929
  return code;
1,006,981✔
930
}
931

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

942
  int32_t code = TSDB_CODE_SUCCESS;
943

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

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

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

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

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

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

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

986
  return code;
987
}
988
*/
989

990
static int8_t colDataHasBlob(SColData* pCol) {
×
991
  if (IS_STR_DATA_BLOB(pCol->type)) {
×
992
    return 1;
×
993
  }
994
  return 0;
×
995
}
996
int32_t insMergeTableDataCxt(SHashObj* pTableHash, SArray** pVgDataBlocks, bool isRebuild) {
547,193,590✔
997
  SHashObj* pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
547,193,590✔
998
  SArray*   pVgroupList = taosArrayInit(8, POINTER_BYTES);
547,197,540✔
999
  if (NULL == pVgroupHash || NULL == pVgroupList) {
547,195,134✔
1000
    taosHashCleanup(pVgroupHash);
46✔
1001
    taosArrayDestroy(pVgroupList);
×
1002
    return terrno;
×
1003
  }
1004

1005
  int32_t code = TSDB_CODE_SUCCESS;
547,195,088✔
1006
  bool    colFormat = false;
547,195,088✔
1007

1008
  void* p = taosHashIterate(pTableHash, NULL);
547,195,088✔
1009
  if (p) {
547,198,133✔
1010
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
547,198,133✔
1011
    colFormat = (0 != (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT));
547,198,133✔
1012
  }
1013

1014
  while (TSDB_CODE_SUCCESS == code && NULL != p) {
1,135,705,316✔
1015
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
588,507,137✔
1016
    if (colFormat) {
588,507,137✔
1017
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
3,476,768✔
1018
      if (pCol && pCol->nVal <= 0) {
3,476,436✔
1019
        p = taosHashIterate(pTableHash, p);
135✔
1020
        continue;
135✔
1021
      }
1022

1023
      if (pTableCxt->pData->pCreateTbReq) {
3,476,301✔
1024
        pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
3,336,683✔
1025
      }
1026
      int8_t isBlob = IS_STR_DATA_BLOB(pCol->type) ? 1 : 0;
3,475,969✔
1027
      if (isBlob == 0) {
3,476,301✔
1028
        taosArraySort(pTableCxt->pData->aCol, insColDataComp);
3,476,301✔
1029
        code = tColDataSortMerge(&pTableCxt->pData->aCol);
3,476,265✔
1030
      } else {
1031
        taosArraySort(pTableCxt->pData->aCol, insColDataComp);
×
1032
        code = tColDataSortMergeWithBlob(&pTableCxt->pData->aCol, pTableCxt->pData->pBlobSet);
×
1033
      }
1034
    } else {
1035
      // skip the table has no data to insert
1036
      // eg: import a csv without valid data
1037
      // if (0 == taosArrayGetSize(pTableCxt->pData->aRowP)) {
1038
      //   parserWarn("no row in tableDataCxt uid:%" PRId64 " ", pTableCxt->pMeta->uid);
1039
      //   p = taosHashIterate(pTableHash, p);
1040
      //   continue;
1041
      // }
1042
      if (pTableCxt->hasBlob == 0) {
585,030,369✔
1043
        if (!pTableCxt->ordered) {
585,008,416✔
1044
          code = tRowSort(pTableCxt->pData->aRowP);
1,392,636✔
1045
        }
1046
        if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
585,008,416✔
1047
          code = tRowMerge(pTableCxt->pData->aRowP, pTableCxt->pSchema, PREFER_NON_NULL);
1,524,892✔
1048
        }
1049
      } else {
1050
        if (!pTableCxt->ordered) {
21,608✔
1051
          code = tRowSortWithBlob(pTableCxt->pData->aRowP, pTableCxt->pSchema, pTableCxt->pData->pBlobSet);
741✔
1052
        }
1053
        if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
21,608✔
1054
          code = tRowMergeWithBlob(pTableCxt->pData->aRowP, pTableCxt->pSchema, pTableCxt->pData->pBlobSet, 0);
741✔
1055
        }
1056
      }
1057
    }
1058

1059
    if (TSDB_CODE_SUCCESS == code) {
588,506,325✔
1060
      SVgroupDataCxt* pVgCxt = NULL;
588,506,325✔
1061
      int32_t         vgId = pTableCxt->pMeta->vgId;
588,506,325✔
1062
      void**          pp = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
588,505,870✔
1063
      if (NULL == pp) {
588,506,440✔
1064
        code = createVgroupDataCxt(vgId, pVgroupHash, pVgroupList, &pVgCxt);
562,359,154✔
1065
      } else {
1066
        pVgCxt = *(SVgroupDataCxt**)pp;
26,147,286✔
1067
      }
1068
      if (TSDB_CODE_SUCCESS == code) {
588,506,596✔
1069
        code = fillVgroupDataCxt(pTableCxt, pVgCxt, isRebuild, true);
588,506,589✔
1070
      }
1071
    }
1072
    if (TSDB_CODE_SUCCESS == code) {
588,502,903✔
1073
      p = taosHashIterate(pTableHash, p);
588,502,903✔
1074
    }
1075
  }
1076

1077
  taosHashCleanup(pVgroupHash);
547,198,179✔
1078
  if (TSDB_CODE_SUCCESS == code) {
547,196,720✔
1079
    *pVgDataBlocks = pVgroupList;
547,197,433✔
1080
  } else {
1081
    insDestroyVgroupDataCxtList(pVgroupList);
24✔
1082
  }
1083

1084
  return code;
547,196,569✔
1085
}
1086

1087
static int32_t buildSubmitReq(int32_t vgId, SSubmitReq2* pReq, void** pData, uint32_t* pLen) {
563,229,619✔
1088
  int32_t  code = TSDB_CODE_SUCCESS;
563,229,619✔
1089
  uint32_t len = 0;
563,229,619✔
1090
  void*    pBuf = NULL;
563,229,619✔
1091
  tEncodeSize(tEncodeSubmitReq, pReq, len, code);
563,229,619✔
1092
  if (TSDB_CODE_SUCCESS == code) {
563,229,226✔
1093
    SEncoder encoder;
560,448,835✔
1094
    len += sizeof(SSubmitReq2Msg);
563,227,033✔
1095
    pBuf = taosMemoryMalloc(len);
563,227,033✔
1096
    if (NULL == pBuf) {
563,223,957✔
1097
      return terrno;
×
1098
    }
1099
    ((SSubmitReq2Msg*)pBuf)->header.vgId = htonl(vgId);
563,223,957✔
1100
    ((SSubmitReq2Msg*)pBuf)->header.contLen = htonl(len);
563,227,997✔
1101
    ((SSubmitReq2Msg*)pBuf)->version = htobe64(1);
563,228,497✔
1102
    tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SSubmitReq2Msg)), len - sizeof(SSubmitReq2Msg));
563,231,584✔
1103
    code = tEncodeSubmitReq(&encoder, pReq);
563,230,632✔
1104
    tEncoderClear(&encoder);
563,233,645✔
1105
  }
1106

1107
  if (TSDB_CODE_SUCCESS == code) {
563,232,534✔
1108
    *pData = pBuf;
563,232,534✔
1109
    *pLen = len;
563,232,580✔
1110
  } else {
1111
    taosMemoryFree(pBuf);
×
1112
  }
1113
  return code;
563,227,283✔
1114
}
1115

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

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

1129
  if (p->aSubmitBlobData != NULL) {
563,231,893✔
1130
    for (int32_t i = 0; i < taosArrayGetSize(p->aSubmitTbData); i++) {
43,216✔
1131
      SSubmitTbData* pSubmitTbData = taosArrayGet(p->aSubmitTbData, i);
21,608✔
1132
      SBlobSet**     ppBlob = taosArrayGet(p->aSubmitBlobData, i);
21,608✔
1133
      SBlobSet*      pBlob = ppBlob ? *ppBlob : NULL;
21,608✔
1134
      int32_t        nrow = taosArrayGetSize(pSubmitTbData->aRowP);
21,608✔
1135
      int32_t        nblob = 0;
21,608✔
1136
      if (nrow > 0 && pBlob) {
21,608✔
1137
        nblob = taosArrayGetSize(pBlob->pSeqTable);
21,608✔
1138
      }
1139
      uTrace("blob %p row size %d, pData size %d", pBlob, nblob, nrow);
21,608✔
1140
      pSubmitTbData->pBlobSet = pBlob;
21,608✔
1141
      if (ppBlob != NULL) *ppBlob = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
21,608✔
1142
    }
1143
  } else {
1144
    for (int32_t i = 0; i < taosArrayGetSize(p->aSubmitTbData); i++) {
1,154,555,375✔
1145
      SSubmitTbData* pSubmitTbData = taosArrayGet(p->aSubmitTbData, i);
591,345,552✔
1146
      pSubmitTbData->pBlobSet = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
591,346,998✔
1147
    }
1148
  }
1149

1150
  return code;
563,231,348✔
1151
}
1152
int32_t insBuildVgDataBlocks(SHashObj* pVgroupsHashObj, SArray* pVgDataCxtList, SArray** pVgDataBlocks, bool append) {
547,997,052✔
1153
  size_t  numOfVg = taosArrayGetSize(pVgDataCxtList);
547,997,052✔
1154
  SArray* pDataBlocks = (append && *pVgDataBlocks) ? *pVgDataBlocks : taosArrayInit(numOfVg, POINTER_BYTES);
547,998,875✔
1155
  if (NULL == pDataBlocks) {
547,997,760✔
1156
    return TSDB_CODE_OUT_OF_MEMORY;
×
1157
  }
1158

1159
  int32_t code = TSDB_CODE_SUCCESS;
547,997,760✔
1160
  for (size_t i = 0; TSDB_CODE_SUCCESS == code && i < numOfVg; ++i) {
1,111,229,926✔
1161
    SVgroupDataCxt* src = taosArrayGetP(pVgDataCxtList, i);
563,231,554✔
1162
    if (taosArrayGetSize(src->pData->aSubmitTbData) <= 0) {
563,234,041✔
1163
      continue;
×
1164
    }
1165
    SVgDataBlocks* dst = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
563,231,051✔
1166
    if (NULL == dst) {
563,226,685✔
1167
      code = terrno;
×
1168
    }
1169

1170
    if (TSDB_CODE_SUCCESS == code) {
563,226,685✔
1171
      dst->numOfTables = taosArrayGetSize(src->pData->aSubmitTbData);
563,227,152✔
1172
      code = taosHashGetDup(pVgroupsHashObj, (const char*)&src->vgId, sizeof(src->vgId), &dst->vg);
563,229,301✔
1173
    }
1174
    if (TSDB_CODE_SUCCESS == code) {
563,233,391✔
1175
      code = insResetBlob(src->pData);
563,233,391✔
1176
    }
1177

1178
    if (TSDB_CODE_SUCCESS == code) {
563,228,759✔
1179
      code = buildSubmitReq(src->vgId, src->pData, &dst->pData, &dst->size);
563,228,747✔
1180
    }
1181
    if (TSDB_CODE_SUCCESS == code) {
563,228,251✔
1182
      code = (NULL == taosArrayPush(pDataBlocks, &dst) ? terrno : TSDB_CODE_SUCCESS);
563,232,063✔
1183
    }
1184
    if (TSDB_CODE_SUCCESS != code) {
563,232,472✔
1185
      destroyVgDataBlocks(dst);
×
1186
    }
1187
  }
1188

1189
  if (append) {
547,998,372✔
1190
    if (NULL == *pVgDataBlocks) {
801,221✔
1191
      *pVgDataBlocks = pDataBlocks;
801,221✔
1192
    }
1193
    return code;
801,221✔
1194
  }
1195

1196
  if (TSDB_CODE_SUCCESS == code) {
547,197,151✔
1197
    *pVgDataBlocks = pDataBlocks;
547,191,362✔
1198
  } else {
1199
    taosArrayDestroyP(pDataBlocks, destroyVgDataBlocks);
5,863✔
1200
  }
1201

1202
  return code;
547,194,514✔
1203
}
1204

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

1212
  return false;
×
1213
}
1214

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

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

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

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

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

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

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

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

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

1383
  pTableCxt->pData->flags |= TD_REQ_FROM_TAOX;
35,837✔
1384
  if (tmp == NULL) {
35,837✔
1385
    ret = initTableColSubmitData(pTableCxt);
32,131✔
1386
    if (ret != TSDB_CODE_SUCCESS) {
32,131✔
1387
      uError("initTableColSubmitData error");
×
1388
      goto end;
×
1389
    }
1390
  }
1391

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

1399
  int32_t numOfRows = *(int32_t*)p;
35,837✔
1400
  p += sizeof(int32_t);
35,837✔
1401

1402
  int32_t numOfCols = *(int32_t*)p;
35,837✔
1403
  p += sizeof(int32_t);
35,837✔
1404

1405
  p += sizeof(int32_t);
35,837✔
1406
  p += sizeof(uint64_t);
35,837✔
1407

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

1416
  int32_t* colLength = (int32_t*)p;
35,837✔
1417
  p += sizeof(int32_t) * numOfCols;
35,837✔
1418

1419
  char* pStart = p;
35,837✔
1420

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

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

1431
  bool hasTs = false;
35,837✔
1432
  if (tFields == NULL) {
35,837✔
1433
    int32_t len = TMIN(numOfCols, boundInfo->numOfBound);
1,656✔
1434
    for (int j = 0; j < len; j++) {
4,968✔
1435
      SSchema*    pColSchema = &pSchema[j];
3,864✔
1436
      SSchemaExt* pColExtSchema = &pExtSchemas[j];
3,864✔
1437
      PRCESS_DATA(j, j)
3,864✔
1438
    }
1439
  } else {
1440
    for (int i = 0; i < numFields; i++) {
184,610✔
1441
      for (int j = 0; j < boundInfo->numOfBound; j++) {
1,390,941✔
1442
        SSchema*    pColSchema = &pSchema[j];
1,390,941✔
1443
        SSchemaExt* pColExtSchema = &pExtSchemas[j];
1,390,941✔
1444
        char*       fieldName = NULL;
1,390,941✔
1445
        if (raw) {
1,390,941✔
1446
          fieldName = ((SSchemaWrapper*)tFields)->pSchema[i].name;
1,389,561✔
1447
        } else {
1448
          fieldName = ((TAOS_FIELD*)tFields)[i].name;
1,380✔
1449
        }
1450
        if (strcmp(pColSchema->name, fieldName) == 0) {
1,390,941✔
1451
          PRCESS_DATA(i, j)
150,429✔
1452
          break;
150,429✔
1453
        }
1454
      }
1455
    }
1456
  }
1457

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

1464
  // process NULL data
1465
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
192,415✔
1466
    if (boundInfo->pColIndex[c] != -1) {
157,130✔
1467
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, c);
3,665✔
1468
      ret = tColDataAddValueByDataBlock(pCol, 0, 0, numOfRows, NULL, NULL);
3,665✔
1469
      if (ret != 0) {
3,665✔
1470
        goto end;
×
1471
      }
1472
    } else {
1473
      boundInfo->pColIndex[c] = c;  // restore for next block
153,465✔
1474
    }
1475
  }
1476

1477
end:
35,837✔
1478
  return ret;
35,837✔
1479
}
1480

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

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

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

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