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

taosdata / TDengine / #4934

21 Jan 2026 06:06AM UTC coverage: 66.691% (+0.02%) from 66.671%
#4934

push

travis-ci

web-flow
 enh:stmt support interval opt (#34335)

0 of 26 new or added lines in 3 files covered. (0.0%)

562 existing lines in 98 files now uncovered.

203203 of 304692 relevant lines covered (66.69%)

129902664.38 hits per line

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

75.38
/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) {
628,284,284✔
31
  bool inEscape = false;
628,284,284✔
32
  bool inQuote = false;
628,284,284✔
33
  char quotaStr = 0;
628,284,284✔
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;
227,954,558✔
38
    }
39

40
    if (*(pToken->z + i) == TS_ESCAPE_CHAR) {
2,147,483,647✔
41
      if (!inQuote) {
24,108,596✔
42
        inEscape = !inEscape;
24,109,518✔
43
      }
44
    }
45

46
    if (*(pToken->z + i) == '\'' || *(pToken->z + i) == '"') {
2,147,483,647✔
47
      if (!inEscape) {
6,218,292✔
48
        if (!inQuote) {
6,216,800✔
49
          quotaStr = *(pToken->z + i);
3,108,400✔
50
          inQuote = !inQuote;
3,108,400✔
51
        } else if (quotaStr == *(pToken->z + i)) {
3,108,400✔
52
          inQuote = !inQuote;
3,108,400✔
53
        }
54
      }
55
    }
56
  }
57

58
  return NULL;
400,344,855✔
59
}
60

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

68
  int32_t code = TSDB_CODE_SUCCESS;
628,283,844✔
69
  char*   p = tableNameGetPosition(pTableName, TS_PATH_DELIMITER[0]);
628,283,844✔
70

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

81
    char name[TSDB_DB_FNAME_LEN + TSDB_NAME_QUOTE] = {0};
227,953,797✔
82
    strncpy(name, pTableName->z, dbLen);
227,942,160✔
83
    int32_t actualDbLen = strdequote(name);
227,948,143✔
84

85
    // after dbname dequote
86
    if (actualDbLen <= 0) {
227,944,730✔
87
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg2);
1,203✔
88
    }
89
    if (actualDbLen >= TSDB_DB_NAME_LEN) {
227,943,562✔
90
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg5);
×
91
    }
92

93
    code = tNameSetDbName(pName, acctId, name, actualDbLen);
227,943,562✔
94
    if (code != TSDB_CODE_SUCCESS) {
227,953,552✔
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;
227,953,588✔
100
    if (tbLen <= 0) {
227,952,080✔
101
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
12,056✔
102
    }
103
    if (tbLen >= TSDB_TABLE_NAME_LEN + TSDB_NAME_QUOTE) {
227,940,024✔
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};
227,939,907✔
108
    strncpy(tbname, p + 1, tbLen);
227,940,104✔
109
    int32_t actualTbLen = strdequote(tbname);
227,938,530✔
110

111
    // after tbname dequote
112
    if (actualTbLen <= 0) {
227,955,586✔
113
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
4,177✔
114
    }
115
    if (actualTbLen >= TSDB_TABLE_NAME_LEN) {
227,951,409✔
116
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
117✔
117
    }
118

119
    code = tNameFromString(pName, tbname, T_NAME_TABLE);
227,951,292✔
120
    if (code != 0) {
227,951,327✔
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;
400,344,148✔
126
    if (tbLen <= 0) {
400,341,062✔
127
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
×
128
    }
129

130
    if (tbLen >= TSDB_TABLE_NAME_LEN + TSDB_NAME_QUOTE) {
400,341,867✔
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};
400,341,750✔
135
    strncpy(tbname, pTableName->z, tbLen);
400,343,394✔
136
    int32_t actualTbLen = strdequote(tbname);
400,341,194✔
137
    // after tbname dequote
138
    if (actualTbLen <= 0) {
400,340,482✔
139
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
850✔
140
    }
141
    if (actualTbLen >= TSDB_TABLE_NAME_LEN) {
400,339,725✔
142
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
117✔
143
    }
144

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

149
    code = tNameSetDbName(pName, acctId, dbName, strlen(dbName));
400,344,144✔
150
    if (code != TSDB_CODE_SUCCESS) {
400,341,838✔
151
      code = generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg2);
×
152
      return code;
×
153
    }
154

155
    code = tNameFromString(pName, tbname, T_NAME_TABLE);
400,341,838✔
156
    if (code != 0) {
400,338,823✔
157
      code = generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
×
158
    }
159
  }
160

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

165
  return code;
628,295,007✔
166
}
167

168
int16_t insFindCol(SToken* pColname, int16_t start, int16_t end, SSchema* pSchema) {
920,754,522✔
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;
915,090,292✔
172
    }
173
    ++start;
2,147,483,647✔
174
  }
175
  return -1;
5,665,704✔
176
}
177

178
int32_t insBuildCreateTbReq(SVCreateTbReq* pTbReq, const char* tname, STag* pTag, int64_t suid, const char* sname,
11,492,036✔
179
                            SArray* tagName, uint8_t tagNum, int32_t ttl) {
180
  pTbReq->type = TD_CHILD_TABLE;
11,492,036✔
181
  pTbReq->ctb.pTag = (uint8_t*)pTag;
11,495,226✔
182
  pTbReq->name = taosStrdup(tname);
11,484,021✔
183
  if (!pTbReq->name) return terrno;
11,488,595✔
184
  pTbReq->ctb.suid = suid;
11,484,448✔
185
  pTbReq->ctb.tagNum = tagNum;
11,485,086✔
186
  if (sname) {
11,480,620✔
187
    pTbReq->ctb.stbName = taosStrdup(sname);
10,854,118✔
188
    if (!pTbReq->ctb.stbName) return terrno;
10,856,929✔
189
  }
190
  pTbReq->ctb.tagName = taosArrayDup(tagName, NULL);
11,482,793✔
191
  if (!pTbReq->ctb.tagName) return terrno;
11,492,993✔
192
  pTbReq->ttl = ttl;
11,487,311✔
193
  pTbReq->commentLen = -1;
11,490,501✔
194

195
  return TSDB_CODE_SUCCESS;
11,485,676✔
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) {
574,628,821✔
205
  SSchema* pSchemas = getTableColumnSchema(pTableMeta);
574,628,821✔
206
  int32_t  code = 0;
574,627,179✔
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;
574,641,000✔
215
}
216

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

219
int32_t insInitBoundColsInfo(int32_t numOfBound, SBoundColInfo* pInfo) {
595,036,627✔
220
  pInfo->numOfCols = numOfBound;
595,036,627✔
221
  pInfo->numOfBound = numOfBound;
595,047,928✔
222
  pInfo->hasBoundCols = false;
595,042,294✔
223
  pInfo->mixTagsCols = false;
595,045,841✔
224
  pInfo->pColIndex = taosMemoryCalloc(numOfBound, sizeof(int16_t));
595,034,652✔
225
  if (NULL == pInfo->pColIndex) {
595,040,222✔
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;
595,047,206✔
232
}
233

234
void insResetBoundColsInfo(SBoundColInfo* pInfo) {
2,428✔
235
  pInfo->numOfBound = pInfo->numOfCols;
2,428✔
236
  pInfo->hasBoundCols = false;
2,428✔
237
  pInfo->mixTagsCols = false;
2,428✔
238
  for (int32_t i = 0; i < pInfo->numOfCols; ++i) {
12,140✔
239
    pInfo->pColIndex[i] = i;
9,712✔
240
  }
241
}
2,428✔
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;
23,450,229✔
247
  }
248

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

253
  if (tRowKeyCompare(rowKey, &pTableCxt->lastKey) == 0) {
2,147,483,647✔
254
    pTableCxt->duplicateTs = true;
1,742,149✔
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,
577,155,844✔
263
                                  bool colMode, bool ignoreColVals) {
264
  STableDataCxt* pTableCxt = taosMemoryCalloc(1, sizeof(STableDataCxt));
577,155,844✔
265
  if (NULL == pTableCxt) {
577,137,927✔
266
    *pOutput = NULL;
×
267
    return terrno;
×
268
  }
269

270
  int32_t code = TSDB_CODE_SUCCESS;
577,137,927✔
271

272
  pTableCxt->lastKey = (SRowKey){0};
577,137,927✔
273
  pTableCxt->ordered = true;
577,139,913✔
274
  pTableCxt->duplicateTs = false;
577,149,379✔
275

276
  pTableCxt->pMeta = tableMetaDup(pTableMeta);
577,145,015✔
277
  if (NULL == pTableCxt->pMeta) {
577,168,897✔
278
    code = TSDB_CODE_OUT_OF_MEMORY;
×
279
  }
280
  if (TSDB_CODE_SUCCESS == code) {
577,172,335✔
281
    pTableCxt->pSchema =
577,164,925✔
282
        tBuildTSchema(getTableColumnSchema(pTableMeta), pTableMeta->tableInfo.numOfColumns, pTableMeta->sversion);
577,170,391✔
283
    if (NULL == pTableCxt->pSchema) {
577,162,671✔
284
      code = TSDB_CODE_OUT_OF_MEMORY;
×
285
    }
286
  }
287
  pTableCxt->hasBlob = schemaHasBlob(pTableCxt->pSchema);
577,177,106✔
288

289
  if (TSDB_CODE_SUCCESS == code) {
577,162,333✔
290
    code = insInitBoundColsInfo(pTableMeta->tableInfo.numOfColumns, &pTableCxt->boundColsInfo);
577,166,633✔
291
  }
292
  if (TSDB_CODE_SUCCESS == code && !ignoreColVals) {
577,177,291✔
293
    pTableCxt->pValues = taosArrayInit(pTableMeta->tableInfo.numOfColumns, sizeof(SColVal));
574,416,563✔
294
    if (NULL == pTableCxt->pValues) {
574,406,265✔
295
      code = terrno;
×
296
    } else {
297
      code = initColValues(pTableMeta, pTableCxt->pValues);
574,405,849✔
298
    }
299
  }
300
  if (TSDB_CODE_SUCCESS == code) {
577,183,060✔
301
    pTableCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
577,185,154✔
302
    if (NULL == pTableCxt->pData) {
577,156,473✔
303
      code = terrno;
×
304
    } else {
305
      pTableCxt->pData->flags = (pCreateTbReq != NULL && NULL != *pCreateTbReq) ? SUBMIT_REQ_AUTO_CREATE_TABLE : 0;
577,167,288✔
306
      pTableCxt->pData->flags |= colMode ? SUBMIT_REQ_COLUMN_DATA_FORMAT : 0;
577,173,154✔
307
      pTableCxt->pData->suid = pTableMeta->suid;
577,149,971✔
308
      pTableCxt->pData->uid = pTableMeta->uid;
577,163,126✔
309
      pTableCxt->pData->sver = pTableMeta->sversion;
577,162,397✔
310
      pTableCxt->pData->pCreateTbReq = pCreateTbReq != NULL ? *pCreateTbReq : NULL;
577,158,325✔
311
      int8_t flag = pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
577,160,895✔
312
      if (pCreateTbReq != NULL) *pCreateTbReq = NULL;
577,151,542✔
313
      if (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
577,152,458✔
314
        pTableCxt->pData->aCol = taosArrayInit(128, sizeof(SColData));
3,324,517✔
315
        if (NULL == pTableCxt->pData->aCol) {
3,323,230✔
316
          code = terrno;
×
317
        }
318
      } else {
319
        pTableCxt->pData->aRowP = taosArrayInit(128, POINTER_BYTES);
573,834,957✔
320
        if (NULL == pTableCxt->pData->aRowP) {
573,838,982✔
321
          code = terrno;
×
322
        }
323
      }
324
    }
325
  }
326
  if (TSDB_CODE_SUCCESS == code) {
577,170,418✔
327
    *pOutput = pTableCxt;
577,170,418✔
328
    parserDebug("uid:%" PRId64 ", create table data context, code:%d, vgId:%d", pTableMeta->uid, code,
577,168,189✔
329
                pTableMeta->vgId);
330
  } else {
331
    insDestroyTableDataCxt(pTableCxt);
×
332
  }
333

334
  return code;
577,168,937✔
335
}
336

337
static int32_t rebuildTableData(SSubmitTbData* pSrc, SSubmitTbData** pDst, int8_t hasBlob) {
3,970,568✔
338
  int32_t        code = TSDB_CODE_SUCCESS;
3,970,568✔
339
  SSubmitTbData* pTmp = taosMemoryCalloc(1, sizeof(SSubmitTbData));
3,970,568✔
340
  if (NULL == pTmp) {
3,970,175✔
341
    code = terrno;
×
342
  } else {
343
    pTmp->flags = pSrc->flags;
3,970,175✔
344
    pTmp->suid = pSrc->suid;
3,971,132✔
345
    pTmp->uid = pSrc->uid;
3,972,053✔
346
    pTmp->sver = pSrc->sver;
3,970,458✔
347
    pTmp->pCreateTbReq = NULL;
3,969,930✔
348
    if (pTmp->flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
3,969,820✔
349
      if (pSrc->pCreateTbReq) {
3,830,223✔
350
        code = cloneSVreateTbReq(pSrc->pCreateTbReq, &pTmp->pCreateTbReq);
3,827,352✔
351
      } else {
352
        pTmp->flags &= ~SUBMIT_REQ_AUTO_CREATE_TABLE;
×
353
      }
354
    }
355
    if (TSDB_CODE_SUCCESS == code) {
3,971,525✔
356
      if (pTmp->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
3,971,525✔
357
        pTmp->aCol = taosArrayInit(128, sizeof(SColData));
3,334,948✔
358
        if (NULL == pTmp->aCol) {
3,337,217✔
359
          code = terrno;
×
360
          taosMemoryFree(pTmp);
×
361
        }
362
      } else {
363
        pTmp->aRowP = taosArrayInit(128, POINTER_BYTES);
636,467✔
364
        if (NULL == pTmp->aRowP) {
635,608✔
365
          code = terrno;
×
366
          taosMemoryFree(pTmp);
×
367
        }
368

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

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

380
  taosMemoryFree(pSrc);
3,972,506✔
381
  if (TSDB_CODE_SUCCESS == code) {
3,972,367✔
382
    *pDst = pTmp;
3,972,367✔
383
  }
384

385
  return code;
3,973,005✔
386
}
387

388
static void resetColValues(SArray* pValues) {
32,690,502✔
389
  int32_t num = taosArrayGetSize(pValues);
32,690,502✔
390
  for (int32_t i = 0; i < num; ++i) {
368,991,073✔
391
    SColVal* pVal = taosArrayGet(pValues, i);
336,300,571✔
392
    pVal->flag = CV_FLAG_NONE;
336,300,571✔
393
  }
394
}
32,690,502✔
395

396
int32_t insGetTableDataCxt(SHashObj* pHash, void* id, int32_t idLen, STableMeta* pTableMeta,
1,615,390,602✔
397
                           SVCreateTbReq** pCreateTbReq, STableDataCxt** pTableCxt, bool colMode, bool ignoreColVals) {
398
  STableDataCxt** tmp = (STableDataCxt**)taosHashGet(pHash, id, idLen);
1,615,390,602✔
399
  if (NULL != tmp) {
1,615,409,356✔
400
    *pTableCxt = *tmp;
1,038,239,430✔
401
    if (!ignoreColVals) {
1,038,239,430✔
402
      resetColValues((*pTableCxt)->pValues);
32,690,502✔
403
    }
404
    return TSDB_CODE_SUCCESS;
1,038,239,430✔
405
  }
406
  int32_t code = createTableDataCxt(pTableMeta, pCreateTbReq, pTableCxt, colMode, ignoreColVals);
577,169,926✔
407
  if (TSDB_CODE_SUCCESS == code) {
577,171,174✔
408
    void* pData = *pTableCxt;  // deal scan coverity
577,173,413✔
409
    code = taosHashPut(pHash, id, idLen, &pData, POINTER_BYTES);
577,174,598✔
410
  }
411

412
  if (TSDB_CODE_SUCCESS != code) {
577,183,837✔
413
    insDestroyTableDataCxt(*pTableCxt);
×
414
  }
415
  return code;
577,183,837✔
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,937,222,766✔
425
  }
426
}
427

428
void insDestroyTableDataCxt(STableDataCxt* pTableCxt) {
579,594,394✔
429
  if (NULL == pTableCxt) {
579,594,394✔
430
    return;
×
431
  }
432

433
  taosMemoryFreeClear(pTableCxt->pMeta);
579,594,394✔
434
  tDestroyTSchema(pTableCxt->pSchema);
579,588,738✔
435
  qDestroyBoundColInfo(&pTableCxt->boundColsInfo);
579,588,399✔
436
  taosArrayDestroyEx(pTableCxt->pValues, destroyColVal);
579,590,080✔
437
  if (pTableCxt->pData) {
579,588,498✔
438
    tDestroySubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
7,078,951✔
439
    taosMemoryFree(pTableCxt->pData);
7,079,908✔
440
  }
441
  taosMemoryFree(pTableCxt);
579,585,606✔
442
}
443

444
static void destroyColValSml(void* p) {
9,828,892✔
445
  SColVal* pVal = p;
9,828,892✔
446
  if (TSDB_DATA_TYPE_NCHAR == pVal->value.type || TSDB_DATA_TYPE_GEOMETRY == pVal->value.type ||
9,828,892✔
447
      TSDB_DATA_TYPE_VARBINARY == pVal->value.type) {
9,601,021✔
448
    taosMemoryFreeClear(pVal->value.pData);
232,329✔
449
  }
450
}
9,828,972✔
451

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

457
  taosMemoryFreeClear(pTableCxt->pMeta);
703,087✔
458
  tDestroyTSchema(pTableCxt->pSchema);
703,099✔
459
  qDestroyBoundColInfo(&pTableCxt->boundColsInfo);
703,099✔
460
  taosArrayDestroyEx(pTableCxt->pValues, destroyColValSml);
703,063✔
461
  if (pTableCxt->pData) {
703,087✔
462
    tDestroySubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
703,087✔
463
    taosMemoryFree(pTableCxt->pData);
702,982✔
464
  }
465
  taosMemoryFree(pTableCxt);
703,087✔
466
}
467

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

473
  tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
551,563,305✔
474
  taosMemoryFree(pVgCxt->pData);
551,555,964✔
475

476
  taosMemoryFree(pVgCxt);
551,557,664✔
477
}
478

479
void insDestroyVgroupDataCxtList(SArray* pVgCxtList) {
1,073,426,307✔
480
  if (NULL == pVgCxtList) {
1,073,426,307✔
481
    return;
536,799,451✔
482
  }
483

484
  size_t size = taosArrayGetSize(pVgCxtList);
536,626,856✔
485
  for (int32_t i = 0; i < size; i++) {
1,088,192,833✔
486
    void* p = taosArrayGetP(pVgCxtList, i);
551,568,366✔
487
    insDestroyVgroupDataCxt(p);
551,568,046✔
488
  }
489

490
  taosArrayDestroy(pVgCxtList);
536,624,467✔
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) {
536,577,550✔
509
  if (NULL == pTableCxtHash) {
536,577,550✔
510
    return;
3,305,373✔
511
  }
512

513
  void** p = taosHashIterate(pTableCxtHash, NULL);
533,272,177✔
514
  while (p) {
1,106,358,694✔
515
    insDestroyTableDataCxt(*(STableDataCxt**)p);
573,084,284✔
516

517
    p = taosHashIterate(pTableCxtHash, p);
573,080,593✔
518
  }
519

520
  taosHashCleanup(pTableCxtHash);
533,274,410✔
521
}
522

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

528
  void** p = taosHashIterate(pTableCxtHash, NULL);
960,854✔
529
  while (p) {
1,663,901✔
530
    insDestroyTableDataCxtSml(*(STableDataCxt**)p);
703,047✔
531

532
    p = taosHashIterate(pTableCxtHash, p);
703,046✔
533
  }
534

535
  taosHashCleanup(pTableCxtHash);
960,854✔
536
}
537

538
static int32_t fillVgroupDataCxt(STableDataCxt* pTableCxt, SVgroupDataCxt* pVgCxt, bool isRebuild, bool clear) {
578,259,363✔
539
  int32_t code = 0;
578,259,363✔
540
  if (NULL == pVgCxt->pData->aSubmitTbData) {
578,259,363✔
541
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
551,197,026✔
542
    if (pVgCxt->pData->aSubmitTbData == NULL) {
551,195,442✔
543
      return terrno;
×
544
    }
545
    if (pTableCxt->hasBlob) {
551,197,273✔
546
      pVgCxt->pData->aSubmitBlobData = taosArrayInit(128, sizeof(SBlobSet*));
21,586✔
547
      if (NULL == pVgCxt->pData->aSubmitBlobData) {
21,586✔
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;
578,261,292✔
555

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

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

568
  if (isRebuild) {
578,259,843✔
569
    code = rebuildTableData(pTableCxt->pData, &pTableCxt->pData, pTableCxt->hasBlob);
3,972,691✔
570
  } else if (clear) {
574,287,152✔
571
    taosMemoryFreeClear(pTableCxt->pData);
572,593,335✔
572
  }
573
  parserDebug("uid:%" PRId64 ", add table data context to vgId:%d", pTableCxt->pMeta->uid, pVgCxt->vgId);
578,257,310✔
574

575
  return code;
578,259,789✔
576
}
577

578
static int32_t createVgroupDataCxt(int32_t vgId, SHashObj* pVgroupHash, SArray* pVgroupList, SVgroupDataCxt** pOutput) {
551,661,923✔
579
  SVgroupDataCxt* pVgCxt = taosMemoryCalloc(1, sizeof(SVgroupDataCxt));
551,661,923✔
580
  if (NULL == pVgCxt) {
551,661,764✔
581
    return terrno;
×
582
  }
583
  pVgCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitReq2));
551,661,764✔
584
  if (NULL == pVgCxt->pData) {
551,659,948✔
585
    insDestroyVgroupDataCxt(pVgCxt);
×
586
    return terrno;
×
587
  }
588

589
  pVgCxt->vgId = vgId;
551,660,087✔
590
  int32_t code = taosHashPut(pVgroupHash, &pVgCxt->vgId, sizeof(pVgCxt->vgId), &pVgCxt, POINTER_BYTES);
551,660,613✔
591
  if (TSDB_CODE_SUCCESS == code) {
551,661,582✔
592
    if (NULL == taosArrayPush(pVgroupList, &pVgCxt)) {
551,663,308✔
593
      code = terrno;
×
594
      insDestroyVgroupDataCxt(pVgCxt);
×
595
      return code;
1✔
596
    }
597
    //    uDebug("td23101 2vgId:%d, uid:%" PRIu64, pVgCxt->vgId, pTableCxt->pMeta->uid);
598
    *pOutput = pVgCxt;
551,663,308✔
599
  } else {
600
    insDestroyVgroupDataCxt(pVgCxt);
×
601
  }
602
  return code;
551,661,664✔
603
}
604

605
int insColDataComp(const void* lp, const void* rp) {
14,925,820✔
606
  SColData* pLeft = (SColData*)lp;
14,925,820✔
607
  SColData* pRight = (SColData*)rp;
14,925,820✔
608
  if (pLeft->cid < pRight->cid) {
14,925,820✔
609
    return -1;
14,888,716✔
610
  } else if (pLeft->cid > pRight->cid) {
41,538✔
611
    return 1;
41,538✔
612
  }
613

614
  return 0;
×
615
}
616

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

623
  SVgroupInfo      vgInfo = {0};
10,342✔
624
  SRequestConnInfo conn = {.pTrans = pBuildInfo->transport,
20,496✔
625
                           .requestId = pBuildInfo->requestId,
10,342✔
626
                           .requestObjRefId = pBuildInfo->requestSelf,
10,342✔
627
                           .mgmtEps = pBuildInfo->mgmtEpSet};
628

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

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

639
  return TSDB_CODE_SUCCESS;
10,342✔
640
}
641

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

647
  if (pTbData->getFromHash) {
2,548,131✔
648
    pTbInfo = (STableVgUid*)tSimpleHashGet(pBuildInfo->pTableHash, pTbData->tbName, strlen(pTbData->tbName));
2,487,607✔
649
  }
650

651
  if (NULL == pTbInfo) {
2,548,237✔
652
    SName sname;
48,658✔
653
    code = qCreateSName2(&sname, pTbData->tbName, pBuildInfo->acctId, pBuildInfo->dbname, NULL, 0);
61,244✔
654
    if (TSDB_CODE_SUCCESS != code) {
61,243✔
655
      return code;
390✔
656
    }
657

658
    STableMeta*      pTableMeta = NULL;
61,243✔
659
    SRequestConnInfo conn = {.pTrans = pBuildInfo->transport,
109,901✔
660
                             .requestId = pBuildInfo->requestId,
61,242✔
661
                             .requestObjRefId = pBuildInfo->requestSelf,
61,244✔
662
                             .mgmtEps = pBuildInfo->mgmtEpSet};
663
    code = catalogGetTableMeta((SCatalog*)pBuildInfo->pCatalog, &conn, &sname, &pTableMeta);
61,244✔
664

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

670
    if (TSDB_CODE_SUCCESS != code) {
60,813✔
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;
60,813✔
676
    *vgId = pTableMeta->vgId;
60,854✔
677
    *suid = pTableMeta->suid;
60,853✔
678

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

685
    taosMemoryFree(pTableMeta);
60,866✔
686
  } else {
687
    *uid = pTbInfo->uid;
2,486,993✔
688
    *vgId = pTbInfo->vgid;
2,487,154✔
689
    *suid = pTbInfo->suid;
2,487,231✔
690
  }
691

692
  return code;
2,547,613✔
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,
854,680✔
707
                                            char* tbname) {
708
  if (NULL == pVgCxt->pData->aSubmitTbData) {
854,680✔
709
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
463,305✔
710
    if (NULL == pVgCxt->pData->aSubmitTbData) {
463,412✔
711
      return terrno;
×
712
    }
713
    if (pTbCtx->hasBlob) {
463,412✔
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;
854,787✔
722
  SArray** rowP = NULL;
854,787✔
723

724
  rowP = (SArray**)tSimpleHashGet(pTableNameHash, tbname, strlen(tbname));
854,787✔
725

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

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

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

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

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

785
  return TSDB_CODE_SUCCESS;
854,732✔
786
}
787

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

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

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

802
  pTbCtx->pMeta->vgId = vgId;
1,693,261✔
803
  pTbCtx->pMeta->uid = uid;
1,693,344✔
804
  pTbCtx->pData->uid = uid;
1,693,435✔
805

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

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

817
  SVgroupDataCxt* pVgCxt = NULL;
1,693,416✔
818
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
1,693,545✔
819
  if (NULL == pp) {
1,693,513✔
820
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
296,587✔
821
    if (NULL == pp) {
296,588✔
822
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
296,588✔
823
    } else {
824
      pVgCxt = *(SVgroupDataCxt**)pp;
×
825
    }
826
  } else {
827
    pVgCxt = *(SVgroupDataCxt**)pp;
1,396,926✔
828
  }
829

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

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

844
int32_t insAppendStmt2TableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
854,566✔
845
                                   SStbInterlaceInfo* pBuildInfo, SVCreateTbReq* ctbReq) {
846
  int32_t  code = TSDB_CODE_SUCCESS;
854,566✔
847
  uint64_t uid;
273,789✔
848
  int32_t  vgId;
273,825✔
849
  uint64_t suid;
273,825✔
850

851
  pTbCtx->pData->aRowP = pTbData->aCol;
854,602✔
852
  pTbCtx->pData->pBlobSet = pTbData->pBlobSet;
854,717✔
853

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

873
    pTbCtx->pMeta->vgId = vgId;
854,374✔
874
    pTbCtx->pMeta->uid = uid;
854,446✔
875
    pTbCtx->pData->uid = uid;
854,338✔
876
    pTbCtx->pData->pCreateTbReq = NULL;
854,338✔
877

878
    if (ctbReq != NULL) {
854,338✔
879
      tdDestroySVCreateTbReq(ctbReq);
880
      taosMemoryFree(ctbReq);
491,669✔
881
      ctbReq = NULL;
491,639✔
882
    }
883
  }
884

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

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

905
  SVgroupDataCxt* pVgCxt = NULL;
854,635✔
906
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
854,635✔
907
  if (NULL == pp) {
854,628✔
908
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
463,248✔
909
    if (NULL == pp) {
463,285✔
910
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
463,285✔
911
    } else {
912
      pVgCxt = *(SVgroupDataCxt**)pp;
×
913
    }
914
  } else {
915
    pVgCxt = *(SVgroupDataCxt**)pp;
391,380✔
916
  }
917

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

922
  if (taosArrayGetSize(pVgCxt->pData->aSubmitTbData) >= 20000) {
854,826✔
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;
854,770✔
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) {
536,029,406✔
997
  SHashObj* pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
536,029,406✔
998
  SArray*   pVgroupList = taosArrayInit(8, POINTER_BYTES);
536,032,504✔
999
  if (NULL == pVgroupHash || NULL == pVgroupList) {
536,030,820✔
1000
    taosHashCleanup(pVgroupHash);
1,166✔
1001
    taosArrayDestroy(pVgroupList);
×
1002
    return terrno;
×
1003
  }
1004

1005
  int32_t code = TSDB_CODE_SUCCESS;
536,029,654✔
1006
  bool    colFormat = false;
536,029,654✔
1007

1008
  void* p = taosHashIterate(pTableHash, NULL);
536,029,654✔
1009
  if (p) {
536,035,729✔
1010
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
536,036,013✔
1011
    colFormat = (0 != (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT));
536,035,761✔
1012
  }
1013

1014
  while (TSDB_CODE_SUCCESS == code && NULL != p) {
1,112,607,745✔
1015
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
576,570,743✔
1016
    if (colFormat) {
576,570,029✔
1017
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
3,338,491✔
1018
      if (pCol && pCol->nVal <= 0) {
3,338,455✔
1019
        p = taosHashIterate(pTableHash, p);
108✔
1020
        continue;
108✔
1021
      }
1022

1023
      if (pTableCxt->pData->pCreateTbReq) {
3,338,028✔
1024
        pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
3,204,965✔
1025
      }
1026
      int8_t isBlob = IS_STR_DATA_BLOB(pCol->type) ? 1 : 0;
3,337,426✔
1027
      if (isBlob == 0) {
3,336,114✔
1028
        taosArraySort(pTableCxt->pData->aCol, insColDataComp);
3,337,390✔
1029
        code = tColDataSortMerge(&pTableCxt->pData->aCol);
3,337,709✔
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) {
573,231,538✔
1043
        if (!pTableCxt->ordered) {
573,210,235✔
1044
          code = tRowSort(pTableCxt->pData->aRowP);
1,384,376✔
1045
        }
1046
        if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
573,208,764✔
1047
          code = tRowMerge(pTableCxt->pData->aRowP, pTableCxt->pSchema, PREFER_NON_NULL);
1,514,578✔
1048
        }
1049
      } else {
1050
        if (!pTableCxt->ordered) {
21,586✔
1051
          code = tRowSortWithBlob(pTableCxt->pData->aRowP, pTableCxt->pSchema, pTableCxt->pData->pBlobSet);
741✔
1052
        }
1053
        if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
21,586✔
1054
          code = tRowMergeWithBlob(pTableCxt->pData->aRowP, pTableCxt->pSchema, pTableCxt->pData->pBlobSet, 0);
741✔
1055
        }
1056
      }
1057
    }
1058

1059
    if (TSDB_CODE_SUCCESS == code) {
576,569,515✔
1060
      SVgroupDataCxt* pVgCxt = NULL;
576,569,515✔
1061
      int32_t         vgId = pTableCxt->pMeta->vgId;
576,569,505✔
1062
      void**          pp = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
576,570,069✔
1063
      if (NULL == pp) {
576,568,812✔
1064
        code = createVgroupDataCxt(vgId, pVgroupHash, pVgroupList, &pVgCxt);
550,901,158✔
1065
      } else {
1066
        pVgCxt = *(SVgroupDataCxt**)pp;
25,667,654✔
1067
      }
1068
      if (TSDB_CODE_SUCCESS == code) {
576,569,457✔
1069
        code = fillVgroupDataCxt(pTableCxt, pVgCxt, isRebuild, true);
576,569,151✔
1070
      }
1071
    }
1072
    if (TSDB_CODE_SUCCESS == code) {
576,566,504✔
1073
      p = taosHashIterate(pTableHash, p);
576,566,504✔
1074
    }
1075
  }
1076

1077
  taosHashCleanup(pVgroupHash);
536,037,002✔
1078
  if (TSDB_CODE_SUCCESS == code) {
536,034,406✔
1079
    *pVgDataBlocks = pVgroupList;
536,034,767✔
1080
  } else {
UNCOV
1081
    insDestroyVgroupDataCxtList(pVgroupList);
×
1082
  }
1083

1084
  return code;
536,032,485✔
1085
}
1086

1087
static int32_t buildSubmitReq(int32_t vgId, SSubmitReq2* pReq, void** pData, uint32_t* pLen) {
551,654,502✔
1088
  int32_t  code = TSDB_CODE_SUCCESS;
551,654,502✔
1089
  uint32_t len = 0;
551,654,502✔
1090
  void*    pBuf = NULL;
551,654,502✔
1091
  tEncodeSize(tEncodeSubmitReq, pReq, len, code);
551,654,502✔
1092
  if (TSDB_CODE_SUCCESS == code) {
551,655,625✔
1093
    SEncoder encoder;
549,001,050✔
1094
    len += sizeof(SSubmitReq2Msg);
551,654,494✔
1095
    pBuf = taosMemoryMalloc(len);
551,654,494✔
1096
    if (NULL == pBuf) {
551,646,216✔
1097
      return terrno;
×
1098
    }
1099
    ((SSubmitReq2Msg*)pBuf)->header.vgId = htonl(vgId);
551,646,216✔
1100
    ((SSubmitReq2Msg*)pBuf)->header.contLen = htonl(len);
551,652,880✔
1101
    ((SSubmitReq2Msg*)pBuf)->version = htobe64(1);
551,654,984✔
1102
    tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SSubmitReq2Msg)), len - sizeof(SSubmitReq2Msg));
551,657,773✔
1103
    code = tEncodeSubmitReq(&encoder, pReq);
551,658,818✔
1104
    tEncoderClear(&encoder);
551,662,667✔
1105
  }
1106

1107
  if (TSDB_CODE_SUCCESS == code) {
551,659,538✔
1108
    *pData = pBuf;
551,659,538✔
1109
    *pLen = len;
551,660,430✔
1110
  } else {
1111
    taosMemoryFree(pBuf);
×
1112
  }
1113
  return code;
551,648,078✔
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) {
551,652,971✔
1124
  int32_t code = 0;
551,652,971✔
1125
  if (p->raw) {
551,652,971✔
1126
    return TSDB_CODE_SUCCESS;  // no blob data in raw mode
×
1127
  }
1128

1129
  if (p->aSubmitBlobData != NULL) {
551,656,719✔
1130
    for (int32_t i = 0; i < taosArrayGetSize(p->aSubmitTbData); i++) {
43,172✔
1131
      SSubmitTbData* pSubmitTbData = taosArrayGet(p->aSubmitTbData, i);
21,586✔
1132
      SBlobSet**     ppBlob = taosArrayGet(p->aSubmitBlobData, i);
21,586✔
1133
      SBlobSet*      pBlob = ppBlob ? *ppBlob : NULL;
21,586✔
1134
      int32_t        nrow = taosArrayGetSize(pSubmitTbData->aRowP);
21,586✔
1135
      int32_t        nblob = 0;
21,586✔
1136
      if (nrow > 0 && pBlob) {
21,586✔
1137
        nblob = taosArrayGetSize(pBlob->pSeqTable);
21,586✔
1138
      }
1139
      uTrace("blob %p row size %d, pData size %d", pBlob, nblob, nrow);
21,586✔
1140
      pSubmitTbData->pBlobSet = pBlob;
21,586✔
1141
      if (ppBlob != NULL) *ppBlob = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
21,586✔
1142
    }
1143
  } else {
1144
    for (int32_t i = 0; i < taosArrayGetSize(p->aSubmitTbData); i++) {
1,130,725,832✔
1145
      SSubmitTbData* pSubmitTbData = taosArrayGet(p->aSubmitTbData, i);
579,086,653✔
1146
      pSubmitTbData->pBlobSet = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
579,094,247✔
1147
    }
1148
  }
1149

1150
  return code;
551,664,029✔
1151
}
1152
int32_t insBuildVgDataBlocks(SHashObj* pVgroupsHashObj, SArray* pVgDataCxtList, SArray** pVgDataBlocks, bool append) {
536,722,988✔
1153
  size_t  numOfVg = taosArrayGetSize(pVgDataCxtList);
536,722,988✔
1154
  SArray* pDataBlocks = (append && *pVgDataBlocks) ? *pVgDataBlocks : taosArrayInit(numOfVg, POINTER_BYTES);
536,726,380✔
1155
  if (NULL == pDataBlocks) {
536,723,407✔
1156
    return TSDB_CODE_OUT_OF_MEMORY;
×
1157
  }
1158

1159
  int32_t code = TSDB_CODE_SUCCESS;
536,723,407✔
1160
  for (size_t i = 0; TSDB_CODE_SUCCESS == code && i < numOfVg; ++i) {
1,088,382,214✔
1161
    SVgroupDataCxt* src = taosArrayGetP(pVgDataCxtList, i);
551,659,186✔
1162
    if (taosArrayGetSize(src->pData->aSubmitTbData) <= 0) {
551,661,871✔
1163
      continue;
×
1164
    }
1165
    SVgDataBlocks* dst = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
551,659,027✔
1166
    if (NULL == dst) {
551,654,851✔
1167
      code = terrno;
×
1168
    }
1169

1170
    if (TSDB_CODE_SUCCESS == code) {
551,654,851✔
1171
      dst->numOfTables = taosArrayGetSize(src->pData->aSubmitTbData);
551,655,314✔
1172
      code = taosHashGetDup(pVgroupsHashObj, (const char*)&src->vgId, sizeof(src->vgId), &dst->vg);
551,656,459✔
1173
    }
1174
    if (TSDB_CODE_SUCCESS == code) {
551,661,709✔
1175
      code = insResetBlob(src->pData);
551,661,709✔
1176
    }
1177

1178
    if (TSDB_CODE_SUCCESS == code) {
551,659,599✔
1179
      code = buildSubmitReq(src->vgId, src->pData, &dst->pData, &dst->size);
551,659,589✔
1180
    }
1181
    if (TSDB_CODE_SUCCESS == code) {
551,649,973✔
1182
      code = (NULL == taosArrayPush(pDataBlocks, &dst) ? terrno : TSDB_CODE_SUCCESS);
551,659,643✔
1183
    }
1184
    if (TSDB_CODE_SUCCESS != code) {
551,657,992✔
1185
      destroyVgDataBlocks(dst);
×
1186
    }
1187
  }
1188

1189
  if (append) {
536,723,028✔
1190
    if (NULL == *pVgDataBlocks) {
693,688✔
1191
      *pVgDataBlocks = pDataBlocks;
693,720✔
1192
    }
1193
    return code;
693,688✔
1194
  }
1195

1196
  if (TSDB_CODE_SUCCESS == code) {
536,029,340✔
1197
    *pVgDataBlocks = pDataBlocks;
536,025,649✔
1198
  } else {
1199
    taosArrayDestroyP(pDataBlocks, destroyVgDataBlocks);
3,691✔
1200
  }
1201

1202
  return code;
536,030,229✔
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,933✔
1216
  if (*fields != pColSchema->type) {
159,933✔
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)) {
159,657✔
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)) {
159,381✔
1258
    int32_t bytes = *(int32_t*)(fields + sizeof(int8_t));
27,917✔
1259
    if (IS_STR_DATA_BLOB(pColSchema->type)) {
27,917✔
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,917✔
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) {
159,105✔
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;
159,105✔
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,702✔
1341
                     int numFields, bool needChangeLength, char* errstr, int32_t errstrLen, bool raw) {
1342
  int       ret = 0;
34,702✔
1343
  int8_t    hasBlob = 0;
34,702✔
1344
  SBlobSet* pBlobSet = NULL;
34,702✔
1345
  if (data == NULL) {
34,702✔
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,702✔
1351
  SVCreateTbReq* pCreateReqTmp = NULL;
34,702✔
1352
  if (tmp == NULL && pCreateTb != NULL) {
34,702✔
1353
    ret = cloneSVreateTbReq(pCreateTb, &pCreateReqTmp);
3,320✔
1354
    if (ret != TSDB_CODE_SUCCESS) {
3,320✔
1355
      uError("cloneSVreateTbReq error");
×
1356
      goto end;
×
1357
    }
1358
  }
1359

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

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

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

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

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

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

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

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

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

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

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

1425
  if (tFields != NULL && numFields != numOfCols) {
34,702✔
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,702✔
1432
  if (tFields == NULL) {
34,702✔
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++) {
178,566✔
1441
      for (int j = 0; j < boundInfo->numOfBound; j++) {
1,348,422✔
1442
        SSchema*    pColSchema = &pSchema[j];
1,348,422✔
1443
        SSchemaExt* pColExtSchema = &pExtSchemas[j];
1,348,422✔
1444
        char*       fieldName = NULL;
1,348,422✔
1445
        if (raw) {
1,348,422✔
1446
          fieldName = ((SSchemaWrapper*)tFields)->pSchema[i].name;
1,347,042✔
1447
        } else {
1448
          fieldName = ((TAOS_FIELD*)tFields)[i].name;
1,380✔
1449
        }
1450
        if (strcmp(pColSchema->name, fieldName) == 0) {
1,348,422✔
1451
          PRCESS_DATA(i, j)
145,520✔
1452
          break;
145,520✔
1453
        }
1454
      }
1455
    }
1456
  }
1457

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

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