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

taosdata / TDengine / #4118

17 May 2025 06:43AM UTC coverage: 62.797% (+0.7%) from 62.054%
#4118

push

travis-ci

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

merge: from main to 3.0 branch

156841 of 318088 branches covered (49.31%)

Branch coverage included in aggregate %.

176 of 225 new or added lines in 20 files covered. (78.22%)

2989 existing lines in 163 files now uncovered.

242067 of 317143 relevant lines covered (76.33%)

6956088.31 hits per line

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

70.97
/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 "tmisce.h"
25

26
void qDestroyBoundColInfo(void* pInfo) {
44,373✔
27
  if (NULL == pInfo) {
44,373✔
28
    return;
23,543✔
29
  }
30

31
  SBoundColInfo* pBoundInfo = (SBoundColInfo*)pInfo;
20,830✔
32

33
  taosMemoryFreeClear(pBoundInfo->pColIndex);
20,830!
34
}
35

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

41
  for (uint32_t i = 0; i < pToken->n; ++i) {
15,808,796✔
42
    if (*(pToken->z + i) == target && (!inEscape) && (!inQuote)) {
14,744,600!
43
      return pToken->z + i;
995,341✔
44
    }
45

46
    if (*(pToken->z + i) == TS_ESCAPE_CHAR) {
13,749,259✔
47
      if (!inQuote) {
187,754!
48
        inEscape = !inEscape;
187,783✔
49
      }
50
    }
51

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

64
  return NULL;
1,064,196✔
65
}
66

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

73
  int32_t code = TSDB_CODE_SUCCESS;
2,059,506✔
74
  char*   p = tableNameGetPosition(pTableName, TS_PATH_DELIMITER[0]);
2,059,506✔
75

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

85
    code = tNameSetDbName(pName, acctId, name, actualDbLen);
995,307✔
86
    if (code != TSDB_CODE_SUCCESS) {
995,321!
87
      return buildInvalidOperationMsg(pMsgBuf, msg1);
×
88
    }
89

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

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

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

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

118
    if (dbName == NULL) {
1,064,322✔
119
      return buildInvalidOperationMsg(pMsgBuf, msg3);
13✔
120
    }
121
    if (name[0] == '\0') return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
1,064,309!
122

123
    code = tNameSetDbName(pName, acctId, dbName, strlen(dbName));
1,064,309✔
124
    if (code != TSDB_CODE_SUCCESS) {
1,064,289!
125
      code = buildInvalidOperationMsg(pMsgBuf, msg2);
×
126
      return code;
×
127
    }
128

129
    code = tNameFromString(pName, name, T_NAME_TABLE);
1,064,289✔
130
    if (code != 0) {
1,064,224!
131
      code = buildInvalidOperationMsg(pMsgBuf, msg1);
×
132
    }
133
  }
134

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

139
  return code;
2,059,590✔
140
}
141

142
int16_t insFindCol(SToken* pColname, int16_t start, int16_t end, SSchema* pSchema) {
9,460,865✔
143
  while (start < end) {
58,644,648✔
144
    if (strlen(pSchema[start].name) == pColname->n && strncmp(pColname->z, pSchema[start].name, pColname->n) == 0) {
58,616,393✔
145
      return start;
9,432,610✔
146
    }
147
    ++start;
49,183,783✔
148
  }
149
  return -1;
28,255✔
150
}
151

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

169
  return TSDB_CODE_SUCCESS;
68,488✔
170
}
171

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

178
static int32_t initColValues(STableMeta* pTableMeta, SArray* pValues) {
1,904,811✔
179
  SSchema* pSchemas = getTableColumnSchema(pTableMeta);
1,904,811✔
180
  int32_t  code = 0;
1,904,838✔
181
  for (int32_t i = 0; i < pTableMeta->tableInfo.numOfColumns; ++i) {
79,215,335✔
182
    SColVal val = COL_VAL_NONE(pSchemas[i].colId, pSchemas[i].type);
77,289,239✔
183
    if (NULL == taosArrayPush(pValues, &val)) {
77,310,497!
184
      code = terrno;
×
185
      break;
×
186
    }
187
  }
188
  return code;
1,926,096✔
189
}
190

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

193
int32_t insInitBoundColsInfo(int32_t numOfBound, SBoundColInfo* pInfo) {
2,000,054✔
194
  pInfo->numOfCols = numOfBound;
2,000,054✔
195
  pInfo->numOfBound = numOfBound;
2,000,054✔
196
  pInfo->hasBoundCols = false;
2,000,054✔
197
  pInfo->mixTagsCols = false;
2,000,054✔
198
  pInfo->pColIndex = taosMemoryCalloc(numOfBound, sizeof(int16_t));
2,000,054!
199
  if (NULL == pInfo->pColIndex) {
2,000,229!
200
    return terrno;
×
201
  }
202
  for (int32_t i = 0; i < numOfBound; ++i) {
80,171,692✔
203
    pInfo->pColIndex[i] = i;
78,171,463✔
204
  }
205
  return TSDB_CODE_SUCCESS;
2,000,229✔
206
}
207

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

217
void insCheckTableDataOrder(STableDataCxt* pTableCxt, SRowKey* rowKey) {
183,246,629✔
218
  // once the data block is disordered, we do NOT keep last timestamp any more
219
  if (!pTableCxt->ordered) {
183,246,629✔
220
    return;
6,731,522✔
221
  }
222

223
  if (tRowKeyCompare(rowKey, &pTableCxt->lastKey) < 0) {
176,515,107✔
224
    pTableCxt->ordered = false;
18,623✔
225
  }
226

227
  if (tRowKeyCompare(rowKey, &pTableCxt->lastKey) == 0) {
176,596,164✔
228
    pTableCxt->duplicateTs = true;
107✔
229
  }
230

231
  // TODO: for variable length data type, we need to copy it out
232
  pTableCxt->lastKey = *rowKey;
176,530,059✔
233
  return;
176,530,059✔
234
}
235

236
void insDestroyBoundColInfo(SBoundColInfo* pInfo) { taosMemoryFreeClear(pInfo->pColIndex); }
7,741,511!
237

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

246
  int32_t code = TSDB_CODE_SUCCESS;
1,903,625✔
247

248
  pTableCxt->lastKey = (SRowKey){0};
1,903,625✔
249
  pTableCxt->ordered = true;
1,903,625✔
250
  pTableCxt->duplicateTs = false;
1,903,625✔
251

252
  pTableCxt->pMeta = tableMetaDup(pTableMeta);
1,903,625✔
253
  if (NULL == pTableCxt->pMeta) {
1,903,600!
254
    code = TSDB_CODE_OUT_OF_MEMORY;
×
255
  }
256
  if (TSDB_CODE_SUCCESS == code) {
1,903,600!
257
    pTableCxt->pSchema =
1,903,683✔
258
        tBuildTSchema(getTableColumnSchema(pTableMeta), pTableMeta->tableInfo.numOfColumns, pTableMeta->sversion);
1,903,630✔
259
    if (NULL == pTableCxt->pSchema) {
1,903,683!
260
      code = TSDB_CODE_OUT_OF_MEMORY;
×
261
    }
262
  }
263
  if (TSDB_CODE_SUCCESS == code) {
1,903,653✔
264
    code = insInitBoundColsInfo(pTableMeta->tableInfo.numOfColumns, &pTableCxt->boundColsInfo);
1,903,604✔
265
  }
266
  if (TSDB_CODE_SUCCESS == code && !ignoreColVals) {
1,903,727✔
267
    pTableCxt->pValues = taosArrayInit(pTableMeta->tableInfo.numOfColumns, sizeof(SColVal));
1,894,417✔
268
    if (NULL == pTableCxt->pValues) {
1,894,367!
269
      code = terrno;
×
270
    } else {
271
      code = initColValues(pTableMeta, pTableCxt->pValues);
1,894,367✔
272
    }
273
  }
274
  if (TSDB_CODE_SUCCESS == code) {
1,903,642✔
275
    pTableCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
1,903,635!
276
    if (NULL == pTableCxt->pData) {
1,903,708!
277
      code = terrno;
×
278
    } else {
279
      pTableCxt->pData->flags = (pCreateTbReq != NULL && NULL != *pCreateTbReq) ? SUBMIT_REQ_AUTO_CREATE_TABLE : 0;
1,903,708!
280
      pTableCxt->pData->flags |= colMode ? SUBMIT_REQ_COLUMN_DATA_FORMAT : 0;
1,903,708✔
281
      pTableCxt->pData->suid = pTableMeta->suid;
1,903,708✔
282
      pTableCxt->pData->uid = pTableMeta->uid;
1,903,708✔
283
      pTableCxt->pData->sver = pTableMeta->sversion;
1,903,708✔
284
      pTableCxt->pData->pCreateTbReq = pCreateTbReq != NULL ? *pCreateTbReq : NULL;
1,903,708!
285
      if (pCreateTbReq != NULL) *pCreateTbReq = NULL;
1,903,708!
286
      if (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,903,708✔
287
        pTableCxt->pData->aCol = taosArrayInit(128, sizeof(SColData));
20,982✔
288
        if (NULL == pTableCxt->pData->aCol) {
20,979!
289
          code = terrno;
×
290
        }
291
      } else {
292
        pTableCxt->pData->aRowP = taosArrayInit(128, POINTER_BYTES);
1,882,726✔
293
        if (NULL == pTableCxt->pData->aRowP) {
1,882,657!
294
          code = terrno;
×
295
        }
296
      }
297
    }
298
  }
299
  if (TSDB_CODE_SUCCESS == code) {
1,903,643!
300
    *pOutput = pTableCxt;
1,903,643✔
301
    parserDebug("uid:%" PRId64 ", create table data context, code:%d, vgId:%d", pTableMeta->uid, code, pTableMeta->vgId);
1,903,643✔
302
  } else {
303
    insDestroyTableDataCxt(pTableCxt);
×
304
  }
305

306
  return code;
1,903,664✔
307
}
308

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

346
  taosMemoryFree(pSrc);
43,428!
347
  if (TSDB_CODE_SUCCESS == code) {
43,439✔
348
    *pDst = pTmp;
43,437✔
349
  }
350

351
  return code;
43,439✔
352
}
353

354
static void resetColValues(SArray* pValues) {
15,573✔
355
  int32_t num = taosArrayGetSize(pValues);
15,573✔
356
  for (int32_t i = 0; i < num; ++i) {
414,479✔
357
    SColVal* pVal = taosArrayGet(pValues, i);
398,906✔
358
    pVal->flag = CV_FLAG_NONE;
398,906✔
359
  }
360
}
15,573✔
361

362
int32_t insGetTableDataCxt(SHashObj* pHash, void* id, int32_t idLen, STableMeta* pTableMeta,
3,933,153✔
363
                           SVCreateTbReq** pCreateTbReq, STableDataCxt** pTableCxt, bool colMode, bool ignoreColVals) {
364
  STableDataCxt** tmp = (STableDataCxt**)taosHashGet(pHash, id, idLen);
3,933,153✔
365
  if (NULL != tmp) {
3,933,261✔
366
    *pTableCxt = *tmp;
2,029,653✔
367
    if (!ignoreColVals) {
2,029,653✔
368
      resetColValues((*pTableCxt)->pValues);
15,573✔
369
    }
370
    return TSDB_CODE_SUCCESS;
2,029,653✔
371
  }
372
  int32_t code = createTableDataCxt(pTableMeta, pCreateTbReq, pTableCxt, colMode, ignoreColVals);
1,903,608✔
373
  if (TSDB_CODE_SUCCESS == code) {
1,903,665!
374
    void* pData = *pTableCxt;  // deal scan coverity
1,903,673✔
375
    code = taosHashPut(pHash, id, idLen, &pData, POINTER_BYTES);
1,903,673✔
376
  }
377

378
  if (TSDB_CODE_SUCCESS != code) {
1,903,715!
379
    insDestroyTableDataCxt(*pTableCxt);
×
380
  }
381
  return code;
1,903,675✔
382
}
383

384
static void destroyColVal(void* p) {
77,614,699✔
385
  SColVal* pVal = p;
77,614,699✔
386
  if (TSDB_DATA_TYPE_NCHAR == pVal->value.type || TSDB_DATA_TYPE_GEOMETRY == pVal->value.type ||
77,614,699✔
387
      TSDB_DATA_TYPE_VARBINARY == pVal->value.type || TSDB_DATA_TYPE_DECIMAL == pVal->value.type) {
72,109,378✔
388
    taosMemoryFreeClear(pVal->value.pData);
5,697,133!
389
  }
390
}
77,614,699✔
391

392
void insDestroyTableDataCxt(STableDataCxt* pTableCxt) {
1,923,728✔
393
  if (NULL == pTableCxt) {
1,923,728!
394
    return;
×
395
  }
396

397
  taosMemoryFreeClear(pTableCxt->pMeta);
1,923,728!
398
  tDestroyTSchema(pTableCxt->pSchema);
1,923,737!
399
  insDestroyBoundColInfo(&pTableCxt->boundColsInfo);
1,923,740✔
400
  taosArrayDestroyEx(pTableCxt->pValues, destroyColVal);
1,923,745✔
401
  if (pTableCxt->pData) {
1,923,752✔
402
    tDestroySubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
57,269✔
403
    taosMemoryFree(pTableCxt->pData);
57,261!
404
  }
405
  taosMemoryFree(pTableCxt);
1,923,761!
406
}
407

408
void insDestroyVgroupDataCxt(SVgroupDataCxt* pVgCxt) {
1,874,267✔
409
  if (NULL == pVgCxt) {
1,874,267!
410
    return;
×
411
  }
412

413
  tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
1,874,267✔
414
  taosMemoryFree(pVgCxt->pData);
1,874,355!
415
  taosMemoryFree(pVgCxt);
1,874,345!
416
}
417

418
void insDestroyVgroupDataCxtList(SArray* pVgCxtList) {
3,645,613✔
419
  if (NULL == pVgCxtList) {
3,645,613✔
420
    return;
1,813,674✔
421
  }
422

423
  size_t size = taosArrayGetSize(pVgCxtList);
1,831,939✔
424
  for (int32_t i = 0; i < size; i++) {
3,706,385✔
425
    void* p = taosArrayGetP(pVgCxtList, i);
1,874,318✔
426
    insDestroyVgroupDataCxt(p);
1,874,296✔
427
  }
428

429
  taosArrayDestroy(pVgCxtList);
1,832,067✔
430
}
431

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

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

441
    p = taosHashIterate(pVgCxtHash, p);
×
442
  }
443

444
  taosHashCleanup(pVgCxtHash);
×
445
}
446

447
void insDestroyTableDataCxtHashMap(SHashObj* pTableCxtHash) {
1,816,352✔
448
  if (NULL == pTableCxtHash) {
1,816,352✔
449
    return;
20,864✔
450
  }
451

452
  void** p = taosHashIterate(pTableCxtHash, NULL);
1,795,488✔
453
  while (p) {
3,678,064✔
454
    insDestroyTableDataCxt(*(STableDataCxt**)p);
1,882,554✔
455

456
    p = taosHashIterate(pTableCxtHash, p);
1,882,552✔
457
  }
458

459
  taosHashCleanup(pTableCxtHash);
1,795,510✔
460
}
461

462
static int32_t fillVgroupDataCxt(STableDataCxt* pTableCxt, SVgroupDataCxt* pVgCxt, bool isRebuild, bool clear) {
1,966,743✔
463
  if (NULL == pVgCxt->pData->aSubmitTbData) {
1,966,743✔
464
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
1,874,604✔
465
    if (NULL == pVgCxt->pData->aSubmitTbData) {
1,874,579!
466
      return terrno;
×
467
    }
468
  }
469

470
  // push data to submit, rebuild empty data for next submit
471
  if (NULL == taosArrayPush(pVgCxt->pData->aSubmitTbData, pTableCxt->pData)) {
3,933,395!
472
    return terrno;
×
473
  }
474
  int32_t code = 0;
1,966,677✔
475
  if (isRebuild) {
1,966,677✔
476
    code = rebuildTableData(pTableCxt->pData, &pTableCxt->pData);
43,436✔
477
  } else if (clear) {
1,923,241✔
478
    taosMemoryFreeClear(pTableCxt->pData);
1,866,571!
479
  }
480

481
  parserDebug("uid:%" PRId64 ", add table data context to vgId:%d", pTableCxt->pMeta->uid, pVgCxt->vgId);
1,966,650✔
482

483
  return code;
1,966,640✔
484
}
485

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

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

514
int insColDataComp(const void* lp, const void* rp) {
89,960✔
515
  SColData* pLeft = (SColData*)lp;
89,960✔
516
  SColData* pRight = (SColData*)rp;
89,960✔
517
  if (pLeft->cid < pRight->cid) {
89,960✔
518
    return -1;
89,001✔
519
  } else if (pLeft->cid > pRight->cid) {
959!
520
    return 1;
972✔
521
  }
522

523
  return 0;
×
524
}
525

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

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

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

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

548
  return TSDB_CODE_SUCCESS;
56✔
549
}
550

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

556
  if (pTbData->getFromHash) {
56,742✔
557
    pTbInfo = (STableVgUid*)tSimpleHashGet(pBuildInfo->pTableHash, pTbData->tbName, strlen(pTbData->tbName));
44,541✔
558
  }
559

560
  if (NULL == pTbInfo) {
56,649✔
561
    SName sname;
562
    code = qCreateSName(&sname, pTbData->tbName, pBuildInfo->acctId, pBuildInfo->dbname, NULL, 0);
12,229✔
563
    if (TSDB_CODE_SUCCESS != code) {
12,231!
564
      return code;
28✔
565
    }
566

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

574
    if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
12,233✔
575
      parserDebug("tb:%s.%s not exist", sname.dbname, sname.tname);
28!
576
      return code;
28✔
577
    }
578

579
    if (TSDB_CODE_SUCCESS != code) {
12,205!
580
      return code;
×
581
    }
582

583
    *uid = pTableMeta->uid;
12,205✔
584
    *vgId = pTableMeta->vgId;
12,205✔
585

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

592
    taosMemoryFree(pTableMeta);
12,204!
593
  } else {
594
    *uid = pTbInfo->uid;
44,420✔
595
    *vgId = pTbInfo->vgid;
44,420✔
596
  }
597

598
  return code;
56,624✔
599
}
600

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

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

609
  return code;
×
610
}
611

612
int32_t insAppendStmtTableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
56,807✔
613
                                  SStbInterlaceInfo* pBuildInfo, SVCreateTbReq* ctbReq) {
614
  int32_t  code = TSDB_CODE_SUCCESS;
56,807✔
615
  uint64_t uid;
616
  int32_t  vgId;
617

618
  pTbCtx->pData->aRowP = pTbData->aCol;
56,807✔
619

620
  code = insGetStmtTableVgUid(pAllVgHash, pBuildInfo, pTbData, &uid, &vgId);
56,807✔
621
  if (ctbReq !=NULL && code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
56,647✔
622
    pTbCtx->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
28✔
623
    vgId = (int32_t)ctbReq->uid;
28✔
624
    uid = 0;
28✔
625
    pTbCtx->pMeta->vgId = (int32_t)ctbReq->uid;
28✔
626
    ctbReq->uid = 0;
28✔
627
    pTbCtx->pData->pCreateTbReq = ctbReq;
28✔
628
    code = TSDB_CODE_SUCCESS;
28✔
629
  } else {
630
    if (TSDB_CODE_SUCCESS != code) {
56,619!
631
      return code;
×
632
    }
633
    pTbCtx->pMeta->vgId = vgId;
56,619✔
634
    pTbCtx->pMeta->uid = uid;
56,619✔
635
    pTbCtx->pData->uid = uid;
56,619✔
636
    pTbCtx->pData->pCreateTbReq = NULL;
56,619✔
637

638
    if (ctbReq != NULL) {
56,619✔
639
      tdDestroySVCreateTbReq(ctbReq);
640
      taosMemoryFree(ctbReq);
35!
641
      ctbReq = NULL;
35✔
642
    }
643
  }
644

645
  if (!pTbData->isOrdered) {
56,647✔
646
    code = tRowSort(pTbCtx->pData->aRowP);
50,838✔
647
  }
648
  if (code == TSDB_CODE_SUCCESS && (!pTbData->isOrdered || pTbData->isDuplicateTs)) {
56,645!
649
    code = tRowMerge(pTbCtx->pData->aRowP, pTbCtx->pSchema, 0);
50,836✔
650
  }
651

652
  if (TSDB_CODE_SUCCESS != code) {
56,512!
653
    return code;
×
654
  }
655

656
  SVgroupDataCxt* pVgCxt = NULL;
56,512✔
657
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
56,512✔
658
  if (NULL == pp) {
56,805✔
659
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
23,442✔
660
    if (NULL == pp) {
23,434!
661
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
23,434✔
662
    } else {
663
      pVgCxt = *(SVgroupDataCxt**)pp;
×
664
    }
665
  } else {
666
    pVgCxt = *(SVgroupDataCxt**)pp;
33,363✔
667
  }
668

669
  if (TSDB_CODE_SUCCESS == code) {
56,800✔
670
    code = fillVgroupDataCxt(pTbCtx, pVgCxt, false, false);
56,769✔
671
  }
672

673
  if (taosArrayGetSize(pVgCxt->pData->aSubmitTbData) >= 20000) {
56,684!
674
    code = qBuildStmtFinOutput1((SQuery*)pBuildInfo->pQuery, pAllVgHash, pBuildInfo->pVgroupList);
×
675
    // taosArrayClear(pVgCxt->pData->aSubmitTbData);
676
    tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
×
677
    // insDestroyVgroupDataCxt(pVgCxt);
678
  }
679

680
  return code;
56,617✔
681
}
682

683
/*
684
int32_t insMergeStmtTableDataCxt(STableDataCxt* pTableCxt, SArray* pTableList, SArray** pVgDataBlocks, bool isRebuild,
685
int32_t tbNum) { SHashObj* pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, false);
686
  SArray*   pVgroupList = taosArrayInit(8, POINTER_BYTES);
687
  if (NULL == pVgroupHash || NULL == pVgroupList) {
688
    taosHashCleanup(pVgroupHash);
689
    taosArrayDestroy(pVgroupList);
690
    return TSDB_CODE_OUT_OF_MEMORY;
691
  }
692

693
  int32_t code = TSDB_CODE_SUCCESS;
694

695
  for (int32_t i = 0; i < tbNum; ++i) {
696
    STableColsData *pTableCols = (STableColsData*)taosArrayGet(pTableList, i);
697
    pTableCxt->pMeta->vgId = pTableCols->vgId;
698
    pTableCxt->pMeta->uid = pTableCols->uid;
699
    pTableCxt->pData->uid = pTableCols->uid;
700
    pTableCxt->pData->aCol = pTableCols->aCol;
701

702
    SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
703
    if (pCol->nVal <= 0) {
704
      continue;
705
    }
706

707
    if (pTableCxt->pData->pCreateTbReq) {
708
      pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
709
    }
710

711
    taosArraySort(pTableCxt->pData->aCol, insColDataComp);
712

713
    tColDataSortMerge(pTableCxt->pData->aCol);
714

715
    if (TSDB_CODE_SUCCESS == code) {
716
      SVgroupDataCxt* pVgCxt = NULL;
717
      int32_t         vgId = pTableCxt->pMeta->vgId;
718
      void**          pp = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
719
      if (NULL == pp) {
720
        code = createVgroupDataCxt(pTableCxt, pVgroupHash, pVgroupList, &pVgCxt);
721
      } else {
722
        pVgCxt = *(SVgroupDataCxt**)pp;
723
      }
724
      if (TSDB_CODE_SUCCESS == code) {
725
        code = fillVgroupDataCxt(pTableCxt, pVgCxt, false, false);
726
      }
727
    }
728
  }
729

730
  taosHashCleanup(pVgroupHash);
731
  if (TSDB_CODE_SUCCESS == code) {
732
    *pVgDataBlocks = pVgroupList;
733
  } else {
734
    insDestroyVgroupDataCxtList(pVgroupList);
735
  }
736

737
  return code;
738
}
739
*/
740

741
int32_t insMergeTableDataCxt(SHashObj* pTableHash, SArray** pVgDataBlocks, bool isRebuild) {
1,809,695✔
742
  SHashObj* pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
1,809,695✔
743
  SArray*   pVgroupList = taosArrayInit(8, POINTER_BYTES);
1,809,882✔
744
  if (NULL == pVgroupHash || NULL == pVgroupList) {
1,809,923!
UNCOV
745
    taosHashCleanup(pVgroupHash);
×
746
    taosArrayDestroy(pVgroupList);
×
747
    return terrno;
×
748
  }
749

750
  int32_t code = TSDB_CODE_SUCCESS;
1,809,924✔
751
  bool    colFormat = false;
1,809,924✔
752

753
  void* p = taosHashIterate(pTableHash, NULL);
1,809,924✔
754
  if (p) {
1,809,918!
755
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
1,809,929✔
756
    colFormat = (0 != (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT));
1,809,929✔
757
  }
758

759
  while (TSDB_CODE_SUCCESS == code && NULL != p) {
3,719,943✔
760
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
1,910,041✔
761
    if (colFormat) {
1,910,041✔
762
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
40,955✔
763
      if (pCol && pCol->nVal <= 0) {
40,954!
764
        p = taosHashIterate(pTableHash, p);
16✔
765
        continue;
16✔
766
      }
767

768
      if (pTableCxt->pData->pCreateTbReq) {
40,938✔
769
        pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
40,143✔
770
      }
771

772
      taosArraySort(pTableCxt->pData->aCol, insColDataComp);
40,938✔
773

774
      code = tColDataSortMerge(&pTableCxt->pData->aCol);
40,930✔
775
    } else {
776
      // skip the table has no data to insert
777
      // eg: import a csv without valid data
778
      // if (0 == taosArrayGetSize(pTableCxt->pData->aRowP)) {
779
      //   parserWarn("no row in tableDataCxt uid:%" PRId64 " ", pTableCxt->pMeta->uid);
780
      //   p = taosHashIterate(pTableHash, p);
781
      //   continue;
782
      // }
783
      if (!pTableCxt->ordered) {
1,869,086✔
784
        code = tRowSort(pTableCxt->pData->aRowP);
18,618✔
785
      }
786
      if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
1,869,091✔
787
        code = tRowMerge(pTableCxt->pData->aRowP, pTableCxt->pSchema, 0);
18,669✔
788
      }
789
    }
790

791
    if (TSDB_CODE_SUCCESS == code) {
1,910,009!
792
      SVgroupDataCxt* pVgCxt = NULL;
1,910,009✔
793
      int32_t         vgId = pTableCxt->pMeta->vgId;
1,910,009✔
794
      void**          pp = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
1,910,009✔
795
      if (NULL == pp) {
1,910,005✔
796
        code = createVgroupDataCxt(vgId, pVgroupHash, pVgroupList, &pVgCxt);
1,851,163✔
797
      } else {
798
        pVgCxt = *(SVgroupDataCxt**)pp;
58,842✔
799
      }
800
      if (TSDB_CODE_SUCCESS == code) {
1,910,014!
801
        code = fillVgroupDataCxt(pTableCxt, pVgCxt, isRebuild, true);
1,910,016✔
802
      }
803
    }
804
    if (TSDB_CODE_SUCCESS == code) {
1,909,976!
805
      p = taosHashIterate(pTableHash, p);
1,909,989✔
806
    }
807
  }
808

809
  taosHashCleanup(pVgroupHash);
1,809,902✔
810
  if (TSDB_CODE_SUCCESS == code) {
1,809,916!
811
    *pVgDataBlocks = pVgroupList;
1,809,916✔
812
  } else {
UNCOV
813
    insDestroyVgroupDataCxtList(pVgroupList);
×
814
  }
815

816
  return code;
1,809,920✔
817
}
818

819
static int32_t buildSubmitReq(int32_t vgId, SSubmitReq2* pReq, void** pData, uint32_t* pLen) {
1,874,519✔
820
  int32_t  code = TSDB_CODE_SUCCESS;
1,874,519✔
821
  uint32_t len = 0;
1,874,519✔
822
  void*    pBuf = NULL;
1,874,519✔
823
  tEncodeSize(tEncodeSubmitReq, pReq, len, code);
1,874,519!
824
  if (TSDB_CODE_SUCCESS == code) {
1,874,448!
825
    SEncoder encoder;
826
    len += sizeof(SSubmitReq2Msg);
1,874,505✔
827
    pBuf = taosMemoryMalloc(len);
1,874,505!
828
    if (NULL == pBuf) {
1,874,571!
829
      return terrno;
×
830
    }
831
    ((SSubmitReq2Msg*)pBuf)->header.vgId = htonl(vgId);
1,874,571✔
832
    ((SSubmitReq2Msg*)pBuf)->header.contLen = htonl(len);
1,874,571✔
833
    ((SSubmitReq2Msg*)pBuf)->version = htobe64(1);
1,874,571✔
834
    tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SSubmitReq2Msg)), len - sizeof(SSubmitReq2Msg));
1,874,487✔
835
    code = tEncodeSubmitReq(&encoder, pReq);
1,874,557✔
836
    tEncoderClear(&encoder);
1,874,589✔
837
  }
838

839
  if (TSDB_CODE_SUCCESS == code) {
1,874,636!
840
    *pData = pBuf;
1,874,636✔
841
    *pLen = len;
1,874,636✔
842
  } else {
843
    taosMemoryFree(pBuf);
×
844
  }
845
  return code;
1,874,527✔
846
}
847

848
static void destroyVgDataBlocks(void* p) {
×
849
  if (p == NULL) return;
×
850
  SVgDataBlocks* pVg = p;
×
851
  taosMemoryFree(pVg->pData);
×
852
  taosMemoryFree(pVg);
×
853
}
854

855
int32_t insBuildVgDataBlocks(SHashObj* pVgroupsHashObj, SArray* pVgDataCxtList, SArray** pVgDataBlocks, bool append) {
1,832,211✔
856
  size_t  numOfVg = taosArrayGetSize(pVgDataCxtList);
1,832,211✔
857
  SArray* pDataBlocks = (append && *pVgDataBlocks) ? *pVgDataBlocks : taosArrayInit(numOfVg, POINTER_BYTES);
1,832,301!
858
  if (NULL == pDataBlocks) {
1,832,346!
859
    return TSDB_CODE_OUT_OF_MEMORY;
×
860
  }
861

862
  int32_t code = TSDB_CODE_SUCCESS;
1,832,346✔
863
  for (size_t i = 0; TSDB_CODE_SUCCESS == code && i < numOfVg; ++i) {
3,706,926✔
864
    SVgroupDataCxt* src = taosArrayGetP(pVgDataCxtList, i);
1,874,557✔
865
    if (taosArrayGetSize(src->pData->aSubmitTbData) <= 0) {
1,874,587!
866
      continue;
×
867
    }
868
    SVgDataBlocks* dst = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
1,874,612!
869
    if (NULL == dst) {
1,874,564!
870
      code = terrno;
×
871
    }
872
    if (TSDB_CODE_SUCCESS == code) {
1,874,564!
873
      dst->numOfTables = taosArrayGetSize(src->pData->aSubmitTbData);
1,874,591✔
874
      code = taosHashGetDup(pVgroupsHashObj, (const char*)&src->vgId, sizeof(src->vgId), &dst->vg);
1,874,606✔
875
    }
876
    if (TSDB_CODE_SUCCESS == code) {
1,874,630!
877
      code = buildSubmitReq(src->vgId, src->pData, &dst->pData, &dst->size);
1,874,630✔
878
    }
879
    if (TSDB_CODE_SUCCESS == code) {
1,874,532!
880
      code = (NULL == taosArrayPush(pDataBlocks, &dst) ? terrno : TSDB_CODE_SUCCESS);
1,874,570!
881
    }
882
    if (TSDB_CODE_SUCCESS != code) {
1,874,567!
883
      destroyVgDataBlocks(dst);
×
884
    }
885
  }
886

887
  if (append) {
1,832,369✔
888
    if (NULL == *pVgDataBlocks) {
22,422!
889
      *pVgDataBlocks = pDataBlocks;
22,426✔
890
    }
891
    return code;
22,422✔
892
  }
893

894
  if (TSDB_CODE_SUCCESS == code) {
1,809,947✔
895
    *pVgDataBlocks = pDataBlocks;
1,809,853✔
896
  } else {
897
    taosArrayDestroyP(pDataBlocks, destroyVgDataBlocks);
94✔
898
  }
899

900
  return code;
1,809,848✔
901
}
902

903
static bool findFileds(SSchema* pSchema, TAOS_FIELD* fields, int numFields) {
×
904
  for (int i = 0; i < numFields; i++) {
×
905
    if (strcmp(pSchema->name, fields[i].name) == 0) {
×
906
      return true;
×
907
    }
908
  }
909

910
  return false;
×
911
}
912

913
int32_t checkSchema(SSchema* pColSchema, SSchemaExt* pColExtSchema, int8_t* fields, char* errstr, int32_t errstrLen) {
597✔
914
  if (*fields != pColSchema->type) {
597✔
915
    if (errstr != NULL)
1!
916
      snprintf(errstr, errstrLen, "column type not equal, name:%s, schema type:%s, data type:%s", pColSchema->name,
×
917
               tDataTypes[pColSchema->type].name, tDataTypes[*fields].name);
×
918
    return TSDB_CODE_INVALID_PARA;
1✔
919
  }
920

921
  if (IS_DECIMAL_TYPE(pColSchema->type)) {
596!
922
    uint8_t precision = 0, scale = 0;
1✔
923
    decimalFromTypeMod(pColExtSchema->typeMod, &precision, &scale);
1✔
924
    uint8_t precisionData = 0, scaleData  = 0;
1✔
925
    int32_t bytes = *(int32_t*)(fields + sizeof(int8_t));
1✔
926
    extractDecimalTypeInfoFromBytes(&bytes, &precisionData, &scaleData);
1✔
927
    if (precision != precisionData || scale != scaleData) {
1!
928
      if (errstr != NULL)
×
929
        snprintf(errstr, errstrLen,
×
930
                 "column decimal type not equal, name:%s, schema type:%s, precision:%d, scale:%d, data type:%s, "
931
                 "precision:%d, scale:%d",
932
                 pColSchema->name, tDataTypes[pColSchema->type].name, precision, scale, tDataTypes[*fields].name,
×
933
                 precisionData, scaleData);
934
      return TSDB_CODE_INVALID_PARA;
×
935
    }
936
    return 0;
1✔
937
  }
938

939
  if (IS_VAR_DATA_TYPE(pColSchema->type) && *(int32_t*)(fields + sizeof(int8_t)) > pColSchema->bytes) {
595!
940
    if (errstr != NULL)
1!
941
      snprintf(errstr, errstrLen,
×
942
               "column var data bytes error, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
943
               pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name,
×
944
               *(int32_t*)(fields + sizeof(int8_t)));
×
945
    return TSDB_CODE_INVALID_PARA;
1✔
946
  }
947

948
  if (!IS_VAR_DATA_TYPE(pColSchema->type) && *(int32_t*)(fields + sizeof(int8_t)) != pColSchema->bytes) {
594!
949
    if (errstr != NULL)
×
950
      snprintf(errstr, errstrLen,
×
951
               "column normal data bytes not equal, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
952
               pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name,
×
953
               *(int32_t*)(fields + sizeof(int8_t)));
×
954
    return TSDB_CODE_INVALID_PARA;
×
955
  }
956
  return 0;
594✔
957
}
958

959
#define PRCESS_DATA(i, j)                                                                                 \
960
  ret = checkSchema(pColSchema, pColExtSchema, fields, errstr, errstrLen);                                               \
961
  if (ret != 0) {                                                                                         \
962
    goto end;                                                                                             \
963
  }                                                                                                       \
964
                                                                                                          \
965
  if (pColSchema->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {                                                 \
966
    hasTs = true;                                                                                         \
967
  }                                                                                                       \
968
                                                                                                          \
969
  int8_t* offset = pStart;                                                                                \
970
  if (IS_VAR_DATA_TYPE(pColSchema->type)) {                                                               \
971
    pStart += numOfRows * sizeof(int32_t);                                                                \
972
  } else {                                                                                                \
973
    pStart += BitmapLen(numOfRows);                                                                       \
974
  }                                                                                                       \
975
  char* pData = pStart;                                                                                   \
976
                                                                                                          \
977
  SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, j);                                               \
978
  ret = tColDataAddValueByDataBlock(pCol, pColSchema->type, pColSchema->bytes, numOfRows, offset, pData); \
979
  if (ret != 0) {                                                                                         \
980
    goto end;                                                                                             \
981
  }                                                                                                       \
982
  fields += sizeof(int8_t) + sizeof(int32_t);                                                             \
983
  if (needChangeLength && version == BLOCK_VERSION_1) {                                                   \
984
    pStart += htonl(colLength[i]);                                                                        \
985
  } else {                                                                                                \
986
    pStart += colLength[i];                                                                               \
987
  }                                                                                                       \
988
  boundInfo->pColIndex[j] = -1;
989

990
int rawBlockBindData(SQuery* query, STableMeta* pTableMeta, void* data, SVCreateTbReq* pCreateTb, void* tFields,
134✔
991
                     int numFields, bool needChangeLength, char* errstr, int32_t errstrLen, bool raw) {
992
  int ret = 0;
134✔
993
  if (data == NULL) {
134!
994
    uError("rawBlockBindData, data is NULL");
×
995
    return TSDB_CODE_APP_ERROR;
×
996
  }
997
  void* tmp =
998
      taosHashGet(((SVnodeModifyOpStmt*)(query->pRoot))->pTableBlockHashObj, &pTableMeta->uid, sizeof(pTableMeta->uid));
134✔
999
  SVCreateTbReq* pCreateReqTmp = NULL;
134✔
1000
  if (tmp == NULL && pCreateTb != NULL) {
134✔
1001
    ret = cloneSVreateTbReq(pCreateTb, &pCreateReqTmp);
18✔
1002
    if (ret != TSDB_CODE_SUCCESS) {
18!
1003
      uError("cloneSVreateTbReq error");
×
1004
      goto end;
×
1005
    }
1006
  }
1007

1008
  STableDataCxt* pTableCxt = NULL;
134✔
1009
  ret = insGetTableDataCxt(((SVnodeModifyOpStmt*)(query->pRoot))->pTableBlockHashObj, &pTableMeta->uid,
134✔
1010
                           sizeof(pTableMeta->uid), pTableMeta, &pCreateReqTmp, &pTableCxt, true, false);
1011
  if (pCreateReqTmp != NULL) {
134!
1012
    tdDestroySVCreateTbReq(pCreateReqTmp);
×
1013
    taosMemoryFree(pCreateReqTmp);
×
1014
  }
1015

1016
  if (ret != TSDB_CODE_SUCCESS) {
134!
1017
    uError("insGetTableDataCxt error");
×
1018
    goto end;
×
1019
  }
1020

1021
  pTableCxt->pData->flags |= TD_REQ_FROM_TAOX;
134✔
1022
  if (tmp == NULL) {
134✔
1023
    ret = initTableColSubmitData(pTableCxt);
121✔
1024
    if (ret != TSDB_CODE_SUCCESS) {
121!
1025
      uError("initTableColSubmitData error");
×
1026
      goto end;
×
1027
    }
1028
  }
1029

1030
  char* p = (char*)data;
134✔
1031
  // | version | total length | total rows | blankFill | total columns | flag seg| block group id | column schema | each
1032
  // column length |
1033
  int32_t version = *(int32_t*)data;
134✔
1034
  p += sizeof(int32_t);
134✔
1035
  p += sizeof(int32_t);
134✔
1036

1037
  int32_t numOfRows = *(int32_t*)p;
134✔
1038
  p += sizeof(int32_t);
134✔
1039

1040
  int32_t numOfCols = *(int32_t*)p;
134✔
1041
  p += sizeof(int32_t);
134✔
1042

1043
  p += sizeof(int32_t);
134✔
1044
  p += sizeof(uint64_t);
134✔
1045

1046
  int8_t* fields = p;
134✔
1047
  if (*fields >= TSDB_DATA_TYPE_MAX || *fields < 0) {
134!
1048
    uError("fields type error:%d", *fields);
×
1049
    ret = TSDB_CODE_INVALID_PARA;
×
1050
    goto end;
×
1051
  }
1052
  p += numOfCols * (sizeof(int8_t) + sizeof(int32_t));
134✔
1053

1054
  int32_t* colLength = (int32_t*)p;
134✔
1055
  p += sizeof(int32_t) * numOfCols;
134✔
1056

1057
  char* pStart = p;
134✔
1058

1059
  SSchema*          pSchema     = getTableColumnSchema(pTableCxt->pMeta);
134✔
1060
  SSchemaExt*       pExtSchemas = getTableColumnExtSchema(pTableCxt->pMeta);
134✔
1061
  SBoundColInfo* boundInfo = &pTableCxt->boundColsInfo;
134✔
1062

1063
  if (tFields != NULL && numFields != numOfCols) {
134!
1064
    if (errstr != NULL) snprintf(errstr, errstrLen, "numFields:%d not equal to data cols:%d", numFields, numOfCols);
×
1065
    ret = TSDB_CODE_INVALID_PARA;
×
1066
    goto end;
×
1067
  }
1068

1069
  bool hasTs = false;
134✔
1070
  if (tFields == NULL) {
134✔
1071
    int32_t len = TMIN(numOfCols, boundInfo->numOfBound);
6✔
1072
    for (int j = 0; j < len; j++) {
18✔
1073
      SSchema* pColSchema = &pSchema[j];
14✔
1074
      SSchemaExt* pColExtSchema = &pExtSchemas[j];
14✔
1075
      PRCESS_DATA(j, j)
14!
1076
    }
1077
  } else {
1078
    for (int i = 0; i < numFields; i++) {
674✔
1079
      for (int j = 0; j < boundInfo->numOfBound; j++) {
4,392✔
1080
        SSchema* pColSchema = &pSchema[j];
4,391✔
1081
        SSchemaExt* pColExtSchema = &pExtSchemas[j];
4,391✔
1082
        char*    fieldName = NULL;
4,391✔
1083
        if (raw) {
4,391✔
1084
          fieldName = ((SSchemaWrapper*)tFields)->pSchema[i].name;
4,386✔
1085
        } else {
1086
          fieldName = ((TAOS_FIELD*)tFields)[i].name;
5✔
1087
        }
1088
        if (strcmp(pColSchema->name, fieldName) == 0) {
4,391✔
1089
          PRCESS_DATA(i, j)
545!
1090
          break;
545✔
1091
        }
1092
      }
1093
    }
1094
  }
1095

1096
  if (!hasTs) {
132!
1097
    if (errstr != NULL) snprintf(errstr, errstrLen, "timestamp column(primary key) not found in raw data");
×
1098
    ret = TSDB_CODE_INVALID_PARA;
×
1099
    goto end;
×
1100
  }
1101

1102
  // process NULL data
1103
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
701✔
1104
    if (boundInfo->pColIndex[c] != -1) {
569✔
1105
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, c);
13✔
1106
      ret = tColDataAddValueByDataBlock(pCol, 0, 0, numOfRows, NULL, NULL);
13✔
1107
      if (ret != 0) {
13!
1108
        goto end;
×
1109
      }
1110
    } else {
1111
      boundInfo->pColIndex[c] = c;  // restore for next block
556✔
1112
    }
1113
  }
1114

1115
end:
132✔
1116
  return ret;
134✔
1117
}
1118

1119
int rawBlockBindRawData(SHashObj* pVgroupHash, SArray* pVgroupList, STableMeta* pTableMeta, void* data) {
17✔
1120
  int code = transformRawSSubmitTbData(data, pTableMeta->suid, pTableMeta->uid, pTableMeta->sversion);
17✔
1121
  if (code != 0){
17!
1122
    return code;
×
1123
  }
1124
  SVgroupDataCxt* pVgCxt = NULL;
17✔
1125
  void**          pp = taosHashGet(pVgroupHash, &pTableMeta->vgId, sizeof(pTableMeta->vgId));
17✔
1126
  if (NULL == pp) {
17!
1127
    code = createVgroupDataCxt(pTableMeta->vgId, pVgroupHash, pVgroupList, &pVgCxt);
17✔
1128
    if (code != 0){
17!
1129
      return code;
×
1130
    }
1131
  } else {
1132
    pVgCxt = *(SVgroupDataCxt**)pp;
×
1133
  }
1134
  if (NULL == pVgCxt->pData->aSubmitTbData) {
17!
1135
    pVgCxt->pData->aSubmitTbData = taosArrayInit(0, POINTER_BYTES);
17✔
1136
    pVgCxt->pData->raw = true;
17✔
1137
    if (NULL == pVgCxt->pData->aSubmitTbData) {
17!
1138
      return terrno;
×
1139
    }
1140
  }
1141

1142
  // push data to submit, rebuild empty data for next submit
1143
  if (NULL == taosArrayPush(pVgCxt->pData->aSubmitTbData, &data)) {
34!
1144
    return terrno;
×
1145
  }
1146

1147
  uTrace("add raw data to vgId:%d, len:%d", pTableMeta->vgId, *(int32_t*)data);
17!
1148

1149
  return 0;
17✔
1150
}
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