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

taosdata / TDengine / #4513

17 Jul 2025 02:02AM UTC coverage: 31.359% (-31.1%) from 62.446%
#4513

push

travis-ci

web-flow
Merge pull request #31914 from taosdata/fix/3.0/compare-ans-failed

fix:Convert line endings from LF to CRLF for ans file

68541 of 301034 branches covered (22.77%)

Branch coverage included in aggregate %.

117356 of 291771 relevant lines covered (40.22%)

602262.98 hits per line

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

31.92
/source/libs/parser/src/parInsertUtil.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "parInsertUtil.h"
17

18
#include "catalog.h"
19
#include "parInt.h"
20
#include "parUtil.h"
21
#include "querynodes.h"
22
#include "tRealloc.h"
23
#include "tdatablock.h"
24
#include "tdataformat.h"
25
#include "tmisce.h"
26

27
void qDestroyBoundColInfo(void* pInfo) {
×
28
  if (NULL == pInfo) {
×
29
    return;
×
30
  }
31

32
  SBoundColInfo* pBoundInfo = (SBoundColInfo*)pInfo;
×
33

34
  taosMemoryFreeClear(pBoundInfo->pColIndex);
×
35
}
36

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

42
  for (uint32_t i = 0; i < pToken->n; ++i) {
326,069✔
43
    if (*(pToken->z + i) == target && (!inEscape) && (!inQuote)) {
297,709!
44
      return pToken->z + i;
15✔
45
    }
46

47
    if (*(pToken->z + i) == TS_ESCAPE_CHAR) {
297,694!
48
      if (!inQuote) {
×
49
        inEscape = !inEscape;
×
50
      }
51
    }
52

53
    if (*(pToken->z + i) == '\'' || *(pToken->z + i) == '"') {
297,694!
54
      if (!inEscape) {
×
55
        if (!inQuote) {
×
56
          quotaStr = *(pToken->z + i);
×
57
          inQuote = !inQuote;
×
58
        } else if (quotaStr == *(pToken->z + i)) {
×
59
          inQuote = !inQuote;
×
60
        }
61
      }
62
    }
63
  }
64

65
  return NULL;
28,360✔
66
}
67

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

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

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

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

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

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

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

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

119
    if (dbName == NULL) {
28,360!
120
      return buildInvalidOperationMsg(pMsgBuf, msg3);
×
121
    }
122
    if (name[0] == '\0') return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
28,360!
123

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

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

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

140
  return code;
28,375✔
141
}
142

143
int16_t insFindCol(SToken* pColname, int16_t start, int16_t end, SSchema* pSchema) {
88✔
144
  while (start < end) {
88!
145
    if (strlen(pSchema[start].name) == pColname->n && strncmp(pColname->z, pSchema[start].name, pColname->n) == 0) {
88!
146
      return start;
88✔
147
    }
148
    ++start;
×
149
  }
150
  return -1;
×
151
}
152

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

170
  return TSDB_CODE_SUCCESS;
29✔
171
}
172

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

179
static int32_t initColValues(STableMeta* pTableMeta, SArray* pValues) {
28,313✔
180
  SSchema* pSchemas = getTableColumnSchema(pTableMeta);
28,313✔
181
  int32_t  code = 0;
28,313✔
182
  for (int32_t i = 0; i < pTableMeta->tableInfo.numOfColumns; ++i) {
309,468✔
183
    SColVal val = COL_VAL_NONE(pSchemas[i].colId, pSchemas[i].type);
281,155✔
184
    if (NULL == taosArrayPush(pValues, &val)) {
281,155!
185
      code = terrno;
×
186
      break;
×
187
    }
188
  }
189
  return code;
28,313✔
190
}
191

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

194
int32_t insInitBoundColsInfo(int32_t numOfBound, SBoundColInfo* pInfo) {
28,366✔
195
  pInfo->numOfCols = numOfBound;
28,366✔
196
  pInfo->numOfBound = numOfBound;
28,366✔
197
  pInfo->hasBoundCols = false;
28,366✔
198
  pInfo->mixTagsCols = false;
28,366✔
199
  pInfo->pColIndex = taosMemoryCalloc(numOfBound, sizeof(int16_t));
28,366!
200
  if (NULL == pInfo->pColIndex) {
28,366!
201
    return terrno;
×
202
  }
203
  for (int32_t i = 0; i < numOfBound; ++i) {
309,614✔
204
    pInfo->pColIndex[i] = i;
281,248✔
205
  }
206
  return TSDB_CODE_SUCCESS;
28,366✔
207
}
208

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

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

224
  if (tRowKeyCompare(rowKey, &pTableCxt->lastKey) < 0) {
28,345!
225
    pTableCxt->ordered = false;
×
226
  }
227

228
  if (tRowKeyCompare(rowKey, &pTableCxt->lastKey) == 0) {
28,345!
229
    pTableCxt->duplicateTs = true;
×
230
  }
231

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

237
void insDestroyBoundColInfo(SBoundColInfo* pInfo) { taosMemoryFreeClear(pInfo->pColIndex); }
75,986!
238

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

247
  int32_t code = TSDB_CODE_SUCCESS;
28,313✔
248

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

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

308
  return code;
28,313✔
309
}
310

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

348
  taosMemoryFree(pSrc);
×
349
  if (TSDB_CODE_SUCCESS == code) {
×
350
    *pDst = pTmp;
×
351
  }
352

353
  return code;
×
354
}
355

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

364
int32_t insGetTableDataCxt(SHashObj* pHash, void* id, int32_t idLen, STableMeta* pTableMeta,
28,313✔
365
                           SVCreateTbReq** pCreateTbReq, STableDataCxt** pTableCxt, bool colMode, bool ignoreColVals) {
366
  STableDataCxt** tmp = (STableDataCxt**)taosHashGet(pHash, id, idLen);
28,313✔
367
  if (NULL != tmp) {
28,313!
368
    *pTableCxt = *tmp;
×
369
    if (!ignoreColVals) {
×
370
      resetColValues((*pTableCxt)->pValues);
×
371
    }
372
    return TSDB_CODE_SUCCESS;
×
373
  }
374
  int32_t code = createTableDataCxt(pTableMeta, pCreateTbReq, pTableCxt, colMode, ignoreColVals);
28,313✔
375
  if (TSDB_CODE_SUCCESS == code) {
28,313!
376
    void* pData = *pTableCxt;  // deal scan coverity
28,313✔
377
    code = taosHashPut(pHash, id, idLen, &pData, POINTER_BYTES);
28,313✔
378
  }
379

380
  if (TSDB_CODE_SUCCESS != code) {
28,313!
381
    insDestroyTableDataCxt(*pTableCxt);
×
382
  }
383
  return code;
28,313✔
384
}
385

386
static void destroyColVal(void* p) {
281,155✔
387
  SColVal* pVal = p;
281,155✔
388
  if (TSDB_DATA_TYPE_NCHAR == pVal->value.type || TSDB_DATA_TYPE_GEOMETRY == pVal->value.type ||
281,155!
389
      TSDB_DATA_TYPE_VARBINARY == pVal->value.type || TSDB_DATA_TYPE_DECIMAL == pVal->value.type) {
253,152!
390
    taosMemoryFreeClear(pVal->value.pData);
28,003!
391
  }
392
}
281,155✔
393

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

399
  taosMemoryFreeClear(pTableCxt->pMeta);
28,313!
400
  tDestroyTSchema(pTableCxt->pSchema);
28,313!
401
  insDestroyBoundColInfo(&pTableCxt->boundColsInfo);
28,313✔
402
  taosArrayDestroyEx(pTableCxt->pValues, destroyColVal);
28,313✔
403
  if (pTableCxt->pData) {
28,313✔
404
    tDestroySubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
4✔
405
    taosMemoryFree(pTableCxt->pData);
4!
406
  }
407
  taosMemoryFree(pTableCxt);
28,313!
408
}
409

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

415
  tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
25,897✔
416
  taosMemoryFree(pVgCxt->pData);
25,897!
417
  taosMemoryFree(pVgCxt);
25,897!
418
}
419

420
void insDestroyVgroupDataCxtList(SArray* pVgCxtList) {
38,598✔
421
  if (NULL == pVgCxtList) {
38,598✔
422
    return;
19,301✔
423
  }
424

425
  size_t size = taosArrayGetSize(pVgCxtList);
19,297✔
426
  for (int32_t i = 0; i < size; i++) {
45,194✔
427
    void* p = taosArrayGetP(pVgCxtList, i);
25,897✔
428
    insDestroyVgroupDataCxt(p);
25,897✔
429
  }
430

431
  taosArrayDestroy(pVgCxtList);
19,297✔
432
}
433

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

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

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

446
  taosHashCleanup(pVgCxtHash);
×
447
}
448

449
void insDestroyTableDataCxtHashMap(SHashObj* pTableCxtHash) {
19,302✔
450
  if (NULL == pTableCxtHash) {
19,302!
451
    return;
×
452
  }
453

454
  void** p = taosHashIterate(pTableCxtHash, NULL);
19,302✔
455
  while (p) {
47,615✔
456
    insDestroyTableDataCxt(*(STableDataCxt**)p);
28,313✔
457

458
    p = taosHashIterate(pTableCxtHash, p);
28,313✔
459
  }
460

461
  taosHashCleanup(pTableCxtHash);
19,302✔
462
}
463

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

472
  // push data to submit, rebuild empty data for next submit
473
  if (NULL == taosArrayPush(pVgCxt->pData->aSubmitTbData, pTableCxt->pData)) {
56,618!
474
    return terrno;
×
475
  }
476
  int32_t code = 0;
28,309✔
477
  if (isRebuild) {
28,309!
478
    code = rebuildTableData(pTableCxt->pData, &pTableCxt->pData);
×
479
  } else if (clear) {
28,309!
480
    taosMemoryFreeClear(pTableCxt->pData);
28,309!
481
  }
482

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

485
  return code;
28,309✔
486
}
487

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

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

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

524
  return 0;
×
525
}
526

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

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

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

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

549
  return TSDB_CODE_SUCCESS;
×
550
}
551

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

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

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

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

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

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

585
    *uid = pTableMeta->uid;
×
586
    *vgId = pTableMeta->vgId;
×
587

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

594
    taosMemoryFree(pTableMeta);
×
595
  } else {
596
    *uid = pTbInfo->uid;
×
597
    *vgId = pTbInfo->vgid;
×
598
  }
599

600
  return code;
×
601
}
602

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

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

611
  return code;
×
612
}
613

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

623
  int32_t        code = TSDB_CODE_SUCCESS;
×
624
  SArray**       rowP = NULL;
×
625

626
  rowP = (SArray**)tSimpleHashGet(pTableNameHash, tbname, strlen(tbname));
×
627

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

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

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

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

655
    return TSDB_CODE_SUCCESS;
×
656
  }
657

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

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

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

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

670
  return TSDB_CODE_SUCCESS;
×
671
}
672

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

679
  pTbCtx->pData->aRowP = pTbData->aCol;
×
680

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

701
    if (ctbReq != NULL) {
×
702
      tdDestroySVCreateTbReq(ctbReq);
703
      taosMemoryFree(ctbReq);
×
704
      ctbReq = NULL;
×
705
    }
706
  }
707

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

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

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

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

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

743
  return code;
×
744
}
745

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

756
  int32_t code = TSDB_CODE_SUCCESS;
757

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

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

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

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

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

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

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

800
  return code;
801
}
802
*/
803

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

813
  int32_t code = TSDB_CODE_SUCCESS;
19,297✔
814
  bool    colFormat = false;
19,297✔
815

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

822
  while (TSDB_CODE_SUCCESS == code && NULL != p) {
47,606!
823
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
28,309✔
824
    if (colFormat) {
28,309!
825
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
×
826
      if (pCol && pCol->nVal <= 0) {
×
827
        p = taosHashIterate(pTableHash, p);
×
828
        continue;
×
829
      }
830

831
      if (pTableCxt->pData->pCreateTbReq) {
×
832
        pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
×
833
      }
834

835
      taosArraySort(pTableCxt->pData->aCol, insColDataComp);
×
836

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

854
    if (TSDB_CODE_SUCCESS == code) {
28,309!
855
      SVgroupDataCxt* pVgCxt = NULL;
28,309✔
856
      int32_t         vgId = pTableCxt->pMeta->vgId;
28,309✔
857
      void**          pp = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
28,309✔
858
      if (NULL == pp) {
28,309✔
859
        code = createVgroupDataCxt(vgId, pVgroupHash, pVgroupList, &pVgCxt);
25,897✔
860
      } else {
861
        pVgCxt = *(SVgroupDataCxt**)pp;
2,412✔
862
      }
863
      if (TSDB_CODE_SUCCESS == code) {
28,309!
864
        code = fillVgroupDataCxt(pTableCxt, pVgCxt, isRebuild, true);
28,309✔
865
      }
866
    }
867
    if (TSDB_CODE_SUCCESS == code) {
28,309!
868
      p = taosHashIterate(pTableHash, p);
28,309✔
869
    }
870
  }
871

872
  taosHashCleanup(pVgroupHash);
19,297✔
873
  if (TSDB_CODE_SUCCESS == code) {
19,297!
874
    *pVgDataBlocks = pVgroupList;
19,297✔
875
  } else {
876
    insDestroyVgroupDataCxtList(pVgroupList);
×
877
  }
878

879
  return code;
19,297✔
880
}
881

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

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

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

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

925
  int32_t code = TSDB_CODE_SUCCESS;
19,297✔
926
  for (size_t i = 0; TSDB_CODE_SUCCESS == code && i < numOfVg; ++i) {
45,194!
927
    SVgroupDataCxt* src = taosArrayGetP(pVgDataCxtList, i);
25,897✔
928
    if (taosArrayGetSize(src->pData->aSubmitTbData) <= 0) {
25,897!
929
      continue;
×
930
    }
931
    SVgDataBlocks* dst = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
25,897!
932
    if (NULL == dst) {
25,897!
933
      code = terrno;
×
934
    }
935
    if (TSDB_CODE_SUCCESS == code) {
25,897!
936
      dst->numOfTables = taosArrayGetSize(src->pData->aSubmitTbData);
25,897✔
937
      code = taosHashGetDup(pVgroupsHashObj, (const char*)&src->vgId, sizeof(src->vgId), &dst->vg);
25,897✔
938
    }
939
    if (TSDB_CODE_SUCCESS == code) {
25,897!
940
      code = buildSubmitReq(src->vgId, src->pData, &dst->pData, &dst->size);
25,897✔
941
    }
942
    if (TSDB_CODE_SUCCESS == code) {
25,897!
943
      code = (NULL == taosArrayPush(pDataBlocks, &dst) ? terrno : TSDB_CODE_SUCCESS);
25,897!
944
    }
945
    if (TSDB_CODE_SUCCESS != code) {
25,897!
946
      destroyVgDataBlocks(dst);
×
947
    }
948
  }
949

950
  if (append) {
19,297!
951
    if (NULL == *pVgDataBlocks) {
×
952
      *pVgDataBlocks = pDataBlocks;
×
953
    }
954
    return code;
×
955
  }
956

957
  if (TSDB_CODE_SUCCESS == code) {
19,297!
958
    *pVgDataBlocks = pDataBlocks;
19,297✔
959
  } else {
960
    taosArrayDestroyP(pDataBlocks, destroyVgDataBlocks);
×
961
  }
962

963
  return code;
19,297✔
964
}
965

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

973
  return false;
×
974
}
975

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1120
  char* pStart = p;
×
1121

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

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

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

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

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

1178
end:
×
1179
  return ret;
×
1180
}
1181

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

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

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

1212
  return 0;
×
1213
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc