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

taosdata / TDengine / #4967

09 Feb 2026 01:16AM UTC coverage: 66.86% (+0.03%) from 66.832%
#4967

push

travis-ci

web-flow
docs: add support for recording STMT to CSV files (#34276)

* docs: add support for recording STMT to CSV files

* docs: update version for STMT recording feature in CSV files

205726 of 307696 relevant lines covered (66.86%)

127587752.1 hits per line

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

75.0
/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) {
655,581,581✔
31
  bool inEscape = false;
655,581,581✔
32
  bool inQuote = false;
655,581,581✔
33
  char quotaStr = 0;
655,581,581✔
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;
240,668,397✔
38
    }
39

40
    if (*(pToken->z + i) == TS_ESCAPE_CHAR) {
2,147,483,647✔
41
      if (!inQuote) {
42,785,318✔
42
        inEscape = !inEscape;
42,788,841✔
43
      }
44
    }
45

46
    if (*(pToken->z + i) == '\'' || *(pToken->z + i) == '"') {
2,147,483,647✔
47
      if (!inEscape) {
6,314,659✔
48
        if (!inQuote) {
6,313,150✔
49
          quotaStr = *(pToken->z + i);
3,156,575✔
50
          inQuote = !inQuote;
3,156,575✔
51
        } else if (quotaStr == *(pToken->z + i)) {
3,156,575✔
52
          inQuote = !inQuote;
3,156,575✔
53
        }
54
      }
55
    }
56
  }
57

58
  return NULL;
414,932,990✔
59
}
60

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

68
  int32_t code = TSDB_CODE_SUCCESS;
655,581,410✔
69
  char*   p = tableNameGetPosition(pTableName, TS_PATH_DELIMITER[0]);
655,581,410✔
70

71
  if (p != NULL) {  // db has been specified in sql string so we ignore current db path
655,601,834✔
72
    // before dbname dequote
73
    int32_t dbLen = p - pTableName->z;
240,669,768✔
74
    if (dbLen <= 0) {
240,662,599✔
75
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg2);
×
76
    }
77
    if (dbLen >= TSDB_DB_FNAME_LEN + TSDB_NAME_QUOTE) {
240,662,599✔
78
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg5);
×
79
    }
80

81
    char name[TSDB_DB_FNAME_LEN + TSDB_NAME_QUOTE] = {0};
240,662,599✔
82
    strncpy(name, pTableName->z, dbLen);
240,660,214✔
83
    int32_t actualDbLen = strdequote(name);
240,662,536✔
84

85
    // after dbname dequote
86
    if (actualDbLen <= 0) {
240,659,750✔
87
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg2);
3,435✔
88
    }
89
    if (actualDbLen >= TSDB_DB_NAME_LEN) {
240,657,802✔
90
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg5);
×
91
    }
92

93
    code = tNameSetDbName(pName, acctId, name, actualDbLen);
240,657,802✔
94
    if (code != TSDB_CODE_SUCCESS) {
240,667,981✔
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;
240,667,981✔
100
    if (tbLen <= 0) {
240,668,789✔
101
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
11,441✔
102
    }
103
    if (tbLen >= TSDB_TABLE_NAME_LEN + TSDB_NAME_QUOTE) {
240,657,348✔
104
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
129✔
105
    }
106

107
    char tbname[TSDB_TABLE_NAME_LEN + TSDB_NAME_QUOTE] = {0};
240,657,219✔
108
    strncpy(tbname, p + 1, tbLen);
240,661,476✔
109
    int32_t actualTbLen = strdequote(tbname);
240,651,180✔
110

111
    // after tbname dequote
112
    if (actualTbLen <= 0) {
240,667,269✔
113
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
8,687✔
114
    }
115
    if (actualTbLen >= TSDB_TABLE_NAME_LEN) {
240,658,903✔
116
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
129✔
117
    }
118

119
    code = tNameFromString(pName, tbname, T_NAME_TABLE);
240,658,774✔
120
    if (code != 0) {
240,667,690✔
121
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
78✔
122
    }
123
  } else {  // get current DB name first, and then set it into path
124
    // before tbname dequote
125
    int32_t tbLen = pTableName->n;
414,932,066✔
126
    if (tbLen <= 0) {
414,927,070✔
127
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
×
128
    }
129

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

134
    char tbname[TSDB_TABLE_NAME_LEN + TSDB_NAME_QUOTE] = {0};
414,930,279✔
135
    strncpy(tbname, pTableName->z, tbLen);
414,929,926✔
136
    int32_t actualTbLen = strdequote(tbname);
414,929,411✔
137
    // after tbname dequote
138
    if (actualTbLen <= 0) {
414,923,166✔
139
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
×
140
    }
141
    if (actualTbLen >= TSDB_TABLE_NAME_LEN) {
414,928,074✔
142
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
129✔
143
    }
144

145
    if (dbName == NULL || strlen(dbName) == 0) {
414,927,945✔
146
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED, msg3);
150✔
147
    }
148

149
    code = tNameSetDbName(pName, acctId, dbName, strlen(dbName));
414,929,332✔
150
    if (code != TSDB_CODE_SUCCESS) {
414,925,227✔
151
      code = generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg2);
×
152
      return code;
×
153
    }
154

155
    code = tNameFromString(pName, tbname, T_NAME_TABLE);
414,925,227✔
156
    if (code != 0) {
414,921,583✔
157
      code = generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg1);
×
158
    }
159
  }
160

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

165
  return code;
655,589,809✔
166
}
167
#if 0 // converted to static inline function in parInsertUtil.h
168
int16_t insFindCol(SToken* pColname, int16_t start, int16_t end, SSchema* pSchema) {
169
  while (start < end) {
170
    if (strncmp(pColname->z, pSchema[start].name, pColname->n) == 0) {
171
      return start;
172
    }
173
    ++start;
174
  }
175
  return -1;
176
}
177
#endif
178

179
int32_t insBuildCreateTbReq(SVCreateTbReq* pTbReq, const char* tname, STag* pTag, int64_t suid, const char* sname,
15,110,664✔
180
                            SArray* tagName, uint8_t tagNum, int32_t ttl) {
181
  pTbReq->type = TD_CHILD_TABLE;
15,110,664✔
182
  pTbReq->ctb.pTag = (uint8_t*)pTag;
15,116,460✔
183
  pTbReq->name = taosStrdup(tname);
15,110,374✔
184
  if (!pTbReq->name) return terrno;
15,102,006✔
185
  pTbReq->ctb.suid = suid;
15,105,502✔
186
  pTbReq->ctb.tagNum = tagNum;
15,102,939✔
187
  if (sname) {
15,102,296✔
188
    pTbReq->ctb.stbName = taosStrdup(sname);
14,474,334✔
189
    if (!pTbReq->ctb.stbName) return terrno;
14,473,628✔
190
  }
191
  pTbReq->ctb.tagName = taosArrayDup(tagName, NULL);
15,107,350✔
192
  if (!pTbReq->ctb.tagName) return terrno;
15,111,262✔
193
  pTbReq->ttl = ttl;
15,112,240✔
194
  pTbReq->commentLen = -1;
15,111,280✔
195

196
  return TSDB_CODE_SUCCESS;
15,111,289✔
197
}
198

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

205
static int32_t initColValues(STableMeta* pTableMeta, SArray* pValues) {
594,489,640✔
206
  SSchema* pSchemas = getTableColumnSchema(pTableMeta);
594,489,640✔
207
  int32_t  code = 0;
594,491,046✔
208
  for (int32_t i = 0; i < pTableMeta->tableInfo.numOfColumns; ++i) {
2,147,483,647✔
209
    SColVal val = COL_VAL_NONE(pSchemas[i].colId, pSchemas[i].type);
2,147,483,647✔
210
    if (NULL == taosArrayPush(pValues, &val)) {
2,147,483,647✔
211
      code = terrno;
×
212
      break;
×
213
    }
214
  }
215
  return code;
594,503,894✔
216
}
217

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

220
int32_t insInitBoundColsInfo(int32_t numOfBound, SBoundColInfo* pInfo) {
618,743,924✔
221
  pInfo->numOfCols = numOfBound;
618,743,924✔
222
  pInfo->numOfBound = numOfBound;
618,762,263✔
223
  pInfo->hasBoundCols = false;
618,756,231✔
224
  pInfo->mixTagsCols = false;
618,756,140✔
225
  pInfo->pColIndex = taosMemoryCalloc(numOfBound, sizeof(int16_t));
618,750,890✔
226
  if (NULL == pInfo->pColIndex) {
618,748,521✔
227
    return terrno;
×
228
  }
229
  for (int32_t i = 0; i < numOfBound; ++i) {
2,147,483,647✔
230
    pInfo->pColIndex[i] = i;
2,147,483,647✔
231
  }
232
  return TSDB_CODE_SUCCESS;
618,753,950✔
233
}
234

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

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

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

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

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

263
static int32_t createTableDataCxt(STableMeta* pTableMeta, SVCreateTbReq** pCreateTbReq, STableDataCxt** pOutput,
597,016,116✔
264
                                  bool colMode, bool ignoreColVals) {
265
  STableDataCxt* pTableCxt = taosMemoryCalloc(1, sizeof(STableDataCxt));
597,016,116✔
266
  if (NULL == pTableCxt) {
597,010,646✔
267
    *pOutput = NULL;
×
268
    return terrno;
×
269
  }
270

271
  int32_t code = TSDB_CODE_SUCCESS;
597,010,646✔
272

273
  pTableCxt->lastKey = (SRowKey){0};
597,010,646✔
274
  pTableCxt->ordered = true;
597,017,113✔
275
  pTableCxt->duplicateTs = false;
597,021,330✔
276

277
  pTableCxt->pMeta = tableMetaDup(pTableMeta);
597,025,205✔
278
  if (NULL == pTableCxt->pMeta) {
597,038,127✔
279
    code = TSDB_CODE_OUT_OF_MEMORY;
×
280
  }
281
  if (TSDB_CODE_SUCCESS == code) {
597,040,106✔
282
    pTableCxt->pSchema =
597,032,591✔
283
        tBuildTSchema(getTableColumnSchema(pTableMeta), pTableMeta->tableInfo.numOfColumns, pTableMeta->sversion);
597,031,147✔
284
    if (NULL == pTableCxt->pSchema) {
597,027,788✔
285
      code = TSDB_CODE_OUT_OF_MEMORY;
×
286
    }
287
  }
288
  pTableCxt->hasBlob = schemaHasBlob(pTableCxt->pSchema);
597,052,639✔
289

290
  if (TSDB_CODE_SUCCESS == code) {
597,027,717✔
291
    code = insInitBoundColsInfo(pTableMeta->tableInfo.numOfColumns, &pTableCxt->boundColsInfo);
597,039,938✔
292
  }
293
  if (TSDB_CODE_SUCCESS == code && !ignoreColVals) {
597,034,026✔
294
    pTableCxt->pValues = taosArrayInit(pTableMeta->tableInfo.numOfColumns, sizeof(SColVal));
594,280,470✔
295
    if (NULL == pTableCxt->pValues) {
594,267,468✔
296
      code = terrno;
×
297
    } else {
298
      code = initColValues(pTableMeta, pTableCxt->pValues);
594,273,990✔
299
    }
300
  }
301
  if (TSDB_CODE_SUCCESS == code) {
597,050,526✔
302
    pTableCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
597,049,115✔
303
    if (NULL == pTableCxt->pData) {
597,019,152✔
304
      code = terrno;
×
305
    } else {
306
      pTableCxt->pData->flags = (pCreateTbReq != NULL && NULL != *pCreateTbReq) ? SUBMIT_REQ_AUTO_CREATE_TABLE : 0;
597,031,931✔
307
      pTableCxt->pData->flags |= colMode ? SUBMIT_REQ_COLUMN_DATA_FORMAT : 0;
597,038,679✔
308
      pTableCxt->pData->suid = pTableMeta->suid;
597,038,752✔
309
      pTableCxt->pData->uid = pTableMeta->uid;
597,037,153✔
310
      pTableCxt->pData->sver = pTableMeta->sversion;
597,033,725✔
311
      pTableCxt->pData->pCreateTbReq = pCreateTbReq != NULL ? *pCreateTbReq : NULL;
597,026,180✔
312
      int8_t flag = pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
597,034,789✔
313
      if (pCreateTbReq != NULL) *pCreateTbReq = NULL;
597,024,295✔
314
      if (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
597,029,939✔
315
        pTableCxt->pData->aCol = taosArrayInit(128, sizeof(SColData));
6,573,622✔
316
        if (NULL == pTableCxt->pData->aCol) {
6,567,857✔
317
          code = terrno;
×
318
        }
319
      } else {
320
        pTableCxt->pData->aRowP = taosArrayInit(128, POINTER_BYTES);
590,451,792✔
321
        if (NULL == pTableCxt->pData->aRowP) {
590,455,090✔
322
          code = terrno;
×
323
        }
324
      }
325
    }
326
  }
327
  if (TSDB_CODE_SUCCESS == code) {
597,036,080✔
328
    *pOutput = pTableCxt;
597,036,080✔
329
    parserDebug("uid:%" PRId64 ", create table data context, code:%d, vgId:%d", pTableMeta->uid, code,
597,037,610✔
330
                pTableMeta->vgId);
331
  } else {
332
    insDestroyTableDataCxt(pTableCxt);
×
333
  }
334

335
  return code;
597,029,563✔
336
}
337

338
static int32_t rebuildTableData(SSubmitTbData* pSrc, SSubmitTbData** pDst, int8_t hasBlob) {
7,216,133✔
339
  int32_t        code = TSDB_CODE_SUCCESS;
7,216,133✔
340
  SSubmitTbData* pTmp = taosMemoryCalloc(1, sizeof(SSubmitTbData));
7,216,133✔
341
  if (NULL == pTmp) {
7,209,757✔
342
    code = terrno;
×
343
  } else {
344
    pTmp->flags = pSrc->flags;
7,209,757✔
345
    pTmp->suid = pSrc->suid;
7,211,659✔
346
    pTmp->uid = pSrc->uid;
7,215,481✔
347
    pTmp->sver = pSrc->sver;
7,219,638✔
348
    pTmp->pCreateTbReq = NULL;
7,217,419✔
349
    if (pTmp->flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
7,213,570✔
350
      if (pSrc->pCreateTbReq) {
7,070,559✔
351
        code = cloneSVreateTbReq(pSrc->pCreateTbReq, &pTmp->pCreateTbReq);
7,068,331✔
352
      } else {
353
        pTmp->flags &= ~SUBMIT_REQ_AUTO_CREATE_TABLE;
×
354
      }
355
    }
356
    if (TSDB_CODE_SUCCESS == code) {
7,218,968✔
357
      if (pTmp->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
7,218,968✔
358
        pTmp->aCol = taosArrayInit(128, sizeof(SColData));
6,580,998✔
359
        if (NULL == pTmp->aCol) {
6,579,757✔
360
          code = terrno;
×
361
          taosMemoryFree(pTmp);
×
362
        }
363
      } else {
364
        pTmp->aRowP = taosArrayInit(128, POINTER_BYTES);
637,653✔
365
        if (NULL == pTmp->aRowP) {
638,622✔
366
          code = terrno;
×
367
          taosMemoryFree(pTmp);
×
368
        }
369

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

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

381
  taosMemoryFree(pSrc);
7,220,915✔
382
  if (TSDB_CODE_SUCCESS == code) {
7,219,629✔
383
    *pDst = pTmp;
7,219,312✔
384
  }
385

386
  return code;
7,220,263✔
387
}
388

389
static void resetColValues(SArray* pValues) {
33,065,705✔
390
  int32_t num = taosArrayGetSize(pValues);
33,065,705✔
391
  for (int32_t i = 0; i < num; ++i) {
372,402,267✔
392
    SColVal* pVal = taosArrayGet(pValues, i);
339,336,562✔
393
    pVal->flag = CV_FLAG_NONE;
339,336,562✔
394
  }
395
}
33,065,705✔
396

397
int32_t insGetTableDataCxt(SHashObj* pHash, void* id, int32_t idLen, STableMeta* pTableMeta,
1,661,759,761✔
398
                           SVCreateTbReq** pCreateTbReq, STableDataCxt** pTableCxt, bool colMode, bool ignoreColVals) {
399
  STableDataCxt** tmp = (STableDataCxt**)taosHashGet(pHash, id, idLen);
1,661,759,761✔
400
  if (NULL != tmp) {
1,661,782,846✔
401
    *pTableCxt = *tmp;
1,064,744,072✔
402
    if (!ignoreColVals) {
1,064,744,072✔
403
      resetColValues((*pTableCxt)->pValues);
33,065,705✔
404
    }
405
    return TSDB_CODE_SUCCESS;
1,064,744,072✔
406
  }
407
  int32_t code = createTableDataCxt(pTableMeta, pCreateTbReq, pTableCxt, colMode, ignoreColVals);
597,038,774✔
408
  if (TSDB_CODE_SUCCESS == code) {
597,034,966✔
409
    void* pData = *pTableCxt;  // deal scan coverity
597,036,675✔
410
    code = taosHashPut(pHash, id, idLen, &pData, POINTER_BYTES);
597,037,492✔
411
  }
412

413
  if (TSDB_CODE_SUCCESS != code) {
597,043,053✔
414
    insDestroyTableDataCxt(*pTableCxt);
×
415
  }
416
  return code;
597,044,665✔
417
}
418

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

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

424
  if (IS_VAR_DATA_TYPE(pVal->value.type) || TSDB_DATA_TYPE_DECIMAL == pVal->value.type) {
2,147,483,647✔
425
    taosMemoryFreeClear(pVal->value.pData);
1,821,001,110✔
426
  }
427
}
428

429
void insDestroyTableDataCxt(STableDataCxt* pTableCxt) {
602,691,106✔
430
  if (NULL == pTableCxt) {
602,691,106✔
431
    return;
×
432
  }
433

434
  taosMemoryFreeClear(pTableCxt->pMeta);
602,691,106✔
435
  tDestroyTSchema(pTableCxt->pSchema);
602,688,473✔
436
  qDestroyBoundColInfo(&pTableCxt->boundColsInfo);
602,688,616✔
437
  taosArrayDestroyEx(pTableCxt->pValues, destroyColVal);
602,688,739✔
438
  if (pTableCxt->pData) {
602,688,725✔
439
    tDestroySubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
13,580,746✔
440
    taosMemoryFree(pTableCxt->pData);
13,577,214✔
441
  }
442
  taosMemoryFree(pTableCxt);
602,687,583✔
443
}
444

445
static void destroyColValSml(void* p) {
9,785,262✔
446
  SColVal* pVal = p;
9,785,262✔
447
  if (TSDB_DATA_TYPE_NCHAR == pVal->value.type || TSDB_DATA_TYPE_GEOMETRY == pVal->value.type ||
9,785,262✔
448
      TSDB_DATA_TYPE_VARBINARY == pVal->value.type) {
9,555,347✔
449
    taosMemoryFreeClear(pVal->value.pData);
234,096✔
450
  }
451
}
9,785,343✔
452

453
static void insDestroyTableDataCxtSml(STableDataCxt* pTableCxt) {
707,817✔
454
  if (NULL == pTableCxt) {
707,817✔
455
    return;
×
456
  }
457

458
  taosMemoryFreeClear(pTableCxt->pMeta);
707,817✔
459
  tDestroyTSchema(pTableCxt->pSchema);
707,817✔
460
  qDestroyBoundColInfo(&pTableCxt->boundColsInfo);
707,817✔
461
  taosArrayDestroyEx(pTableCxt->pValues, destroyColValSml);
707,817✔
462
  if (pTableCxt->pData) {
707,817✔
463
    tDestroySubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
707,817✔
464
    taosMemoryFree(pTableCxt->pData);
707,817✔
465
  }
466
  taosMemoryFree(pTableCxt);
707,805✔
467
}
468

469
void insDestroyVgroupDataCxt(SVgroupDataCxt* pVgCxt) {
569,837,767✔
470
  if (NULL == pVgCxt) {
569,837,767✔
471
    return;
×
472
  }
473

474
  tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
569,837,767✔
475
  taosMemoryFree(pVgCxt->pData);
569,834,456✔
476

477
  taosMemoryFree(pVgCxt);
569,827,547✔
478
}
479

480
void insDestroyVgroupDataCxtList(SArray* pVgCxtList) {
1,109,833,558✔
481
  if (NULL == pVgCxtList) {
1,109,833,558✔
482
    return;
555,031,070✔
483
  }
484

485
  size_t size = taosArrayGetSize(pVgCxtList);
554,802,488✔
486
  for (int32_t i = 0; i < size; i++) {
1,124,639,146✔
487
    void* p = taosArrayGetP(pVgCxtList, i);
569,843,823✔
488
    insDestroyVgroupDataCxt(p);
569,847,120✔
489
  }
490

491
  taosArrayDestroy(pVgCxtList);
554,795,323✔
492
}
493

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

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

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

506
  taosHashCleanup(pVgCxtHash);
×
507
}
508

509
void insDestroyTableDataCxtHashMap(SHashObj* pTableCxtHash) {
554,805,703✔
510
  if (NULL == pTableCxtHash) {
554,805,703✔
511
    return;
6,550,694✔
512
  }
513

514
  void** p = taosHashIterate(pTableCxtHash, NULL);
548,255,009✔
515
  while (p) {
1,137,953,326✔
516
    insDestroyTableDataCxt(*(STableDataCxt**)p);
589,697,005✔
517

518
    p = taosHashIterate(pTableCxtHash, p);
589,694,896✔
519
  }
520

521
  taosHashCleanup(pTableCxtHash);
548,256,321✔
522
}
523

524
void insDestroyTableDataCxtHashMapSml(SHashObj* pTableCxtHash) {
970,570✔
525
  if (NULL == pTableCxtHash) {
970,570✔
526
    return;
×
527
  }
528

529
  void** p = taosHashIterate(pTableCxtHash, NULL);
970,570✔
530
  while (p) {
1,678,387✔
531
    insDestroyTableDataCxtSml(*(STableDataCxt**)p);
707,817✔
532

533
    p = taosHashIterate(pTableCxtHash, p);
707,805✔
534
  }
535

536
  taosHashCleanup(pTableCxtHash);
970,570✔
537
}
538

539
static int32_t fillVgroupDataCxt(STableDataCxt* pTableCxt, SVgroupDataCxt* pVgCxt, bool isRebuild, bool clear) {
598,343,172✔
540
  int32_t code = 0;
598,343,172✔
541
  if (NULL == pVgCxt->pData->aSubmitTbData) {
598,343,172✔
542
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
569,496,881✔
543
    if (pVgCxt->pData->aSubmitTbData == NULL) {
569,493,300✔
544
      return terrno;
×
545
    }
546
    if (pTableCxt->hasBlob) {
569,493,305✔
547
      pVgCxt->pData->aSubmitBlobData = taosArrayInit(128, sizeof(SBlobSet*));
21,818✔
548
      if (NULL == pVgCxt->pData->aSubmitBlobData) {
21,818✔
549
        return terrno;
×
550
      }
551
    }
552
  }
553

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

557
  if (NULL == taosArrayPush(pVgCxt->pData->aSubmitTbData, pTableCxt->pData)) {
1,196,704,301✔
558
    return terrno;
×
559
  }
560

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

569
  if (isRebuild) {
598,343,989✔
570
    code = rebuildTableData(pTableCxt->pData, &pTableCxt->pData, pTableCxt->hasBlob);
7,217,718✔
571
  } else if (clear) {
591,126,271✔
572
    taosMemoryFreeClear(pTableCxt->pData);
589,191,445✔
573
  }
574
  parserDebug("uid:%" PRId64 ", add table data context to vgId:%d", pTableCxt->pMeta->uid, pVgCxt->vgId);
598,345,403✔
575

576
  return code;
598,347,363✔
577
}
578

579
static int32_t createVgroupDataCxt(int32_t vgId, SHashObj* pVgroupHash, SArray* pVgroupList, SVgroupDataCxt** pOutput) {
569,938,271✔
580
  SVgroupDataCxt* pVgCxt = taosMemoryCalloc(1, sizeof(SVgroupDataCxt));
569,938,271✔
581
  if (NULL == pVgCxt) {
569,939,675✔
582
    return terrno;
×
583
  }
584
  pVgCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitReq2));
569,939,675✔
585
  if (NULL == pVgCxt->pData) {
569,933,486✔
586
    insDestroyVgroupDataCxt(pVgCxt);
×
587
    return terrno;
×
588
  }
589

590
  pVgCxt->vgId = vgId;
569,934,645✔
591
  int32_t code = taosHashPut(pVgroupHash, &pVgCxt->vgId, sizeof(pVgCxt->vgId), &pVgCxt, POINTER_BYTES);
569,937,774✔
592
  if (TSDB_CODE_SUCCESS == code) {
569,945,138✔
593
    if (NULL == taosArrayPush(pVgroupList, &pVgCxt)) {
569,944,262✔
594
      code = terrno;
×
595
      insDestroyVgroupDataCxt(pVgCxt);
×
596
      return code;
×
597
    }
598
    //    uDebug("td23101 2vgId:%d, uid:%" PRIu64, pVgCxt->vgId, pTableCxt->pMeta->uid);
599
    *pOutput = pVgCxt;
569,944,262✔
600
  } else {
601
    insDestroyVgroupDataCxt(pVgCxt);
×
602
  }
603
  return code;
569,942,460✔
604
}
605

606
int insColDataComp(const void* lp, const void* rp) {
27,988,453✔
607
  SColData* pLeft = (SColData*)lp;
27,988,453✔
608
  SColData* pRight = (SColData*)rp;
27,988,453✔
609
  if (pLeft->cid < pRight->cid) {
27,988,453✔
610
    return -1;
27,950,584✔
611
  } else if (pLeft->cid > pRight->cid) {
48,300✔
612
    return 1;
48,300✔
613
  }
614

615
  return 0;
×
616
}
617

618
int32_t insTryAddTableVgroupInfo(SHashObj* pAllVgHash, SStbInterlaceInfo* pBuildInfo, int32_t* vgId,
62,661✔
619
                                 STableColsData* pTbData, SName* sname) {
620
  if (*vgId >= 0 && taosHashGet(pAllVgHash, (const char*)vgId, sizeof(*vgId))) {
62,661✔
621
    return TSDB_CODE_SUCCESS;
52,378✔
622
  }
623

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

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

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

640
  return TSDB_CODE_SUCCESS;
10,345✔
641
}
642

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

648
  if (pTbData->getFromHash) {
2,738,611✔
649
    pTbInfo = (STableVgUid*)tSimpleHashGet(pBuildInfo->pTableHash, pTbData->tbName, strlen(pTbData->tbName));
2,676,393✔
650
  }
651

652
  if (NULL == pTbInfo) {
2,738,990✔
653
    SName sname;
49,632✔
654
    code = qCreateSName2(&sname, pTbData->tbName, pBuildInfo->acctId, pBuildInfo->dbname, NULL, 0);
63,083✔
655
    if (TSDB_CODE_SUCCESS != code) {
63,033✔
656
      return code;
360✔
657
    }
658

659
    STableMeta*      pTableMeta = NULL;
63,033✔
660
    SRequestConnInfo conn = {.pTrans = pBuildInfo->transport,
112,548✔
661
                             .requestId = pBuildInfo->requestId,
63,033✔
662
                             .requestObjRefId = pBuildInfo->requestSelf,
62,994✔
663
                             .mgmtEps = pBuildInfo->mgmtEpSet};
664
    code = catalogGetTableMeta((SCatalog*)pBuildInfo->pCatalog, &conn, &sname, &pTableMeta);
62,994✔
665

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

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

676
    *uid = pTableMeta->uid;
62,700✔
677
    *vgId = pTableMeta->vgId;
62,622✔
678
    *suid = pTableMeta->suid;
62,660✔
679

680
    STableVgUid tbInfo = {.uid = *uid, .vgid = *vgId, .suid = *suid};
62,660✔
681
    code = tSimpleHashPut(pBuildInfo->pTableHash, pTbData->tbName, strlen(pTbData->tbName), &tbInfo, sizeof(tbInfo));
62,583✔
682
    if (TSDB_CODE_SUCCESS == code) {
62,673✔
683
      code = insTryAddTableVgroupInfo(pAllVgHash, pBuildInfo, vgId, pTbData, &sname);
62,634✔
684
    }
685

686
    taosMemoryFree(pTableMeta);
62,761✔
687
  } else {
688
    *uid = pTbInfo->uid;
2,675,907✔
689
    *vgId = pTbInfo->vgid;
2,676,046✔
690
    *suid = pTbInfo->suid;
2,676,185✔
691
  }
692

693
  return code;
2,738,957✔
694
}
695

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

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

704
  return code;
×
705
}
706

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

722
  int32_t  code = TSDB_CODE_SUCCESS;
804,911✔
723
  SArray** rowP = NULL;
804,911✔
724

725
  rowP = (SArray**)tSimpleHashGet(pTableNameHash, tbname, strlen(tbname));
804,911✔
726

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

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

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

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

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

763
  if (pTbCtx->hasBlob == 0) {
805,052✔
764
    pTbCtx->pData->pBlobSet = NULL;  // if no blob, set it to NULL
805,016✔
765
  }
766

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

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

779
  code = tSimpleHashPut(pTableNameHash, tbname, strlen(tbname), &pTbCtx->pData->aRowP, sizeof(SArray*));
804,988✔
780

781
  if (code != TSDB_CODE_SUCCESS) {
804,959✔
782
    return code;
×
783
  }
784

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

787
  return TSDB_CODE_SUCCESS;
804,986✔
788
}
789

790
int32_t insAppendStmtTableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
1,933,725✔
791
                                  SStbInterlaceInfo* pBuildInfo) {
792
  int32_t  code = TSDB_CODE_SUCCESS;
1,933,725✔
793
  uint64_t uid;
1,921,015✔
794
  int32_t  vgId;
1,921,265✔
795
  uint64_t suid;
1,921,365✔
796

797
  pTbCtx->pData->aRowP = pTbData->aCol;
1,934,275✔
798

799
  code = insGetStmtTableVgUid(pAllVgHash, pBuildInfo, pTbData, &uid, &vgId, &suid);
1,934,675✔
800
  if (TSDB_CODE_SUCCESS != code) {
1,933,975✔
801
    return code;
×
802
  }
803

804
  pTbCtx->pMeta->vgId = vgId;
1,933,975✔
805
  pTbCtx->pMeta->uid = uid;
1,934,025✔
806
  pTbCtx->pData->uid = uid;
1,934,324✔
807

808
  if (!pTbCtx->ordered) {
1,933,933✔
809
    code = tRowSort(pTbCtx->pData->aRowP);
9✔
810
  }
811
  if (code == TSDB_CODE_SUCCESS && (!pTbCtx->ordered || pTbCtx->duplicateTs)) {
1,933,924✔
812
    code = tRowMerge(pTbCtx->pData->aRowP, pTbCtx->pSchema, 0);
9✔
813
  }
814

815
  if (TSDB_CODE_SUCCESS != code) {
1,933,724✔
816
    return code;
×
817
  }
818

819
  SVgroupDataCxt* pVgCxt = NULL;
1,933,724✔
820
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
1,933,874✔
821
  if (NULL == pp) {
1,934,383✔
822
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
313,898✔
823
    if (NULL == pp) {
313,940✔
824
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
313,940✔
825
    } else {
826
      pVgCxt = *(SVgroupDataCxt**)pp;
×
827
    }
828
  } else {
829
    pVgCxt = *(SVgroupDataCxt**)pp;
1,620,485✔
830
  }
831

832
  if (TSDB_CODE_SUCCESS == code) {
1,934,324✔
833
    code = fillVgroupDataCxt(pTbCtx, pVgCxt, false, false);
1,934,324✔
834
  }
835

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

843
  return code;
1,933,132✔
844
}
845

846
int32_t insAppendStmt2TableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
804,892✔
847
                                   SStbInterlaceInfo* pBuildInfo, SVCreateTbReq* ctbReq) {
848
  int32_t  code = TSDB_CODE_SUCCESS;
804,892✔
849
  uint64_t uid;
289,390✔
850
  int32_t  vgId;
289,390✔
851
  uint64_t suid;
289,390✔
852

853
  pTbCtx->pData->aRowP = pTbData->aCol;
804,892✔
854
  pTbCtx->pData->pBlobSet = pTbData->pBlobSet;
804,971✔
855

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

875
    pTbCtx->pMeta->vgId = vgId;
804,642✔
876
    pTbCtx->pMeta->uid = uid;
804,565✔
877
    pTbCtx->pData->uid = uid;
804,566✔
878
    pTbCtx->pData->pCreateTbReq = NULL;
804,605✔
879

880
    if (ctbReq != NULL) {
804,605✔
881
      tdDestroySVCreateTbReq(ctbReq);
882
      taosMemoryFree(ctbReq);
435,321✔
883
      ctbReq = NULL;
435,294✔
884
    }
885
  }
886

887
  if (pTbCtx->hasBlob == 0) {
805,001✔
888
    if (!pTbData->isOrdered) {
805,082✔
889
      code = tRowSort(pTbCtx->pData->aRowP);
×
890
    }
891
    if (code == TSDB_CODE_SUCCESS && (!pTbData->isOrdered || pTbData->isDuplicateTs)) {
804,926✔
892
      code = tRowMerge(pTbCtx->pData->aRowP, pTbCtx->pSchema, PREFER_NON_NULL);
×
893
    }
894
  } else {
895
    if (!pTbData->isOrdered) {
36✔
896
      code = tRowSortWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet);
×
897
    }
898
    if (code == TSDB_CODE_SUCCESS && (!pTbData->isOrdered || pTbData->isDuplicateTs)) {
36✔
899
      code = tRowMergeWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet, 0);
×
900
    }
901
  }
902

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

907
  SVgroupDataCxt* pVgCxt = NULL;
804,995✔
908
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
804,995✔
909
  if (NULL == pp) {
804,989✔
910
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
444,469✔
911
    if (NULL == pp) {
444,514✔
912
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
444,514✔
913
    } else {
914
      pVgCxt = *(SVgroupDataCxt**)pp;
×
915
    }
916
  } else {
917
    pVgCxt = *(SVgroupDataCxt**)pp;
360,520✔
918
  }
919

920
  if (code == TSDB_CODE_SUCCESS) {
804,977✔
921
    code = checkAndMergeSVgroupDataCxtByTbname(pTbCtx, pVgCxt, pBuildInfo->pTableRowDataHash, pTbData->tbName);
804,959✔
922
  }
923

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

931
  return code;
804,944✔
932
}
933

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

944
  int32_t code = TSDB_CODE_SUCCESS;
945

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

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

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

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

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

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

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

988
  return code;
989
}
990
*/
991

992
static int8_t colDataHasBlob(SColData* pCol) {
×
993
  if (IS_STR_DATA_BLOB(pCol->type)) {
×
994
    return 1;
×
995
  }
996
  return 0;
×
997
}
998
int32_t insMergeTableDataCxt(SHashObj* pTableHash, SArray** pVgDataBlocks, bool isRebuild) {
554,209,125✔
999
  SHashObj* pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
554,209,125✔
1000
  SArray*   pVgroupList = taosArrayInit(8, POINTER_BYTES);
554,219,540✔
1001
  if (NULL == pVgroupHash || NULL == pVgroupList) {
554,210,529✔
1002
    taosHashCleanup(pVgroupHash);
×
1003
    taosArrayDestroy(pVgroupList);
×
1004
    return terrno;
×
1005
  }
1006

1007
  int32_t code = TSDB_CODE_SUCCESS;
554,211,438✔
1008
  bool    colFormat = false;
554,211,438✔
1009

1010
  void* p = taosHashIterate(pTableHash, NULL);
554,211,438✔
1011
  if (p) {
554,223,666✔
1012
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
554,224,973✔
1013
    colFormat = (0 != (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT));
554,225,377✔
1014
  }
1015

1016
  while (TSDB_CODE_SUCCESS == code && NULL != p) {
1,150,644,649✔
1017
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
596,417,966✔
1018
    if (colFormat) {
596,417,966✔
1019
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
6,585,272✔
1020
      if (pCol && pCol->nVal <= 0) {
6,584,638✔
1021
        p = taosHashIterate(pTableHash, p);
126✔
1022
        continue;
126✔
1023
      }
1024

1025
      if (pTableCxt->pData->pCreateTbReq) {
6,584,829✔
1026
        pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
6,446,402✔
1027
      }
1028
      int8_t isBlob = IS_STR_DATA_BLOB(pCol->type) ? 1 : 0;
6,582,909✔
1029
      if (isBlob == 0) {
6,583,543✔
1030
        taosArraySort(pTableCxt->pData->aCol, insColDataComp);
6,583,226✔
1031
        code = tColDataSortMerge(&pTableCxt->pData->aCol);
6,581,958✔
1032
      } else {
1033
        taosArraySort(pTableCxt->pData->aCol, insColDataComp);
317✔
1034
        code = tColDataSortMergeWithBlob(&pTableCxt->pData->aCol, pTableCxt->pData->pBlobSet);
×
1035
      }
1036
    } else {
1037
      // skip the table has no data to insert
1038
      // eg: import a csv without valid data
1039
      // if (0 == taosArrayGetSize(pTableCxt->pData->aRowP)) {
1040
      //   parserWarn("no row in tableDataCxt uid:%" PRId64 " ", pTableCxt->pMeta->uid);
1041
      //   p = taosHashIterate(pTableHash, p);
1042
      //   continue;
1043
      // }
1044
      if (pTableCxt->hasBlob == 0) {
589,832,694✔
1045
        if (!pTableCxt->ordered) {
589,813,125✔
1046
          code = tRowSort(pTableCxt->pData->aRowP);
1,405,264✔
1047
        }
1048
        if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
589,809,977✔
1049
          code = tRowMerge(pTableCxt->pData->aRowP, pTableCxt->pSchema, PREFER_NON_NULL);
1,536,163✔
1050
        }
1051
      } else {
1052
        if (!pTableCxt->ordered) {
21,818✔
1053
          code = tRowSortWithBlob(pTableCxt->pData->aRowP, pTableCxt->pSchema, pTableCxt->pData->pBlobSet);
748✔
1054
        }
1055
        if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
21,818✔
1056
          code = tRowMergeWithBlob(pTableCxt->pData->aRowP, pTableCxt->pSchema, pTableCxt->pData->pBlobSet, 0);
748✔
1057
        }
1058
      }
1059
    }
1060

1061
    if (TSDB_CODE_SUCCESS == code) {
596,412,226✔
1062
      SVgroupDataCxt* pVgCxt = NULL;
596,412,226✔
1063
      int32_t         vgId = pTableCxt->pMeta->vgId;
596,412,319✔
1064
      void**          pp = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
596,416,195✔
1065
      if (NULL == pp) {
596,412,439✔
1066
        code = createVgroupDataCxt(vgId, pVgroupHash, pVgroupList, &pVgCxt);
569,180,443✔
1067
      } else {
1068
        pVgCxt = *(SVgroupDataCxt**)pp;
27,231,996✔
1069
      }
1070
      if (TSDB_CODE_SUCCESS == code) {
596,415,545✔
1071
        code = fillVgroupDataCxt(pTableCxt, pVgCxt, isRebuild, true);
596,415,545✔
1072
      }
1073
    }
1074
    if (TSDB_CODE_SUCCESS == code) {
596,411,677✔
1075
      p = taosHashIterate(pTableHash, p);
596,411,677✔
1076
    }
1077
  }
1078

1079
  taosHashCleanup(pVgroupHash);
554,226,683✔
1080
  if (TSDB_CODE_SUCCESS == code) {
554,212,532✔
1081
    *pVgDataBlocks = pVgroupList;
554,213,069✔
1082
  } else {
1083
    insDestroyVgroupDataCxtList(pVgroupList);
×
1084
  }
1085

1086
  return code;
554,214,905✔
1087
}
1088

1089
static int32_t buildSubmitReq(int32_t vgId, SSubmitReq2* pReq, void** pData, uint32_t* pLen) {
569,930,332✔
1090
  int32_t  code = TSDB_CODE_SUCCESS;
569,930,332✔
1091
  uint32_t len = 0;
569,930,332✔
1092
  void*    pBuf = NULL;
569,930,332✔
1093
  tEncodeSize(tEncodeSubmitReq, pReq, len, code);
569,930,332✔
1094
  if (TSDB_CODE_SUCCESS == code) {
569,928,391✔
1095
    SEncoder encoder;
567,356,935✔
1096
    len += sizeof(SSubmitReq2Msg);
569,938,455✔
1097
    pBuf = taosMemoryMalloc(len);
569,938,455✔
1098
    if (NULL == pBuf) {
569,913,156✔
1099
      return terrno;
×
1100
    }
1101
    ((SSubmitReq2Msg*)pBuf)->header.vgId = htonl(vgId);
569,913,156✔
1102
    ((SSubmitReq2Msg*)pBuf)->header.contLen = htonl(len);
569,927,857✔
1103
    ((SSubmitReq2Msg*)pBuf)->version = htobe64(1);
569,931,398✔
1104
    tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SSubmitReq2Msg)), len - sizeof(SSubmitReq2Msg));
569,940,815✔
1105
    code = tEncodeSubmitReq(&encoder, pReq);
569,940,651✔
1106
    tEncoderClear(&encoder);
569,942,727✔
1107
  }
1108

1109
  if (TSDB_CODE_SUCCESS == code) {
569,928,812✔
1110
    *pData = pBuf;
569,928,812✔
1111
    *pLen = len;
569,937,247✔
1112
  } else {
1113
    taosMemoryFree(pBuf);
×
1114
  }
1115
  return code;
569,929,190✔
1116
}
1117

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

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

1131
  if (p->aSubmitBlobData != NULL) {
569,937,009✔
1132
    for (int32_t i = 0; i < taosArrayGetSize(p->aSubmitTbData); i++) {
43,636✔
1133
      SSubmitTbData* pSubmitTbData = taosArrayGet(p->aSubmitTbData, i);
21,818✔
1134
      SBlobSet**     ppBlob = taosArrayGet(p->aSubmitBlobData, i);
21,818✔
1135
      SBlobSet*      pBlob = ppBlob ? *ppBlob : NULL;
21,818✔
1136
      int32_t        nrow = taosArrayGetSize(pSubmitTbData->aRowP);
21,818✔
1137
      int32_t        nblob = 0;
21,818✔
1138
      if (nrow > 0 && pBlob) {
21,818✔
1139
        nblob = taosArrayGetSize(pBlob->pSeqTable);
21,818✔
1140
      }
1141
      uTrace("blob %p row size %d, pData size %d", pBlob, nblob, nrow);
21,818✔
1142
      pSubmitTbData->pBlobSet = pBlob;
21,818✔
1143
      if (ppBlob != NULL) *ppBlob = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
21,818✔
1144
    }
1145
  } else {
1146
    for (int32_t i = 0; i < taosArrayGetSize(p->aSubmitTbData); i++) {
1,169,038,697✔
1147
      SSubmitTbData* pSubmitTbData = taosArrayGet(p->aSubmitTbData, i);
599,121,163✔
1148
      pSubmitTbData->pBlobSet = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
599,129,699✔
1149
    }
1150
  }
1151

1152
  return code;
569,941,767✔
1153
}
1154
int32_t insBuildVgDataBlocks(SHashObj* pVgroupsHashObj, SArray* pVgDataCxtList, SArray** pVgDataBlocks, bool append) {
554,904,932✔
1155
  size_t  numOfVg = taosArrayGetSize(pVgDataCxtList);
554,904,932✔
1156
  SArray* pDataBlocks = (append && *pVgDataBlocks) ? *pVgDataBlocks : taosArrayInit(numOfVg, POINTER_BYTES);
554,910,971✔
1157
  if (NULL == pDataBlocks) {
554,908,338✔
1158
    return TSDB_CODE_OUT_OF_MEMORY;
×
1159
  }
1160

1161
  int32_t code = TSDB_CODE_SUCCESS;
554,908,338✔
1162
  for (size_t i = 0; TSDB_CODE_SUCCESS == code && i < numOfVg; ++i) {
1,124,846,022✔
1163
    SVgroupDataCxt* src = taosArrayGetP(pVgDataCxtList, i);
569,938,692✔
1164
    if (taosArrayGetSize(src->pData->aSubmitTbData) <= 0) {
569,941,027✔
1165
      continue;
×
1166
    }
1167
    SVgDataBlocks* dst = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
569,943,433✔
1168
    if (NULL == dst) {
569,920,937✔
1169
      code = terrno;
×
1170
    }
1171

1172
    if (TSDB_CODE_SUCCESS == code) {
569,920,937✔
1173
      dst->numOfTables = taosArrayGetSize(src->pData->aSubmitTbData);
569,921,723✔
1174
      code = taosHashGetDup(pVgroupsHashObj, (const char*)&src->vgId, sizeof(src->vgId), &dst->vg);
569,926,154✔
1175
    }
1176
    if (TSDB_CODE_SUCCESS == code) {
569,934,373✔
1177
      code = insResetBlob(src->pData);
569,934,373✔
1178
    }
1179

1180
    if (TSDB_CODE_SUCCESS == code) {
569,941,794✔
1181
      code = buildSubmitReq(src->vgId, src->pData, &dst->pData, &dst->size);
569,941,794✔
1182
    }
1183
    if (TSDB_CODE_SUCCESS == code) {
569,924,021✔
1184
      code = (NULL == taosArrayPush(pDataBlocks, &dst) ? terrno : TSDB_CODE_SUCCESS);
569,934,179✔
1185
    }
1186
    if (TSDB_CODE_SUCCESS != code) {
569,935,217✔
1187
      destroyVgDataBlocks(dst);
×
1188
    }
1189
  }
1190

1191
  if (append) {
554,907,330✔
1192
    if (NULL == *pVgDataBlocks) {
689,854✔
1193
      *pVgDataBlocks = pDataBlocks;
689,842✔
1194
    }
1195
    return code;
690,097✔
1196
  }
1197

1198
  if (TSDB_CODE_SUCCESS == code) {
554,217,476✔
1199
    *pVgDataBlocks = pDataBlocks;
554,206,424✔
1200
  } else {
1201
    taosArrayDestroyP(pDataBlocks, destroyVgDataBlocks);
11,532✔
1202
  }
1203

1204
  return code;
554,215,888✔
1205
}
1206

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

1214
  return false;
×
1215
}
1216

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

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

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

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

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

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

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

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

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

1385
  pTableCxt->pData->flags |= TD_REQ_FROM_TAOX;
36,196✔
1386
  if (tmp == NULL) {
36,196✔
1387
    ret = initTableColSubmitData(pTableCxt);
32,439✔
1388
    if (ret != TSDB_CODE_SUCCESS) {
32,439✔
1389
      uError("initTableColSubmitData error");
×
1390
      goto end;
×
1391
    }
1392
  }
1393

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

1401
  int32_t numOfRows = *(int32_t*)p;
36,196✔
1402
  p += sizeof(int32_t);
36,196✔
1403

1404
  int32_t numOfCols = *(int32_t*)p;
36,196✔
1405
  p += sizeof(int32_t);
36,196✔
1406

1407
  p += sizeof(int32_t);
36,196✔
1408
  p += sizeof(uint64_t);
36,196✔
1409

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

1418
  int32_t* colLength = (int32_t*)p;
36,196✔
1419
  p += sizeof(int32_t) * numOfCols;
36,196✔
1420

1421
  char* pStart = p;
36,196✔
1422

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

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

1433
  bool hasTs = false;
36,196✔
1434
  if (tFields == NULL) {
36,196✔
1435
    int32_t len = TMIN(numOfCols, boundInfo->numOfBound);
1,698✔
1436
    for (int j = 0; j < len; j++) {
5,094✔
1437
      SSchema*    pColSchema = &pSchema[j];
3,962✔
1438
      SSchemaExt* pColExtSchema = &pExtSchemas[j];
3,962✔
1439
      PRCESS_DATA(j, j)
3,962✔
1440
    }
1441
  } else {
1442
    for (int i = 0; i < numFields; i++) {
186,002✔
1443
      for (int j = 0; j < boundInfo->numOfBound; j++) {
1,390,563✔
1444
        SSchema*    pColSchema = &pSchema[j];
1,390,563✔
1445
        SSchemaExt* pColExtSchema = &pExtSchemas[j];
1,390,563✔
1446
        char*       fieldName = NULL;
1,390,563✔
1447
        if (raw) {
1,390,563✔
1448
          fieldName = ((SSchemaWrapper*)tFields)->pSchema[i].name;
1,389,148✔
1449
        } else {
1450
          fieldName = ((TAOS_FIELD*)tFields)[i].name;
1,415✔
1451
        }
1452
        if (strcmp(pColSchema->name, fieldName) == 0) {
1,390,563✔
1453
          PRCESS_DATA(i, j)
151,504✔
1454
          break;
151,504✔
1455
        }
1456
      }
1457
    }
1458
  }
1459

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

1466
  // process NULL data
1467
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
193,972✔
1468
    if (boundInfo->pColIndex[c] != -1) {
158,342✔
1469
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, c);
3,725✔
1470
      ret = tColDataAddValueByDataBlock(pCol, 0, 0, numOfRows, NULL, NULL);
3,725✔
1471
      if (ret != 0) {
3,725✔
1472
        goto end;
×
1473
      }
1474
    } else {
1475
      boundInfo->pColIndex[c] = c;  // restore for next block
154,617✔
1476
    }
1477
  }
1478

1479
end:
36,196✔
1480
  return ret;
36,196✔
1481
}
1482

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

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

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

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