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

taosdata / TDengine / #4928

15 Jan 2026 04:09PM UTC coverage: 66.708% (+0.6%) from 66.12%
#4928

push

travis-ci

web-flow
merge: from main to 3.0 branch #34317

840 of 1255 new or added lines in 23 files covered. (66.93%)

826 existing lines in 119 files now uncovered.

203035 of 304362 relevant lines covered (66.71%)

130886757.17 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) {
602,628,855✔
31
  bool inEscape = false;
602,628,855✔
32
  bool inQuote = false;
602,628,855✔
33
  char quotaStr = 0;
602,628,855✔
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;
223,993,696✔
38
    }
39

40
    if (*(pToken->z + i) == TS_ESCAPE_CHAR) {
2,147,483,647✔
41
      if (!inQuote) {
23,461,754✔
42
        inEscape = !inEscape;
23,459,306✔
43
      }
44
    }
45

46
    if (*(pToken->z + i) == '\'' || *(pToken->z + i) == '"') {
2,147,483,647✔
47
      if (!inEscape) {
6,206,330✔
48
        if (!inQuote) {
6,204,950✔
49
          quotaStr = *(pToken->z + i);
3,102,475✔
50
          inQuote = !inQuote;
3,102,475✔
51
        } else if (quotaStr == *(pToken->z + i)) {
3,102,475✔
52
          inQuote = !inQuote;
3,102,475✔
53
        }
54
      }
55
    }
56
  }
57

58
  return NULL;
378,642,333✔
59
}
60

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

68
  int32_t code = TSDB_CODE_SUCCESS;
602,625,097✔
69
  char*   p = tableNameGetPosition(pTableName, TS_PATH_DELIMITER[0]);
602,625,097✔
70

71
  if (p != NULL) {  // db has been specified in sql string so we ignore current db path
602,634,790✔
72
    // before dbname dequote
73
    int32_t dbLen = p - pTableName->z;
223,993,307✔
74
    if (dbLen <= 0) {
223,983,659✔
75
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg2);
×
76
    }
77
    if (dbLen >= TSDB_DB_FNAME_LEN + TSDB_NAME_QUOTE) {
223,983,659✔
78
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg5);
×
79
    }
80

81
    char name[TSDB_DB_FNAME_LEN + TSDB_NAME_QUOTE] = {0};
223,983,659✔
82
    strncpy(name, pTableName->z, dbLen);
223,986,165✔
83
    int32_t actualDbLen = strdequote(name);
223,985,880✔
84

85
    // after dbname dequote
86
    if (actualDbLen <= 0) {
223,986,521✔
UNCOV
87
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg2);
×
88
    }
89
    if (actualDbLen >= TSDB_DB_NAME_LEN) {
223,989,540✔
90
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg5);
×
91
    }
92

93
    code = tNameSetDbName(pName, acctId, name, actualDbLen);
223,989,540✔
94
    if (code != TSDB_CODE_SUCCESS) {
223,992,251✔
UNCOV
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;
223,992,251✔
100
    if (tbLen <= 0) {
223,982,419✔
UNCOV
101
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
×
102
    }
103
    if (tbLen >= TSDB_TABLE_NAME_LEN + TSDB_NAME_QUOTE) {
223,990,737✔
104
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
117✔
105
    }
106

107
    char tbname[TSDB_TABLE_NAME_LEN + TSDB_NAME_QUOTE] = {0};
223,990,620✔
108
    strncpy(tbname, p + 1, tbLen);
223,991,897✔
109
    int32_t actualTbLen = strdequote(tbname);
223,982,613✔
110

111
    // after tbname dequote
112
    if (actualTbLen <= 0) {
223,994,245✔
113
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
310✔
114
    }
115
    if (actualTbLen >= TSDB_TABLE_NAME_LEN) {
223,995,533✔
116
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
117✔
117
    }
118

119
    code = tNameFromString(pName, tbname, T_NAME_TABLE);
223,995,416✔
120
    if (code != 0) {
223,992,279✔
121
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
4,642✔
122
    }
123
  } else {  // get current DB name first, and then set it into path
124
    // before tbname dequote
125
    int32_t tbLen = pTableName->n;
378,641,483✔
126
    if (tbLen <= 0) {
378,636,984✔
127
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
×
128
    }
129

130
    if (tbLen >= TSDB_TABLE_NAME_LEN + TSDB_NAME_QUOTE) {
378,639,988✔
131
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
117✔
132
    }
133

134
    char tbname[TSDB_TABLE_NAME_LEN + TSDB_NAME_QUOTE] = {0};
378,639,871✔
135
    strncpy(tbname, pTableName->z, tbLen);
378,638,053✔
136
    int32_t actualTbLen = strdequote(tbname);
378,636,981✔
137
    // after tbname dequote
138
    if (actualTbLen <= 0) {
378,633,499✔
139
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
2,035✔
140
    }
141
    if (actualTbLen >= TSDB_TABLE_NAME_LEN) {
378,631,640✔
142
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
117✔
143
    }
144

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

149
    code = tNameSetDbName(pName, acctId, dbName, strlen(dbName));
378,641,009✔
150
    if (code != TSDB_CODE_SUCCESS) {
378,638,582✔
151
      code = generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg2);
×
152
      return code;
×
153
    }
154

155
    code = tNameFromString(pName, tbname, T_NAME_TABLE);
378,638,582✔
156
    if (code != 0) {
378,634,877✔
157
      code = generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
×
158
    }
159
  }
160

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

165
  return code;
602,630,300✔
166
}
167

168
int16_t insFindCol(SToken* pColname, int16_t start, int16_t end, SSchema* pSchema) {
914,310,413✔
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;
908,634,593✔
172
    }
173
    ++start;
2,147,483,647✔
174
  }
175
  return -1;
5,676,051✔
176
}
177

178
int32_t insBuildCreateTbReq(SVCreateTbReq* pTbReq, const char* tname, STag* pTag, int64_t suid, const char* sname,
11,440,941✔
179
                            SArray* tagName, uint8_t tagNum, int32_t ttl) {
180
  pTbReq->type = TD_CHILD_TABLE;
11,440,941✔
181
  pTbReq->ctb.pTag = (uint8_t*)pTag;
11,445,666✔
182
  pTbReq->name = taosStrdup(tname);
11,436,216✔
183
  if (!pTbReq->name) return terrno;
11,441,831✔
184
  pTbReq->ctb.suid = suid;
11,433,956✔
185
  pTbReq->ctb.tagNum = tagNum;
11,434,545✔
186
  if (sname) {
11,435,805✔
187
    pTbReq->ctb.stbName = taosStrdup(sname);
10,812,810✔
188
    if (!pTbReq->ctb.stbName) return terrno;
10,814,690✔
189
  }
190
  pTbReq->ctb.tagName = taosArrayDup(tagName, NULL);
11,436,110✔
191
  if (!pTbReq->ctb.tagName) return terrno;
11,444,322✔
192
  pTbReq->ttl = ttl;
11,435,482✔
193
  pTbReq->commentLen = -1;
11,437,098✔
194

195
  return TSDB_CODE_SUCCESS;
11,433,003✔
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) {
549,396,302✔
205
  SSchema* pSchemas = getTableColumnSchema(pTableMeta);
549,396,302✔
206
  int32_t  code = 0;
549,394,834✔
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;
549,417,392✔
215
}
216

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

219
int32_t insInitBoundColsInfo(int32_t numOfBound, SBoundColInfo* pInfo) {
569,656,498✔
220
  pInfo->numOfCols = numOfBound;
569,656,498✔
221
  pInfo->numOfBound = numOfBound;
569,673,387✔
222
  pInfo->hasBoundCols = false;
569,671,118✔
223
  pInfo->mixTagsCols = false;
569,664,953✔
224
  pInfo->pColIndex = taosMemoryCalloc(numOfBound, sizeof(int16_t));
569,669,519✔
225
  if (NULL == pInfo->pColIndex) {
569,663,215✔
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;
569,662,526✔
232
}
233

234
void insResetBoundColsInfo(SBoundColInfo* pInfo) {
2,430✔
235
  pInfo->numOfBound = pInfo->numOfCols;
2,430✔
236
  pInfo->hasBoundCols = false;
2,430✔
237
  pInfo->mixTagsCols = false;
2,430✔
238
  for (int32_t i = 0; i < pInfo->numOfCols; ++i) {
12,150✔
239
    pInfo->pColIndex[i] = i;
9,720✔
240
  }
241
}
2,430✔
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;
20,810,472✔
247
  }
248

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

253
  if (tRowKeyCompare(rowKey, &pTableCxt->lastKey) == 0) {
2,147,483,647✔
254
    pTableCxt->duplicateTs = true;
1,725,010✔
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,
551,932,544✔
263
                                  bool colMode, bool ignoreColVals) {
264
  STableDataCxt* pTableCxt = taosMemoryCalloc(1, sizeof(STableDataCxt));
551,932,544✔
265
  if (NULL == pTableCxt) {
551,897,746✔
266
    *pOutput = NULL;
×
267
    return terrno;
×
268
  }
269

270
  int32_t code = TSDB_CODE_SUCCESS;
551,897,746✔
271

272
  pTableCxt->lastKey = (SRowKey){0};
551,897,746✔
273
  pTableCxt->ordered = true;
551,909,007✔
274
  pTableCxt->duplicateTs = false;
551,911,732✔
275

276
  pTableCxt->pMeta = tableMetaDup(pTableMeta);
551,917,659✔
277
  if (NULL == pTableCxt->pMeta) {
551,943,395✔
278
    code = TSDB_CODE_OUT_OF_MEMORY;
×
279
  }
280
  if (TSDB_CODE_SUCCESS == code) {
551,943,794✔
281
    pTableCxt->pSchema =
551,938,965✔
282
        tBuildTSchema(getTableColumnSchema(pTableMeta), pTableMeta->tableInfo.numOfColumns, pTableMeta->sversion);
551,943,525✔
283
    if (NULL == pTableCxt->pSchema) {
551,940,339✔
284
      code = TSDB_CODE_OUT_OF_MEMORY;
×
285
    }
286
  }
287
  pTableCxt->hasBlob = schemaHasBlob(pTableCxt->pSchema);
551,938,919✔
288

289
  if (TSDB_CODE_SUCCESS == code) {
551,941,481✔
290
    code = insInitBoundColsInfo(pTableMeta->tableInfo.numOfColumns, &pTableCxt->boundColsInfo);
551,936,257✔
291
  }
292
  if (TSDB_CODE_SUCCESS == code && !ignoreColVals) {
551,936,801✔
293
    pTableCxt->pValues = taosArrayInit(pTableMeta->tableInfo.numOfColumns, sizeof(SColVal));
549,200,880✔
294
    if (NULL == pTableCxt->pValues) {
549,201,246✔
295
      code = terrno;
×
296
    } else {
297
      code = initColValues(pTableMeta, pTableCxt->pValues);
549,201,100✔
298
    }
299
  }
300
  if (TSDB_CODE_SUCCESS == code) {
551,950,313✔
301
    pTableCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
551,953,080✔
302
    if (NULL == pTableCxt->pData) {
551,921,924✔
303
      code = terrno;
×
304
    } else {
305
      pTableCxt->pData->flags = (pCreateTbReq != NULL && NULL != *pCreateTbReq) ? SUBMIT_REQ_AUTO_CREATE_TABLE : 0;
551,945,071✔
306
      pTableCxt->pData->flags |= colMode ? SUBMIT_REQ_COLUMN_DATA_FORMAT : 0;
551,941,394✔
307
      pTableCxt->pData->suid = pTableMeta->suid;
551,947,403✔
308
      pTableCxt->pData->uid = pTableMeta->uid;
551,928,339✔
309
      pTableCxt->pData->sver = pTableMeta->sversion;
551,937,135✔
310
      pTableCxt->pData->pCreateTbReq = pCreateTbReq != NULL ? *pCreateTbReq : NULL;
551,932,648✔
311
      int8_t flag = pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
551,926,021✔
312
      if (pCreateTbReq != NULL) *pCreateTbReq = NULL;
551,929,179✔
313
      if (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
551,921,044✔
314
        pTableCxt->pData->aCol = taosArrayInit(128, sizeof(SColData));
3,292,836✔
315
        if (NULL == pTableCxt->pData->aCol) {
3,288,408✔
316
          code = terrno;
×
317
        }
318
      } else {
319
        pTableCxt->pData->aRowP = taosArrayInit(128, POINTER_BYTES);
548,628,320✔
320
        if (NULL == pTableCxt->pData->aRowP) {
548,641,654✔
321
          code = terrno;
×
322
        }
323
      }
324
    }
325
  }
326
  if (TSDB_CODE_SUCCESS == code) {
551,932,337✔
327
    *pOutput = pTableCxt;
551,932,337✔
328
    parserDebug("uid:%" PRId64 ", create table data context, code:%d, vgId:%d", pTableMeta->uid, code,
551,935,231✔
329
                pTableMeta->vgId);
330
  } else {
331
    insDestroyTableDataCxt(pTableCxt);
×
332
  }
333

334
  return code;
551,932,676✔
335
}
336

337
static int32_t rebuildTableData(SSubmitTbData* pSrc, SSubmitTbData** pDst, int8_t hasBlob) {
3,927,526✔
338
  int32_t        code = TSDB_CODE_SUCCESS;
3,927,526✔
339
  SSubmitTbData* pTmp = taosMemoryCalloc(1, sizeof(SSubmitTbData));
3,927,526✔
340
  if (NULL == pTmp) {
3,926,605✔
341
    code = terrno;
×
342
  } else {
343
    pTmp->flags = pSrc->flags;
3,926,605✔
344
    pTmp->suid = pSrc->suid;
3,927,865✔
345
    pTmp->uid = pSrc->uid;
3,927,235✔
346
    pTmp->sver = pSrc->sver;
3,927,235✔
347
    pTmp->pCreateTbReq = NULL;
3,926,290✔
348
    if (pTmp->flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
3,927,235✔
349
      if (pSrc->pCreateTbReq) {
3,784,095✔
350
        code = cloneSVreateTbReq(pSrc->pCreateTbReq, &pTmp->pCreateTbReq);
3,783,465✔
351
      } else {
352
        pTmp->flags &= ~SUBMIT_REQ_AUTO_CREATE_TABLE;
×
353
      }
354
    }
355
    if (TSDB_CODE_SUCCESS == code) {
3,927,800✔
356
      if (pTmp->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
3,927,800✔
357
        pTmp->aCol = taosArrayInit(128, sizeof(SColData));
3,298,246✔
358
        if (NULL == pTmp->aCol) {
3,298,876✔
359
          code = terrno;
×
360
          taosMemoryFree(pTmp);
×
361
        }
362
      } else {
363
        pTmp->aRowP = taosArrayInit(128, POINTER_BYTES);
629,595✔
364
        if (NULL == pTmp->aRowP) {
630,249✔
365
          code = terrno;
×
366
          taosMemoryFree(pTmp);
×
367
        }
368

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

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

380
  taosMemoryFree(pSrc);
3,928,495✔
381
  if (TSDB_CODE_SUCCESS == code) {
3,927,865✔
382
    *pDst = pTmp;
3,927,235✔
383
  }
384

385
  return code;
3,927,824✔
386
}
387

388
static void resetColValues(SArray* pValues) {
32,421,291✔
389
  int32_t num = taosArrayGetSize(pValues);
32,421,291✔
390
  for (int32_t i = 0; i < num; ++i) {
365,177,385✔
391
    SColVal* pVal = taosArrayGet(pValues, i);
332,756,094✔
392
    pVal->flag = CV_FLAG_NONE;
332,756,094✔
393
  }
394
}
32,421,291✔
395

396
int32_t insGetTableDataCxt(SHashObj* pHash, void* id, int32_t idLen, STableMeta* pTableMeta,
1,612,008,107✔
397
                           SVCreateTbReq** pCreateTbReq, STableDataCxt** pTableCxt, bool colMode, bool ignoreColVals) {
398
  STableDataCxt** tmp = (STableDataCxt**)taosHashGet(pHash, id, idLen);
1,612,008,107✔
399
  if (NULL != tmp) {
1,612,024,112✔
400
    *pTableCxt = *tmp;
1,060,077,186✔
401
    if (!ignoreColVals) {
1,060,077,186✔
402
      resetColValues((*pTableCxt)->pValues);
32,421,291✔
403
    }
404
    return TSDB_CODE_SUCCESS;
1,060,077,186✔
405
  }
406
  int32_t code = createTableDataCxt(pTableMeta, pCreateTbReq, pTableCxt, colMode, ignoreColVals);
551,946,926✔
407
  if (TSDB_CODE_SUCCESS == code) {
551,940,134✔
408
    void* pData = *pTableCxt;  // deal scan coverity
551,942,043✔
409
    code = taosHashPut(pHash, id, idLen, &pData, POINTER_BYTES);
551,946,469✔
410
  }
411

412
  if (TSDB_CODE_SUCCESS != code) {
551,951,446✔
413
    insDestroyTableDataCxt(*pTableCxt);
×
414
  }
415
  return code;
551,944,540✔
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,915,155,517✔
425
  }
426
}
427

428
void insDestroyTableDataCxt(STableDataCxt* pTableCxt) {
554,328,219✔
429
  if (NULL == pTableCxt) {
554,328,219✔
430
    return;
×
431
  }
432

433
  taosMemoryFreeClear(pTableCxt->pMeta);
554,328,219✔
434
  tDestroyTSchema(pTableCxt->pSchema);
554,326,270✔
435
  qDestroyBoundColInfo(&pTableCxt->boundColsInfo);
554,326,002✔
436
  taosArrayDestroyEx(pTableCxt->pValues, destroyColVal);
554,328,140✔
437
  if (pTableCxt->pData) {
554,325,090✔
438
    tDestroySubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
7,001,174✔
439
    taosMemoryFree(pTableCxt->pData);
7,000,836✔
440
  }
441
  taosMemoryFree(pTableCxt);
554,324,469✔
442
}
443

444
static void destroyColValSml(void* p) {
9,780,464✔
445
  SColVal* pVal = p;
9,780,464✔
446
  if (TSDB_DATA_TYPE_NCHAR == pVal->value.type || TSDB_DATA_TYPE_GEOMETRY == pVal->value.type ||
9,780,464✔
447
      TSDB_DATA_TYPE_VARBINARY == pVal->value.type) {
9,558,425✔
448
    taosMemoryFreeClear(pVal->value.pData);
227,224✔
449
  }
450
}
9,782,309✔
451

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

457
  taosMemoryFreeClear(pTableCxt->pMeta);
697,486✔
458
  tDestroyTSchema(pTableCxt->pSchema);
697,404✔
459
  qDestroyBoundColInfo(&pTableCxt->boundColsInfo);
697,404✔
460
  taosArrayDestroyEx(pTableCxt->pValues, destroyColValSml);
697,457✔
461
  if (pTableCxt->pData) {
697,375✔
462
    tDestroySubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
697,375✔
463
    taosMemoryFree(pTableCxt->pData);
697,281✔
464
  }
465
  taosMemoryFree(pTableCxt);
697,445✔
466
}
467

468
void insDestroyVgroupDataCxt(SVgroupDataCxt* pVgCxt) {
526,411,059✔
469
  if (NULL == pVgCxt) {
526,411,059✔
470
    return;
×
471
  }
472

473
  tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
526,411,059✔
474
  taosMemoryFree(pVgCxt->pData);
526,401,140✔
475

476
  taosMemoryFree(pVgCxt);
526,402,866✔
477
}
478

479
void insDestroyVgroupDataCxtList(SArray* pVgCxtList) {
1,023,440,454✔
480
  if (NULL == pVgCxtList) {
1,023,440,454✔
481
    return;
511,800,641✔
482
  }
483

484
  size_t size = taosArrayGetSize(pVgCxtList);
511,639,813✔
485
  for (int32_t i = 0; i < size; i++) {
1,038,047,892✔
486
    void* p = taosArrayGetP(pVgCxtList, i);
526,412,940✔
487
    insDestroyVgroupDataCxt(p);
526,413,070✔
488
  }
489

490
  taosArrayDestroy(pVgCxtList);
511,634,952✔
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) {
511,580,119✔
509
  if (NULL == pTableCxtHash) {
511,580,119✔
510
    return;
3,266,135✔
511
  }
512

513
  void** p = taosHashIterate(pTableCxtHash, NULL);
508,313,984✔
514
  while (p) {
1,056,215,053✔
515
    insDestroyTableDataCxt(*(STableDataCxt**)p);
547,899,365✔
516

517
    p = taosHashIterate(pTableCxtHash, p);
547,896,861✔
518
  }
519

520
  taosHashCleanup(pTableCxtHash);
508,315,688✔
521
}
522

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

528
  void** p = taosHashIterate(pTableCxtHash, NULL);
951,844✔
529
  while (p) {
1,649,219✔
530
    insDestroyTableDataCxtSml(*(STableDataCxt**)p);
697,416✔
531

532
    p = taosHashIterate(pTableCxtHash, p);
697,281✔
533
  }
534

535
  taosHashCleanup(pTableCxtHash);
951,803✔
536
}
537

538
static int32_t fillVgroupDataCxt(STableDataCxt* pTableCxt, SVgroupDataCxt* pVgCxt, bool isRebuild, bool clear) {
553,027,833✔
539
  int32_t code = 0;
553,027,833✔
540
  if (NULL == pVgCxt->pData->aSubmitTbData) {
553,027,833✔
541
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
526,034,723✔
542
    if (pVgCxt->pData->aSubmitTbData == NULL) {
526,035,137✔
543
      return terrno;
×
544
    }
545
    if (pTableCxt->hasBlob) {
526,035,783✔
546
      pVgCxt->pData->aSubmitBlobData = taosArrayInit(128, sizeof(SBlobSet*));
21,579✔
547
      if (NULL == pVgCxt->pData->aSubmitBlobData) {
21,579✔
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;
553,033,297✔
555

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

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

568
  if (isRebuild) {
553,028,232✔
569
    code = rebuildTableData(pTableCxt->pData, &pTableCxt->pData, pTableCxt->hasBlob);
3,927,550✔
570
  } else if (clear) {
549,100,682✔
571
    taosMemoryFreeClear(pTableCxt->pData);
547,408,341✔
572
  }
573
  parserDebug("uid:%" PRId64 ", add table data context to vgId:%d", pTableCxt->pMeta->uid, pVgCxt->vgId);
553,032,639✔
574

575
  return code;
553,031,885✔
576
}
577

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

589
  pVgCxt->vgId = vgId;
526,505,018✔
590
  int32_t code = taosHashPut(pVgroupHash, &pVgCxt->vgId, sizeof(pVgCxt->vgId), &pVgCxt, POINTER_BYTES);
526,504,815✔
591
  if (TSDB_CODE_SUCCESS == code) {
526,506,203✔
592
    if (NULL == taosArrayPush(pVgroupList, &pVgCxt)) {
526,506,802✔
593
      code = terrno;
×
594
      insDestroyVgroupDataCxt(pVgCxt);
×
595
      return code;
10✔
596
    }
597
    //    uDebug("td23101 2vgId:%d, uid:%" PRIu64, pVgCxt->vgId, pTableCxt->pMeta->uid);
598
    *pOutput = pVgCxt;
526,506,802✔
599
  } else {
600
    insDestroyVgroupDataCxt(pVgCxt);
×
601
  }
602
  return code;
526,506,249✔
603
}
604

605
int insColDataComp(const void* lp, const void* rp) {
14,780,879✔
606
  SColData* pLeft = (SColData*)lp;
14,780,879✔
607
  SColData* pRight = (SColData*)rp;
14,780,879✔
608
  if (pLeft->cid < pRight->cid) {
14,780,879✔
609
    return -1;
14,741,252✔
610
  } else if (pLeft->cid > pRight->cid) {
44,436✔
611
    return 1;
44,436✔
612
  }
613

UNCOV
614
  return 0;
×
615
}
616

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

623
  SVgroupInfo      vgInfo = {0};
9,914✔
624
  SRequestConnInfo conn = {.pTrans = pBuildInfo->transport,
19,640✔
625
                           .requestId = pBuildInfo->requestId,
9,914✔
626
                           .requestObjRefId = pBuildInfo->requestSelf,
9,914✔
627
                           .mgmtEps = pBuildInfo->mgmtEpSet};
628

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

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

639
  return TSDB_CODE_SUCCESS;
9,914✔
640
}
641

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

647
  if (pTbData->getFromHash) {
2,551,651✔
648
    pTbInfo = (STableVgUid*)tSimpleHashGet(pBuildInfo->pTableHash, pTbData->tbName, strlen(pTbData->tbName));
2,492,417✔
649
  }
650

651
  if (NULL == pTbInfo) {
2,551,544✔
652
    SName sname;
47,383✔
653
    code = qCreateSName2(&sname, pTbData->tbName, pBuildInfo->acctId, pBuildInfo->dbname, NULL, 0);
59,999✔
654
    if (TSDB_CODE_SUCCESS != code) {
59,999✔
655
      return code;
400✔
656
    }
657

658
    STableMeta*      pTableMeta = NULL;
59,999✔
659
    SRequestConnInfo conn = {.pTrans = pBuildInfo->transport,
107,383✔
660
                             .requestId = pBuildInfo->requestId,
59,999✔
661
                             .requestObjRefId = pBuildInfo->requestSelf,
60,000✔
662
                             .mgmtEps = pBuildInfo->mgmtEpSet};
663
    code = catalogGetTableMeta((SCatalog*)pBuildInfo->pCatalog, &conn, &sname, &pTableMeta);
60,000✔
664

665
    if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
59,939✔
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) {
59,539✔
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;
59,539✔
676
    *vgId = pTableMeta->vgId;
59,540✔
677
    *suid = pTableMeta->suid;
59,579✔
678

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

685
    taosMemoryFree(pTableMeta);
59,600✔
686
  } else {
687
    *uid = pTbInfo->uid;
2,491,545✔
688
    *vgId = pTbInfo->vgid;
2,491,624✔
689
    *suid = pTbInfo->suid;
2,491,507✔
690
  }
691

692
  return code;
2,551,077✔
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,
858,232✔
707
                                            char* tbname) {
708
  if (NULL == pVgCxt->pData->aSubmitTbData) {
858,232✔
709
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
469,384✔
710
    if (NULL == pVgCxt->pData->aSubmitTbData) {
469,694✔
711
      return terrno;
×
712
    }
713
    if (pTbCtx->hasBlob) {
469,694✔
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;
858,578✔
722
  SArray** rowP = NULL;
858,578✔
723

724
  rowP = (SArray**)tSimpleHashGet(pTableNameHash, tbname, strlen(tbname));
858,578✔
725

726
  if (rowP != NULL && *rowP != NULL) {
858,568✔
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) {
858,568✔
762
    pTbCtx->pData->pBlobSet = NULL;  // if no blob, set it to NULL
858,582✔
763
  }
764

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

769
  if (pTbCtx->hasBlob) {
858,562✔
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*));
858,517✔
778

779
  if (code != TSDB_CODE_SUCCESS) {
858,477✔
780
    return code;
×
781
  }
782

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

785
  return TSDB_CODE_SUCCESS;
858,544✔
786
}
787

788
int32_t insAppendStmtTableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
1,693,016✔
789
                                  SStbInterlaceInfo* pBuildInfo) {
790
  int32_t  code = TSDB_CODE_SUCCESS;
1,693,016✔
791
  uint64_t uid;
1,681,354✔
792
  int32_t  vgId;
1,681,527✔
793
  uint64_t suid;
1,681,570✔
794

795
  pTbCtx->pData->aRowP = pTbData->aCol;
1,693,275✔
796

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

802
  pTbCtx->pMeta->vgId = vgId;
1,693,020✔
803
  pTbCtx->pMeta->uid = uid;
1,693,153✔
804
  pTbCtx->pData->uid = uid;
1,693,105✔
805

806
  if (!pTbCtx->ordered) {
1,693,198✔
807
    code = tRowSort(pTbCtx->pData->aRowP);
10✔
808
  }
809
  if (code == TSDB_CODE_SUCCESS && (!pTbCtx->ordered || pTbCtx->duplicateTs)) {
1,693,061✔
810
    code = tRowMerge(pTbCtx->pData->aRowP, pTbCtx->pSchema, 0);
50✔
811
  }
812

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

817
  SVgroupDataCxt* pVgCxt = NULL;
1,692,931✔
818
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
1,692,974✔
819
  if (NULL == pp) {
1,693,490✔
820
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
296,091✔
821
    if (NULL == pp) {
296,091✔
822
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
296,091✔
823
    } else {
824
      pVgCxt = *(SVgroupDataCxt**)pp;
×
825
    }
826
  } else {
827
    pVgCxt = *(SVgroupDataCxt**)pp;
1,397,399✔
828
  }
829

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

834
  if (taosArrayGetSize(pVgCxt->pData->aSubmitTbData) >= 20000) {
1,692,889✔
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,692,801✔
842
}
843

844
int32_t insAppendStmt2TableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
858,390✔
845
                                   SStbInterlaceInfo* pBuildInfo, SVCreateTbReq* ctbReq) {
846
  int32_t  code = TSDB_CODE_SUCCESS;
858,390✔
847
  uint64_t uid;
269,632✔
848
  int32_t  vgId;
269,632✔
849
  uint64_t suid;
269,632✔
850

851
  pTbCtx->pData->aRowP = pTbData->aCol;
858,390✔
852
  pTbCtx->pData->pBlobSet = pTbData->pBlobSet;
858,390✔
853

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

873
    pTbCtx->pMeta->vgId = vgId;
858,024✔
874
    pTbCtx->pMeta->uid = uid;
857,986✔
875
    pTbCtx->pData->uid = uid;
858,132✔
876
    pTbCtx->pData->pCreateTbReq = NULL;
858,132✔
877

878
    if (ctbReq != NULL) {
858,096✔
879
      tdDestroySVCreateTbReq(ctbReq);
880
      taosMemoryFree(ctbReq);
499,588✔
881
      ctbReq = NULL;
499,626✔
882
    }
883
  }
884

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

901
  if (TSDB_CODE_SUCCESS != code) {
858,416✔
902
    return code;
×
903
  }
904

905
  SVgroupDataCxt* pVgCxt = NULL;
858,416✔
906
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
858,416✔
907
  if (NULL == pp) {
858,454✔
908
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
469,534✔
909
    if (NULL == pp) {
469,572✔
910
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
469,572✔
911
    } else {
912
      pVgCxt = *(SVgroupDataCxt**)pp;
×
913
    }
914
  } else {
915
    pVgCxt = *(SVgroupDataCxt**)pp;
388,920✔
916
  }
917

918
  if (code == TSDB_CODE_SUCCESS) {
858,356✔
919
    code = checkAndMergeSVgroupDataCxtByTbname(pTbCtx, pVgCxt, pBuildInfo->pTableRowDataHash, pTbData->tbName);
858,336✔
920
  }
921

922
  if (taosArrayGetSize(pVgCxt->pData->aSubmitTbData) >= 20000) {
858,437✔
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;
858,410✔
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) {
511,028,270✔
997
  SHashObj* pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
511,028,270✔
998
  SArray*   pVgroupList = taosArrayInit(8, POINTER_BYTES);
511,031,776✔
999
  if (NULL == pVgroupHash || NULL == pVgroupList) {
511,031,659✔
UNCOV
1000
    taosHashCleanup(pVgroupHash);
×
1001
    taosArrayDestroy(pVgroupList);
×
1002
    return terrno;
×
1003
  }
1004

1005
  int32_t code = TSDB_CODE_SUCCESS;
511,031,659✔
1006
  bool    colFormat = false;
511,031,659✔
1007

1008
  void* p = taosHashIterate(pTableHash, NULL);
511,031,659✔
1009
  if (p) {
511,036,591✔
1010
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
511,036,591✔
1011
    colFormat = (0 != (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT));
511,036,591✔
1012
  }
1013

1014
  while (TSDB_CODE_SUCCESS == code && NULL != p) {
1,062,377,336✔
1015
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
551,340,389✔
1016
    if (colFormat) {
551,340,389✔
1017
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
3,299,308✔
1018
      if (pCol && pCol->nVal <= 0) {
3,299,308✔
1019
        p = taosHashIterate(pTableHash, p);
117✔
1020
        continue;
117✔
1021
      }
1022

1023
      if (pTableCxt->pData->pCreateTbReq) {
3,298,876✔
1024
        pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
3,166,033✔
1025
      }
1026
      int8_t isBlob = IS_STR_DATA_BLOB(pCol->type) ? 1 : 0;
3,298,561✔
1027
      if (isBlob == 0) {
3,298,246✔
1028
        taosArraySort(pTableCxt->pData->aCol, insColDataComp);
3,298,246✔
1029
        code = tColDataSortMerge(&pTableCxt->pData->aCol);
3,298,561✔
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) {
548,041,081✔
1043
        if (!pTableCxt->ordered) {
548,020,034✔
1044
          code = tRowSort(pTableCxt->pData->aRowP);
1,357,863✔
1045
        }
1046
        if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
548,019,712✔
1047
          code = tRowMerge(pTableCxt->pData->aRowP, pTableCxt->pSchema, PREFER_NON_NULL);
1,486,857✔
1048
        }
1049
      } else {
1050
        if (!pTableCxt->ordered) {
21,579✔
1051
          code = tRowSortWithBlob(pTableCxt->pData->aRowP, pTableCxt->pSchema, pTableCxt->pData->pBlobSet);
743✔
1052
        }
1053
        if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
21,579✔
1054
          code = tRowMergeWithBlob(pTableCxt->pData->aRowP, pTableCxt->pSchema, pTableCxt->pData->pBlobSet, 0);
743✔
1055
        }
1056
      }
1057
    }
1058

1059
    if (TSDB_CODE_SUCCESS == code) {
551,339,905✔
1060
      SVgroupDataCxt* pVgCxt = NULL;
551,339,905✔
1061
      int32_t         vgId = pTableCxt->pMeta->vgId;
551,339,408✔
1062
      void**          pp = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
551,340,004✔
1063
      if (NULL == pp) {
551,339,806✔
1064
        code = createVgroupDataCxt(vgId, pVgroupHash, pVgroupList, &pVgCxt);
525,741,213✔
1065
      } else {
1066
        pVgCxt = *(SVgroupDataCxt**)pp;
25,598,593✔
1067
      }
1068
      if (TSDB_CODE_SUCCESS == code) {
551,338,668✔
1069
        code = fillVgroupDataCxt(pTableCxt, pVgCxt, isRebuild, true);
551,339,535✔
1070
      }
1071
    }
1072
    if (TSDB_CODE_SUCCESS == code) {
551,338,861✔
1073
      p = taosHashIterate(pTableHash, p);
551,338,861✔
1074
    }
1075
  }
1076

1077
  taosHashCleanup(pVgroupHash);
511,036,947✔
1078
  if (TSDB_CODE_SUCCESS == code) {
511,032,731✔
1079
    *pVgDataBlocks = pVgroupList;
511,033,361✔
1080
  } else {
1081
    insDestroyVgroupDataCxtList(pVgroupList);
×
1082
  }
1083

1084
  return code;
511,033,279✔
1085
}
1086

1087
static int32_t buildSubmitReq(int32_t vgId, SSubmitReq2* pReq, void** pData, uint32_t* pLen) {
526,498,280✔
1088
  int32_t  code = TSDB_CODE_SUCCESS;
526,498,280✔
1089
  uint32_t len = 0;
526,498,280✔
1090
  void*    pBuf = NULL;
526,498,280✔
1091
  tEncodeSize(tEncodeSubmitReq, pReq, len, code);
526,498,280✔
1092
  if (TSDB_CODE_SUCCESS == code) {
526,502,196✔
1093
    SEncoder encoder;
523,895,608✔
1094
    len += sizeof(SSubmitReq2Msg);
526,505,647✔
1095
    pBuf = taosMemoryMalloc(len);
526,505,647✔
1096
    if (NULL == pBuf) {
526,497,246✔
1097
      return terrno;
×
1098
    }
1099
    ((SSubmitReq2Msg*)pBuf)->header.vgId = htonl(vgId);
526,497,246✔
1100
    ((SSubmitReq2Msg*)pBuf)->header.contLen = htonl(len);
526,499,229✔
1101
    ((SSubmitReq2Msg*)pBuf)->version = htobe64(1);
526,500,335✔
1102
    tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SSubmitReq2Msg)), len - sizeof(SSubmitReq2Msg));
526,506,405✔
1103
    code = tEncodeSubmitReq(&encoder, pReq);
526,505,201✔
1104
    tEncoderClear(&encoder);
526,506,306✔
1105
  }
1106

1107
  if (TSDB_CODE_SUCCESS == code) {
526,504,297✔
1108
    *pData = pBuf;
526,504,297✔
1109
    *pLen = len;
526,505,489✔
1110
  } else {
1111
    taosMemoryFree(pBuf);
×
1112
  }
1113
  return code;
526,501,296✔
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) {
526,500,075✔
1124
  int32_t code = 0;
526,500,075✔
1125
  if (p->raw) {
526,500,075✔
1126
    return TSDB_CODE_SUCCESS;  // no blob data in raw mode
×
1127
  }
1128

1129
  if (p->aSubmitBlobData != NULL) {
526,503,961✔
1130
    for (int32_t i = 0; i < taosArrayGetSize(p->aSubmitTbData); i++) {
43,158✔
1131
      SSubmitTbData* pSubmitTbData = taosArrayGet(p->aSubmitTbData, i);
21,579✔
1132
      SBlobSet**     ppBlob = taosArrayGet(p->aSubmitBlobData, i);
21,579✔
1133
      SBlobSet*      pBlob = ppBlob ? *ppBlob : NULL;
21,579✔
1134
      int32_t        nrow = taosArrayGetSize(pSubmitTbData->aRowP);
21,579✔
1135
      int32_t        nblob = 0;
21,579✔
1136
      if (nrow > 0 && pBlob) {
21,579✔
1137
        nblob = taosArrayGetSize(pBlob->pSeqTable);
21,579✔
1138
      }
1139
      uTrace("blob %p row size %d, pData size %d", pBlob, nblob, nrow);
21,579✔
1140
      pSubmitTbData->pBlobSet = pBlob;
21,579✔
1141
      if (ppBlob != NULL) *ppBlob = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
21,579✔
1142
    }
1143
  } else {
1144
    for (int32_t i = 0; i < taosArrayGetSize(p->aSubmitTbData); i++) {
1,080,345,951✔
1145
      SSubmitTbData* pSubmitTbData = taosArrayGet(p->aSubmitTbData, i);
553,861,791✔
1146
      pSubmitTbData->pBlobSet = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
553,865,998✔
1147
    }
1148
  }
1149

1150
  return code;
526,505,459✔
1151
}
1152
int32_t insBuildVgDataBlocks(SHashObj* pVgroupsHashObj, SArray* pVgDataCxtList, SArray** pVgDataBlocks, bool append) {
511,733,231✔
1153
  size_t  numOfVg = taosArrayGetSize(pVgDataCxtList);
511,733,231✔
1154
  SArray* pDataBlocks = (append && *pVgDataBlocks) ? *pVgDataBlocks : taosArrayInit(numOfVg, POINTER_BYTES);
511,735,049✔
1155
  if (NULL == pDataBlocks) {
511,730,970✔
1156
    return TSDB_CODE_OUT_OF_MEMORY;
×
1157
  }
1158

1159
  int32_t code = TSDB_CODE_SUCCESS;
511,730,970✔
1160
  for (size_t i = 0; TSDB_CODE_SUCCESS == code && i < numOfVg; ++i) {
1,038,234,333✔
1161
    SVgroupDataCxt* src = taosArrayGetP(pVgDataCxtList, i);
526,502,293✔
1162
    if (taosArrayGetSize(src->pData->aSubmitTbData) <= 0) {
526,502,827✔
1163
      continue;
×
1164
    }
1165
    SVgDataBlocks* dst = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
526,502,581✔
1166
    if (NULL == dst) {
526,497,657✔
1167
      code = terrno;
×
1168
    }
1169

1170
    if (TSDB_CODE_SUCCESS == code) {
526,497,657✔
1171
      dst->numOfTables = taosArrayGetSize(src->pData->aSubmitTbData);
526,497,300✔
1172
      code = taosHashGetDup(pVgroupsHashObj, (const char*)&src->vgId, sizeof(src->vgId), &dst->vg);
526,502,459✔
1173
    }
1174
    if (TSDB_CODE_SUCCESS == code) {
526,505,607✔
1175
      code = insResetBlob(src->pData);
526,505,607✔
1176
    }
1177

1178
    if (TSDB_CODE_SUCCESS == code) {
526,504,974✔
1179
      code = buildSubmitReq(src->vgId, src->pData, &dst->pData, &dst->size);
526,504,974✔
1180
    }
1181
    if (TSDB_CODE_SUCCESS == code) {
526,503,178✔
1182
      code = (NULL == taosArrayPush(pDataBlocks, &dst) ? terrno : TSDB_CODE_SUCCESS);
526,504,124✔
1183
    }
1184
    if (TSDB_CODE_SUCCESS != code) {
526,503,677✔
1185
      destroyVgDataBlocks(dst);
×
1186
    }
1187
  }
1188

1189
  if (append) {
511,732,040✔
1190
    if (NULL == *pVgDataBlocks) {
700,702✔
1191
      *pVgDataBlocks = pDataBlocks;
700,815✔
1192
    }
1193
    return code;
700,743✔
1194
  }
1195

1196
  if (TSDB_CODE_SUCCESS == code) {
511,031,338✔
1197
    *pVgDataBlocks = pDataBlocks;
511,027,401✔
1198
  } else {
1199
    taosArrayDestroyP(pDataBlocks, destroyVgDataBlocks);
4,268✔
1200
  }
1201

1202
  return code;
511,033,918✔
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) {
159,464✔
1216
  if (*fields != pColSchema->type) {
159,464✔
1217
    if (errstr != NULL) {
277✔
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};
277✔
1222
      snprintf(buf, sizeof(buf), "column type not equal, name:%s, schema type:%s, data type:%s", pColSchema->name,
554✔
1223
               tDataTypes[pColSchema->type].name, tDataTypes[*fields].name);
554✔
1224
      uError("checkSchema %s", buf);
277✔
1225
    }
1226
    return TSDB_CODE_INVALID_PARA;
277✔
1227
  }
1228

1229
  if (IS_DECIMAL_TYPE(pColSchema->type)) {
159,187✔
1230
    uint8_t precision = 0, scale = 0;
277✔
1231
    decimalFromTypeMod(pColExtSchema->typeMod, &precision, &scale);
277✔
1232
    uint8_t precisionData = 0, scaleData = 0;
277✔
1233
    int32_t bytes = *(int32_t*)(fields + sizeof(int8_t));
277✔
1234
    extractDecimalTypeInfoFromBytes(&bytes, &precisionData, &scaleData);
277✔
1235
    if (precision != precisionData || scale != scaleData) {
277✔
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;
277✔
1255
  }
1256

1257
  if (IS_VAR_DATA_TYPE(pColSchema->type)) {
158,910✔
1258
    int32_t bytes = *(int32_t*)(fields + sizeof(int8_t));
27,936✔
1259
    if (IS_STR_DATA_BLOB(pColSchema->type)) {
27,936✔
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) {
27,936✔
1267
        if (errstr != NULL) {
277✔
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};
277✔
1274
          snprintf(buf, sizeof(buf),
554✔
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,
831✔
1277
                   *(int32_t*)(fields + sizeof(int8_t)));
277✔
1278
          uError("checkSchema %s", buf);
277✔
1279
        }
1280
        return TSDB_CODE_INVALID_PARA;
277✔
1281
      }
1282
    }
1283
  }
1284

1285
  if (!IS_VAR_DATA_TYPE(pColSchema->type) && *(int32_t*)(fields + sizeof(int8_t)) != pColSchema->bytes) {
158,633✔
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;
158,633✔
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,
34,721✔
1341
                     int numFields, bool needChangeLength, char* errstr, int32_t errstrLen, bool raw) {
1342
  int       ret = 0;
34,721✔
1343
  int8_t    hasBlob = 0;
34,721✔
1344
  SBlobSet* pBlobSet = NULL;
34,721✔
1345
  if (data == NULL) {
34,721✔
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));
34,721✔
1351
  SVCreateTbReq* pCreateReqTmp = NULL;
34,721✔
1352
  if (tmp == NULL && pCreateTb != NULL) {
34,721✔
1353
    ret = cloneSVreateTbReq(pCreateTb, &pCreateReqTmp);
3,324✔
1354
    if (ret != TSDB_CODE_SUCCESS) {
3,324✔
1355
      uError("cloneSVreateTbReq error");
×
1356
      goto end;
×
1357
    }
1358
  }
1359

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

1368
  hasBlob = pTableCxt->hasBlob;
34,721✔
1369
  if (hasBlob && pTableCxt->pData->pBlobSet == NULL) {
34,721✔
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) {
34,721✔
1378
    uError("insGetTableDataCxt error");
×
1379
    goto end;
×
1380
  }
1381
  pBlobSet = pTableCxt->pData->pBlobSet;
34,721✔
1382

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

1392
  char* p = (char*)data;
34,721✔
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;
34,721✔
1396
  p += sizeof(int32_t);
34,721✔
1397
  p += sizeof(int32_t);
34,721✔
1398

1399
  int32_t numOfRows = *(int32_t*)p;
34,721✔
1400
  p += sizeof(int32_t);
34,721✔
1401

1402
  int32_t numOfCols = *(int32_t*)p;
34,721✔
1403
  p += sizeof(int32_t);
34,721✔
1404

1405
  p += sizeof(int32_t);
34,721✔
1406
  p += sizeof(uint64_t);
34,721✔
1407

1408
  int8_t* fields = (int8_t*)p;
34,721✔
1409
  if (*fields >= TSDB_DATA_TYPE_MAX || *fields < 0) {
34,721✔
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));
34,721✔
1415

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

1419
  char* pStart = p;
34,721✔
1420

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

1425
  if (tFields != NULL && numFields != numOfCols) {
34,721✔
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;
34,721✔
1432
  if (tFields == NULL) {
34,721✔
1433
    int32_t len = TMIN(numOfCols, boundInfo->numOfBound);
1,662✔
1434
    for (int j = 0; j < len; j++) {
4,986✔
1435
      SSchema*    pColSchema = &pSchema[j];
3,878✔
1436
      SSchemaExt* pColExtSchema = &pExtSchemas[j];
3,878✔
1437
      PRCESS_DATA(j, j)
3,878✔
1438
    }
1439
  } else {
1440
    for (int i = 0; i < numFields; i++) {
178,084✔
1441
      for (int j = 0; j < boundInfo->numOfBound; j++) {
1,325,892✔
1442
        SSchema*    pColSchema = &pSchema[j];
1,325,892✔
1443
        SSchemaExt* pColExtSchema = &pExtSchemas[j];
1,325,892✔
1444
        char*       fieldName = NULL;
1,325,892✔
1445
        if (raw) {
1,325,892✔
1446
          fieldName = ((SSchemaWrapper*)tFields)->pSchema[i].name;
1,324,507✔
1447
        } else {
1448
          fieldName = ((TAOS_FIELD*)tFields)[i].name;
1,385✔
1449
        }
1450
        if (strcmp(pColSchema->name, fieldName) == 0) {
1,325,892✔
1451
          PRCESS_DATA(i, j)
145,025✔
1452
          break;
145,025✔
1453
        }
1454
      }
1455
    }
1456
  }
1457

1458
  if (!hasTs) {
34,167✔
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) {
185,840✔
1466
    if (boundInfo->pColIndex[c] != -1) {
151,673✔
1467
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, c);
3,601✔
1468
      ret = tColDataAddValueByDataBlock(pCol, 0, 0, numOfRows, NULL, NULL);
3,601✔
1469
      if (ret != 0) {
3,601✔
1470
        goto end;
×
1471
      }
1472
    } else {
1473
      boundInfo->pColIndex[c] = c;  // restore for next block
148,072✔
1474
    }
1475
  }
1476

1477
end:
34,721✔
1478
  return ret;
34,721✔
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