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

taosdata / TDengine / #4506

15 Jul 2025 12:33AM UTC coverage: 62.026% (-0.7%) from 62.706%
#4506

push

travis-ci

web-flow
docs: update stream docs (#31874)

155391 of 320094 branches covered (48.55%)

Branch coverage included in aggregate %.

240721 of 318525 relevant lines covered (75.57%)

6529048.03 hits per line

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

69.27
/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) {
29,649✔
28
  if (NULL == pInfo) {
29,649✔
29
    return;
9,401✔
30
  }
31

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

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

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

42
  for (uint32_t i = 0; i < pToken->n; ++i) {
15,413,063✔
43
    if (*(pToken->z + i) == target && (!inEscape) && (!inQuote)) {
14,370,397!
44
      return pToken->z + i;
973,336✔
45
    }
46

47
    if (*(pToken->z + i) == TS_ESCAPE_CHAR) {
13,397,061✔
48
      if (!inQuote) {
167,631!
49
        inEscape = !inEscape;
167,665✔
50
      }
51
    }
52

53
    if (*(pToken->z + i) == '\'' || *(pToken->z + i) == '"') {
13,397,061✔
54
      if (!inEscape) {
24,117✔
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,042,666✔
66
}
67

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

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

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

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

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

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

100
    code = tNameFromString(pName, tbname, T_NAME_TABLE);
973,347✔
101
    if (code != 0) {
973,320!
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,042,759✔
106
    strncpy(tbname, pTableName->z, pTableName->n);
1,042,759✔
107
    int32_t tbLen = strdequote(tbname);
1,042,759✔
108
    if (tbLen >= TSDB_TABLE_NAME_LEN) {
1,042,763!
109
      return buildInvalidOperationMsg(pMsgBuf, msg1);
×
110
    }
111
    if (tbLen == 0) {
1,042,777!
112
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, "invalid table name");
×
113
    }
114

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

119
    if (dbName == NULL) {
1,042,772✔
120
      return buildInvalidOperationMsg(pMsgBuf, msg3);
2✔
121
    }
122
    if (name[0] == '\0') return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
1,042,770!
123

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

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

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

140
  return code;
2,016,068✔
141
}
142

143
int16_t insFindCol(SToken* pColname, int16_t start, int16_t end, SSchema* pSchema) {
9,446,031✔
144
  while (start < end) {
58,625,962✔
145
    if (strlen(pSchema[start].name) == pColname->n && strncmp(pColname->z, pSchema[start].name, pColname->n) == 0) {
58,598,177✔
146
      return start;
9,418,246✔
147
    }
148
    ++start;
49,179,931✔
149
  }
150
  return -1;
27,785✔
151
}
152

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

170
  return TSDB_CODE_SUCCESS;
68,249✔
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,876,922✔
180
  SSchema* pSchemas = getTableColumnSchema(pTableMeta);
1,876,922✔
181
  int32_t  code = 0;
1,876,929✔
182
  for (int32_t i = 0; i < pTableMeta->tableInfo.numOfColumns; ++i) {
78,567,659✔
183
    SColVal val = COL_VAL_NONE(pSchemas[i].colId, pSchemas[i].type);
76,664,525✔
184
    if (NULL == taosArrayPush(pValues, &val)) {
76,690,730!
185
      code = terrno;
×
186
      break;
×
187
    }
188
  }
189
  return code;
1,903,134✔
190
}
191

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

194
int32_t insInitBoundColsInfo(int32_t numOfBound, SBoundColInfo* pInfo) {
1,967,801✔
195
  pInfo->numOfCols = numOfBound;
1,967,801✔
196
  pInfo->numOfBound = numOfBound;
1,967,801✔
197
  pInfo->hasBoundCols = false;
1,967,801✔
198
  pInfo->mixTagsCols = false;
1,967,801✔
199
  pInfo->pColIndex = taosMemoryCalloc(numOfBound, sizeof(int16_t));
1,967,801!
200
  if (NULL == pInfo->pColIndex) {
1,967,916!
201
    return terrno;
×
202
  }
203
  for (int32_t i = 0; i < numOfBound; ++i) {
79,426,042✔
204
    pInfo->pColIndex[i] = i;
77,458,126✔
205
  }
206
  return TSDB_CODE_SUCCESS;
1,967,916✔
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) {
125,983,067✔
219
  // once the data block is disordered, we do NOT keep last timestamp any more
220
  if (!pTableCxt->ordered) {
125,983,067✔
221
    return;
7,126,007✔
222
  }
223

224
  if (tRowKeyCompare(rowKey, &pTableCxt->lastKey) < 0) {
118,857,060✔
225
    pTableCxt->ordered = false;
17,316✔
226
  }
227

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

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

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

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

247
  int32_t code = TSDB_CODE_SUCCESS;
1,875,712✔
248

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

253
  pTableCxt->pMeta = tableMetaDup(pTableMeta);
1,875,712✔
254
  if (NULL == pTableCxt->pMeta) {
1,875,716!
255
    code = TSDB_CODE_OUT_OF_MEMORY;
×
256
  }
257
  if (TSDB_CODE_SUCCESS == code) {
1,875,716!
258
    pTableCxt->pSchema =
1,875,742✔
259
        tBuildTSchema(getTableColumnSchema(pTableMeta), pTableMeta->tableInfo.numOfColumns, pTableMeta->sversion);
1,875,725✔
260
    if (NULL == pTableCxt->pSchema) {
1,875,742!
261
      code = TSDB_CODE_OUT_OF_MEMORY;
×
262
    }
263
  }
264
  if (TSDB_CODE_SUCCESS == code) {
1,875,733✔
265
    code = insInitBoundColsInfo(pTableMeta->tableInfo.numOfColumns, &pTableCxt->boundColsInfo);
1,875,690✔
266
  }
267
  if (TSDB_CODE_SUCCESS == code && !ignoreColVals) {
1,875,789✔
268
    pTableCxt->pValues = taosArrayInit(pTableMeta->tableInfo.numOfColumns, sizeof(SColVal));
1,866,476✔
269
    if (NULL == pTableCxt->pValues) {
1,866,454!
270
      code = terrno;
×
271
    } else {
272
      code = initColValues(pTableMeta, pTableCxt->pValues);
1,866,454✔
273
    }
274
  }
275
  if (TSDB_CODE_SUCCESS == code) {
1,875,716✔
276
    pTableCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
1,875,710!
277
    if (NULL == pTableCxt->pData) {
1,875,768!
278
      code = terrno;
×
279
    } else {
280
      pTableCxt->pData->flags = (pCreateTbReq != NULL && NULL != *pCreateTbReq) ? SUBMIT_REQ_AUTO_CREATE_TABLE : 0;
1,875,768!
281
      pTableCxt->pData->flags |= colMode ? SUBMIT_REQ_COLUMN_DATA_FORMAT : 0;
1,875,768✔
282
      pTableCxt->pData->suid = pTableMeta->suid;
1,875,768✔
283
      pTableCxt->pData->uid = pTableMeta->uid;
1,875,768✔
284
      pTableCxt->pData->sver = pTableMeta->sversion;
1,875,768✔
285
      pTableCxt->pData->pCreateTbReq = pCreateTbReq != NULL ? *pCreateTbReq : NULL;
1,875,768✔
286
      if (pCreateTbReq != NULL) *pCreateTbReq = NULL;
1,875,768!
287
      if (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,875,768✔
288
        pTableCxt->pData->aCol = taosArrayInit(128, sizeof(SColData));
20,380✔
289
        if (NULL == pTableCxt->pData->aCol) {
20,370!
290
          code = terrno;
×
291
        }
292
      } else {
293
        pTableCxt->pData->aRowP = taosArrayInit(128, POINTER_BYTES);
1,855,388✔
294
        if (NULL == pTableCxt->pData->aRowP) {
1,855,349!
295
          code = terrno;
×
296
        }
297
      }
298
    }
299
  }
300
  if (TSDB_CODE_SUCCESS == code) {
1,875,725!
301
    *pOutput = pTableCxt;
1,875,725✔
302
    parserDebug("uid:%" PRId64 ", create table data context, code:%d, vgId:%d", pTableMeta->uid, code,
1,875,725✔
303
                pTableMeta->vgId);
304
  } else {
305
    insDestroyTableDataCxt(pTableCxt);
×
306
  }
307

308
  return code;
1,875,734✔
309
}
310

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

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

353
  return code;
42,510✔
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,905,198✔
365
                           SVCreateTbReq** pCreateTbReq, STableDataCxt** pTableCxt, bool colMode, bool ignoreColVals) {
366
  STableDataCxt** tmp = (STableDataCxt**)taosHashGet(pHash, id, idLen);
3,905,198✔
367
  if (NULL != tmp) {
3,905,384✔
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,875,732✔
375
  if (TSDB_CODE_SUCCESS == code) {
1,875,740!
376
    void* pData = *pTableCxt;  // deal scan coverity
1,875,746✔
377
    code = taosHashPut(pHash, id, idLen, &pData, POINTER_BYTES);
1,875,746✔
378
  }
379

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

386
static void destroyColVal(void* p) {
76,898,241✔
387
  SColVal* pVal = p;
76,898,241✔
388
  if (TSDB_DATA_TYPE_NCHAR == pVal->value.type || TSDB_DATA_TYPE_GEOMETRY == pVal->value.type ||
76,898,241✔
389
      TSDB_DATA_TYPE_VARBINARY == pVal->value.type || TSDB_DATA_TYPE_DECIMAL == pVal->value.type) {
71,431,909✔
390
    taosMemoryFreeClear(pVal->value.pData);
5,657,968!
391
  }
392
}
76,898,241✔
393

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

399
  taosMemoryFreeClear(pTableCxt->pMeta);
1,895,742!
400
  tDestroyTSchema(pTableCxt->pSchema);
1,895,755!
401
  insDestroyBoundColInfo(&pTableCxt->boundColsInfo);
1,895,758✔
402
  taosArrayDestroyEx(pTableCxt->pValues, destroyColVal);
1,895,762✔
403
  if (pTableCxt->pData) {
1,895,758✔
404
    tDestroySubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
56,342✔
405
    taosMemoryFree(pTableCxt->pData);
56,351!
406
  }
407
  taosMemoryFree(pTableCxt);
1,895,778!
408
}
409

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

415
  tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
1,834,876✔
416
  taosMemoryFree(pVgCxt->pData);
1,834,955!
417
  taosMemoryFree(pVgCxt);
1,834,953!
418
}
419

420
void insDestroyVgroupDataCxtList(SArray* pVgCxtList) {
3,581,004✔
421
  if (NULL == pVgCxtList) {
3,581,004✔
422
    return;
1,788,225✔
423
  }
424

425
  size_t size = taosArrayGetSize(pVgCxtList);
1,792,779✔
426
  for (int32_t i = 0; i < size; i++) {
3,627,793✔
427
    void* p = taosArrayGetP(pVgCxtList, i);
1,834,934✔
428
    insDestroyVgroupDataCxt(p);
1,834,936✔
429
  }
430

431
  taosArrayDestroy(pVgCxtList);
1,792,859✔
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,790,534✔
450
  if (NULL == pTableCxtHash) {
1,790,534✔
451
    return;
20,277✔
452
  }
453

454
  void** p = taosHashIterate(pTableCxtHash, NULL);
1,770,257✔
455
  while (p) {
3,625,455✔
456
    insDestroyTableDataCxt(*(STableDataCxt**)p);
1,855,188✔
457

458
    p = taosHashIterate(pTableCxtHash, p);
1,855,188✔
459
  }
460

461
  taosHashCleanup(pTableCxtHash);
1,770,267✔
462
}
463

464
static int32_t fillVgroupDataCxt(STableDataCxt* pTableCxt, SVgroupDataCxt* pVgCxt, bool isRebuild, bool clear) {
1,882,050✔
465
  if (NULL == pVgCxt->pData->aSubmitTbData) {
1,882,050✔
466
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
1,825,261✔
467
    if (NULL == pVgCxt->pData->aSubmitTbData) {
1,825,236!
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,764,065!
474
    return terrno;
×
475
  }
476
  int32_t code = 0;
1,882,040✔
477
  if (isRebuild) {
1,882,040✔
478
    code = rebuildTableData(pTableCxt->pData, &pTableCxt->pData);
42,505✔
479
  } else if (clear) {
1,839,535!
480
    taosMemoryFreeClear(pTableCxt->pData);
1,839,539!
481
  }
482

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

485
  return code;
1,882,033✔
486
}
487

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

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

515
int insColDataComp(const void* lp, const void* rp) {
82,066✔
516
  SColData* pLeft = (SColData*)lp;
82,066✔
517
  SColData* pRight = (SColData*)rp;
82,066✔
518
  if (pLeft->cid < pRight->cid) {
82,066!
519
    return -1;
82,082✔
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,
365✔
528
                                 STableColsData* pTbData, SName* sname) {
529
  if (*vgId >= 0 && taosHashGet(pAllVgHash, (const char*)vgId, sizeof(*vgId))) {
365!
530
    return TSDB_CODE_SUCCESS;
337✔
531
  }
532

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

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

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

549
  return TSDB_CODE_SUCCESS;
31✔
550
}
551

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

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

561
  if (NULL == pTbInfo) {
11,510✔
562
    SName sname;
563
    code = qCreateSName(&sname, pTbData->tbName, pBuildInfo->acctId, pBuildInfo->dbname, NULL, 0);
412✔
564
    if (TSDB_CODE_SUCCESS != code) {
413!
565
      return code;
46✔
566
    }
567

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

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

580
    if (TSDB_CODE_SUCCESS != code) {
366!
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;
366✔
586
    *vgId = pTableMeta->vgId;
366✔
587

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

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

600
  return code;
11,465✔
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,
11,496✔
615
                                            char* tbname) {
616
  if (NULL == pVgCxt->pData->aSubmitTbData) {
11,496✔
617
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
9,983✔
618
    if (NULL == pVgCxt->pData->aSubmitTbData) {
9,988!
619
      return terrno;
×
620
    }
621
  }
622

623
  int32_t        code = TSDB_CODE_SUCCESS;
11,501✔
624
  SArray**       rowP = NULL;
11,501✔
625

626
  rowP = (SArray**)tSimpleHashGet(pTableNameHash, tbname, strlen(tbname));
11,501✔
627

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

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

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

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

655
    return TSDB_CODE_SUCCESS;
32✔
656
  }
657

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

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

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

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

670
  return TSDB_CODE_SUCCESS;
11,472✔
671
}
672

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

679
  pTbCtx->pData->aRowP = pTbData->aCol;
11,508✔
680

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

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

708
  if (!pTbData->isOrdered) {
11,509✔
709
    code = tRowSort(pTbCtx->pData->aRowP);
5,713✔
710
  }
711
  if (code == TSDB_CODE_SUCCESS && (!pTbData->isOrdered || pTbData->isDuplicateTs)) {
11,520!
712
    code = tRowMerge(pTbCtx->pData->aRowP, pTbCtx->pSchema, 0);
5,725✔
713
  }
714

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

719
  SVgroupDataCxt* pVgCxt = NULL;
11,506✔
720
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
11,506✔
721
  if (NULL == pp) {
11,507✔
722
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
9,986✔
723
    if (NULL == pp) {
9,984!
724
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
9,984✔
725
    } else {
726
      pVgCxt = *(SVgroupDataCxt**)pp;
×
727
    }
728
  } else {
729
    pVgCxt = *(SVgroupDataCxt**)pp;
1,521✔
730
  }
731

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

736
  if (taosArrayGetSize(pVgCxt->pData->aSubmitTbData) >= 20000) {
11,508!
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;
11,498✔
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,784,072✔
805
  SHashObj* pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
1,784,072✔
806
  SArray*   pVgroupList = taosArrayInit(8, POINTER_BYTES);
1,784,164✔
807
  if (NULL == pVgroupHash || NULL == pVgroupList) {
1,784,195!
808
    taosHashCleanup(pVgroupHash);
×
809
    taosArrayDestroy(pVgroupList);
×
810
    return terrno;
×
811
  }
812

813
  int32_t code = TSDB_CODE_SUCCESS;
1,784,196✔
814
  bool    colFormat = false;
1,784,196✔
815

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

822
  while (TSDB_CODE_SUCCESS == code && NULL != p) {
3,666,288✔
823
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
1,882,085✔
824
    if (colFormat) {
1,882,085✔
825
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
40,328✔
826
      if (pCol && pCol->nVal <= 0) {
40,327!
827
        p = taosHashIterate(pTableHash, p);
16✔
828
        continue;
16✔
829
      }
830

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

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

837
      code = tColDataSortMerge(&pTableCxt->pData->aCol);
40,302✔
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,841,757✔
847
        code = tRowSort(pTableCxt->pData->aRowP);
17,316✔
848
      }
849
      if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
1,841,757✔
850
        code = tRowMerge(pTableCxt->pData->aRowP, pTableCxt->pSchema, 0);
17,363✔
851
      }
852
    }
853

854
    if (TSDB_CODE_SUCCESS == code) {
1,882,061!
855
      SVgroupDataCxt* pVgCxt = NULL;
1,882,061✔
856
      int32_t         vgId = pTableCxt->pMeta->vgId;
1,882,061✔
857
      void**          pp = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
1,882,061✔
858
      if (NULL == pp) {
1,882,056✔
859
        code = createVgroupDataCxt(vgId, pVgroupHash, pVgroupList, &pVgCxt);
1,825,257✔
860
      } else {
861
        pVgCxt = *(SVgroupDataCxt**)pp;
56,799✔
862
      }
863
      if (TSDB_CODE_SUCCESS == code) {
1,882,062!
864
        code = fillVgroupDataCxt(pTableCxt, pVgCxt, isRebuild, true);
1,882,066✔
865
      }
866
    }
867
    if (TSDB_CODE_SUCCESS == code) {
1,882,036!
868
      p = taosHashIterate(pTableHash, p);
1,882,037✔
869
    }
870
  }
871

872
  taosHashCleanup(pVgroupHash);
1,784,203✔
873
  if (TSDB_CODE_SUCCESS == code) {
1,784,202!
874
    *pVgDataBlocks = pVgroupList;
1,784,206✔
875
  } else {
876
    insDestroyVgroupDataCxtList(pVgroupList);
×
877
  }
878

879
  return code;
1,784,207✔
880
}
881

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

902
  if (TSDB_CODE_SUCCESS == code) {
1,835,259!
903
    *pData = pBuf;
1,835,259✔
904
    *pLen = len;
1,835,259✔
905
  } else {
906
    taosMemoryFree(pBuf);
×
907
  }
908
  return code;
1,835,183✔
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,105✔
919
  size_t  numOfVg = taosArrayGetSize(pVgDataCxtList);
1,793,105✔
920
  SArray* pDataBlocks = (append && *pVgDataBlocks) ? *pVgDataBlocks : taosArrayInit(numOfVg, POINTER_BYTES);
1,793,154!
921
  if (NULL == pDataBlocks) {
1,793,163!
922
    return TSDB_CODE_OUT_OF_MEMORY;
×
923
  }
924

925
  int32_t code = TSDB_CODE_SUCCESS;
1,793,163✔
926
  for (size_t i = 0; TSDB_CODE_SUCCESS == code && i < numOfVg; ++i) {
3,628,369✔
927
    SVgroupDataCxt* src = taosArrayGetP(pVgDataCxtList, i);
1,835,214✔
928
    if (taosArrayGetSize(src->pData->aSubmitTbData) <= 0) {
1,835,254!
929
      continue;
×
930
    }
931
    SVgDataBlocks* dst = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
1,835,253!
932
    if (NULL == dst) {
1,835,222!
933
      code = terrno;
×
934
    }
935
    if (TSDB_CODE_SUCCESS == code) {
1,835,222!
936
      dst->numOfTables = taosArrayGetSize(src->pData->aSubmitTbData);
1,835,234✔
937
      code = taosHashGetDup(pVgroupsHashObj, (const char*)&src->vgId, sizeof(src->vgId), &dst->vg);
1,835,235✔
938
    }
939
    if (TSDB_CODE_SUCCESS == code) {
1,835,252!
940
      code = buildSubmitReq(src->vgId, src->pData, &dst->pData, &dst->size);
1,835,252✔
941
    }
942
    if (TSDB_CODE_SUCCESS == code) {
1,835,188✔
943
      code = (NULL == taosArrayPush(pDataBlocks, &dst) ? terrno : TSDB_CODE_SUCCESS);
1,835,193!
944
    }
945
    if (TSDB_CODE_SUCCESS != code) {
1,835,196!
946
      destroyVgDataBlocks(dst);
×
947
    }
948
  }
949

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

957
  if (TSDB_CODE_SUCCESS == code) {
1,784,191✔
958
    *pVgDataBlocks = pDataBlocks;
1,784,153✔
959
  } else {
960
    taosArrayDestroyP(pDataBlocks, destroyVgDataBlocks);
38✔
961
  }
962

963
  return code;
1,784,149✔
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