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

taosdata / TDengine / #4791

13 Oct 2025 06:50AM UTC coverage: 57.628% (-0.8%) from 58.476%
#4791

push

travis-ci

web-flow
Merge pull request #33213 from taosdata/fix/huoh/timemoe_model_directory

fix: fix tdgpt timemoe model directory

136628 of 303332 branches covered (45.04%)

Branch coverage included in aggregate %.

208121 of 294900 relevant lines covered (70.57%)

4250784.02 hits per line

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

63.53
/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 "taoserror.h"
24
#include "tarray.h"
25
#include "tdatablock.h"
26
#include "tdataformat.h"
27
#include "tmisce.h"
28
#include "ttypes.h"
29

30
void qDestroyBoundColInfo(void* pInfo) {
21,575✔
31
  if (NULL == pInfo) {
21,575✔
32
    return;
10,484✔
33
  }
34

35
  SBoundColInfo* pBoundInfo = (SBoundColInfo*)pInfo;
11,091✔
36

37
  taosMemoryFreeClear(pBoundInfo->pColIndex);
11,091!
38
}
39

40
static char* tableNameGetPosition(SToken* pToken, char target) {
104,316✔
41
  bool inEscape = false;
104,316✔
42
  bool inQuote = false;
104,316✔
43
  char quotaStr = 0;
104,316✔
44

45
  for (uint32_t i = 0; i < pToken->n; ++i) {
846,309✔
46
    if (*(pToken->z + i) == target && (!inEscape) && (!inQuote)) {
816,880!
47
      return pToken->z + i;
74,887✔
48
    }
49

50
    if (*(pToken->z + i) == TS_ESCAPE_CHAR) {
741,993✔
51
      if (!inQuote) {
66,891!
52
        inEscape = !inEscape;
66,911✔
53
      }
54
    }
55

56
    if (*(pToken->z + i) == '\'' || *(pToken->z + i) == '"') {
741,993!
57
      if (!inEscape) {
20✔
58
        if (!inQuote) {
6✔
59
          quotaStr = *(pToken->z + i);
3✔
60
          inQuote = !inQuote;
3✔
61
        } else if (quotaStr == *(pToken->z + i)) {
3!
62
          inQuote = !inQuote;
3✔
63
        }
64
      }
65
    }
66
  }
67

68
  return NULL;
29,429✔
69
}
70

71
int32_t insCreateSName(SName* pName, SToken* pTableName, int32_t acctId, const char* dbName, SMsgBuf* pMsgBuf) {
104,322✔
72
  const char* msg1 = "name too long";
104,322✔
73
  const char* msg2 = "invalid database name";
104,322✔
74
  const char* msg3 = "db is not specified";
104,322✔
75
  const char* msg4 = "invalid table name";
104,322✔
76

77
  int32_t code = TSDB_CODE_SUCCESS;
104,322✔
78
  char*   p = tableNameGetPosition(pTableName, TS_PATH_DELIMITER[0]);
104,322✔
79

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

89
    code = tNameSetDbName(pName, acctId, name, actualDbLen);
74,864✔
90
    if (code != TSDB_CODE_SUCCESS) {
74,877!
91
      return buildInvalidOperationMsg(pMsgBuf, msg1);
×
92
    }
93

94
    int32_t tbLen = pTableName->n - dbLen - 1;
74,877✔
95
    if (tbLen <= 0) {
74,877!
96
      return buildInvalidOperationMsg(pMsgBuf, msg4);
×
97
    }
98

99
    char tbname[TSDB_TABLE_FNAME_LEN] = {0};
74,877✔
100
    strncpy(tbname, p + 1, tbLen);
74,877✔
101
    /*tbLen = */ (void)strdequote(tbname);
74,877✔
102

103
    code = tNameFromString(pName, tbname, T_NAME_TABLE);
74,902✔
104
    if (code != 0) {
74,850!
105
      return buildInvalidOperationMsg(pMsgBuf, msg1);
×
106
    }
107
  } else {  // get current DB name first, and then set it into path
108
    char tbname[TSDB_TABLE_FNAME_LEN] = {0};
29,465✔
109
    strncpy(tbname, pTableName->z, pTableName->n);
29,465✔
110
    int32_t tbLen = strdequote(tbname);
29,465✔
111
    if (tbLen >= TSDB_TABLE_NAME_LEN) {
29,485!
112
      return buildInvalidOperationMsg(pMsgBuf, msg1);
5✔
113
    }
114
    if (tbLen == 0) {
29,487!
115
      return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, "invalid table name");
×
116
    }
117

118
    char name[TSDB_TABLE_FNAME_LEN] = {0};
29,487✔
119
    strncpy(name, pTableName->z, pTableName->n);
29,487✔
120
    (void)strdequote(name);
29,487✔
121

122
    if (dbName == NULL) {
29,495✔
123
      return buildInvalidOperationMsg(pMsgBuf, msg3);
13✔
124
    }
125
    if (name[0] == '\0') return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, msg4);
29,482!
126

127
    code = tNameSetDbName(pName, acctId, dbName, strlen(dbName));
29,482✔
128
    if (code != TSDB_CODE_SUCCESS) {
29,487!
129
      code = buildInvalidOperationMsg(pMsgBuf, msg2);
×
130
      return code;
×
131
    }
132

133
    code = tNameFromString(pName, name, T_NAME_TABLE);
29,487✔
134
    if (code != 0) {
29,458!
135
      code = buildInvalidOperationMsg(pMsgBuf, msg1);
×
136
    }
137
  }
138

139
  if (NULL != strchr(pName->tname, '.')) {
104,315!
140
    code = generateSyntaxErrMsgExt(pMsgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, "The table name cannot contain '.'");
×
141
  }
142

143
  return code;
104,322✔
144
}
145

146
int16_t insFindCol(SToken* pColname, int16_t start, int16_t end, SSchema* pSchema) {
122,676✔
147
  while (start < end) {
127,054✔
148
    if (strlen(pSchema[start].name) == pColname->n && strncmp(pColname->z, pSchema[start].name, pColname->n) == 0) {
126,610✔
149
      return start;
122,232✔
150
    }
151
    ++start;
4,378✔
152
  }
153
  return -1;
444✔
154
}
155

156
int32_t insBuildCreateTbReq(SVCreateTbReq* pTbReq, const char* tname, STag* pTag, int64_t suid, const char* sname,
13,547✔
157
                            SArray* tagName, uint8_t tagNum, int32_t ttl) {
158
  pTbReq->type = TD_CHILD_TABLE;
13,547✔
159
  pTbReq->ctb.pTag = (uint8_t*)pTag;
13,547✔
160
  pTbReq->name = taosStrdup(tname);
13,547!
161
  if (!pTbReq->name) return terrno;
13,552!
162
  pTbReq->ctb.suid = suid;
13,552✔
163
  pTbReq->ctb.tagNum = tagNum;
13,552✔
164
  if (sname) {
13,552✔
165
    pTbReq->ctb.stbName = taosStrdup(sname);
10,429!
166
    if (!pTbReq->ctb.stbName) return terrno;
10,434!
167
  }
168
  pTbReq->ctb.tagName = taosArrayDup(tagName, NULL);
13,557✔
169
  if (!pTbReq->ctb.tagName) return terrno;
13,575!
170
  pTbReq->ttl = ttl;
13,576✔
171
  pTbReq->commentLen = -1;
13,576✔
172

173
  return TSDB_CODE_SUCCESS;
13,576✔
174
}
175

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

182
static int32_t initColValues(STableMeta* pTableMeta, SArray* pValues) {
85,777✔
183
  SSchema* pSchemas = getTableColumnSchema(pTableMeta);
85,777✔
184
  int32_t  code = 0;
85,830✔
185
  for (int32_t i = 0; i < pTableMeta->tableInfo.numOfColumns; ++i) {
20,320,958✔
186
    SColVal val = COL_VAL_NONE(pSchemas[i].colId, pSchemas[i].type);
20,224,280✔
187
    if (NULL == taosArrayPush(pValues, &val)) {
20,235,128!
188
      code = terrno;
×
189
      break;
×
190
    }
191
  }
192
  return code;
96,678✔
193
}
194

195
int32_t insInitColValues(STableMeta* pTableMeta, SArray* aColValues) { return initColValues(pTableMeta, aColValues); }
53✔
196

197
int32_t insInitBoundColsInfo(int32_t numOfBound, SBoundColInfo* pInfo) {
99,348✔
198
  pInfo->numOfCols = numOfBound;
99,348✔
199
  pInfo->numOfBound = numOfBound;
99,348✔
200
  pInfo->hasBoundCols = false;
99,348✔
201
  pInfo->mixTagsCols = false;
99,348✔
202
  pInfo->pColIndex = taosMemoryCalloc(numOfBound, sizeof(int16_t));
99,348!
203
  if (NULL == pInfo->pColIndex) {
99,451!
204
    return terrno;
×
205
  }
206
  for (int32_t i = 0; i < numOfBound; ++i) {
20,776,205✔
207
    pInfo->pColIndex[i] = i;
20,676,754✔
208
  }
209
  return TSDB_CODE_SUCCESS;
99,451✔
210
}
211

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

221
void insCheckTableDataOrder(STableDataCxt* pTableCxt, SRowKey* rowKey) {
37,867,261✔
222
  // once the data block is disordered, we do NOT keep last timestamp any more
223
  if (!pTableCxt->ordered) {
37,867,261✔
224
    return;
1,330,830✔
225
  }
226

227
  if (tRowKeyCompare(rowKey, &pTableCxt->lastKey) < 0) {
36,536,431✔
228
    pTableCxt->ordered = false;
393✔
229
  }
230

231
  if (tRowKeyCompare(rowKey, &pTableCxt->lastKey) == 0) {
36,506,818✔
232
    pTableCxt->duplicateTs = true;
13✔
233
  }
234

235
  // TODO: for variable length data type, we need to copy it out
236
  pTableCxt->lastKey = *rowKey;
36,476,834✔
237
  return;
36,476,834✔
238
}
239

240
void insDestroyBoundColInfo(SBoundColInfo* pInfo) { taosMemoryFreeClear(pInfo->pColIndex); }
261,378!
241

242
static int32_t createTableDataCxt(STableMeta* pTableMeta, SVCreateTbReq** pCreateTbReq, STableDataCxt** pOutput,
85,750✔
243
                                  bool colMode, bool ignoreColVals) {
244
  STableDataCxt* pTableCxt = taosMemoryCalloc(1, sizeof(STableDataCxt));
85,750!
245
  if (NULL == pTableCxt) {
85,801!
246
    *pOutput = NULL;
×
247
    return terrno;
×
248
  }
249

250
  int32_t code = TSDB_CODE_SUCCESS;
85,801✔
251

252
  pTableCxt->lastKey = (SRowKey){0};
85,801✔
253
  pTableCxt->ordered = true;
85,801✔
254
  pTableCxt->duplicateTs = false;
85,801✔
255

256
  pTableCxt->pMeta = tableMetaDup(pTableMeta);
85,801✔
257
  if (NULL == pTableCxt->pMeta) {
85,866!
258
    code = TSDB_CODE_OUT_OF_MEMORY;
×
259
  }
260
  if (TSDB_CODE_SUCCESS == code) {
85,866✔
261
    pTableCxt->pSchema =
85,863✔
262
        tBuildTSchema(getTableColumnSchema(pTableMeta), pTableMeta->tableInfo.numOfColumns, pTableMeta->sversion);
85,827✔
263
    if (NULL == pTableCxt->pSchema) {
85,863!
264
      code = TSDB_CODE_OUT_OF_MEMORY;
×
265
    }
266
  }
267
  pTableCxt->hasBlob = schemaHasBlob(pTableCxt->pSchema);
85,902✔
268

269
  if (TSDB_CODE_SUCCESS == code) {
85,861!
270
    code = insInitBoundColsInfo(pTableMeta->tableInfo.numOfColumns, &pTableCxt->boundColsInfo);
85,868✔
271
  }
272
  if (TSDB_CODE_SUCCESS == code && !ignoreColVals) {
85,844✔
273
    pTableCxt->pValues = taosArrayInit(pTableMeta->tableInfo.numOfColumns, sizeof(SColVal));
85,794✔
274
    if (NULL == pTableCxt->pValues) {
85,731!
275
      code = terrno;
×
276
    } else {
277
      code = initColValues(pTableMeta, pTableCxt->pValues);
85,731✔
278
    }
279
  }
280
  if (TSDB_CODE_SUCCESS == code) {
85,878✔
281
    pTableCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
85,866!
282
    if (NULL == pTableCxt->pData) {
85,784!
283
      code = terrno;
×
284
    } else {
285
      pTableCxt->pData->flags = (pCreateTbReq != NULL && NULL != *pCreateTbReq) ? SUBMIT_REQ_AUTO_CREATE_TABLE : 0;
85,784!
286
      pTableCxt->pData->flags |= colMode ? SUBMIT_REQ_COLUMN_DATA_FORMAT : 0;
85,784✔
287
      pTableCxt->pData->suid = pTableMeta->suid;
85,784✔
288
      pTableCxt->pData->uid = pTableMeta->uid;
85,784✔
289
      pTableCxt->pData->sver = pTableMeta->sversion;
85,784✔
290
      pTableCxt->pData->pCreateTbReq = pCreateTbReq != NULL ? *pCreateTbReq : NULL;
85,784!
291
      int8_t flag = pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
85,784✔
292
      if (pCreateTbReq != NULL) *pCreateTbReq = NULL;
85,784!
293
      if (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
85,784✔
294
        pTableCxt->pData->aCol = taosArrayInit(128, sizeof(SColData));
11,229✔
295
        if (NULL == pTableCxt->pData->aCol) {
11,223!
296
          code = terrno;
×
297
        }
298
      } else {
299
        pTableCxt->pData->aRowP = taosArrayInit(128, POINTER_BYTES);
74,555✔
300
        if (NULL == pTableCxt->pData->aRowP) {
74,586!
301
          code = terrno;
×
302
        }
303
      }
304
    }
305
  }
306
  if (TSDB_CODE_SUCCESS == code) {
85,821!
307
    *pOutput = pTableCxt;
85,821✔
308
    parserDebug("uid:%" PRId64 ", create table data context, code:%d, vgId:%d", pTableMeta->uid, code,
85,821✔
309
                pTableMeta->vgId);
310
  } else {
311
    insDestroyTableDataCxt(pTableCxt);
×
312
  }
313

314
  return code;
85,826✔
315
}
316

317
static int32_t rebuildTableData(SSubmitTbData* pSrc, SSubmitTbData** pDst, int8_t hasBlob) {
14,236✔
318
  int32_t        code = TSDB_CODE_SUCCESS;
14,236✔
319
  SSubmitTbData* pTmp = taosMemoryCalloc(1, sizeof(SSubmitTbData));
14,236!
320
  if (NULL == pTmp) {
14,238!
321
    code = terrno;
×
322
  } else {
323
    pTmp->flags = pSrc->flags;
14,238✔
324
    pTmp->suid = pSrc->suid;
14,238✔
325
    pTmp->uid = pSrc->uid;
14,238✔
326
    pTmp->sver = pSrc->sver;
14,238✔
327
    pTmp->pCreateTbReq = NULL;
14,238✔
328
    if (pTmp->flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
14,238✔
329
      if (pSrc->pCreateTbReq) {
13,279✔
330
        code = cloneSVreateTbReq(pSrc->pCreateTbReq, &pTmp->pCreateTbReq);
13,273✔
331
      } else {
332
        pTmp->flags &= ~SUBMIT_REQ_AUTO_CREATE_TABLE;
6✔
333
      }
334
    }
335
    if (TSDB_CODE_SUCCESS == code) {
14,247!
336
      if (pTmp->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
14,247✔
337
        pTmp->aCol = taosArrayInit(128, sizeof(SColData));
11,119✔
338
        if (NULL == pTmp->aCol) {
11,114!
339
          code = terrno;
×
340
          taosMemoryFree(pTmp);
×
341
        }
342
      } else {
343
        pTmp->aRowP = taosArrayInit(128, POINTER_BYTES);
3,128✔
344
        if (NULL == pTmp->aRowP) {
3,129!
345
          code = terrno;
×
346
          taosMemoryFree(pTmp);
×
347
        }
348

349
        if (code != 0) {
3,129!
350
          taosArrayDestroy(pTmp->aRowP);
×
351
          taosMemoryFree(pTmp);
×
352
        }
353
      }
354

355
    } else {
356
      taosMemoryFree(pTmp);
×
357
    }
358
  }
359

360
  taosMemoryFree(pSrc);
14,242!
361
  if (TSDB_CODE_SUCCESS == code) {
14,248!
362
    *pDst = pTmp;
14,248✔
363
  }
364

365
  return code;
14,248✔
366
}
367

368
static void resetColValues(SArray* pValues) {
38✔
369
  int32_t num = taosArrayGetSize(pValues);
38✔
370
  for (int32_t i = 0; i < num; ++i) {
188✔
371
    SColVal* pVal = taosArrayGet(pValues, i);
150✔
372
    pVal->flag = CV_FLAG_NONE;
150✔
373
  }
374
}
38✔
375

376
int32_t insGetTableDataCxt(SHashObj* pHash, void* id, int32_t idLen, STableMeta* pTableMeta,
85,776✔
377
                           SVCreateTbReq** pCreateTbReq, STableDataCxt** pTableCxt, bool colMode, bool ignoreColVals) {
378
  STableDataCxt** tmp = (STableDataCxt**)taosHashGet(pHash, id, idLen);
85,776✔
379
  if (NULL != tmp) {
85,872✔
380
    *pTableCxt = *tmp;
38✔
381
    if (!ignoreColVals) {
38!
382
      resetColValues((*pTableCxt)->pValues);
38✔
383
    }
384
    return TSDB_CODE_SUCCESS;
38✔
385
  }
386
  int32_t code = createTableDataCxt(pTableMeta, pCreateTbReq, pTableCxt, colMode, ignoreColVals);
85,834✔
387
  if (TSDB_CODE_SUCCESS == code) {
85,796!
388
    void* pData = *pTableCxt;  // deal scan coverity
85,802✔
389
    code = taosHashPut(pHash, id, idLen, &pData, POINTER_BYTES);
85,802✔
390
  }
391

392
  if (TSDB_CODE_SUCCESS != code) {
85,872!
393
    insDestroyTableDataCxt(*pTableCxt);
×
394
  }
395
  return code;
85,871✔
396
}
397

398
static void destroyColVal(void* p) {
20,554,337✔
399
  SColVal* pVal = p;
20,554,337✔
400
  if (TSDB_DATA_TYPE_NCHAR == pVal->value.type || TSDB_DATA_TYPE_GEOMETRY == pVal->value.type ||
20,554,337!
401
      TSDB_DATA_TYPE_VARBINARY == pVal->value.type || TSDB_DATA_TYPE_DECIMAL == pVal->value.type) {
20,546,788!
402
    taosMemoryFreeClear(pVal->value.pData);
6,688!
403
  }
404
}
20,554,337✔
405

406
void insDestroyTableDataCxt(STableDataCxt* pTableCxt) {
96,169✔
407
  if (NULL == pTableCxt) {
96,169!
408
    return;
×
409
  }
410

411
  taosMemoryFreeClear(pTableCxt->pMeta);
96,169!
412
  tDestroyTSchema(pTableCxt->pSchema);
96,160!
413
  insDestroyBoundColInfo(&pTableCxt->boundColsInfo);
96,160✔
414
  taosArrayDestroyEx(pTableCxt->pValues, destroyColVal);
96,179✔
415
  if (pTableCxt->pData) {
96,175✔
416
    tDestroySubmitTbData(pTableCxt->pData, TSDB_MSG_FLG_ENCODE);
24,873✔
417
    taosMemoryFree(pTableCxt->pData);
24,855!
418
  }
419
  taosMemoryFree(pTableCxt);
96,162!
420
}
421

422
void insDestroyVgroupDataCxt(SVgroupDataCxt* pVgCxt) {
92,690✔
423
  if (NULL == pVgCxt) {
92,690!
424
    return;
×
425
  }
426

427
  tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
92,690✔
428
  taosMemoryFree(pVgCxt->pData);
92,719!
429

430
  taosMemoryFree(pVgCxt);
92,711!
431
}
432

433
void insDestroyVgroupDataCxtList(SArray* pVgCxtList) {
168,276✔
434
  if (NULL == pVgCxtList) {
168,276✔
435
    return;
79,315✔
436
  }
437

438
  size_t size = taosArrayGetSize(pVgCxtList);
88,961✔
439
  for (int32_t i = 0; i < size; i++) {
181,725✔
440
    void* p = taosArrayGetP(pVgCxtList, i);
92,707✔
441
    insDestroyVgroupDataCxt(p);
92,709✔
442
  }
443

444
  taosArrayDestroy(pVgCxtList);
89,018✔
445
}
446

447
void insDestroyVgroupDataCxtHashMap(SHashObj* pVgCxtHash) {
×
448
  if (NULL == pVgCxtHash) {
×
449
    return;
×
450
  }
451

452
  void** p = taosHashIterate(pVgCxtHash, NULL);
×
453
  while (p) {
×
454
    insDestroyVgroupDataCxt(*(SVgroupDataCxt**)p);
×
455

456
    p = taosHashIterate(pVgCxtHash, p);
×
457
  }
458

459
  taosHashCleanup(pVgCxtHash);
×
460
}
461

462
void insDestroyTableDataCxtHashMap(SHashObj* pTableCxtHash) {
80,944✔
463
  if (NULL == pTableCxtHash) {
80,944✔
464
    return;
11,113✔
465
  }
466

467
  void** p = taosHashIterate(pTableCxtHash, NULL);
69,831✔
468
  while (p) {
144,595✔
469
    insDestroyTableDataCxt(*(STableDataCxt**)p);
74,747✔
470

471
    p = taosHashIterate(pTableCxtHash, p);
74,738✔
472
  }
473

474
  taosHashCleanup(pTableCxtHash);
69,848✔
475
}
476

477
static int32_t fillVgroupDataCxt(STableDataCxt* pTableCxt, SVgroupDataCxt* pVgCxt, bool isRebuild, bool clear) {
90,961✔
478
  int32_t code = 0;
90,961✔
479
  if (NULL == pVgCxt->pData->aSubmitTbData) {
90,961✔
480
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
87,001✔
481
    if (pVgCxt->pData->aSubmitTbData == NULL) {
86,951!
482
      return terrno;
×
483
    }
484
    if (pTableCxt->hasBlob) {
86,951!
485
      pVgCxt->pData->aSubmitBlobData = taosArrayInit(128, sizeof(SBlobSet*));
×
486
      if (NULL == pVgCxt->pData->aSubmitBlobData) {
×
487
        return terrno;
×
488
      }
489
    }
490
  }
491

492
  // push data to submit, rebuild empty data for next submit
493
  if (!pTableCxt->hasBlob) pTableCxt->pData->pBlobSet = NULL;
90,911!
494

495
  if (NULL == taosArrayPush(pVgCxt->pData->aSubmitTbData, pTableCxt->pData)) {
181,894!
496
    return terrno;
×
497
  }
498

499
  if (pTableCxt->hasBlob) {
90,983!
500
    parserDebug("blob row transfer %p, pData %p, %s", pTableCxt->pData->pBlobSet, pTableCxt->pData, __func__);
×
501
    if (NULL == taosArrayPush(pVgCxt->pData->aSubmitBlobData, &pTableCxt->pData->pBlobSet)) {
×
502
      return terrno;
×
503
    }
504
    pTableCxt->pData->pBlobSet = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
×
505
  }
506

507
  if (isRebuild) {
90,983✔
508
    code = rebuildTableData(pTableCxt->pData, &pTableCxt->pData, pTableCxt->hasBlob);
14,243✔
509
  } else if (clear) {
76,740✔
510
    taosMemoryFreeClear(pTableCxt->pData);
71,071!
511
  }
512
  parserDebug("uid:%" PRId64 ", add table data context to vgId:%d", pTableCxt->pMeta->uid, pVgCxt->vgId);
91,010✔
513

514
  return code;
90,958✔
515
}
516

517
static int32_t createVgroupDataCxt(int32_t vgId, SHashObj* pVgroupHash, SArray* pVgroupList, SVgroupDataCxt** pOutput) {
92,703✔
518
  SVgroupDataCxt* pVgCxt = taosMemoryCalloc(1, sizeof(SVgroupDataCxt));
92,703!
519
  if (NULL == pVgCxt) {
92,696!
520
    return terrno;
×
521
  }
522
  pVgCxt->pData = taosMemoryCalloc(1, sizeof(SSubmitReq2));
92,696!
523
  if (NULL == pVgCxt->pData) {
92,701!
524
    insDestroyVgroupDataCxt(pVgCxt);
×
525
    return terrno;
×
526
  }
527

528
  pVgCxt->vgId = vgId;
92,701✔
529
  int32_t code = taosHashPut(pVgroupHash, &pVgCxt->vgId, sizeof(pVgCxt->vgId), &pVgCxt, POINTER_BYTES);
92,701✔
530
  if (TSDB_CODE_SUCCESS == code) {
92,729!
531
    if (NULL == taosArrayPush(pVgroupList, &pVgCxt)) {
92,721!
532
      code = terrno;
×
533
      insDestroyVgroupDataCxt(pVgCxt);
×
534
      return code;
×
535
    }
536
    //    uDebug("td23101 2vgId:%d, uid:%" PRIu64, pVgCxt->vgId, pTableCxt->pMeta->uid);
537
    *pOutput = pVgCxt;
92,721✔
538
  } else {
539
    insDestroyVgroupDataCxt(pVgCxt);
×
540
  }
541
  return code;
92,726✔
542
}
543

544
int insColDataComp(const void* lp, const void* rp) {
56,736✔
545
  SColData* pLeft = (SColData*)lp;
56,736✔
546
  SColData* pRight = (SColData*)rp;
56,736✔
547
  if (pLeft->cid < pRight->cid) {
56,736✔
548
    return -1;
55,290✔
549
  } else if (pLeft->cid > pRight->cid) {
1,446!
550
    return 1;
1,461✔
551
  }
552

553
  return 0;
×
554
}
555

556
int32_t insTryAddTableVgroupInfo(SHashObj* pAllVgHash, SStbInterlaceInfo* pBuildInfo, int32_t* vgId,
973✔
557
                                 STableColsData* pTbData, SName* sname) {
558
  if (*vgId >= 0 && taosHashGet(pAllVgHash, (const char*)vgId, sizeof(*vgId))) {
973!
559
    return TSDB_CODE_SUCCESS;
745✔
560
  }
561

562
  SVgroupInfo      vgInfo = {0};
228✔
563
  SRequestConnInfo conn = {.pTrans = pBuildInfo->transport,
228✔
564
                           .requestId = pBuildInfo->requestId,
228✔
565
                           .requestObjRefId = pBuildInfo->requestSelf,
228✔
566
                           .mgmtEps = pBuildInfo->mgmtEpSet};
567

568
  int32_t code = catalogGetTableHashVgroup((SCatalog*)pBuildInfo->pCatalog, &conn, sname, &vgInfo);
228✔
569
  if (TSDB_CODE_SUCCESS != code) {
228!
570
    return code;
×
571
  }
572

573
  code = taosHashPut(pAllVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
228✔
574
  if (TSDB_CODE_SUCCESS != code) {
228!
575
    return code;
×
576
  }
577

578
  return TSDB_CODE_SUCCESS;
228✔
579
}
580

581
int32_t insGetStmtTableVgUid(SHashObj* pAllVgHash, SStbInterlaceInfo* pBuildInfo, STableColsData* pTbData,
13,140✔
582
                             uint64_t* uid, int32_t* vgId, uint64_t* suid) {
583
  STableVgUid* pTbInfo = NULL;
13,140✔
584
  int32_t      code = 0;
13,140✔
585

586
  if (pTbData->getFromHash) {
13,140✔
587
    pTbInfo = (STableVgUid*)tSimpleHashGet(pBuildInfo->pTableHash, pTbData->tbName, strlen(pTbData->tbName));
12,170✔
588
  }
589

590
  if (NULL == pTbInfo) {
13,138✔
591
    SName sname;
592
    code = qCreateSName2(&sname, pTbData->tbName, pBuildInfo->acctId, pBuildInfo->dbname, NULL, 0);
1,023✔
593
    if (TSDB_CODE_SUCCESS != code) {
1,021!
594
      return code;
51✔
595
    }
596

597
    STableMeta*      pTableMeta = NULL;
1,021✔
598
    SRequestConnInfo conn = {.pTrans = pBuildInfo->transport,
1,021✔
599
                             .requestId = pBuildInfo->requestId,
1,021✔
600
                             .requestObjRefId = pBuildInfo->requestSelf,
1,021✔
601
                             .mgmtEps = pBuildInfo->mgmtEpSet};
602
    code = catalogGetTableMeta((SCatalog*)pBuildInfo->pCatalog, &conn, &sname, &pTableMeta);
1,021✔
603

604
    if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
1,024✔
605
      parserWarn("stmt2 async bind don't find table:%s.%s, try auto create table", sname.dbname, sname.tname);
51!
606
      return code;
51✔
607
    }
608

609
    if (TSDB_CODE_SUCCESS != code) {
973!
610
      parserError("stmt2 async get table meta:%s.%s failed, code:%d", sname.dbname, sname.tname, code);
×
611
      return code;
×
612
    }
613

614
    *uid = pTableMeta->uid;
973✔
615
    *vgId = pTableMeta->vgId;
973✔
616
    *suid = pTableMeta->suid;
973✔
617

618
    STableVgUid tbInfo = {.uid = *uid, .vgid = *vgId, .suid = *suid};
973✔
619
    code = tSimpleHashPut(pBuildInfo->pTableHash, pTbData->tbName, strlen(pTbData->tbName), &tbInfo, sizeof(tbInfo));
973✔
620
    if (TSDB_CODE_SUCCESS == code) {
973!
621
      code = insTryAddTableVgroupInfo(pAllVgHash, pBuildInfo, vgId, pTbData, &sname);
973✔
622
    }
623

624
    taosMemoryFree(pTableMeta);
973!
625
  } else {
626
    *uid = pTbInfo->uid;
12,115✔
627
    *vgId = pTbInfo->vgid;
12,115✔
628
    *suid = pTbInfo->suid;
12,115✔
629
  }
630

631
  return code;
13,087✔
632
}
633

634
int32_t qBuildStmtFinOutput1(SQuery* pQuery, SHashObj* pAllVgHash, SArray* pVgDataBlocks) {
×
635
  int32_t             code = TSDB_CODE_SUCCESS;
×
636
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
×
637

638
  if (TSDB_CODE_SUCCESS == code) {
×
639
    code = insBuildVgDataBlocks(pAllVgHash, pVgDataBlocks, &pStmt->pDataBlocks, true);
×
640
  }
641

642
  return code;
×
643
}
644

645
int32_t checkAndMergeSVgroupDataCxtByTbname(STableDataCxt* pTbCtx, SVgroupDataCxt* pVgCxt, SSHashObj* pTableNameHash,
7,506✔
646
                                            char* tbname) {
647
  if (NULL == pVgCxt->pData->aSubmitTbData) {
7,506✔
648
    pVgCxt->pData->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
5,718✔
649
    if (NULL == pVgCxt->pData->aSubmitTbData) {
5,719!
650
      return terrno;
×
651
    }
652
    if (pTbCtx->hasBlob) {
5,719!
653
      pVgCxt->pData->aSubmitBlobData = taosArrayInit(128, sizeof(SBlobSet*));
×
654
      if (pVgCxt->pData->aSubmitBlobData == NULL) {
×
655
        return terrno;
×
656
      }
657
    }
658
  }
659

660
  int32_t  code = TSDB_CODE_SUCCESS;
7,507✔
661
  SArray** rowP = NULL;
7,507✔
662

663
  rowP = (SArray**)tSimpleHashGet(pTableNameHash, tbname, strlen(tbname));
7,507✔
664

665
  if (rowP != NULL && *rowP != NULL) {
7,511!
666
    for (int32_t j = 0; j < taosArrayGetSize(*rowP); ++j) {
46✔
667
      SRow* pRow = (SRow*)taosArrayGetP(pTbCtx->pData->aRowP, j);
32✔
668
      if (pRow) {
32✔
669
        if (NULL == taosArrayPush(*rowP, &pRow)) {
40!
670
          return terrno;
×
671
        }
672
      }
673

674
      if (pTbCtx->hasBlob == 0) {
32!
675
        code = tRowSort(*rowP);
32✔
676
        TAOS_CHECK_RETURN(code);
32!
677

678
        code = tRowMerge(*rowP, pTbCtx->pSchema, 0);
32✔
679
        TAOS_CHECK_RETURN(code);
32!
680
      } else {
681
        code = tRowSortWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet);
×
682
        TAOS_CHECK_RETURN(code);
×
683

684
        code = tRowMergeWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet, 0);
×
685
        TAOS_CHECK_RETURN(code);
×
686
      }
687
    }
688

689
    parserDebug("merge same uid data: %" PRId64 ", vgId:%d", pTbCtx->pData->uid, pVgCxt->vgId);
14!
690

691
    if (pTbCtx->pData->pCreateTbReq != NULL) {
14✔
692
      tdDestroySVCreateTbReq(pTbCtx->pData->pCreateTbReq);
12!
693
      taosMemoryFree(pTbCtx->pData->pCreateTbReq);
12!
694
      pTbCtx->pData->pCreateTbReq = NULL;
12✔
695
    }
696
    return TSDB_CODE_SUCCESS;
14✔
697
  }
698

699
  if (pTbCtx->hasBlob == 0) {
7,497✔
700
    pTbCtx->pData->pBlobSet = NULL;  // if no blob, set it to NULL
7,495✔
701
  }
702

703
  if (NULL == taosArrayPush(pVgCxt->pData->aSubmitTbData, pTbCtx->pData)) {
14,991!
704
    return terrno;
×
705
  }
706

707
  if (pTbCtx->hasBlob) {
7,494!
708
    parserDebug("blob row transfer %p, pData %p, %s", pTbCtx->pData->pBlobSet, pTbCtx->pData, __func__);
×
709
    if (NULL == taosArrayPush(pVgCxt->pData->aSubmitBlobData, &pTbCtx->pData->pBlobSet)) {
×
710
      return terrno;
×
711
    }
712
    pTbCtx->pData->pBlobSet = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
×
713
  }
714

715
  code = tSimpleHashPut(pTableNameHash, tbname, strlen(tbname), &pTbCtx->pData->aRowP, sizeof(SArray*));
7,494✔
716

717
  if (code != TSDB_CODE_SUCCESS) {
7,493!
718
    return code;
×
719
  }
720

721
  parserDebug("uid:%" PRId64 ", add table data context to vgId:%d", pTbCtx->pMeta->uid, pVgCxt->vgId);
7,493!
722

723
  return TSDB_CODE_SUCCESS;
7,496✔
724
}
725

726
int32_t insAppendStmtTableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
5,622✔
727
                                  SStbInterlaceInfo* pBuildInfo) {
728
  int32_t  code = TSDB_CODE_SUCCESS;
5,622✔
729
  uint64_t uid;
730
  int32_t  vgId;
731
  uint64_t suid;
732

733
  pTbCtx->pData->aRowP = pTbData->aCol;
5,622✔
734

735
  code = insGetStmtTableVgUid(pAllVgHash, pBuildInfo, pTbData, &uid, &vgId, &suid);
5,622✔
736
  if (TSDB_CODE_SUCCESS != code) {
5,628!
737
    return code;
×
738
  }
739

740
  pTbCtx->pMeta->vgId = vgId;
5,628✔
741
  pTbCtx->pMeta->uid = uid;
5,628✔
742
  pTbCtx->pData->uid = uid;
5,628✔
743

744
  if (!pTbCtx->ordered) {
5,628✔
745
    code = tRowSort(pTbCtx->pData->aRowP);
1✔
746
  }
747
  if (code == TSDB_CODE_SUCCESS && (!pTbCtx->ordered || pTbCtx->duplicateTs)) {
5,628!
748
    code = tRowMerge(pTbCtx->pData->aRowP, pTbCtx->pSchema, 0);
1✔
749
  }
750

751
  if (TSDB_CODE_SUCCESS != code) {
5,629!
752
    return code;
×
753
  }
754

755
  SVgroupDataCxt* pVgCxt = NULL;
5,629✔
756
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
5,629✔
757
  if (NULL == pp) {
5,628✔
758
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
5,056✔
759
    if (NULL == pp) {
5,055!
760
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
5,055✔
761
    } else {
762
      pVgCxt = *(SVgroupDataCxt**)pp;
×
763
    }
764
  } else {
765
    pVgCxt = *(SVgroupDataCxt**)pp;
572✔
766
  }
767

768
  if (TSDB_CODE_SUCCESS == code) {
5,629!
769
    code = fillVgroupDataCxt(pTbCtx, pVgCxt, false, false);
5,629✔
770
  }
771

772
  if (taosArrayGetSize(pVgCxt->pData->aSubmitTbData) >= 20000) {
5,627!
773
    code = qBuildStmtFinOutput1((SQuery*)pBuildInfo->pQuery, pAllVgHash, pBuildInfo->pVgroupList);
×
774
    // taosArrayClear(pVgCxt->pData->aSubmitTbData);
775
    tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
×
776
    // insDestroyVgroupDataCxt(pVgCxt);
777
  }
778

779
  return code;
5,625✔
780
}
781

782
int32_t insAppendStmt2TableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
7,507✔
783
                                   SStbInterlaceInfo* pBuildInfo, SVCreateTbReq* ctbReq) {
784
  int32_t  code = TSDB_CODE_SUCCESS;
7,507✔
785
  uint64_t uid;
786
  int32_t  vgId;
787
  uint64_t suid;
788

789
  pTbCtx->pData->aRowP = pTbData->aCol;
7,507✔
790
  pTbCtx->pData->pBlobSet = pTbData->pBlobSet;
7,507✔
791

792
  code = insGetStmtTableVgUid(pAllVgHash, pBuildInfo, pTbData, &uid, &vgId, &suid);
7,507✔
793
  if (ctbReq != NULL && code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
7,510✔
794
    pTbCtx->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
50✔
795
    vgId = (int32_t)ctbReq->uid;
50✔
796
    uid = 0;
50✔
797
    pTbCtx->pMeta->vgId = (int32_t)ctbReq->uid;
50✔
798
    ctbReq->uid = 0;
50✔
799
    pTbCtx->pMeta->uid = 0;
50✔
800
    pTbCtx->pData->uid = 0;
50✔
801
    pTbCtx->pData->pCreateTbReq = ctbReq;
50✔
802
    code = TSDB_CODE_SUCCESS;
50✔
803
  } else {
804
    if (TSDB_CODE_SUCCESS != code) {
7,460✔
805
      return code;
1✔
806
    }
807
    if (pTbCtx->pData->suid != suid) {
7,459✔
808
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
2✔
809
    }
810

811
    pTbCtx->pMeta->vgId = vgId;
7,457✔
812
    pTbCtx->pMeta->uid = uid;
7,457✔
813
    pTbCtx->pData->uid = uid;
7,457✔
814
    pTbCtx->pData->pCreateTbReq = NULL;
7,457✔
815

816
    if (ctbReq != NULL) {
7,457✔
817
      tdDestroySVCreateTbReq(ctbReq);
818
      taosMemoryFree(ctbReq);
61!
819
      ctbReq = NULL;
61✔
820
    }
821
  }
822

823
  if (pTbCtx->hasBlob == 0) {
7,507!
824
    if (!pTbData->isOrdered) {
7,507✔
825
      code = tRowSort(pTbCtx->pData->aRowP);
2✔
826
    }
827
    if (code == TSDB_CODE_SUCCESS && (!pTbData->isOrdered || pTbData->isDuplicateTs)) {
7,507!
828
      code = tRowMerge(pTbCtx->pData->aRowP, pTbCtx->pSchema, PREFER_NON_NULL);
2✔
829
    }
830
  } else {
831
    if (!pTbData->isOrdered) {
×
832
      code = tRowSortWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet);
×
833
    }
834
    if (code == TSDB_CODE_SUCCESS && (!pTbData->isOrdered || pTbData->isDuplicateTs)) {
×
835
      code = tRowMergeWithBlob(pTbCtx->pData->aRowP, pTbCtx->pSchema, pTbCtx->pData->pBlobSet, 0);
×
836
    }
837
  }
838

839
  if (TSDB_CODE_SUCCESS != code) {
7,507!
840
    return code;
×
841
  }
842

843
  SVgroupDataCxt* pVgCxt = NULL;
7,507✔
844
  void**          pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
7,507✔
845
  if (NULL == pp) {
7,511✔
846
    pp = taosHashGet(pBuildInfo->pVgroupHash, &vgId, sizeof(vgId));
5,721✔
847
    if (NULL == pp) {
5,720!
848
      code = createVgroupDataCxt(vgId, pBuildInfo->pVgroupHash, pBuildInfo->pVgroupList, &pVgCxt);
5,720✔
849
    } else {
850
      pVgCxt = *(SVgroupDataCxt**)pp;
×
851
    }
852
  } else {
853
    pVgCxt = *(SVgroupDataCxt**)pp;
1,790✔
854
  }
855

856
  if (code == TSDB_CODE_SUCCESS) {
7,508✔
857
    code = checkAndMergeSVgroupDataCxtByTbname(pTbCtx, pVgCxt, pBuildInfo->pTableRowDataHash, pTbData->tbName);
7,507✔
858
  }
859

860
  if (taosArrayGetSize(pVgCxt->pData->aSubmitTbData) >= 20000) {
7,510!
861
    code = qBuildStmtFinOutput1((SQuery*)pBuildInfo->pQuery, pAllVgHash, pBuildInfo->pVgroupList);
×
862
    // taosArrayClear(pVgCxt->pData->aSubmitTbData);
863
    tDestroySubmitReq(pVgCxt->pData, TSDB_MSG_FLG_ENCODE);
×
864
    // insDestroyVgroupDataCxt(pVgCxt);
865
  }
866

867
  return code;
7,508✔
868
}
869

870
/*
871
int32_t insMergeStmtTableDataCxt(STableDataCxt* pTableCxt, SArray* pTableList, SArray** pVgDataBlocks, bool isRebuild,
872
int32_t tbNum) { SHashObj* pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, false);
873
  SArray*   pVgroupList = taosArrayInit(8, POINTER_BYTES);
874
  if (NULL == pVgroupHash || NULL == pVgroupList) {
875
    taosHashCleanup(pVgroupHash);
876
    taosArrayDestroy(pVgroupList);
877
    return TSDB_CODE_OUT_OF_MEMORY;
878
  }
879

880
  int32_t code = TSDB_CODE_SUCCESS;
881

882
  for (int32_t i = 0; i < tbNum; ++i) {
883
    STableColsData *pTableCols = (STableColsData*)taosArrayGet(pTableList, i);
884
    pTableCxt->pMeta->vgId = pTableCols->vgId;
885
    pTableCxt->pMeta->uid = pTableCols->uid;
886
    pTableCxt->pData->uid = pTableCols->uid;
887
    pTableCxt->pData->aCol = pTableCols->aCol;
888

889
    SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
890
    if (pCol->nVal <= 0) {
891
      continue;
892
    }
893

894
    if (pTableCxt->pData->pCreateTbReq) {
895
      pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
896
    }
897

898
    taosArraySort(pTableCxt->pData->aCol, insColDataComp);
899

900
    tColDataSortMerge(pTableCxt->pData->aCol);
901

902
    if (TSDB_CODE_SUCCESS == code) {
903
      SVgroupDataCxt* pVgCxt = NULL;
904
      int32_t         vgId = pTableCxt->pMeta->vgId;
905
      void**          pp = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
906
      if (NULL == pp) {
907
        code = createVgroupDataCxt(pTableCxt, pVgroupHash, pVgroupList, &pVgCxt);
908
      } else {
909
        pVgCxt = *(SVgroupDataCxt**)pp;
910
      }
911
      if (TSDB_CODE_SUCCESS == code) {
912
        code = fillVgroupDataCxt(pTableCxt, pVgCxt, false, false);
913
      }
914
    }
915
  }
916

917
  taosHashCleanup(pVgroupHash);
918
  if (TSDB_CODE_SUCCESS == code) {
919
    *pVgDataBlocks = pVgroupList;
920
  } else {
921
    insDestroyVgroupDataCxtList(pVgroupList);
922
  }
923

924
  return code;
925
}
926
*/
927

928
static int8_t colDataHasBlob(SColData* pCol) {
×
929
  if (IS_STR_DATA_BLOB(pCol->type)) {
×
930
    return 1;
×
931
  }
932
  return 0;
×
933
}
934
int32_t insMergeTableDataCxt(SHashObj* pTableHash, SArray** pVgDataBlocks, bool isRebuild) {
79,813✔
935
  SHashObj* pVgroupHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
79,813✔
936
  SArray*   pVgroupList = taosArrayInit(8, POINTER_BYTES);
79,933✔
937
  if (NULL == pVgroupHash || NULL == pVgroupList) {
79,900!
938
    taosHashCleanup(pVgroupHash);
7✔
939
    taosArrayDestroy(pVgroupList);
×
940
    return terrno;
×
941
  }
942

943
  int32_t code = TSDB_CODE_SUCCESS;
79,893✔
944
  bool    colFormat = false;
79,893✔
945

946
  void* p = taosHashIterate(pTableHash, NULL);
79,893✔
947
  if (p) {
79,964!
948
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
79,964✔
949
    colFormat = (0 != (pTableCxt->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT));
79,964✔
950
  }
951

952
  while (TSDB_CODE_SUCCESS == code && NULL != p) {
165,345✔
953
    STableDataCxt* pTableCxt = *(STableDataCxt**)p;
85,381✔
954
    if (colFormat) {
85,381✔
955
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, 0);
11,139✔
956
      if (pCol && pCol->nVal <= 0) {
11,136!
957
        p = taosHashIterate(pTableHash, p);
17✔
958
        continue;
17✔
959
      }
960

961
      if (pTableCxt->pData->pCreateTbReq) {
11,119✔
962
        pTableCxt->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
10,153✔
963
      }
964
      int8_t isBlob = IS_STR_DATA_BLOB(pCol->type) ? 1 : 0;
11,119!
965
      if (isBlob == 0) {
11,119!
966
        taosArraySort(pTableCxt->pData->aCol, insColDataComp);
11,121✔
967
        code = tColDataSortMerge(&pTableCxt->pData->aCol);
11,114✔
968
      } else {
969
        taosArraySort(pTableCxt->pData->aCol, insColDataComp);
×
970
        code = tColDataSortMergeWithBlob(&pTableCxt->pData->aCol, pTableCxt->pData->pBlobSet);
×
971
      }
972
    } else {
973
      // skip the table has no data to insert
974
      // eg: import a csv without valid data
975
      // if (0 == taosArrayGetSize(pTableCxt->pData->aRowP)) {
976
      //   parserWarn("no row in tableDataCxt uid:%" PRId64 " ", pTableCxt->pMeta->uid);
977
      //   p = taosHashIterate(pTableHash, p);
978
      //   continue;
979
      // }
980
      if (pTableCxt->hasBlob == 0) {
74,242✔
981
        if (!pTableCxt->ordered) {
74,239✔
982
          code = tRowSort(pTableCxt->pData->aRowP);
393✔
983
        }
984
        if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
74,239!
985
          code = tRowMerge(pTableCxt->pData->aRowP, pTableCxt->pSchema, PREFER_NON_NULL);
401✔
986
        }
987
      } else {
988
        if (!pTableCxt->ordered) {
3!
989
          code = tRowSortWithBlob(pTableCxt->pData->aRowP, pTableCxt->pSchema, pTableCxt->pData->pBlobSet);
×
990
        }
991
        if (code == TSDB_CODE_SUCCESS && (!pTableCxt->ordered || pTableCxt->duplicateTs)) {
3!
992
          code = tRowMergeWithBlob(pTableCxt->pData->aRowP, pTableCxt->pSchema, pTableCxt->pData->pBlobSet, 0);
×
993
        }
994
      }
995
    }
996

997
    if (TSDB_CODE_SUCCESS == code) {
85,359!
998
      SVgroupDataCxt* pVgCxt = NULL;
85,359✔
999
      int32_t         vgId = pTableCxt->pMeta->vgId;
85,359✔
1000
      void**          pp = taosHashGet(pVgroupHash, &vgId, sizeof(vgId));
85,359✔
1001
      if (NULL == pp) {
85,352✔
1002
        code = createVgroupDataCxt(vgId, pVgroupHash, pVgroupList, &pVgCxt);
81,934✔
1003
      } else {
1004
        pVgCxt = *(SVgroupDataCxt**)pp;
3,418✔
1005
      }
1006
      if (TSDB_CODE_SUCCESS == code) {
85,358!
1007
        code = fillVgroupDataCxt(pTableCxt, pVgCxt, isRebuild, true);
85,362✔
1008
      }
1009
    }
1010
    if (TSDB_CODE_SUCCESS == code) {
85,332!
1011
      p = taosHashIterate(pTableHash, p);
85,336✔
1012
    }
1013
  }
1014

1015
  taosHashCleanup(pVgroupHash);
79,964✔
1016
  if (TSDB_CODE_SUCCESS == code) {
79,945!
1017
    *pVgDataBlocks = pVgroupList;
79,949✔
1018
  } else {
1019
    insDestroyVgroupDataCxtList(pVgroupList);
×
1020
  }
1021

1022
  return code;
79,954✔
1023
}
1024

1025
static int32_t buildSubmitReq(int32_t vgId, SSubmitReq2* pReq, void** pData, uint32_t* pLen) {
92,589✔
1026
  int32_t  code = TSDB_CODE_SUCCESS;
92,589✔
1027
  uint32_t len = 0;
92,589✔
1028
  void*    pBuf = NULL;
92,589✔
1029
  tEncodeSize(tEncodeSubmitReq, pReq, len, code);
92,589!
1030
  if (TSDB_CODE_SUCCESS == code) {
92,572!
1031
    SEncoder encoder;
1032
    len += sizeof(SSubmitReq2Msg);
92,608✔
1033
    pBuf = taosMemoryMalloc(len);
92,608!
1034
    if (NULL == pBuf) {
92,642!
1035
      return terrno;
×
1036
    }
1037
    ((SSubmitReq2Msg*)pBuf)->header.vgId = htonl(vgId);
92,642✔
1038
    ((SSubmitReq2Msg*)pBuf)->header.contLen = htonl(len);
92,642✔
1039
    ((SSubmitReq2Msg*)pBuf)->version = htobe64(1);
92,642✔
1040
    tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SSubmitReq2Msg)), len - sizeof(SSubmitReq2Msg));
92,678✔
1041
    code = tEncodeSubmitReq(&encoder, pReq);
92,696✔
1042
    tEncoderClear(&encoder);
92,708✔
1043
  }
1044

1045
  if (TSDB_CODE_SUCCESS == code) {
92,716!
1046
    *pData = pBuf;
92,716✔
1047
    *pLen = len;
92,716✔
1048
  } else {
1049
    taosMemoryFree(pBuf);
×
1050
  }
1051
  return code;
92,671✔
1052
}
1053

1054
static void destroyVgDataBlocks(void* p) {
×
1055
  if (p == NULL) return;
×
1056
  SVgDataBlocks* pVg = p;
×
1057
  taosMemoryFree(pVg->pData);
×
1058
  taosMemoryFree(pVg);
×
1059
}
1060

1061
int32_t insResetBlob(SSubmitReq2* p) {
92,596✔
1062
  int32_t code = 0;
92,596✔
1063
  if (p->raw) {
92,596!
1064
    return TSDB_CODE_SUCCESS;  // no blob data in raw mode
×
1065
  }
1066

1067
  if (p->aSubmitBlobData != NULL) {
92,596!
1068
    for (int32_t i = 0; i < taosArrayGetSize(p->aSubmitTbData); i++) {
×
1069
      SSubmitTbData* pSubmitTbData = taosArrayGet(p->aSubmitTbData, i);
×
1070
      SBlobSet**     ppBlob = taosArrayGet(p->aSubmitBlobData, i);
×
1071
      SBlobSet*      pBlob = ppBlob ? *ppBlob : NULL;
×
1072
      int32_t        nrow = taosArrayGetSize(pSubmitTbData->aRowP);
×
1073
      int32_t        nblob = 0;
×
1074
      if (nrow > 0 && pBlob) {
×
1075
        nblob = taosArrayGetSize(pBlob->pSeqTable);
×
1076
      }
1077
      uTrace("blob %p row size %d, pData size %d", pBlob, nblob, nrow);
×
1078
      pSubmitTbData->pBlobSet = pBlob;
×
1079
      if (ppBlob != NULL) *ppBlob = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
×
1080
    }
1081
  } else {
1082
    for (int32_t i = 0; i < taosArrayGetSize(p->aSubmitTbData); i++) {
190,977✔
1083
      SSubmitTbData* pSubmitTbData = taosArrayGet(p->aSubmitTbData, i);
98,298✔
1084
      pSubmitTbData->pBlobSet = NULL;  // reset blob row to NULL, so that it will not be freed in destroy
98,381✔
1085
    }
1086
  }
1087

1088
  return code;
92,659✔
1089
}
1090
int32_t insBuildVgDataBlocks(SHashObj* pVgroupsHashObj, SArray* pVgDataCxtList, SArray** pVgDataBlocks, bool append) {
88,934✔
1091
  size_t  numOfVg = taosArrayGetSize(pVgDataCxtList);
88,934✔
1092
  SArray* pDataBlocks = (append && *pVgDataBlocks) ? *pVgDataBlocks : taosArrayInit(numOfVg, POINTER_BYTES);
88,981!
1093
  if (NULL == pDataBlocks) {
88,995!
1094
    return TSDB_CODE_OUT_OF_MEMORY;
×
1095
  }
1096

1097
  int32_t code = TSDB_CODE_SUCCESS;
88,995✔
1098
  for (size_t i = 0; TSDB_CODE_SUCCESS == code && i < numOfVg; ++i) {
181,676✔
1099
    SVgroupDataCxt* src = taosArrayGetP(pVgDataCxtList, i);
92,652✔
1100
    if (taosArrayGetSize(src->pData->aSubmitTbData) <= 0) {
92,704!
1101
      continue;
×
1102
    }
1103
    SVgDataBlocks* dst = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
92,703!
1104
    if (NULL == dst) {
92,634!
1105
      code = terrno;
×
1106
    }
1107

1108
    if (TSDB_CODE_SUCCESS == code) {
92,634!
1109
      dst->numOfTables = taosArrayGetSize(src->pData->aSubmitTbData);
92,647✔
1110
      code = taosHashGetDup(pVgroupsHashObj, (const char*)&src->vgId, sizeof(src->vgId), &dst->vg);
92,644✔
1111
    }
1112
    if (TSDB_CODE_SUCCESS == code) {
92,699!
1113
      code = insResetBlob(src->pData);
92,699✔
1114
    }
1115

1116
    if (TSDB_CODE_SUCCESS == code) {
92,667!
1117
      code = buildSubmitReq(src->vgId, src->pData, &dst->pData, &dst->size);
92,678✔
1118
    }
1119
    if (TSDB_CODE_SUCCESS == code) {
92,663!
1120
      code = (NULL == taosArrayPush(pDataBlocks, &dst) ? terrno : TSDB_CODE_SUCCESS);
92,697!
1121
    }
1122
    if (TSDB_CODE_SUCCESS != code) {
92,670!
1123
      destroyVgDataBlocks(dst);
×
1124
    }
1125
  }
1126

1127
  if (append) {
89,024✔
1128
    if (NULL == *pVgDataBlocks) {
9,066!
1129
      *pVgDataBlocks = pDataBlocks;
9,069✔
1130
    }
1131
    return code;
9,066✔
1132
  }
1133

1134
  if (TSDB_CODE_SUCCESS == code) {
79,958✔
1135
    *pVgDataBlocks = pDataBlocks;
79,932✔
1136
  } else {
1137
    taosArrayDestroyP(pDataBlocks, destroyVgDataBlocks);
26✔
1138
  }
1139

1140
  return code;
79,937✔
1141
}
1142

1143
static bool findFileds(SSchema* pSchema, TAOS_FIELD* fields, int numFields) {
×
1144
  for (int i = 0; i < numFields; i++) {
×
1145
    if (strcmp(pSchema->name, fields[i].name) == 0) {
×
1146
      return true;
×
1147
    }
1148
  }
1149

1150
  return false;
×
1151
}
1152

1153
int32_t checkSchema(SSchema* pColSchema, SSchemaExt* pColExtSchema, int8_t* fields, char* errstr, int32_t errstrLen) {
572✔
1154
  if (*fields != pColSchema->type) {
572✔
1155
    if (errstr != NULL) {
1!
1156
      snprintf(errstr, errstrLen, "column type not equal, name:%s, schema type:%s, data type:%s", pColSchema->name,
×
1157
               tDataTypes[pColSchema->type].name, tDataTypes[*fields].name);
×
1158
    } else {
1159
      char buf[512] = {0};
1✔
1160
      snprintf(buf, sizeof(buf), "column type not equal, name:%s, schema type:%s, data type:%s", pColSchema->name,
1✔
1161
               tDataTypes[pColSchema->type].name, tDataTypes[*fields].name);
1✔
1162
      uError("checkSchema %s", buf);
1!
1163
    }
1164
    return TSDB_CODE_INVALID_PARA;
1✔
1165
  }
1166

1167
  if (IS_DECIMAL_TYPE(pColSchema->type)) {
571!
1168
    uint8_t precision = 0, scale = 0;
2✔
1169
    decimalFromTypeMod(pColExtSchema->typeMod, &precision, &scale);
2✔
1170
    uint8_t precisionData = 0, scaleData = 0;
2✔
1171
    int32_t bytes = *(int32_t*)(fields + sizeof(int8_t));
2✔
1172
    extractDecimalTypeInfoFromBytes(&bytes, &precisionData, &scaleData);
2✔
1173
    if (precision != precisionData || scale != scaleData) {
2!
1174
      if (errstr != NULL) {
×
1175
        snprintf(errstr, errstrLen,
×
1176
                 "column decimal type not equal, name:%s, schema type:%s, precision:%d, scale:%d, data type:%s, "
1177
                 "precision:%d, scale:%d",
1178
                 pColSchema->name, tDataTypes[pColSchema->type].name, precision, scale, tDataTypes[*fields].name,
×
1179
                 precisionData, scaleData);
1180
        return TSDB_CODE_INVALID_PARA;
×
1181
      } else {
1182
        char buf[512] = {0};
×
1183
        snprintf(buf, sizeof(buf),
×
1184
                 "column decimal type not equal, name:%s, schema type:%s, precision:%d, scale:%d, data type:%s, "
1185
                 "precision:%d, scale:%d",
1186
                 pColSchema->name, tDataTypes[pColSchema->type].name, precision, scale, tDataTypes[*fields].name,
×
1187
                 precisionData, scaleData);
1188
        uError("checkSchema %s", buf);
×
1189
        return TSDB_CODE_INVALID_PARA;
×
1190
      }
1191
    }
1192
    return 0;
2✔
1193
  }
1194

1195
  if (IS_VAR_DATA_TYPE(pColSchema->type)) {
569!
1196
    int32_t bytes = *(int32_t*)(fields + sizeof(int8_t));
102✔
1197
    if (IS_STR_DATA_BLOB(pColSchema->type)) {
102!
1198
      if (bytes >= TSDB_MAX_BLOB_LEN) {
×
1199
        uError("column blob data bytes exceed max limit, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
×
1200
               pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name, bytes);
1201
        return TSDB_CODE_INVALID_PARA;
×
1202
      }
1203
    } else {
1204
      if (bytes > pColSchema->bytes) {
102✔
1205
        if (errstr != NULL) {
1!
1206
          snprintf(errstr, errstrLen,
×
1207
                   "column var data bytes error, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
1208
                   pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name,
×
1209
                   *(int32_t*)(fields + sizeof(int8_t)));
×
1210
        } else {
1211
          char buf[512] = {0};
1✔
1212
          snprintf(buf, sizeof(buf),
1✔
1213
                   "column var data bytes error, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
1214
                   pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name,
1✔
1215
                   *(int32_t*)(fields + sizeof(int8_t)));
1✔
1216
          uError("checkSchema %s", buf);
1!
1217
        }
1218
        return TSDB_CODE_INVALID_PARA;
1✔
1219
      }
1220
    }
1221
  }
1222

1223
  if (!IS_VAR_DATA_TYPE(pColSchema->type) && *(int32_t*)(fields + sizeof(int8_t)) != pColSchema->bytes) {
568!
1224
    if (errstr != NULL) {
×
1225
      snprintf(errstr, errstrLen,
×
1226
               "column normal data bytes not equal, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
1227
               pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name,
×
1228
               *(int32_t*)(fields + sizeof(int8_t)));
×
1229
    } else {
1230
      char buf[512] = {0};
×
1231
      snprintf(buf, sizeof(buf),
×
1232
               "column normal data bytes not equal, name:%s, schema type:%s, bytes:%d, data type:%s, bytes:%d",
1233
               pColSchema->name, tDataTypes[pColSchema->type].name, pColSchema->bytes, tDataTypes[*fields].name,
×
1234
               *(int32_t*)(fields + sizeof(int8_t)));
×
1235
      uError("checkSchema %s", buf);
×
1236
    }
1237
    return TSDB_CODE_INVALID_PARA;
×
1238
  }
1239
  return 0;
568✔
1240
}
1241

1242
#define PRCESS_DATA(i, j)                                                                                          \
1243
  ret = checkSchema(pColSchema, pColExtSchema, fields, errstr, errstrLen);                                         \
1244
  if (ret != 0) {                                                                                                  \
1245
    goto end;                                                                                                      \
1246
  }                                                                                                                \
1247
                                                                                                                   \
1248
  if (pColSchema->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {                                                          \
1249
    hasTs = true;                                                                                                  \
1250
  }                                                                                                                \
1251
                                                                                                                   \
1252
  int8_t* offset = pStart;                                                                                         \
1253
  if (IS_VAR_DATA_TYPE(pColSchema->type)) {                                                                        \
1254
    pStart += numOfRows * sizeof(int32_t);                                                                         \
1255
  } else {                                                                                                         \
1256
    pStart += BitmapLen(numOfRows);                                                                                \
1257
  }                                                                                                                \
1258
  char* pData = pStart;                                                                                            \
1259
                                                                                                                   \
1260
  SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, j);                                                        \
1261
  if (hasBlob) {                                                                                                   \
1262
    ret = tColDataAddValueByDataBlockWithBlob(pCol, pColSchema->type, pColSchema->bytes, numOfRows, offset, pData, \
1263
                                              pBlobSet);                                                           \
1264
  } else {                                                                                                         \
1265
    ret = tColDataAddValueByDataBlock(pCol, pColSchema->type, pColSchema->bytes, numOfRows, offset, pData);        \
1266
  }                                                                                                                \
1267
  if (ret != 0) {                                                                                                  \
1268
    goto end;                                                                                                      \
1269
  }                                                                                                                \
1270
  fields += sizeof(int8_t) + sizeof(int32_t);                                                                      \
1271
  if (needChangeLength && version == BLOCK_VERSION_1) {                                                            \
1272
    pStart += htonl(colLength[i]);                                                                                 \
1273
  } else {                                                                                                         \
1274
    pStart += colLength[i];                                                                                        \
1275
  }                                                                                                                \
1276
  boundInfo->pColIndex[j] = -1;
1277

1278
int rawBlockBindData(SQuery* query, STableMeta* pTableMeta, void* data, SVCreateTbReq* pCreateTb, void* tFields,
128✔
1279
                     int numFields, bool needChangeLength, char* errstr, int32_t errstrLen, bool raw) {
1280
  int       ret = 0;
128✔
1281
  int8_t    hasBlob = 0;
128✔
1282
  SBlobSet* pBlobSet = NULL;
128✔
1283
  if (data == NULL) {
128!
1284
    uError("rawBlockBindData, data is NULL");
×
1285
    return TSDB_CODE_APP_ERROR;
×
1286
  }
1287
  void* tmp =
1288
      taosHashGet(((SVnodeModifyOpStmt*)(query->pRoot))->pTableBlockHashObj, &pTableMeta->uid, sizeof(pTableMeta->uid));
128✔
1289
  SVCreateTbReq* pCreateReqTmp = NULL;
128✔
1290
  if (tmp == NULL && pCreateTb != NULL) {
128✔
1291
    ret = cloneSVreateTbReq(pCreateTb, &pCreateReqTmp);
12✔
1292
    if (ret != TSDB_CODE_SUCCESS) {
12!
1293
      uError("cloneSVreateTbReq error");
×
1294
      goto end;
×
1295
    }
1296
  }
1297

1298
  STableDataCxt* pTableCxt = NULL;
128✔
1299
  ret = insGetTableDataCxt(((SVnodeModifyOpStmt*)(query->pRoot))->pTableBlockHashObj, &pTableMeta->uid,
128✔
1300
                           sizeof(pTableMeta->uid), pTableMeta, &pCreateReqTmp, &pTableCxt, true, false);
1301
  if (pCreateReqTmp != NULL) {
128!
1302
    tdDestroySVCreateTbReq(pCreateReqTmp);
×
1303
    taosMemoryFree(pCreateReqTmp);
×
1304
  }
1305

1306
  hasBlob = pTableCxt->hasBlob;
128✔
1307
  if (hasBlob && pTableCxt->pData->pBlobSet == NULL) {
128!
1308
    ret = tBlobSetCreate(512, 0, &pTableCxt->pData->pBlobSet);
×
1309
    if (pTableCxt->pData->pBlobSet == NULL) {
×
1310
      uError("create blob set failed");
×
1311
      ret = terrno;
×
1312
    }
1313
  }
1314

1315
  if (ret != TSDB_CODE_SUCCESS) {
128!
1316
    uError("insGetTableDataCxt error");
×
1317
    goto end;
×
1318
  }
1319
  pBlobSet = pTableCxt->pData->pBlobSet;
128✔
1320

1321
  pTableCxt->pData->flags |= TD_REQ_FROM_TAOX;
128✔
1322
  if (tmp == NULL) {
128✔
1323
    ret = initTableColSubmitData(pTableCxt);
115✔
1324
    if (ret != TSDB_CODE_SUCCESS) {
115!
1325
      uError("initTableColSubmitData error");
×
1326
      goto end;
×
1327
    }
1328
  }
1329

1330
  char* p = (char*)data;
128✔
1331
  // | version | total length | total rows | blankFill | total columns | flag seg| block group id | column schema | each
1332
  // column length |
1333
  int32_t version = *(int32_t*)data;
128✔
1334
  p += sizeof(int32_t);
128✔
1335
  p += sizeof(int32_t);
128✔
1336

1337
  int32_t numOfRows = *(int32_t*)p;
128✔
1338
  p += sizeof(int32_t);
128✔
1339

1340
  int32_t numOfCols = *(int32_t*)p;
128✔
1341
  p += sizeof(int32_t);
128✔
1342

1343
  p += sizeof(int32_t);
128✔
1344
  p += sizeof(uint64_t);
128✔
1345

1346
  int8_t* fields = p;
128✔
1347
  if (*fields >= TSDB_DATA_TYPE_MAX || *fields < 0) {
128!
1348
    uError("fields type error:%d", *fields);
×
1349
    ret = TSDB_CODE_INVALID_PARA;
×
1350
    goto end;
×
1351
  }
1352
  p += numOfCols * (sizeof(int8_t) + sizeof(int32_t));
128✔
1353

1354
  int32_t* colLength = (int32_t*)p;
128✔
1355
  p += sizeof(int32_t) * numOfCols;
128✔
1356

1357
  char* pStart = p;
128✔
1358

1359
  SSchema*       pSchema = getTableColumnSchema(pTableMeta);
128✔
1360
  SSchemaExt*    pExtSchemas = getTableColumnExtSchema(pTableMeta);
128✔
1361
  SBoundColInfo* boundInfo = &pTableCxt->boundColsInfo;
128✔
1362

1363
  if (tFields != NULL && numFields != numOfCols) {
128!
1364
    if (errstr != NULL) snprintf(errstr, errstrLen, "numFields:%d not equal to data cols:%d", numFields, numOfCols);
×
1365
    ret = TSDB_CODE_INVALID_PARA;
×
1366
    goto end;
×
1367
  }
1368

1369
  bool hasTs = false;
128✔
1370
  if (tFields == NULL) {
128✔
1371
    int32_t len = TMIN(numOfCols, boundInfo->numOfBound);
6✔
1372
    for (int j = 0; j < len; j++) {
18✔
1373
      SSchema*    pColSchema = &pSchema[j];
14✔
1374
      SSchemaExt* pColExtSchema = &pExtSchemas[j];
14✔
1375
      PRCESS_DATA(j, j)
14!
1376
    }
1377
  } else {
1378
    for (int i = 0; i < numFields; i++) {
642✔
1379
      for (int j = 0; j < boundInfo->numOfBound; j++) {
4,325!
1380
        SSchema*    pColSchema = &pSchema[j];
4,325✔
1381
        SSchemaExt* pColExtSchema = &pExtSchemas[j];
4,325✔
1382
        char*       fieldName = NULL;
4,325✔
1383
        if (raw) {
4,325✔
1384
          fieldName = ((SSchemaWrapper*)tFields)->pSchema[i].name;
4,320✔
1385
        } else {
1386
          fieldName = ((TAOS_FIELD*)tFields)[i].name;
5✔
1387
        }
1388
        if (strcmp(pColSchema->name, fieldName) == 0) {
4,325✔
1389
          PRCESS_DATA(i, j)
520!
1390
          break;
520✔
1391
        }
1392
      }
1393
    }
1394
  }
1395

1396
  if (!hasTs) {
126!
1397
    if (errstr != NULL) snprintf(errstr, errstrLen, "timestamp column(primary key) not found in raw data");
×
1398
    ret = TSDB_CODE_INVALID_PARA;
×
1399
    goto end;
×
1400
  }
1401

1402
  // process NULL data
1403
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
670✔
1404
    if (boundInfo->pColIndex[c] != -1) {
544✔
1405
      SColData* pCol = taosArrayGet(pTableCxt->pData->aCol, c);
13✔
1406
      ret = tColDataAddValueByDataBlock(pCol, 0, 0, numOfRows, NULL, NULL);
13✔
1407
      if (ret != 0) {
13!
1408
        goto end;
×
1409
      }
1410
    } else {
1411
      boundInfo->pColIndex[c] = c;  // restore for next block
531✔
1412
    }
1413
  }
1414

1415
end:
126✔
1416
  return ret;
128✔
1417
}
1418

1419
int rawBlockBindRawData(SHashObj* pVgroupHash, SArray* pVgroupList, STableMeta* pTableMeta, void* data) {
×
1420
  int code = transformRawSSubmitTbData(data, pTableMeta->suid, pTableMeta->uid, pTableMeta->sversion);
×
1421
  if (code != 0) {
×
1422
    return code;
×
1423
  }
1424
  SVgroupDataCxt* pVgCxt = NULL;
×
1425
  void**          pp = taosHashGet(pVgroupHash, &pTableMeta->vgId, sizeof(pTableMeta->vgId));
×
1426
  if (NULL == pp) {
×
1427
    code = createVgroupDataCxt(pTableMeta->vgId, pVgroupHash, pVgroupList, &pVgCxt);
×
1428
    if (code != 0) {
×
1429
      return code;
×
1430
    }
1431
  } else {
1432
    pVgCxt = *(SVgroupDataCxt**)pp;
×
1433
  }
1434
  if (NULL == pVgCxt->pData->aSubmitTbData) {
×
1435
    pVgCxt->pData->aSubmitTbData = taosArrayInit(0, POINTER_BYTES);
×
1436
    pVgCxt->pData->raw = true;
×
1437
    if (NULL == pVgCxt->pData->aSubmitTbData) {
×
1438
      return terrno;
×
1439
    }
1440
  }
1441

1442
  // push data to submit, rebuild empty data for next submit
1443
  if (NULL == taosArrayPush(pVgCxt->pData->aSubmitTbData, &data)) {
×
1444
    return terrno;
×
1445
  }
1446

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

1449
  return 0;
×
1450
}
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