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

taosdata / TDengine / #4469

08 Jul 2025 09:38AM UTC coverage: 62.22% (-1.2%) from 63.381%
#4469

push

travis-ci

web-flow
Merge pull request #31712 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

153678 of 316510 branches covered (48.55%)

Branch coverage included in aggregate %.

56 of 60 new or added lines in 13 files covered. (93.33%)

5035 existing lines in 221 files now uncovered.

238955 of 314529 relevant lines covered (75.97%)

6273248.0 hits per line

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

69.12
/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 "tdatablock.h"
24
#include "tdataformat.h"
25
#include "tmisce.h"
26

27
void qDestroyBoundColInfo(void* pInfo) {
21,226✔
28
  if (NULL == pInfo) {
21,226✔
29
    return;
1,059✔
30
  }
31

32
  SBoundColInfo* pBoundInfo = (SBoundColInfo*)pInfo;
20,167✔
33

34
  taosMemoryFreeClear(pBoundInfo->pColIndex);
20,167!
35
}
36

37
static char* tableNameGetPosition(SToken* pToken, char target) {
2,024,336✔
38
  bool inEscape = false;
2,024,336✔
39
  bool inQuote = false;
2,024,336✔
40
  char quotaStr = 0;
2,024,336✔
41

42
  for (uint32_t i = 0; i < pToken->n; ++i) {
15,595,026✔
43
    if (*(pToken->z + i) == target && (!inEscape) && (!inQuote)) {
14,554,062!
44
      return pToken->z + i;
983,372✔
45
    }
46

47
    if (*(pToken->z + i) == TS_ESCAPE_CHAR) {
13,570,690✔
48
      if (!inQuote) {
165,403!
49
        inEscape = !inEscape;
165,431✔
50
      }
51
    }
52

53
    if (*(pToken->z + i) == '\'' || *(pToken->z + i) == '"') {
13,570,690✔
54
      if (!inEscape) {
24,111✔
55
        if (!inQuote) {
24,056✔
56
          quotaStr = *(pToken->z + i);
12,028✔
57
          inQuote = !inQuote;
12,028✔
58
        } else if (quotaStr == *(pToken->z + i)) {
12,028!
59
          inQuote = !inQuote;
12,028✔
60
        }
61
      }
62
    }
63
  }
64

65
  return NULL;
1,040,964✔
66
}
67

68
int32_t insCreateSName(SName* pName, SToken* pTableName, int32_t acctId, const char* dbName, SMsgBuf* pMsgBuf) {
2,024,334✔
69
  const char* msg1 = "name too long";
2,024,334✔
70
  const char* msg2 = "invalid database name";
2,024,334✔
71
  const char* msg3 = "db is not specified";
2,024,334✔
72
  const char* msg4 = "invalid table name";
2,024,334✔
73

74
  int32_t code = TSDB_CODE_SUCCESS;
2,024,334✔
75
  char*   p = tableNameGetPosition(pTableName, TS_PATH_DELIMITER[0]);
2,024,334✔
76

77
  if (p != NULL) {  // db has been specified in sql string so we ignore current db path
2,024,424✔
78
    int32_t dbLen = p - pTableName->z;
983,357✔
79
    if (dbLen <= 0) {
983,357!
80
      return buildInvalidOperationMsg(pMsgBuf, msg2);
×
81
    }
82
    char name[TSDB_DB_FNAME_LEN] = {0};
983,357✔
83
    strncpy(name, pTableName->z, dbLen);
983,357✔
84
    int32_t actualDbLen = strdequote(name);
983,357✔
85

86
    code = tNameSetDbName(pName, acctId, name, actualDbLen);
983,315✔
87
    if (code != TSDB_CODE_SUCCESS) {
983,326!
88
      return buildInvalidOperationMsg(pMsgBuf, msg1);
×
89
    }
90

91
    int32_t tbLen = pTableName->n - dbLen - 1;
983,326✔
92
    if (tbLen <= 0) {
983,326!
93
      return buildInvalidOperationMsg(pMsgBuf, msg4);
×
94
    }
95

96
    char tbname[TSDB_TABLE_FNAME_LEN] = {0};
983,326✔
97
    strncpy(tbname, p + 1, tbLen);
983,326✔
98
    /*tbLen = */ (void)strdequote(tbname);
983,326✔
99

100
    code = tNameFromString(pName, tbname, T_NAME_TABLE);
983,397✔
101
    if (code != 0) {
983,319!
102
      return buildInvalidOperationMsg(pMsgBuf, msg1);
×
103
    }
104
  } else {  // get current DB name first, and then set it into path
105
    char tbname[TSDB_TABLE_FNAME_LEN] = {0};
1,041,067✔
106
    strncpy(tbname, pTableName->z, pTableName->n);
1,041,067✔
107
    int32_t tbLen = strdequote(tbname);
1,041,067✔
108
    if (tbLen >= TSDB_TABLE_NAME_LEN) {
1,041,047!
109
      return buildInvalidOperationMsg(pMsgBuf, msg1);
×
110
    }
111
    if (tbLen == 0) {
1,041,074!
UNCOV
112
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, "invalid table name");
×
113
    }
114

115
    char name[TSDB_TABLE_FNAME_LEN] = {0};
1,041,074✔
116
    strncpy(name, pTableName->z, pTableName->n);
1,041,074✔
117
    (void)strdequote(name);
1,041,074✔
118

119
    if (dbName == NULL) {
1,041,081✔
120
      return buildInvalidOperationMsg(pMsgBuf, msg3);
7✔
121
    }
122
    if (name[0] == '\0') return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
1,041,074!
123

124
    code = tNameSetDbName(pName, acctId, dbName, strlen(dbName));
1,041,074✔
125
    if (code != TSDB_CODE_SUCCESS) {
1,041,063!
126
      code = buildInvalidOperationMsg(pMsgBuf, msg2);
×
127
      return code;
×
128
    }
129

130
    code = tNameFromString(pName, name, T_NAME_TABLE);
1,041,063✔
131
    if (code != 0) {
1,041,034!
132
      code = buildInvalidOperationMsg(pMsgBuf, msg1);
×
133
    }
134
  }
135

136
  if (NULL != strchr(pName->tname, '.')) {
2,024,380!
137
    code = generateSyntaxErrMsgExt(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, "The table name cannot contain '.'");
×
138
  }
139

140
  return code;
2,024,384✔
141
}
142

143
int16_t insFindCol(SToken* pColname, int16_t start, int16_t end, SSchema* pSchema) {
9,431,504✔
144
  while (start < end) {
58,611,413✔
145
    if (strlen(pSchema[start].name) == pColname->n && strncmp(pColname->z, pSchema[start].name, pColname->n) == 0) {
58,583,632✔
146
      return start;
9,403,723✔
147
    }
148
    ++start;
49,179,909✔
149
  }
150
  return -1;
27,781✔
151
}
152

153
int32_t insBuildCreateTbReq(SVCreateTbReq* pTbReq, const char* tname, STag* pTag, int64_t suid, const char* sname,
68,083✔
154
                            SArray* tagName, uint8_t tagNum, int32_t ttl) {
155
  pTbReq->type = TD_CHILD_TABLE;
68,083✔
156
  pTbReq->ctb.pTag = (uint8_t*)pTag;
68,083✔
157
  pTbReq->name = taosStrdup(tname);
68,083!
158
  if (!pTbReq->name) return terrno;
68,087!
159
  pTbReq->ctb.suid = suid;
68,087✔
160
  pTbReq->ctb.tagNum = tagNum;
68,087✔
161
  if (sname) {
68,087✔
162
    pTbReq->ctb.stbName = taosStrdup(sname);
65,903!
163
    if (!pTbReq->ctb.stbName) return terrno;
65,928!
164
  }
165
  pTbReq->ctb.tagName = taosArrayDup(tagName, NULL);
68,112✔
166
  if (!pTbReq->ctb.tagName) return terrno;
68,114!
167
  pTbReq->ttl = ttl;
68,114✔
168
  pTbReq->commentLen = -1;
68,114✔
169

170
  return TSDB_CODE_SUCCESS;
68,114✔
171
}
172

173
static void initBoundCols(int32_t ncols, int16_t* pBoundCols) {
×
174
  for (int32_t i = 0; i < ncols; ++i) {
×
175
    pBoundCols[i] = i;
×
176
  }
177
}
×
178

179
static int32_t initColValues(STableMeta* pTableMeta, SArray* pValues) {
1,885,641✔
180
  SSchema* pSchemas = getTableColumnSchema(pTableMeta);
1,885,641✔
181
  int32_t  code = 0;
1,885,659✔
182
  for (int32_t i = 0; i < pTableMeta->tableInfo.numOfColumns; ++i) {
77,590,941✔
183
    SColVal val = COL_VAL_NONE(pSchemas[i].colId, pSchemas[i].type);
75,684,106✔
184
    if (NULL == taosArrayPush(pValues, &val)) {
75,705,282!
185
      code = terrno;
×
186
      break;
×
187
    }
188
  }
189
  return code;
1,906,835✔
190
}
191

192
int32_t insInitColValues(STableMeta* pTableMeta, SArray* aColValues) { return initColValues(pTableMeta, aColValues); }
10,487✔
193

194
int32_t insInitBoundColsInfo(int32_t numOfBound, SBoundColInfo* pInfo) {
1,976,361✔
195
  pInfo->numOfCols = numOfBound;
1,976,361✔
196
  pInfo->numOfBound = numOfBound;
1,976,361✔
197
  pInfo->hasBoundCols = false;
1,976,361✔
198
  pInfo->mixTagsCols = false;
1,976,361✔
199
  pInfo->pColIndex = taosMemoryCalloc(numOfBound, sizeof(int16_t));
1,976,361!
200
  if (NULL == pInfo->pColIndex) {
1,976,538!
201
    return terrno;
×
202
  }
203
  for (int32_t i = 0; i < numOfBound; ++i) {
78,524,015✔
204
    pInfo->pColIndex[i] = i;
76,547,477✔
205
  }
206
  return TSDB_CODE_SUCCESS;
1,976,538✔
207
}
208

209
void insResetBoundColsInfo(SBoundColInfo* pInfo) {
22✔
210
  pInfo->numOfBound = pInfo->numOfCols;
22✔
211
  pInfo->hasBoundCols = false;
22✔
212
  pInfo->mixTagsCols = false;
22✔
213
  for (int32_t i = 0; i < pInfo->numOfCols; ++i) {
110✔
214
    pInfo->pColIndex[i] = i;
88✔
215
  }
216
}
22✔
217

218
void insCheckTableDataOrder(STableDataCxt* pTableCxt, SRowKey* rowKey) {
119,533,794✔
219
  // once the data block is disordered, we do NOT keep last timestamp any more
220
  if (!pTableCxt->ordered) {
119,533,794✔
221
    return;
4,404,244✔
222
  }
223

224
  if (tRowKeyCompare(rowKey, &pTableCxt->lastKey) < 0) {
115,129,550✔
225
    pTableCxt->ordered = false;
16,674✔
226
  }
227

228
  if (tRowKeyCompare(rowKey, &pTableCxt->lastKey) == 0) {
115,111,955✔
229
    pTableCxt->duplicateTs = true;
107✔
230
  }
231

232
  // TODO: for variable length data type, we need to copy it out
233
  pTableCxt->lastKey = *rowKey;
115,019,999✔
234
  return;
115,019,999✔
235
}
236

237
void insDestroyBoundColInfo(SBoundColInfo* pInfo) { taosMemoryFreeClear(pInfo->pColIndex); }
5,662,564!
238

239
static int32_t createTableDataCxt(STableMeta* pTableMeta, SVCreateTbReq** pCreateTbReq, STableDataCxt** pOutput,
1,884,324✔
240
                                  bool colMode, bool ignoreColVals) {
241
  STableDataCxt* pTableCxt = taosMemoryCalloc(1, sizeof(STableDataCxt));
1,884,324!
242
  if (NULL == pTableCxt) {
1,884,425!
243
    *pOutput = NULL;
×
244
    return terrno;
×
245
  }
246

247
  int32_t code = TSDB_CODE_SUCCESS;
1,884,425✔
248

249
  pTableCxt->lastKey = (SRowKey){0};
1,884,425✔
250
  pTableCxt->ordered = true;
1,884,425✔
251
  pTableCxt->duplicateTs = false;
1,884,425✔
252

253
  pTableCxt->pMeta = tableMetaDup(pTableMeta);
1,884,425✔
254
  if (NULL == pTableCxt->pMeta) {
1,884,448!
255
    code = TSDB_CODE_OUT_OF_MEMORY;
×
256
  }
257
  if (TSDB_CODE_SUCCESS == code) {
1,884,448!
258
    pTableCxt->pSchema =
1,884,482✔
259
        tBuildTSchema(getTableColumnSchema(pTableMeta), pTableMeta->tableInfo.numOfColumns, pTableMeta->sversion);
1,884,467✔
260
    if (NULL == pTableCxt->pSchema) {
1,884,482!
261
      code = TSDB_CODE_OUT_OF_MEMORY;
×
262
    }
263
  }
264
  if (TSDB_CODE_SUCCESS == code) {
1,884,463✔
265
    code = insInitBoundColsInfo(pTableMeta->tableInfo.numOfColumns, &pTableCxt->boundColsInfo);
1,884,421✔
266
  }
267
  if (TSDB_CODE_SUCCESS == code && !ignoreColVals) {
1,884,553✔
268
    pTableCxt->pValues = taosArrayInit(pTableMeta->tableInfo.numOfColumns, sizeof(SColVal));
1,875,249✔
269
    if (NULL == pTableCxt->pValues) {
1,875,173!
270
      code = terrno;
×
271
    } else {
272
      code = initColValues(pTableMeta, pTableCxt->pValues);
1,875,173✔
273
    }
274
  }
275
  if (TSDB_CODE_SUCCESS == code) {
1,884,480✔
276
    pTableCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
1,884,472!
277
    if (NULL == pTableCxt->pData) {
1,884,522!
278
      code = terrno;
×
279
    } else {
280
      pTableCxt->pData->flags = (pCreateTbReq != NULL && NULL != *pCreateTbReq) ? SUBMIT_REQ_AUTO_CREATE_TABLE : 0;
1,884,522!
281
      pTableCxt->pData->flags |= colMode ? SUBMIT_REQ_COLUMN_DATA_FORMAT : 0;
1,884,522✔
282
      pTableCxt->pData->suid = pTableMeta->suid;
1,884,522✔
283
      pTableCxt->pData->uid = pTableMeta->uid;
1,884,522✔
284
      pTableCxt->pData->sver = pTableMeta->sversion;
1,884,522✔
285
      pTableCxt->pData->pCreateTbReq = pCreateTbReq != NULL ? *pCreateTbReq : NULL;
1,884,522!
286
      if (pCreateTbReq != NULL) *pCreateTbReq = NULL;
1,884,522✔
287
      if (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,884,522✔
288
        pTableCxt->pData->aCol = taosArrayInit(128, sizeof(SColData));
20,305✔
289
        if (NULL == pTableCxt->pData->aCol) {
20,297!
290
          code = terrno;
×
291
        }
292
      } else {
293
        pTableCxt->pData->aRowP = taosArrayInit(128, POINTER_BYTES);
1,864,217✔
294
        if (NULL == pTableCxt->pData->aRowP) {
1,864,169!
295
          code = terrno;
×
296
        }
297
      }
298
    }
299
  }
300
  if (TSDB_CODE_SUCCESS == code) {
1,884,474!
301
    *pOutput = pTableCxt;
1,884,474✔
302
    parserDebug("uid:%" PRId64 ", create table data context, code:%d, vgId:%d", pTableMeta->uid, code,
1,884,474✔
303
                pTableMeta->vgId);
304
  } else {
305
    insDestroyTableDataCxt(pTableCxt);
×
306
  }
307

308
  return code;
1,884,484✔
309
}
310

311
static int32_t rebuildTableData(SSubmitTbData* pSrc, SSubmitTbData** pDst) {
42,463✔
312
  int32_t        code = TSDB_CODE_SUCCESS;
42,463✔
313
  SSubmitTbData* pTmp = taosMemoryCalloc(1, sizeof(SSubmitTbData));
42,463!
314
  if (NULL == pTmp) {
42,472!
315
    code = terrno;
×
316
  } else {
317
    pTmp->flags = pSrc->flags;
42,472✔
318
    pTmp->suid = pSrc->suid;
42,472✔
319
    pTmp->uid = pSrc->uid;
42,472✔
320
    pTmp->sver = pSrc->sver;
42,472✔
321
    pTmp->pCreateTbReq = NULL;
42,472✔
322
    if (pTmp->flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
42,472✔
323
      if (pSrc->pCreateTbReq) {
42,304✔
324
        code = cloneSVreateTbReq(pSrc->pCreateTbReq, &pTmp->pCreateTbReq);
42,290✔
325
      } else {
326
        pTmp->flags &= ~SUBMIT_REQ_AUTO_CREATE_TABLE;
14✔
327
      }
328
    }
329
    if (TSDB_CODE_SUCCESS == code) {
42,470!
330
      if (pTmp->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
42,470✔
331
        pTmp->aCol = taosArrayInit(128, sizeof(SColData));
40,301✔
332
        if (NULL == pTmp->aCol) {
40,301!
333
          code = terrno;
×
334
          taosMemoryFree(pTmp);
×
335
        }
336
      } else {
337
        pTmp->aRowP = taosArrayInit(128, POINTER_BYTES);
2,169✔
338
        if (NULL == pTmp->aRowP) {
2,170!
339
          code = terrno;
×
340
          taosMemoryFree(pTmp);
×
341
        }
342
      }
343
    } else {
344
      taosMemoryFree(pTmp);
×
345
    }
346
  }
347

348
  taosMemoryFree(pSrc);
42,471!
349
  if (TSDB_CODE_SUCCESS == code) {
42,471✔
350
    *pDst = pTmp;
42,467✔
351
  }
352

353
  return code;
42,471✔
354
}
355

356
static void resetColValues(SArray* pValues) {
15,572✔
357
  int32_t num = taosArrayGetSize(pValues);
15,572✔
358
  for (int32_t i = 0; i < num; ++i) {
414,474✔
359
    SColVal* pVal = taosArrayGet(pValues, i);
398,902✔
360
    pVal->flag = CV_FLAG_NONE;
398,902✔
361
  }
362
}
15,572✔
363

364
int32_t insGetTableDataCxt(SHashObj* pHash, void* id, int32_t idLen, STableMeta* pTableMeta,
3,913,968✔
365
                           SVCreateTbReq** pCreateTbReq, STableDataCxt** pTableCxt, bool colMode, bool ignoreColVals) {
366
  STableDataCxt** tmp = (STableDataCxt**)taosHashGet(pHash, id, idLen);
3,913,968✔
367
  if (NULL != tmp) {
3,914,098✔
368
    *pTableCxt = *tmp;
2,029,652✔
369
    if (!ignoreColVals) {
2,029,652✔
370
      resetColValues((*pTableCxt)->pValues);
15,572✔
371
    }
372
    return TSDB_CODE_SUCCESS;
2,029,652✔
373
  }
374
  int32_t code = createTableDataCxt(pTableMeta, pCreateTbReq, pTableCxt, colMode, ignoreColVals);
1,884,446✔
375
  if (TSDB_CODE_SUCCESS == code) {
1,884,482!
376
    void* pData = *pTableCxt;  // deal scan coverity
1,884,485✔
377
    code = taosHashPut(pHash, id, idLen, &pData, POINTER_BYTES);
1,884,485✔
378
  }
379

380
  if (TSDB_CODE_SUCCESS != code) {
1,884,532!
381
    insDestroyTableDataCxt(*pTableCxt);
×
382
  }
383
  return code;
1,884,484✔
384
}
385

386
static void destroyColVal(void* p) {
75,972,357✔
387
  SColVal* pVal = p;
75,972,357✔
388
  if (TSDB_DATA_TYPE_NCHAR == pVal->value.type || TSDB_DATA_TYPE_GEOMETRY == pVal->value.type ||
75,972,357✔
389
      TSDB_DATA_TYPE_VARBINARY == pVal->value.type || TSDB_DATA_TYPE_DECIMAL == pVal->value.type) {
70,714,960✔
390
    taosMemoryFreeClear(pVal->value.pData);
5,448,745!
391
  }
392
}
75,972,357✔
393

394
void insDestroyTableDataCxt(STableDataCxt* pTableCxt) {
1,904,431✔
395
  if (NULL == pTableCxt) {
1,904,431!
396
    return;
×
397
  }
398

399
  taosMemoryFreeClear(pTableCxt->pMeta);
1,904,431!
400
  tDestroyTSchema(pTableCxt->pSchema);
1,904,428!
401
  insDestroyBoundColInfo(&pTableCxt->boundColsInfo);
1,904,430✔
402
  taosArrayDestroyEx(pTableCxt->pValues, destroyColVal);
1,904,453✔
403
  if (pTableCxt->pData) {
1,904,442✔
404
    tDestroySubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
56,217✔
405
    taosMemoryFree(pTableCxt->pData);
56,219!
406
  }
407
  taosMemoryFree(pTableCxt);
1,904,457!
408
}
409

410
void insDestroyVgroupDataCxt(SVgroupDataCxt* pVgCxt) {
1,833,483✔
411
  if (NULL == pVgCxt) {
1,833,483!
412
    return;
×
413
  }
414

415
  tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
1,833,483✔
416
  taosMemoryFree(pVgCxt->pData);
1,833,516!
417
  taosMemoryFree(pVgCxt);
1,833,516!
418
}
419

420
void insDestroyVgroupDataCxtList(SArray* pVgCxtList) {
3,590,438✔
421
  if (NULL == pVgCxtList) {
3,590,438✔
422
    return;
1,797,009✔
423
  }
424

425
  size_t size = taosArrayGetSize(pVgCxtList);
1,793,429✔
426
  for (int32_t i = 0; i < size; i++) {
3,627,022✔
427
    void* p = taosArrayGetP(pVgCxtList, i);
1,833,499✔
428
    insDestroyVgroupDataCxt(p);
1,833,502✔
429
  }
430

431
  taosArrayDestroy(pVgCxtList);
1,793,523✔
432
}
433

434
void insDestroyVgroupDataCxtHashMap(SHashObj* pVgCxtHash) {
×
435
  if (NULL == pVgCxtHash) {
×
436
    return;
×
437
  }
438

439
  void** p = taosHashIterate(pVgCxtHash, NULL);
×
440
  while (p) {
×
441
    insDestroyVgroupDataCxt(*(SVgroupDataCxt**)p);
×
442

443
    p = taosHashIterate(pVgCxtHash, p);
×
444
  }
445

446
  taosHashCleanup(pVgCxtHash);
×
447
}
448

449
void insDestroyTableDataCxtHashMap(SHashObj* pTableCxtHash) {
1,799,316✔
450
  if (NULL == pTableCxtHash) {
1,799,316✔
451
    return;
20,189✔
452
  }
453

454
  void** p = taosHashIterate(pTableCxtHash, NULL);
1,779,127✔
455
  while (p) {
3,643,179✔
456
    insDestroyTableDataCxt(*(STableDataCxt**)p);
1,864,041✔
457

458
    p = taosHashIterate(pTableCxtHash, p);
1,864,037✔
459
  }
460

461
  taosHashCleanup(pTableCxtHash);
1,779,138✔
462
}
463

464
static int32_t fillVgroupDataCxt(STableDataCxt* pTableCxt, SVgroupDataCxt* pVgCxt, bool isRebuild, bool clear) {
1,890,895✔
465
  if (NULL == pVgCxt->pData->aSubmitTbData) {
1,890,895✔
466
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
1,833,045✔
467
    if (NULL == pVgCxt->pData->aSubmitTbData) {
1,833,015!
468
      return terrno;
×
469
    }
470
  }
471

472
  // push data to submit, rebuild empty data for next submit
473
  if (NULL == taosArrayPush(pVgCxt->pData->aSubmitTbData, pTableCxt->pData)) {
3,781,766!
474
    return terrno;
×
475
  }
476
  int32_t code = 0;
1,890,901✔
477
  if (isRebuild) {
1,890,901✔
478
    code = rebuildTableData(pTableCxt->pData, &pTableCxt->pData);
42,473✔
479
  } else if (clear) {
1,848,428✔
480
    taosMemoryFreeClear(pTableCxt->pData);
1,848,427!
481
  }
482

483
  parserDebug("uid:%" PRId64 ", add table data context to vgId:%d", pTableCxt->pMeta->uid, pVgCxt->vgId);
1,890,863✔
484

485
  return code;
1,890,844✔
486
}
487

488
static int32_t createVgroupDataCxt(int32_t vgId, SHashObj* pVgroupHash, SArray* pVgroupList, SVgroupDataCxt** pOutput) {
1,833,778✔
489
  SVgroupDataCxt* pVgCxt = taosMemoryCalloc(1, sizeof(SVgroupDataCxt));
1,833,778!
490
  if (NULL == pVgCxt) {
1,833,813!
491
    return terrno;
×
492
  }
493
  pVgCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitReq2));
1,833,813!
494
  if (NULL == pVgCxt->pData) {
1,833,821!
495
    insDestroyVgroupDataCxt(pVgCxt);
×
496
    return terrno;
×
497
  }
498

499
  pVgCxt->vgId = vgId;
1,833,821✔
500
  int32_t code = taosHashPut(pVgroupHash, &pVgCxt->vgId, sizeof(pVgCxt->vgId), &pVgCxt, POINTER_BYTES);
1,833,821✔
501
  if (TSDB_CODE_SUCCESS == code) {
1,833,823!
502
    if (NULL == taosArrayPush(pVgroupList, &pVgCxt)) {
1,833,814!
503
      code = terrno;
×
504
      insDestroyVgroupDataCxt(pVgCxt);
×
UNCOV
505
      return code;
×
506
    }
507
    //    uDebug("td23101 2vgId:%d, uid:%" PRIu64, pVgCxt->vgId, pTableCxt->pMeta->uid);
508
    *pOutput = pVgCxt;
1,833,814✔
509
  } else {
510
    insDestroyVgroupDataCxt(pVgCxt);
×
511
  }
512
  return code;
1,833,819✔
513
}
514

515
int insColDataComp(const void* lp, const void* rp) {
82,014✔
516
  SColData* pLeft = (SColData*)lp;
82,014✔
517
  SColData* pRight = (SColData*)rp;
82,014✔
518
  if (pLeft->cid < pRight->cid) {
82,014!
519
    return -1;
82,018✔
UNCOV
520
  } else if (pLeft->cid > pRight->cid) {
×
521
    return 1;
6✔
522
  }
523

524
  return 0;
×
525
}
526

527
int32_t insTryAddTableVgroupInfo(SHashObj* pAllVgHash, SStbInterlaceInfo* pBuildInfo, int32_t* vgId,
131✔
528
                                 STableColsData* pTbData, SName* sname) {
529
  if (*vgId >= 0 && taosHashGet(pAllVgHash, (const char*)vgId, sizeof(*vgId))) {
131!
530
    return TSDB_CODE_SUCCESS;
118✔
531
  }
532

533
  SVgroupInfo      vgInfo = {0};
13✔
534
  SRequestConnInfo conn = {.pTrans = pBuildInfo->transport,
13✔
535
                           .requestId = pBuildInfo->requestId,
13✔
536
                           .requestObjRefId = pBuildInfo->requestSelf,
13✔
537
                           .mgmtEps = pBuildInfo->mgmtEpSet};
538

539
  int32_t code = catalogGetTableHashVgroup((SCatalog*)pBuildInfo->pCatalog, &conn, sname, &vgInfo);
13✔
540
  if (TSDB_CODE_SUCCESS != code) {
13!
541
    return code;
×
542
  }
543

544
  code = taosHashPut(pAllVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
13✔
545
  if (TSDB_CODE_SUCCESS != code) {
13!
546
    return code;
×
547
  }
548

549
  return TSDB_CODE_SUCCESS;
13✔
550
}
551

552
int32_t insGetStmtTableVgUid(SHashObj* pAllVgHash, SStbInterlaceInfo* pBuildInfo, STableColsData* pTbData,
1,263✔
553
                             uint64_t* uid, int32_t* vgId) {
554
  STableVgUid* pTbInfo = NULL;
1,263✔
555
  int32_t      code = 0;
1,263✔
556

557
  if (pTbData->getFromHash) {
1,263✔
558
    pTbInfo = (STableVgUid*)tSimpleHashGet(pBuildInfo->pTableHash, pTbData->tbName, strlen(pTbData->tbName));
1,141✔
559
  }
560

561
  if (NULL == pTbInfo) {
1,263✔
562
    SName sname;
563
    code = qCreateSName(&sname, pTbData->tbName, pBuildInfo->acctId, pBuildInfo->dbname, NULL, 0);
165✔
564
    if (TSDB_CODE_SUCCESS != code) {
165!
565
      return code;
34✔
566
    }
567

568
    STableMeta*      pTableMeta = NULL;
165✔
569
    SRequestConnInfo conn = {.pTrans = pBuildInfo->transport,
165✔
570
                             .requestId = pBuildInfo->requestId,
165✔
571
                             .requestObjRefId = pBuildInfo->requestSelf,
165✔
572
                             .mgmtEps = pBuildInfo->mgmtEpSet};
573
    code = catalogGetTableMeta((SCatalog*)pBuildInfo->pCatalog, &conn, &sname, &pTableMeta);
165✔
574

575
    if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
165✔
576
      parserWarn("stmt2 async bind don't find table:%s.%s, try auto create table", sname.dbname, sname.tname);
34!
577
      return code;
34✔
578
    }
579

580
    if (TSDB_CODE_SUCCESS != code) {
131!
581
      parserError("stmt2 async get table meta:%s.%s failed, code:%d", sname.dbname, sname.tname, code);
×
582
      return code;
×
583
    }
584

585
    *uid = pTableMeta->uid;
131✔
586
    *vgId = pTableMeta->vgId;
131✔
587

588
    STableVgUid tbInfo = {.uid = *uid, .vgid = *vgId};
131✔
589
    code = tSimpleHashPut(pBuildInfo->pTableHash, pTbData->tbName, strlen(pTbData->tbName), &tbInfo, sizeof(tbInfo));
131✔
590
    if (TSDB_CODE_SUCCESS == code) {
131!
591
      code = insTryAddTableVgroupInfo(pAllVgHash, pBuildInfo, vgId, pTbData, &sname);
131✔
592
    }
593

594
    taosMemoryFree(pTableMeta);
131!
595
  } else {
596
    *uid = pTbInfo->uid;
1,098✔
597
    *vgId = pTbInfo->vgid;
1,098✔
598
  }
599

600
  return code;
1,229✔
601
}
602

603
int32_t qBuildStmtFinOutput1(SQuery* pQuery, SHashObj* pAllVgHash, SArray* pVgDataBlocks) {
×
604
  int32_t             code = TSDB_CODE_SUCCESS;
×
605
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
×
606

607
  if (TSDB_CODE_SUCCESS == code) {
×
608
    code = insBuildVgDataBlocks(pAllVgHash, pVgDataBlocks, &pStmt->pDataBlocks, true);
×
609
  }
610

611
  return code;
×
612
}
613

614
int32_t checkAndMergeSVgroupDataCxtByTbname(STableDataCxt* pTbCtx, SVgroupDataCxt* pVgCxt, SSHashObj* pTableNameHash,
1,265✔
615
                                            char* tbname) {
616
  if (NULL == pVgCxt->pData->aSubmitTbData) {
1,265✔
617
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
757✔
618
    if (NULL == pVgCxt->pData->aSubmitTbData) {
757!
619
      return terrno;
×
620
    }
621
  }
622

623
  int32_t        code = TSDB_CODE_SUCCESS;
1,265✔
624
  SArray**       rowP = NULL;
1,265✔
625

626
  rowP = (SArray**)tSimpleHashGet(pTableNameHash, tbname, strlen(tbname));
1,265✔
627

628
  if (rowP != NULL && *rowP != NULL) {
1,264!
629
    for (int32_t j = 0; j < taosArrayGetSize(*rowP); ++j) {
81✔
630
      SRow* pRow = (SRow*)taosArrayGetP(pTbCtx->pData->aRowP, j);
57✔
631
      if (pRow) {
57✔
632
        if (NULL == taosArrayPush(*rowP, &pRow)) {
60!
633
          return terrno;
×
634
        }
635
      }
636

637
      code = tRowSort(*rowP);
57✔
638
      if (code != TSDB_CODE_SUCCESS) {
57!
639
        return code;
×
640
      }
641
      code = tRowMerge(*rowP, pTbCtx->pSchema, 0);
57✔
642
      if (code != TSDB_CODE_SUCCESS) {
57!
643
        return code;
×
644
      }
645
    }
646

647
    parserDebug("merge same uid data: %" PRId64 ", vgId:%d", pTbCtx->pData->uid, pVgCxt->vgId);
24!
648

649
    if (pTbCtx->pData->pCreateTbReq != NULL) {
24✔
650
      tdDestroySVCreateTbReq(pTbCtx->pData->pCreateTbReq);
4!
651
      taosMemoryFree(pTbCtx->pData->pCreateTbReq);
4!
652
      pTbCtx->pData->pCreateTbReq = NULL;
4✔
653
    }
654

655
    return TSDB_CODE_SUCCESS;
24✔
656
  }
657

658
  if (NULL == taosArrayPush(pVgCxt->pData->aSubmitTbData, pTbCtx->pData)) {
2,480!
659
    return terrno;
×
660
  }
661

662
  code = tSimpleHashPut(pTableNameHash, tbname, strlen(tbname), &pTbCtx->pData->aRowP, sizeof(SArray*));
1,240✔
663

664
  if (code != TSDB_CODE_SUCCESS) {
1,241!
665
    return code;
×
666
  }
667

668
  parserDebug("uid:%" PRId64 ", add table data context to vgId:%d", pTbCtx->pMeta->uid, pVgCxt->vgId);
1,241!
669

670
  return TSDB_CODE_SUCCESS;
1,241✔
671
}
672

673
int32_t insAppendStmtTableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
1,261✔
674
                                  SStbInterlaceInfo* pBuildInfo, SVCreateTbReq* ctbReq) {
675
  int32_t  code = TSDB_CODE_SUCCESS;
1,261✔
676
  uint64_t uid;
677
  int32_t  vgId;
678

679
  pTbCtx->pData->aRowP = pTbData->aCol;
1,261✔
680

681
  code = insGetStmtTableVgUid(pAllVgHash, pBuildInfo, pTbData, &uid, &vgId);
1,261✔
682
  if (ctbReq != NULL && code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
1,263✔
683
    pTbCtx->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
34✔
684
    vgId = (int32_t)ctbReq->uid;
34✔
685
    uid = 0;
34✔
686
    pTbCtx->pMeta->vgId = (int32_t)ctbReq->uid;
34✔
687
    ctbReq->uid = 0;
34✔
688
    pTbCtx->pMeta->uid = 0;
34✔
689
    pTbCtx->pData->uid = 0;
34✔
690
    pTbCtx->pData->pCreateTbReq = ctbReq;
34✔
691
    code = TSDB_CODE_SUCCESS;
34✔
692
  } else {
693
    if (TSDB_CODE_SUCCESS != code) {
1,229!
694
      return code;
×
695
    }
696
    pTbCtx->pMeta->vgId = vgId;
1,229✔
697
    pTbCtx->pMeta->uid = uid;
1,229✔
698
    pTbCtx->pData->uid = uid;
1,229✔
699
    pTbCtx->pData->pCreateTbReq = NULL;
1,229✔
700

701
    if (ctbReq != NULL) {
1,229✔
702
      tdDestroySVCreateTbReq(ctbReq);
703
      taosMemoryFree(ctbReq);
58!
704
      ctbReq = NULL;
58✔
705
    }
706
  }
707

708
  if (!pTbData->isOrdered) {
1,263✔
709
    code = tRowSort(pTbCtx->pData->aRowP);
543✔
710
  }
711
  if (code == TSDB_CODE_SUCCESS && (!pTbData->isOrdered || pTbData->isDuplicateTs)) {
1,262!
712
    code = tRowMerge(pTbCtx->pData->aRowP, pTbCtx->pSchema, 0);
542✔
713
  }
714

715
  if (TSDB_CODE_SUCCESS != code) {
1,263!
716
    return code;
×
717
  }
718

719
  SVgroupDataCxt* pVgCxt = NULL;
1,263✔
720
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
1,263✔
721
  if (NULL == pp) {
1,264✔
722
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
756✔
723
    if (NULL == pp) {
756!
724
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
756✔
725
    } else {
726
      pVgCxt = *(SVgroupDataCxt**)pp;
×
727
    }
728
  } else {
729
    pVgCxt = *(SVgroupDataCxt**)pp;
508✔
730
  }
731

732
  if (code == TSDB_CODE_SUCCESS) {
1,265!
733
    code = checkAndMergeSVgroupDataCxtByTbname(pTbCtx, pVgCxt, pBuildInfo->pTableRowDataHash, pTbData->tbName);
1,265✔
734
  }
735

736
  if (taosArrayGetSize(pVgCxt->pData->aSubmitTbData) >= 20000) {
1,265!
737
    code = qBuildStmtFinOutput1((SQuery*)pBuildInfo->pQuery, pAllVgHash, pBuildInfo->pVgroupList);
×
738
    // taosArrayClear(pVgCxt->pData->aSubmitTbData);
739
    tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
×
740
    // insDestroyVgroupDataCxt(pVgCxt);
741
  }
742

743
  return code;
1,265✔
744
}
745

746
/*
747
int32_t insMergeStmtTableDataCxt(STableDataCxt* pTableCxt, SArray* pTableList, SArray** pVgDataBlocks, bool isRebuild,
748
int32_t tbNum) { SHashObj* pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, false);
749
  SArray*   pVgroupList = taosArrayInit(8, POINTER_BYTES);
750
  if (NULL == pVgroupHash || NULL == pVgroupList) {
751
    taosHashCleanup(pVgroupHash);
752
    taosArrayDestroy(pVgroupList);
753
    return TSDB_CODE_OUT_OF_MEMORY;
754
  }
755

756
  int32_t code = TSDB_CODE_SUCCESS;
757

758
  for (int32_t i = 0; i < tbNum; ++i) {
759
    STableColsData *pTableCols = (STableColsData*)taosArrayGet(pTableList, i);
760
    pTableCxt->pMeta->vgId = pTableCols->vgId;
761
    pTableCxt->pMeta->uid = pTableCols->uid;
762
    pTableCxt->pData->uid = pTableCols->uid;
763
    pTableCxt->pData->aCol = pTableCols->aCol;
764

765
    SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
766
    if (pCol->nVal <= 0) {
767
      continue;
768
    }
769

770
    if (pTableCxt->pData->pCreateTbReq) {
771
      pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
772
    }
773

774
    taosArraySort(pTableCxt->pData->aCol, insColDataComp);
775

776
    tColDataSortMerge(pTableCxt->pData->aCol);
777

778
    if (TSDB_CODE_SUCCESS == code) {
779
      SVgroupDataCxt* pVgCxt = NULL;
780
      int32_t         vgId = pTableCxt->pMeta->vgId;
781
      void**          pp = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
782
      if (NULL == pp) {
783
        code = createVgroupDataCxt(pTableCxt, pVgroupHash, pVgroupList, &pVgCxt);
784
      } else {
785
        pVgCxt = *(SVgroupDataCxt**)pp;
786
      }
787
      if (TSDB_CODE_SUCCESS == code) {
788
        code = fillVgroupDataCxt(pTableCxt, pVgCxt, false, false);
789
      }
790
    }
791
  }
792

793
  taosHashCleanup(pVgroupHash);
794
  if (TSDB_CODE_SUCCESS == code) {
795
    *pVgDataBlocks = pVgroupList;
796
  } else {
797
    insDestroyVgroupDataCxtList(pVgroupList);
798
  }
799

800
  return code;
801
}
802
*/
803

804
int32_t insMergeTableDataCxt(SHashObj* pTableHash, SArray** pVgDataBlocks, bool isRebuild) {
1,792,905✔
805
  SHashObj* pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
1,792,905✔
806
  SArray*   pVgroupList = taosArrayInit(8, POINTER_BYTES);
1,793,045✔
807
  if (NULL == pVgroupHash || NULL == pVgroupList) {
1,793,078!
UNCOV
808
    taosHashCleanup(pVgroupHash);
×
809
    taosArrayDestroy(pVgroupList);
×
810
    return terrno;
×
811
  }
812

813
  int32_t code = TSDB_CODE_SUCCESS;
1,793,078✔
814
  bool    colFormat = false;
1,793,078✔
815

816
  void* p = taosHashIterate(pTableHash, NULL);
1,793,078✔
817
  if (p) {
1,793,096!
818
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
1,793,096✔
819
    colFormat = (0 != (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT));
1,793,096✔
820
  }
821

822
  while (TSDB_CODE_SUCCESS == code && NULL != p) {
3,684,026✔
823
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
1,890,948✔
824
    if (colFormat) {
1,890,948✔
825
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
40,325✔
826
      if (pCol && pCol->nVal <= 0) {
40,324!
827
        p = taosHashIterate(pTableHash, p);
16✔
828
        continue;
16✔
829
      }
830

831
      if (pTableCxt->pData->pCreateTbReq) {
40,308✔
832
        pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
40,149✔
833
      }
834

835
      taosArraySort(pTableCxt->pData->aCol, insColDataComp);
40,308✔
836

837
      code = tColDataSortMerge(&pTableCxt->pData->aCol);
40,300✔
838
    } else {
839
      // skip the table has no data to insert
840
      // eg: import a csv without valid data
841
      // if (0 == taosArrayGetSize(pTableCxt->pData->aRowP)) {
842
      //   parserWarn("no row in tableDataCxt uid:%" PRId64 " ", pTableCxt->pMeta->uid);
843
      //   p = taosHashIterate(pTableHash, p);
844
      //   continue;
845
      // }
846
      if (!pTableCxt->ordered) {
1,850,623✔
847
        code = tRowSort(pTableCxt->pData->aRowP);
16,674✔
848
      }
849
      if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
1,850,623✔
850
        code = tRowMerge(pTableCxt->pData->aRowP, pTableCxt->pSchema, 0);
16,724✔
851
      }
852
    }
853

854
    if (TSDB_CODE_SUCCESS == code) {
1,890,915!
855
      SVgroupDataCxt* pVgCxt = NULL;
1,890,915✔
856
      int32_t         vgId = pTableCxt->pMeta->vgId;
1,890,915✔
857
      void**          pp = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
1,890,915✔
858
      if (NULL == pp) {
1,890,895✔
859
        code = createVgroupDataCxt(vgId, pVgroupHash, pVgroupList, &pVgCxt);
1,833,028✔
860
      } else {
861
        pVgCxt = *(SVgroupDataCxt**)pp;
57,867✔
862
      }
863
      if (TSDB_CODE_SUCCESS == code) {
1,890,924✔
864
        code = fillVgroupDataCxt(pTableCxt, pVgCxt, isRebuild, true);
1,890,923✔
865
      }
866
    }
867
    if (TSDB_CODE_SUCCESS == code) {
1,890,847!
868
      p = taosHashIterate(pTableHash, p);
1,890,861✔
869
    }
870
  }
871

872
  taosHashCleanup(pVgroupHash);
1,793,078✔
873
  if (TSDB_CODE_SUCCESS == code) {
1,793,085!
874
    *pVgDataBlocks = pVgroupList;
1,793,086✔
875
  } else {
UNCOV
876
    insDestroyVgroupDataCxtList(pVgroupList);
×
877
  }
878

879
  return code;
1,793,090✔
880
}
881

882
static int32_t buildSubmitReq(int32_t vgId, SSubmitReq2* pReq, void** pData, uint32_t* pLen) {
1,833,716✔
883
  int32_t  code = TSDB_CODE_SUCCESS;
1,833,716✔
884
  uint32_t len = 0;
1,833,716✔
885
  void*    pBuf = NULL;
1,833,716✔
886
  tEncodeSize(tEncodeSubmitReq, pReq, len, code);
1,833,716!
887
  if (TSDB_CODE_SUCCESS == code) {
1,833,646!
888
    SEncoder encoder;
889
    len += sizeof(SSubmitReq2Msg);
1,833,756✔
890
    pBuf = taosMemoryMalloc(len);
1,833,756!
891
    if (NULL == pBuf) {
1,833,765!
892
      return terrno;
×
893
    }
894
    ((SSubmitReq2Msg*)pBuf)->header.vgId = htonl(vgId);
1,833,765✔
895
    ((SSubmitReq2Msg*)pBuf)->header.contLen = htonl(len);
1,833,765✔
896
    ((SSubmitReq2Msg*)pBuf)->version = htobe64(1);
1,833,765✔
897
    tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SSubmitReq2Msg)), len - sizeof(SSubmitReq2Msg));
1,833,684✔
898
    code = tEncodeSubmitReq(&encoder, pReq);
1,833,758✔
899
    tEncoderClear(&encoder);
1,833,739✔
900
  }
901

902
  if (TSDB_CODE_SUCCESS == code) {
1,833,802!
903
    *pData = pBuf;
1,833,802✔
904
    *pLen = len;
1,833,802✔
905
  } else {
906
    taosMemoryFree(pBuf);
×
907
  }
908
  return code;
1,833,686✔
909
}
910

911
static void destroyVgDataBlocks(void* p) {
×
912
  if (p == NULL) return;
×
913
  SVgDataBlocks* pVg = p;
×
914
  taosMemoryFree(pVg->pData);
×
915
  taosMemoryFree(pVg);
×
916
}
917

918
int32_t insBuildVgDataBlocks(SHashObj* pVgroupsHashObj, SArray* pVgDataCxtList, SArray** pVgDataBlocks, bool append) {
1,793,703✔
919
  size_t  numOfVg = taosArrayGetSize(pVgDataCxtList);
1,793,703✔
920
  SArray* pDataBlocks = (append && *pVgDataBlocks) ? *pVgDataBlocks : taosArrayInit(numOfVg, POINTER_BYTES);
1,793,776!
921
  if (NULL == pDataBlocks) {
1,793,812!
922
    return TSDB_CODE_OUT_OF_MEMORY;
×
923
  }
924

925
  int32_t code = TSDB_CODE_SUCCESS;
1,793,812✔
926
  for (size_t i = 0; TSDB_CODE_SUCCESS == code && i < numOfVg; ++i) {
3,627,553✔
927
    SVgroupDataCxt* src = taosArrayGetP(pVgDataCxtList, i);
1,833,744✔
928
    if (taosArrayGetSize(src->pData->aSubmitTbData) <= 0) {
1,833,808!
929
      continue;
×
930
    }
931
    SVgDataBlocks* dst = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
1,833,803!
932
    if (NULL == dst) {
1,833,747!
933
      code = terrno;
×
934
    }
935
    if (TSDB_CODE_SUCCESS == code) {
1,833,747!
936
      dst->numOfTables = taosArrayGetSize(src->pData->aSubmitTbData);
1,833,771✔
937
      code = taosHashGetDup(pVgroupsHashObj, (const char*)&src->vgId, sizeof(src->vgId), &dst->vg);
1,833,780✔
938
    }
939
    if (TSDB_CODE_SUCCESS == code) {
1,833,800!
940
      code = buildSubmitReq(src->vgId, src->pData, &dst->pData, &dst->size);
1,833,800✔
941
    }
942
    if (TSDB_CODE_SUCCESS == code) {
1,833,696!
943
      code = (NULL == taosArrayPush(pDataBlocks, &dst) ? terrno : TSDB_CODE_SUCCESS);
1,833,717!
944
    }
945
    if (TSDB_CODE_SUCCESS != code) {
1,833,712!
946
      destroyVgDataBlocks(dst);
×
947
    }
948
  }
949

950
  if (append) {
1,793,809✔
951
    if (NULL == *pVgDataBlocks) {
737!
952
      *pVgDataBlocks = pDataBlocks;
737✔
953
    }
954
    return code;
737✔
955
  }
956

957
  if (TSDB_CODE_SUCCESS == code) {
1,793,072✔
958
    *pVgDataBlocks = pDataBlocks;
1,792,996✔
959
  } else {
960
    taosArrayDestroyP(pDataBlocks, destroyVgDataBlocks);
76✔
961
  }
962

963
  return code;
1,792,977✔
964
}
965

966
static bool findFileds(SSchema* pSchema, TAOS_FIELD* fields, int numFields) {
×
967
  for (int i = 0; i < numFields; i++) {
×
968
    if (strcmp(pSchema->name, fields[i].name) == 0) {
×
969
      return true;
×
970
    }
971
  }
972

973
  return false;
×
974
}
975

976
int32_t checkSchema(SSchema* pColSchema, SSchemaExt* pColExtSchema, int8_t* fields, char* errstr, int32_t errstrLen) {
578✔
977
  if (*fields != pColSchema->type) {
578✔
978
    if (errstr != NULL)
1!
979
      snprintf(errstr, errstrLen, "column type not equal, name:%s, schema type:%s, data type:%s", pColSchema->name,
×
980
               tDataTypes[pColSchema->type].name, tDataTypes[*fields].name);
×
981
    return TSDB_CODE_INVALID_PARA;
1✔
982
  }
983

984
  if (IS_DECIMAL_TYPE(pColSchema->type)) {
577!
985
    uint8_t precision = 0, scale = 0;
1✔
986
    decimalFromTypeMod(pColExtSchema->typeMod, &precision, &scale);
1✔
987
    uint8_t precisionData = 0, scaleData = 0;
1✔
988
    int32_t bytes = *(int32_t*)(fields + sizeof(int8_t));
1✔
989
    extractDecimalTypeInfoFromBytes(&bytes, &precisionData, &scaleData);
1✔
990
    if (precision != precisionData || scale != scaleData) {
1!
991
      if (errstr != NULL)
×
992
        snprintf(errstr, errstrLen,
×
993
                 "column decimal type not equal, name:%s, schema type:%s, precision:%d, scale:%d, data type:%s, "
994
                 "precision:%d, scale:%d",
995
                 pColSchema->name, tDataTypes[pColSchema->type].name, precision, scale, tDataTypes[*fields].name,
×
996
                 precisionData, scaleData);
997
      return TSDB_CODE_INVALID_PARA;
×
998
    }
999
    return 0;
1✔
1000
  }
1001

1002
  if (IS_VAR_DATA_TYPE(pColSchema->type) && *(int32_t*)(fields + sizeof(int8_t)) > pColSchema->bytes) {
576!
1003
    if (errstr != NULL)
1!
1004
      snprintf(errstr, errstrLen,
×
1005
               "column var data bytes error, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
1006
               pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name,
×
1007
               *(int32_t*)(fields + sizeof(int8_t)));
×
1008
    return TSDB_CODE_INVALID_PARA;
1✔
1009
  }
1010

1011
  if (!IS_VAR_DATA_TYPE(pColSchema->type) && *(int32_t*)(fields + sizeof(int8_t)) != pColSchema->bytes) {
575!
1012
    if (errstr != NULL)
×
1013
      snprintf(errstr, errstrLen,
×
1014
               "column normal data bytes not equal, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
1015
               pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name,
×
1016
               *(int32_t*)(fields + sizeof(int8_t)));
×
1017
    return TSDB_CODE_INVALID_PARA;
×
1018
  }
1019
  return 0;
575✔
1020
}
1021

1022
#define PRCESS_DATA(i, j)                                                                                 \
1023
  ret = checkSchema(pColSchema, pColExtSchema, fields, errstr, errstrLen);                                \
1024
  if (ret != 0) {                                                                                         \
1025
    goto end;                                                                                             \
1026
  }                                                                                                       \
1027
                                                                                                          \
1028
  if (pColSchema->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {                                                 \
1029
    hasTs = true;                                                                                         \
1030
  }                                                                                                       \
1031
                                                                                                          \
1032
  int8_t* offset = pStart;                                                                                \
1033
  if (IS_VAR_DATA_TYPE(pColSchema->type)) {                                                               \
1034
    pStart += numOfRows * sizeof(int32_t);                                                                \
1035
  } else {                                                                                                \
1036
    pStart += BitmapLen(numOfRows);                                                                       \
1037
  }                                                                                                       \
1038
  char* pData = pStart;                                                                                   \
1039
                                                                                                          \
1040
  SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, j);                                               \
1041
  ret = tColDataAddValueByDataBlock(pCol, pColSchema->type, pColSchema->bytes, numOfRows, offset, pData); \
1042
  if (ret != 0) {                                                                                         \
1043
    goto end;                                                                                             \
1044
  }                                                                                                       \
1045
  fields += sizeof(int8_t) + sizeof(int32_t);                                                             \
1046
  if (needChangeLength && version == BLOCK_VERSION_1) {                                                   \
1047
    pStart += htonl(colLength[i]);                                                                        \
1048
  } else {                                                                                                \
1049
    pStart += colLength[i];                                                                               \
1050
  }                                                                                                       \
1051
  boundInfo->pColIndex[j] = -1;
1052

1053
int rawBlockBindData(SQuery* query, STableMeta* pTableMeta, void* data, SVCreateTbReq* pCreateTb, void* tFields,
129✔
1054
                     int numFields, bool needChangeLength, char* errstr, int32_t errstrLen, bool raw) {
1055
  int ret = 0;
129✔
1056
  if (data == NULL) {
129!
1057
    uError("rawBlockBindData, data is NULL");
×
1058
    return TSDB_CODE_APP_ERROR;
×
1059
  }
1060
  void* tmp =
1061
      taosHashGet(((SVnodeModifyOpStmt*)(query->pRoot))->pTableBlockHashObj, &pTableMeta->uid, sizeof(pTableMeta->uid));
129✔
1062
  SVCreateTbReq* pCreateReqTmp = NULL;
129✔
1063
  if (tmp == NULL && pCreateTb != NULL) {
129✔
1064
    ret = cloneSVreateTbReq(pCreateTb, &pCreateReqTmp);
14✔
1065
    if (ret != TSDB_CODE_SUCCESS) {
14!
1066
      uError("cloneSVreateTbReq error");
×
1067
      goto end;
×
1068
    }
1069
  }
1070

1071
  STableDataCxt* pTableCxt = NULL;
129✔
1072
  ret = insGetTableDataCxt(((SVnodeModifyOpStmt*)(query->pRoot))->pTableBlockHashObj, &pTableMeta->uid,
129✔
1073
                           sizeof(pTableMeta->uid), pTableMeta, &pCreateReqTmp, &pTableCxt, true, false);
1074
  if (pCreateReqTmp != NULL) {
129!
1075
    tdDestroySVCreateTbReq(pCreateReqTmp);
×
1076
    taosMemoryFree(pCreateReqTmp);
×
1077
  }
1078

1079
  if (ret != TSDB_CODE_SUCCESS) {
129!
1080
    uError("insGetTableDataCxt error");
×
1081
    goto end;
×
1082
  }
1083

1084
  pTableCxt->pData->flags |= TD_REQ_FROM_TAOX;
129✔
1085
  if (tmp == NULL) {
129✔
1086
    ret = initTableColSubmitData(pTableCxt);
116✔
1087
    if (ret != TSDB_CODE_SUCCESS) {
116!
1088
      uError("initTableColSubmitData error");
×
1089
      goto end;
×
1090
    }
1091
  }
1092

1093
  char* p = (char*)data;
129✔
1094
  // | version | total length | total rows | blankFill | total columns | flag seg| block group id | column schema | each
1095
  // column length |
1096
  int32_t version = *(int32_t*)data;
129✔
1097
  p += sizeof(int32_t);
129✔
1098
  p += sizeof(int32_t);
129✔
1099

1100
  int32_t numOfRows = *(int32_t*)p;
129✔
1101
  p += sizeof(int32_t);
129✔
1102

1103
  int32_t numOfCols = *(int32_t*)p;
129✔
1104
  p += sizeof(int32_t);
129✔
1105

1106
  p += sizeof(int32_t);
129✔
1107
  p += sizeof(uint64_t);
129✔
1108

1109
  int8_t* fields = p;
129✔
1110
  if (*fields >= TSDB_DATA_TYPE_MAX || *fields < 0) {
129!
1111
    uError("fields type error:%d", *fields);
×
1112
    ret = TSDB_CODE_INVALID_PARA;
×
1113
    goto end;
×
1114
  }
1115
  p += numOfCols * (sizeof(int8_t) + sizeof(int32_t));
129✔
1116

1117
  int32_t* colLength = (int32_t*)p;
129✔
1118
  p += sizeof(int32_t) * numOfCols;
129✔
1119

1120
  char* pStart = p;
129✔
1121

1122
  SSchema*       pSchema = getTableColumnSchema(pTableMeta);
129✔
1123
  SSchemaExt*    pExtSchemas = getTableColumnExtSchema(pTableMeta);
129✔
1124
  SBoundColInfo* boundInfo = &pTableCxt->boundColsInfo;
129✔
1125

1126
  if (tFields != NULL && numFields != numOfCols) {
129!
1127
    if (errstr != NULL) snprintf(errstr, errstrLen, "numFields:%d not equal to data cols:%d", numFields, numOfCols);
×
1128
    ret = TSDB_CODE_INVALID_PARA;
×
1129
    goto end;
×
1130
  }
1131

1132
  bool hasTs = false;
129✔
1133
  if (tFields == NULL) {
129✔
1134
    int32_t len = TMIN(numOfCols, boundInfo->numOfBound);
6✔
1135
    for (int j = 0; j < len; j++) {
18✔
1136
      SSchema*    pColSchema = &pSchema[j];
14✔
1137
      SSchemaExt* pColExtSchema = &pExtSchemas[j];
14✔
1138
      PRCESS_DATA(j, j)
14!
1139
    }
1140
  } else {
1141
    for (int i = 0; i < numFields; i++) {
649✔
1142
      for (int j = 0; j < boundInfo->numOfBound; j++) {
4,342!
1143
        SSchema*    pColSchema = &pSchema[j];
4,342✔
1144
        SSchemaExt* pColExtSchema = &pExtSchemas[j];
4,342✔
1145
        char*       fieldName = NULL;
4,342✔
1146
        if (raw) {
4,342✔
1147
          fieldName = ((SSchemaWrapper*)tFields)->pSchema[i].name;
4,337✔
1148
        } else {
1149
          fieldName = ((TAOS_FIELD*)tFields)[i].name;
5✔
1150
        }
1151
        if (strcmp(pColSchema->name, fieldName) == 0) {
4,342✔
1152
          PRCESS_DATA(i, j)
526!
1153
          break;
526✔
1154
        }
1155
      }
1156
    }
1157
  }
1158

1159
  if (!hasTs) {
127!
1160
    if (errstr != NULL) snprintf(errstr, errstrLen, "timestamp column(primary key) not found in raw data");
×
1161
    ret = TSDB_CODE_INVALID_PARA;
×
1162
    goto end;
×
1163
  }
1164

1165
  // process NULL data
1166
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
677✔
1167
    if (boundInfo->pColIndex[c] != -1) {
550✔
1168
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, c);
13✔
1169
      ret = tColDataAddValueByDataBlock(pCol, 0, 0, numOfRows, NULL, NULL);
13✔
1170
      if (ret != 0) {
13!
1171
        goto end;
×
1172
      }
1173
    } else {
1174
      boundInfo->pColIndex[c] = c;  // restore for next block
537✔
1175
    }
1176
  }
1177

1178
end:
127✔
1179
  return ret;
129✔
1180
}
1181

1182
int rawBlockBindRawData(SHashObj* pVgroupHash, SArray* pVgroupList, STableMeta* pTableMeta, void* data) {
×
1183
  int code = transformRawSSubmitTbData(data, pTableMeta->suid, pTableMeta->uid, pTableMeta->sversion);
×
1184
  if (code != 0) {
×
1185
    return code;
×
1186
  }
1187
  SVgroupDataCxt* pVgCxt = NULL;
×
1188
  void**          pp = taosHashGet(pVgroupHash, &pTableMeta->vgId, sizeof(pTableMeta->vgId));
×
1189
  if (NULL == pp) {
×
1190
    code = createVgroupDataCxt(pTableMeta->vgId, pVgroupHash, pVgroupList, &pVgCxt);
×
1191
    if (code != 0) {
×
1192
      return code;
×
1193
    }
1194
  } else {
1195
    pVgCxt = *(SVgroupDataCxt**)pp;
×
1196
  }
1197
  if (NULL == pVgCxt->pData->aSubmitTbData) {
×
1198
    pVgCxt->pData->aSubmitTbData = taosArrayInit(0, POINTER_BYTES);
×
1199
    pVgCxt->pData->raw = true;
×
1200
    if (NULL == pVgCxt->pData->aSubmitTbData) {
×
1201
      return terrno;
×
1202
    }
1203
  }
1204

1205
  // push data to submit, rebuild empty data for next submit
1206
  if (NULL == taosArrayPush(pVgCxt->pData->aSubmitTbData, &data)) {
×
1207
    return terrno;
×
1208
  }
1209

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

1212
  return 0;
×
1213
}
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