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

taosdata / TDengine / #4066

12 May 2025 05:35AM UTC coverage: 62.547% (+0.04%) from 62.508%
#4066

push

travis-ci

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

merge: from main to 3.0 branch

155777 of 317858 branches covered (49.01%)

Branch coverage included in aggregate %.

382 of 573 new or added lines in 31 files covered. (66.67%)

648 existing lines in 129 files now uncovered.

241270 of 316936 relevant lines covered (76.13%)

6462449.7 hits per line

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

71.54
/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,039✔
27
  if (NULL == pInfo) {
44,039✔
28
    return;
23,243✔
29
  }
30

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

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

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

41
  for (uint32_t i = 0; i < pToken->n; ++i) {
15,664,671✔
42
    if (*(pToken->z + i) == target && (!inEscape) && (!inQuote)) {
14,600,460!
43
      return pToken->z + i;
977,320✔
44
    }
45

46
    if (*(pToken->z + i) == TS_ESCAPE_CHAR) {
13,623,140✔
47
      if (!inQuote) {
187,741!
48
        inEscape = !inEscape;
187,784✔
49
      }
50
    }
51

52
    if (*(pToken->z + i) == '\'' || *(pToken->z + i) == '"') {
13,623,140✔
53
      if (!inEscape) {
24,114✔
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,211✔
65
}
66

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

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

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

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

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

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

99
    code = tNameFromString(pName, tbname, T_NAME_TABLE);
977,326✔
100
    if (code != 0) {
977,259!
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,308✔
105
    strncpy(tbname, pTableName->z, pTableName->n);
1,064,308✔
106
    int32_t tbLen = strdequote(tbname);
1,064,308✔
107
    if (tbLen >= TSDB_TABLE_NAME_LEN) {
1,064,287!
108
      return buildInvalidOperationMsg(pMsgBuf, msg1);
×
109
    }
110
    if (tbLen == 0) {
1,064,308!
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,308✔
115
    strncpy(name, pTableName->z, pTableName->n);
1,064,308✔
116
    (void)strdequote(name);
1,064,308✔
117

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

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

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

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

139
  return code;
2,041,561✔
140
}
141

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

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

169
  return TSDB_CODE_SUCCESS;
68,475✔
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,886,763✔
179
  SSchema* pSchemas = getTableColumnSchema(pTableMeta);
1,886,763✔
180
  int32_t  code = 0;
1,886,793✔
181
  for (int32_t i = 0; i < pTableMeta->tableInfo.numOfColumns; ++i) {
69,617,995✔
182
    SColVal val = COL_VAL_NONE(pSchemas[i].colId, pSchemas[i].type);
67,715,193✔
183
    if (NULL == taosArrayPush(pValues, &val)) {
67,731,202!
184
      code = terrno;
×
185
      break;
×
186
    }
187
  }
188
  return code;
1,902,802✔
189
}
190

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

193
int32_t insInitBoundColsInfo(int32_t numOfBound, SBoundColInfo* pInfo) {
1,981,881✔
194
  pInfo->numOfCols = numOfBound;
1,981,881✔
195
  pInfo->numOfBound = numOfBound;
1,981,881✔
196
  pInfo->hasBoundCols = false;
1,981,881✔
197
  pInfo->mixTagsCols = false;
1,981,881✔
198
  pInfo->pColIndex = taosMemoryCalloc(numOfBound, sizeof(int16_t));
1,981,881!
199
  if (NULL == pInfo->pColIndex) {
1,982,054!
200
    return terrno;
×
201
  }
202
  for (int32_t i = 0; i < numOfBound; ++i) {
70,208,105✔
203
    pInfo->pColIndex[i] = i;
68,226,051✔
204
  }
205
  return TSDB_CODE_SUCCESS;
1,982,054✔
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) {
145,134,488✔
218
  // once the data block is disordered, we do NOT keep last timestamp any more
219
  if (!pTableCxt->ordered) {
145,134,488✔
220
    return;
6,417,182✔
221
  }
222

223
  if (tRowKeyCompare(rowKey, &pTableCxt->lastKey) < 0) {
138,717,306✔
224
    pTableCxt->ordered = false;
18,426✔
225
  }
226

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

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

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

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

246
  int32_t code = TSDB_CODE_SUCCESS;
1,885,587✔
247

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

252
  pTableCxt->pMeta = tableMetaDup(pTableMeta);
1,885,587✔
253
  if (NULL == pTableCxt->pMeta) {
1,885,576!
254
    code = TSDB_CODE_OUT_OF_MEMORY;
×
255
  }
256
  if (TSDB_CODE_SUCCESS == code) {
1,885,576!
257
    pTableCxt->pSchema =
1,885,604✔
258
        tBuildTSchema(getTableColumnSchema(pTableMeta), pTableMeta->tableInfo.numOfColumns, pTableMeta->sversion);
1,885,604✔
259
    if (NULL == pTableCxt->pSchema) {
1,885,604!
260
      code = TSDB_CODE_OUT_OF_MEMORY;
×
261
    }
262
  }
263
  if (TSDB_CODE_SUCCESS == code) {
1,885,576✔
264
    code = insInitBoundColsInfo(pTableMeta->tableInfo.numOfColumns, &pTableCxt->boundColsInfo);
1,885,553✔
265
  }
266
  if (TSDB_CODE_SUCCESS == code && !ignoreColVals) {
1,885,652✔
267
    pTableCxt->pValues = taosArrayInit(pTableMeta->tableInfo.numOfColumns, sizeof(SColVal));
1,876,371✔
268
    if (NULL == pTableCxt->pValues) {
1,876,321!
269
      code = terrno;
×
270
    } else {
271
      code = initColValues(pTableMeta, pTableCxt->pValues);
1,876,321✔
272
    }
273
  }
274
  if (TSDB_CODE_SUCCESS == code) {
1,885,609✔
275
    pTableCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
1,885,599!
276
    if (NULL == pTableCxt->pData) {
1,885,677!
277
      code = terrno;
×
278
    } else {
279
      pTableCxt->pData->flags = (pCreateTbReq != NULL && NULL != *pCreateTbReq) ? SUBMIT_REQ_AUTO_CREATE_TABLE : 0;
1,885,677!
280
      pTableCxt->pData->flags |= colMode ? SUBMIT_REQ_COLUMN_DATA_FORMAT : 0;
1,885,677✔
281
      pTableCxt->pData->suid = pTableMeta->suid;
1,885,677✔
282
      pTableCxt->pData->uid = pTableMeta->uid;
1,885,677✔
283
      pTableCxt->pData->sver = pTableMeta->sversion;
1,885,677✔
284
      pTableCxt->pData->pCreateTbReq = pCreateTbReq != NULL ? *pCreateTbReq : NULL;
1,885,677✔
285
      if (pCreateTbReq != NULL) *pCreateTbReq = NULL;
1,885,677✔
286
      if (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,885,677✔
287
        pTableCxt->pData->aCol = taosArrayInit(128, sizeof(SColData));
20,977✔
288
        if (NULL == pTableCxt->pData->aCol) {
20,964!
289
          code = terrno;
×
290
        }
291
      } else {
292
        pTableCxt->pData->aRowP = taosArrayInit(128, POINTER_BYTES);
1,864,700✔
293
        if (NULL == pTableCxt->pData->aRowP) {
1,864,624!
294
          code = terrno;
×
295
        }
296
      }
297
    }
298
  }
299
  if (TSDB_CODE_SUCCESS == code) {
1,885,598!
300
    *pOutput = pTableCxt;
1,885,598✔
301
    parserDebug("uid:%" PRId64 ", create table data context, code:%d, vgId:%d", pTableMeta->uid, code, pTableMeta->vgId);
1,885,598✔
302
  } else {
303
    insDestroyTableDataCxt(pTableCxt);
×
304
  }
305

306
  return code;
1,885,613✔
307
}
308

309
static int32_t rebuildTableData(SSubmitTbData* pSrc, SSubmitTbData** pDst) {
43,431✔
310
  int32_t        code = TSDB_CODE_SUCCESS;
43,431✔
311
  SSubmitTbData* pTmp = taosMemoryCalloc(1, sizeof(SSubmitTbData));
43,431!
312
  if (NULL == pTmp) {
43,441!
313
    code = terrno;
×
314
  } else {
315
    pTmp->flags = pSrc->flags;
43,441✔
316
    pTmp->suid = pSrc->suid;
43,441✔
317
    pTmp->uid = pSrc->uid;
43,441✔
318
    pTmp->sver = pSrc->sver;
43,441✔
319
    pTmp->pCreateTbReq = NULL;
43,441✔
320
    if (pTmp->flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
43,441✔
321
      if (pSrc->pCreateTbReq) {
42,642✔
322
        code = cloneSVreateTbReq(pSrc->pCreateTbReq, &pTmp->pCreateTbReq);
42,623✔
323
      } else {
324
        pTmp->flags &= ~SUBMIT_REQ_AUTO_CREATE_TABLE;
19✔
325
      }
326
    }
327
    if (TSDB_CODE_SUCCESS == code) {
43,435!
328
      if (pTmp->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
43,435✔
329
        pTmp->aCol = taosArrayInit(128, sizeof(SColData));
40,936✔
330
        if (NULL == pTmp->aCol) {
40,930!
331
          code = terrno;
×
332
          taosMemoryFree(pTmp);
×
333
        }
334
      } else {
335
        pTmp->aRowP = taosArrayInit(128, POINTER_BYTES);
2,499✔
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,430!
347
  if (TSDB_CODE_SUCCESS == code) {
43,433!
348
    *pDst = pTmp;
43,435✔
349
  }
350

351
  return code;
43,433✔
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,915,133✔
363
                           SVCreateTbReq** pCreateTbReq, STableDataCxt** pTableCxt, bool colMode, bool ignoreColVals) {
364
  STableDataCxt** tmp = (STableDataCxt**)taosHashGet(pHash, id, idLen);
3,915,133✔
365
  if (NULL != tmp) {
3,915,255✔
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,885,602✔
373
  if (TSDB_CODE_SUCCESS == code) {
1,885,608!
374
    void* pData = *pTableCxt;  // deal scan coverity
1,885,618✔
375
    code = taosHashPut(pHash, id, idLen, &pData, POINTER_BYTES);
1,885,618✔
376
  }
377

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

384
static void destroyColVal(void* p) {
67,753,060✔
385
  SColVal* pVal = p;
67,753,060✔
386
  if (TSDB_DATA_TYPE_NCHAR == pVal->value.type || TSDB_DATA_TYPE_GEOMETRY == pVal->value.type ||
67,753,060✔
387
      TSDB_DATA_TYPE_VARBINARY == pVal->value.type || TSDB_DATA_TYPE_DECIMAL == pVal->value.type) {
62,242,340✔
388
    taosMemoryFreeClear(pVal->value.pData);
5,701,743!
389
  }
390
}
67,753,060✔
391

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

397
  taosMemoryFreeClear(pTableCxt->pMeta);
1,905,676!
398
  tDestroyTSchema(pTableCxt->pSchema);
1,905,681!
399
  insDestroyBoundColInfo(&pTableCxt->boundColsInfo);
1,905,688✔
400
  taosArrayDestroyEx(pTableCxt->pValues, destroyColVal);
1,905,695✔
401
  if (pTableCxt->pData) {
1,905,690✔
402
    tDestroySubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
57,251✔
403
    taosMemoryFree(pTableCxt->pData);
57,244!
404
  }
405
  taosMemoryFree(pTableCxt);
1,905,705!
406
}
407

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

413
  tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
1,854,422✔
414
  taosMemoryFree(pVgCxt->pData);
1,854,488!
415
  taosMemoryFree(pVgCxt);
1,854,481!
416
}
417

418
void insDestroyVgroupDataCxtList(SArray* pVgCxtList) {
3,609,304✔
419
  if (NULL == pVgCxtList) {
3,609,304✔
420
    return;
1,795,663✔
421
  }
422

423
  size_t size = taosArrayGetSize(pVgCxtList);
1,813,641✔
424
  for (int32_t i = 0; i < size; i++) {
3,668,224✔
425
    void* p = taosArrayGetP(pVgCxtList, i);
1,854,462✔
426
    insDestroyVgroupDataCxt(p);
1,854,477✔
427
  }
428

429
  taosArrayDestroy(pVgCxtList);
1,813,762✔
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,798,335✔
448
  if (NULL == pTableCxtHash) {
1,798,335✔
449
    return;
20,858✔
450
  }
451

452
  void** p = taosHashIterate(pTableCxtHash, NULL);
1,777,477✔
453
  while (p) {
3,642,005✔
454
    insDestroyTableDataCxt(*(STableDataCxt**)p);
1,864,520✔
455

456
    p = taosHashIterate(pTableCxtHash, p);
1,864,522✔
457
  }
458

459
  taosHashCleanup(pTableCxtHash);
1,777,485✔
460
}
461

462
static int32_t fillVgroupDataCxt(STableDataCxt* pTableCxt, SVgroupDataCxt* pVgCxt, bool isRebuild, bool clear) {
1,948,067✔
463
  if (NULL == pVgCxt->pData->aSubmitTbData) {
1,948,067✔
464
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
1,854,728✔
465
    if (NULL == pVgCxt->pData->aSubmitTbData) {
1,854,717!
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,896,049!
472
    return terrno;
×
473
  }
474
  int32_t code = 0;
1,947,993✔
475
  if (isRebuild) {
1,947,993✔
476
    code = rebuildTableData(pTableCxt->pData, &pTableCxt->pData);
43,438✔
477
  } else if (clear) {
1,904,555✔
478
    taosMemoryFreeClear(pTableCxt->pData);
1,848,527!
479
  }
480

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

483
  return code;
1,947,959✔
484
}
485

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

498
  pVgCxt->vgId = vgId;
1,854,805✔
499
  int32_t code = taosHashPut(pVgroupHash, &pVgCxt->vgId, sizeof(pVgCxt->vgId), &pVgCxt, POINTER_BYTES);
1,854,805✔
500
  if (TSDB_CODE_SUCCESS == code) {
1,854,810!
501
    if (NULL == taosArrayPush(pVgroupList, &pVgCxt)) {
1,854,762!
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,854,762✔
508
  } else {
509
    insDestroyVgroupDataCxt(pVgCxt);
×
510
  }
511
  return code;
1,854,766✔
512
}
513

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

523
  return 0;
×
524
}
525

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

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

538
  int32_t code = catalogGetTableHashVgroup((SCatalog*)pBuildInfo->pCatalog, &conn, sname, &vgInfo);
56✔
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,096✔
552
                             uint64_t* uid, int32_t* vgId) {
553
  STableVgUid* pTbInfo = NULL;
56,096✔
554
  int32_t      code = 0;
56,096✔
555

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

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

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

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

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

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

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

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

598
  return code;
56,028✔
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,144✔
613
                                  SStbInterlaceInfo* pBuildInfo, SVCreateTbReq* ctbReq) {
614
  int32_t  code = TSDB_CODE_SUCCESS;
56,144✔
615
  uint64_t uid;
616
  int32_t  vgId;
617

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

620
  code = insGetStmtTableVgUid(pAllVgHash, pBuildInfo, pTbData, &uid, &vgId);
56,144✔
621
  if (ctbReq !=NULL && code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
56,054✔
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,026!
631
      return code;
×
632
    }
633
    pTbCtx->pMeta->vgId = vgId;
56,026✔
634
    pTbCtx->pMeta->uid = uid;
56,026✔
635
    pTbCtx->pData->uid = uid;
56,026✔
636
    pTbCtx->pData->pCreateTbReq = NULL;
56,026✔
637
    if (ctbReq != NULL) {
56,026✔
638
      tdDestroySVCreateTbReq(ctbReq);
639
    }
640
  }
641

642
  if (!pTbData->isOrdered) {
56,054✔
643
    code = tRowSort(pTbCtx->pData->aRowP);
50,780✔
644
  }
645
  if (code == TSDB_CODE_SUCCESS && (!pTbData->isOrdered || pTbData->isDuplicateTs)) {
56,048!
646
    code = tRowMerge(pTbCtx->pData->aRowP, pTbCtx->pSchema, 0);
50,772✔
647
  }
648

649
  if (TSDB_CODE_SUCCESS != code) {
55,971!
650
    return code;
×
651
  }
652

653
  SVgroupDataCxt* pVgCxt = NULL;
55,971✔
654
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
55,971✔
655
  if (NULL == pp) {
56,187✔
656
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
23,183✔
657
    if (NULL == pp) {
23,172!
658
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
23,172✔
659
    } else {
660
      pVgCxt = *(SVgroupDataCxt**)pp;
×
661
    }
662
  } else {
663
    pVgCxt = *(SVgroupDataCxt**)pp;
33,004✔
664
  }
665

666
  if (TSDB_CODE_SUCCESS == code) {
56,157✔
667
    code = fillVgroupDataCxt(pTbCtx, pVgCxt, false, false);
56,120✔
668
  }
669

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

677
  return code;
55,983✔
678
}
679

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

690
  int32_t code = TSDB_CODE_SUCCESS;
691

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

699
    SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
700
    if (pCol->nVal <= 0) {
701
      continue;
702
    }
703

704
    if (pTableCxt->pData->pCreateTbReq) {
705
      pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
706
    }
707

708
    taosArraySort(pTableCxt->pData->aCol, insColDataComp);
709

710
    tColDataSortMerge(pTableCxt->pData->aCol);
711

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

727
  taosHashCleanup(pVgroupHash);
728
  if (TSDB_CODE_SUCCESS == code) {
729
    *pVgDataBlocks = pVgroupList;
730
  } else {
731
    insDestroyVgroupDataCxtList(pVgroupList);
732
  }
733

734
  return code;
735
}
736
*/
737

738
int32_t insMergeTableDataCxt(SHashObj* pTableHash, SArray** pVgDataBlocks, bool isRebuild) {
1,791,691✔
739
  SHashObj* pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
1,791,691✔
740
  SArray*   pVgroupList = taosArrayInit(8, POINTER_BYTES);
1,791,852✔
741
  if (NULL == pVgroupHash || NULL == pVgroupList) {
1,791,886!
742
    taosHashCleanup(pVgroupHash);
12✔
743
    taosArrayDestroy(pVgroupList);
×
744
    return terrno;
×
745
  }
746

747
  int32_t code = TSDB_CODE_SUCCESS;
1,791,874✔
748
  bool    colFormat = false;
1,791,874✔
749

750
  void* p = taosHashIterate(pTableHash, NULL);
1,791,874✔
751
  if (p) {
1,791,897!
752
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
1,791,898✔
753
    colFormat = (0 != (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT));
1,791,898✔
754
  }
755

756
  while (TSDB_CODE_SUCCESS == code && NULL != p) {
3,683,883✔
757
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
1,892,001✔
758
    if (colFormat) {
1,892,001✔
759
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
40,955✔
760
      if (pCol && pCol->nVal <= 0) {
40,954!
761
        p = taosHashIterate(pTableHash, p);
16✔
762
        continue;
16✔
763
      }
764

765
      if (pTableCxt->pData->pCreateTbReq) {
40,938✔
766
        pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
40,146✔
767
      }
768

769
      taosArraySort(pTableCxt->pData->aCol, insColDataComp);
40,938✔
770

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

788
    if (TSDB_CODE_SUCCESS == code) {
1,891,995!
789
      SVgroupDataCxt* pVgCxt = NULL;
1,891,995✔
790
      int32_t         vgId = pTableCxt->pMeta->vgId;
1,891,995✔
791
      void**          pp = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
1,891,995✔
792
      if (NULL == pp) {
1,891,974✔
793
        code = createVgroupDataCxt(vgId, pVgroupHash, pVgroupList, &pVgCxt);
1,831,566✔
794
      } else {
795
        pVgCxt = *(SVgroupDataCxt**)pp;
60,408✔
796
      }
797
      if (TSDB_CODE_SUCCESS == code) {
1,891,989!
798
        code = fillVgroupDataCxt(pTableCxt, pVgCxt, isRebuild, true);
1,891,990✔
799
      }
800
    }
801
    if (TSDB_CODE_SUCCESS == code) {
1,891,943!
802
      p = taosHashIterate(pTableHash, p);
1,891,967✔
803
    }
804
  }
805

806
  taosHashCleanup(pVgroupHash);
1,791,882✔
807
  if (TSDB_CODE_SUCCESS == code) {
1,791,880✔
808
    *pVgDataBlocks = pVgroupList;
1,791,834✔
809
  } else {
810
    insDestroyVgroupDataCxtList(pVgroupList);
46✔
811
  }
812

813
  return code;
1,791,834✔
814
}
815

816
static int32_t buildSubmitReq(int32_t vgId, SSubmitReq2* pReq, void** pData, uint32_t* pLen) {
1,854,690✔
817
  int32_t  code = TSDB_CODE_SUCCESS;
1,854,690✔
818
  uint32_t len = 0;
1,854,690✔
819
  void*    pBuf = NULL;
1,854,690✔
820
  tEncodeSize(tEncodeSubmitReq, pReq, len, code);
1,854,690!
821
  if (TSDB_CODE_SUCCESS == code) {
1,854,535!
822
    SEncoder encoder;
823
    len += sizeof(SSubmitReq2Msg);
1,854,609✔
824
    pBuf = taosMemoryMalloc(len);
1,854,609!
825
    if (NULL == pBuf) {
1,854,756!
826
      return terrno;
×
827
    }
828
    ((SSubmitReq2Msg*)pBuf)->header.vgId = htonl(vgId);
1,854,756✔
829
    ((SSubmitReq2Msg*)pBuf)->header.contLen = htonl(len);
1,854,756✔
830
    ((SSubmitReq2Msg*)pBuf)->version = htobe64(1);
1,854,756✔
831
    tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SSubmitReq2Msg)), len - sizeof(SSubmitReq2Msg));
1,854,676✔
832
    code = tEncodeSubmitReq(&encoder, pReq);
1,854,712✔
833
    tEncoderClear(&encoder);
1,854,717✔
834
  }
835

836
  if (TSDB_CODE_SUCCESS == code) {
1,854,778!
837
    *pData = pBuf;
1,854,778✔
838
    *pLen = len;
1,854,778✔
839
  } else {
840
    taosMemoryFree(pBuf);
×
841
  }
842
  return code;
1,854,732✔
843
}
844

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

852
int32_t insBuildVgDataBlocks(SHashObj* pVgroupsHashObj, SArray* pVgDataCxtList, SArray** pVgDataBlocks, bool append) {
1,813,905✔
853
  size_t  numOfVg = taosArrayGetSize(pVgDataCxtList);
1,813,905✔
854
  SArray* pDataBlocks = (append && *pVgDataBlocks) ? *pVgDataBlocks : taosArrayInit(numOfVg, POINTER_BYTES);
1,814,012!
855
  if (NULL == pDataBlocks) {
1,814,048!
856
    return TSDB_CODE_OUT_OF_MEMORY;
×
857
  }
858

859
  int32_t code = TSDB_CODE_SUCCESS;
1,814,048✔
860
  for (size_t i = 0; TSDB_CODE_SUCCESS == code && i < numOfVg; ++i) {
3,668,755✔
861
    SVgroupDataCxt* src = taosArrayGetP(pVgDataCxtList, i);
1,854,689✔
862
    if (taosArrayGetSize(src->pData->aSubmitTbData) <= 0) {
1,854,766!
863
      continue;
×
864
    }
865
    SVgDataBlocks* dst = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
1,854,773!
866
    if (NULL == dst) {
1,854,771!
867
      code = terrno;
×
868
    }
869
    if (TSDB_CODE_SUCCESS == code) {
1,854,771!
870
      dst->numOfTables = taosArrayGetSize(src->pData->aSubmitTbData);
1,854,781✔
871
      code = taosHashGetDup(pVgroupsHashObj, (const char*)&src->vgId, sizeof(src->vgId), &dst->vg);
1,854,746✔
872
    }
873
    if (TSDB_CODE_SUCCESS == code) {
1,854,783!
874
      code = buildSubmitReq(src->vgId, src->pData, &dst->pData, &dst->size);
1,854,783✔
875
    }
876
    if (TSDB_CODE_SUCCESS == code) {
1,854,716!
877
      code = (NULL == taosArrayPush(pDataBlocks, &dst) ? terrno : TSDB_CODE_SUCCESS);
1,854,690!
878
    }
879
    if (TSDB_CODE_SUCCESS != code) {
1,854,671!
880
      destroyVgDataBlocks(dst);
×
881
    }
882
  }
883

884
  if (append) {
1,814,066✔
885
    if (NULL == *pVgDataBlocks) {
22,162!
886
      *pVgDataBlocks = pDataBlocks;
22,163✔
887
    }
888
    return code;
22,162✔
889
  }
890

891
  if (TSDB_CODE_SUCCESS == code) {
1,791,904✔
892
    *pVgDataBlocks = pDataBlocks;
1,791,852✔
893
  } else {
894
    taosArrayDestroyP(pDataBlocks, destroyVgDataBlocks);
52✔
895
  }
896

897
  return code;
1,791,824✔
898
}
899

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

907
  return false;
×
908
}
909

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

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

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

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

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

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

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

1013
  if (ret != TSDB_CODE_SUCCESS) {
135!
1014
    uError("insGetTableDataCxt error");
×
1015
    goto end;
×
1016
  }
1017

1018
  pTableCxt->pData->flags |= TD_REQ_FROM_TAOX;
135✔
1019
  if (tmp == NULL) {
135✔
1020
    ret = initTableColSubmitData(pTableCxt);
122✔
1021
    if (ret != TSDB_CODE_SUCCESS) {
122!
1022
      uError("initTableColSubmitData error");
×
1023
      goto end;
×
1024
    }
1025
  }
1026

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

1034
  int32_t numOfRows = *(int32_t*)p;
135✔
1035
  p += sizeof(int32_t);
135✔
1036

1037
  int32_t numOfCols = *(int32_t*)p;
135✔
1038
  p += sizeof(int32_t);
135✔
1039

1040
  p += sizeof(int32_t);
135✔
1041
  p += sizeof(uint64_t);
135✔
1042

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

1051
  int32_t* colLength = (int32_t*)p;
135✔
1052
  p += sizeof(int32_t) * numOfCols;
135✔
1053

1054
  char* pStart = p;
135✔
1055

1056
  SSchema*          pSchema     = getTableColumnSchema(pTableCxt->pMeta);
135✔
1057
  SSchemaExt*       pExtSchemas = getTableColumnExtSchema(pTableCxt->pMeta);
135✔
1058
  SBoundColInfo* boundInfo = &pTableCxt->boundColsInfo;
135✔
1059

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

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

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

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

1112
end:
133✔
1113
  return ret;
135✔
1114
}
1115

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

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

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

1146
  return 0;
17✔
1147
}
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