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

taosdata / TDengine / #4548

22 Jul 2025 02:37AM UTC coverage: 54.273% (-3.0%) from 57.287%
#4548

push

travis-ci

GitHub
Merge pull request #32061 from taosdata/new_testcases

132738 of 315239 branches covered (42.11%)

Branch coverage included in aggregate %.

201371 of 300373 relevant lines covered (67.04%)

3475977.14 hits per line

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

64.23
/source/libs/parser/src/parInsertStmt.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 "geosWrapper.h"
17
#include "os.h"
18
#include "parInsertUtil.h"
19
#include "parInt.h"
20
#include "parToken.h"
21
#include "query.h"
22
#include "tdataformat.h"
23
#include "tglobal.h"
24
#include "ttime.h"
25
#include "ttypes.h"
26

27
typedef struct SKvParam {
28
  int16_t  pos;
29
  SArray*  pTagVals;
30
  SSchema* schema;
31
  char     buf[TSDB_MAX_TAGS_LEN];
32
} SKvParam;
33

34
int32_t qCloneCurrentTbData(STableDataCxt* pDataBlock, SSubmitTbData** pData) {
597✔
35
  *pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
597!
36
  if (NULL == *pData) {
600!
37
    return terrno;
×
38
  }
39

40
  int8_t         flag = 1;
600✔
41
  SSubmitTbData* pNew = *pData;
600✔
42

43
  *pNew = *pDataBlock->pData;
600✔
44

45
  int32_t code = cloneSVreateTbReq(pDataBlock->pData->pCreateTbReq, &pNew->pCreateTbReq);
600✔
46
  if (TSDB_CODE_SUCCESS != code) {
598!
47
    taosMemoryFreeClear(*pData);
×
48
    return code;
×
49
  }
50
  pNew->aCol = taosArrayDup(pDataBlock->pData->aCol, NULL);
598✔
51
  if (!pNew->aCol) {
599!
52
    code = terrno;
×
53
    taosMemoryFreeClear(*pData);
×
54
    return code;
×
55
  }
56

57
  int32_t colNum = taosArrayGetSize(pNew->aCol);
599✔
58
  for (int32_t i = 0; i < colNum; ++i) {
4,438✔
59
    if (pDataBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
3,842✔
60
      SColData* pCol = (SColData*)taosArrayGet(pNew->aCol, i);
3,840✔
61
      tColDataDeepClear(pCol);
3,840✔
62
    } else {
63
      pNew->aCol = taosArrayInit(20, POINTER_BYTES);
2✔
64
      if (pNew->aCol == NULL) {
2!
65
        code = terrno;
×
66
        taosMemoryFreeClear(*pData);
×
67
        return code;
×
68
      }
69
    }
70
  }
71

72
  if (pDataBlock->hasBlob) {
596✔
73
    code = tBlobRowCreate(1024, flag, &pNew->pBlobRow);
1✔
74
  }
75

76
  return code;
599✔
77
}
78

79
int32_t qAppendStmtTableOutput(SQuery* pQuery, SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
13,126✔
80
                               SStbInterlaceInfo* pBuildInfo, SVCreateTbReq* ctbReq) {
81
  // merge according to vgId
82
  return insAppendStmtTableDataCxt(pAllVgHash, pTbData, pTbCtx, pBuildInfo, ctbReq);
13,126✔
83
}
84

85
int32_t qBuildStmtFinOutput(SQuery* pQuery, SHashObj* pAllVgHash, SArray* pVgDataBlocks) {
9,065✔
86
  int32_t             code = TSDB_CODE_SUCCESS;
9,065✔
87
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
9,065✔
88

89
  if (TSDB_CODE_SUCCESS == code) {
9,065!
90
    code = insBuildVgDataBlocks(pAllVgHash, pVgDataBlocks, &pStmt->pDataBlocks, true);
9,067✔
91
  }
92

93
  if (pStmt->freeArrayFunc) {
9,064!
94
    pStmt->freeArrayFunc(pVgDataBlocks);
9,066✔
95
  }
96
  return code;
9,066✔
97
}
98

99
/*
100
int32_t qBuildStmtOutputFromTbList(SQuery* pQuery, SHashObj* pVgHash, SArray* pBlockList, STableDataCxt* pTbCtx, int32_t
101
tbNum) { int32_t             code = TSDB_CODE_SUCCESS; SArray*             pVgDataBlocks = NULL; SVnodeModifyOpStmt*
102
pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
103

104
  // merge according to vgId
105
  if (tbNum > 0) {
106
    code = insMergeStmtTableDataCxt(pTbCtx, pBlockList, &pVgDataBlocks, true, tbNum);
107
  }
108

109
  if (TSDB_CODE_SUCCESS == code) {
110
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks);
111
  }
112

113
  if (pStmt->freeArrayFunc) {
114
    pStmt->freeArrayFunc(pVgDataBlocks);
115
  }
116
  return code;
117
}
118
*/
119

120
int32_t qBuildStmtOutput(SQuery* pQuery, SHashObj* pVgHash, SHashObj* pBlockHash) {
599✔
121
  int32_t             code = TSDB_CODE_SUCCESS;
599✔
122
  SArray*             pVgDataBlocks = NULL;
599✔
123
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
599✔
124

125
  // merge according to vgId
126
  if (taosHashGetSize(pBlockHash) > 0) {
599!
127
    code = insMergeTableDataCxt(pBlockHash, &pVgDataBlocks, true);
599✔
128
  }
129

130
  if (TSDB_CODE_SUCCESS == code) {
599!
131
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks, false);
599✔
132
  }
133

134
  if (pStmt->freeArrayFunc) {
600!
135
    pStmt->freeArrayFunc(pVgDataBlocks);
600✔
136
  }
137
  return code;
599✔
138
}
139

140
int32_t qBindStmtTagsValue(void* pBlock, void* boundTags, int64_t suid, const char* sTableName, char* tName,
11✔
141
                           TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen, void* charsetCxt) {
142
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
11✔
143
  SMsgBuf        pBuf = {.buf = msgBuf, .len = msgBufLen};
11✔
144
  int32_t        code = TSDB_CODE_SUCCESS;
11✔
145
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
11✔
146
  if (NULL == tags) {
11!
147
    return TSDB_CODE_APP_ERROR;
×
148
  }
149

150
  SArray* pTagArray = taosArrayInit(tags->numOfBound, sizeof(STagVal));
11✔
151
  if (!pTagArray) {
11!
152
    return buildInvalidOperationMsg(&pBuf, "out of memory");
×
153
  }
154

155
  SArray* tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
11✔
156
  if (!tagName) {
11!
157
    code = buildInvalidOperationMsg(&pBuf, "out of memory");
×
158
    goto end;
×
159
  }
160

161
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
11✔
162

163
  bool  isJson = false;
11✔
164
  STag* pTag = NULL;
11✔
165

166
  for (int c = 0; c < tags->numOfBound; ++c) {
42✔
167
    if (bind[c].is_null && bind[c].is_null[0]) {
31!
168
      continue;
×
169
    }
170

171
    SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
31✔
172
    int32_t  colLen = pTagSchema->bytes;
31✔
173
    if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
31!
174
      if (!bind[c].length) {
13!
175
        code = buildInvalidOperationMsg(&pBuf, "var tag length is null");
×
176
        goto end;
×
177
      }
178
      colLen = bind[c].length[0];
13✔
179
      if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
13!
180
        code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
×
181
        goto end;
×
182
      }
183
    }
184
    if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
62!
185
      code = terrno;
×
186
      goto end;
×
187
    }
188
    if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
31!
189
      if (colLen > (TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
×
190
        code = buildSyntaxErrMsg(&pBuf, "json string too long than 4095", bind[c].buffer);
×
191
        goto end;
×
192
      }
193

194
      isJson = true;
×
195
      char* tmp = taosMemoryCalloc(1, colLen + 1);
×
196
      if (!tmp) {
×
197
        code = terrno;
×
198
        goto end;
×
199
      }
200
      memcpy(tmp, bind[c].buffer, colLen);
×
201
      code = parseJsontoTagData(tmp, pTagArray, &pTag, &pBuf, charsetCxt);
×
202
      taosMemoryFree(tmp);
×
203
      if (code != TSDB_CODE_SUCCESS) {
×
204
        goto end;
×
205
      }
206
    } else {
207
      STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
31✔
208
      //      strcpy(val.colName, pTagSchema->name);
209
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
31!
210
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
20✔
211
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
12✔
212
          code = initCtxAsText();
1✔
213
          if (code) {
1!
214
            qError("geometry init failed:%s", tstrerror(code));
×
215
            goto end;
×
216
          }
217
          code = checkWKB(bind[c].buffer, colLen);
1✔
218
          if (code) {
1!
219
            qError("stmt bind invalid geometry tag:%s, must be WKB format", (char*)bind[c].buffer);
×
220
            goto end;
×
221
          }
222
        }
223
        val.pData = (uint8_t*)bind[c].buffer;
12✔
224
        val.nData = colLen;
12✔
225
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
19✔
226
        int32_t output = 0;
1✔
227
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
1!
228
        if (p == NULL) {
1!
229
          code = terrno;
×
230
          goto end;
×
231
        }
232
        if (!taosMbsToUcs4(bind[c].buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
1!
233
          if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
234
            taosMemoryFree(p);
×
235
            code = generateSyntaxErrMsg(&pBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pTagSchema->name);
×
236
            goto end;
×
237
          }
238
          char buf[512] = {0};
×
239
          snprintf(buf, tListLen(buf), " taosMbsToUcs4 error:%s", strerror(terrno));
×
240
          taosMemoryFree(p);
×
241
          code = buildSyntaxErrMsg(&pBuf, buf, bind[c].buffer);
×
242
          goto end;
×
243
        }
244
        val.pData = p;
1✔
245
        val.nData = output;
1✔
246
      } else {
247
        memcpy(&val.i64, bind[c].buffer, colLen);
18✔
248
      }
249
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
31!
250
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
251
        goto end;
×
252
      }
253
      if (NULL == taosArrayPush(pTagArray, &val)) {
31!
254
        code = terrno;
×
255
        goto end;
×
256
      }
257
    }
258
  }
259

260
  if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) {
11!
261
    goto end;
×
262
  }
263

264
  if (NULL == pDataBlock->pData->pCreateTbReq) {
11✔
265
    pDataBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
3!
266
    if (NULL == pDataBlock->pData->pCreateTbReq) {
3!
267
      code = terrno;
×
268
      goto end;
×
269
    }
270
  } else {
271
    SVCreateTbReq* tmp = pDataBlock->pData->pCreateTbReq;
8✔
272
    taosMemoryFreeClear(tmp->name);
8!
273
    taosMemoryFreeClear(tmp->ctb.pTag);
8!
274
    taosMemoryFreeClear(tmp->ctb.stbName);
8!
275
    taosArrayDestroy(tmp->ctb.tagName);
8✔
276
    tmp->ctb.tagName = NULL;
8✔
277
  }
278

279
  code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName,
11✔
280
                             pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
11✔
281
  pTag = NULL;
11✔
282

283
end:
11✔
284
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
42✔
285
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
31✔
286
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
31✔
287
      taosMemoryFreeClear(p->pData);
1!
288
    }
289
  }
290
  taosArrayDestroy(pTagArray);
11✔
291
  taosArrayDestroy(tagName);
11✔
292
  taosMemoryFree(pTag);
11!
293

294
  return code;
11✔
295
}
296

297
int32_t convertStmtNcharCol(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_MULTI_BIND* src, TAOS_MULTI_BIND* dst,
24,639✔
298
                            void* charsetCxt) {
299
  int32_t output = 0;
24,639✔
300
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
24,639✔
301
  if (dst->buffer_length < newBuflen) {
24,639!
302
    dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
24,669!
303
    if (NULL == dst->buffer) {
24,757!
304
      return terrno;
×
305
    }
306
  }
307

308
  if (NULL == dst->length) {
24,727!
309
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
24,735!
310
    if (NULL == dst->length) {
24,723!
311
      taosMemoryFreeClear(dst->buffer);
×
312
      return terrno;
×
313
    }
314
  }
315

316
  dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
24,715✔
317

318
  for (int32_t i = 0; i < src->num; ++i) {
64,882✔
319
    if (src->is_null && src->is_null[i]) {
39,940✔
320
      continue;
473✔
321
    }
322

323
    if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
39,699✔
324
                       (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output,
39,467✔
325
                       charsetCxt)) {
326
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
5!
327
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
328
      }
329
      char buf[512] = {0};
×
330
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
331
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
332
    }
333

334
    dst->length[i] = output;
39,694✔
335
  }
336

337
  dst->buffer_type = src->buffer_type;
24,942✔
338
  dst->is_null = src->is_null;
24,942✔
339
  dst->num = src->num;
24,942✔
340

341
  return TSDB_CODE_SUCCESS;
24,942✔
342
}
343

344
int32_t qBindStmtStbColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
5,616✔
345
                              STSchema** pTSchema, SBindInfo* pBindInfos, void* charsetCxt) {
346
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
5,616✔
347
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
5,616✔
348
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
5,619✔
349
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
5,619✔
350
  int32_t          rowNum = bind->num;
5,619✔
351
  TAOS_MULTI_BIND  ncharBind = {0};
5,619✔
352
  TAOS_MULTI_BIND* pBind = NULL;
5,619✔
353
  int32_t          code = 0;
5,619✔
354
  int16_t          lastColId = -1;
5,619✔
355
  bool             colInOrder = true;
5,619✔
356

357
  if (NULL == pTSchema || NULL == *pTSchema) {
5,619!
358
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
72✔
359
  }
360

361
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
39,377✔
362
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
33,753✔
363
    if (pColSchema->colId <= lastColId) {
33,753!
364
      colInOrder = false;
×
365
    } else {
366
      lastColId = pColSchema->colId;
33,753✔
367
    }
368
    // SColData* pCol = taosArrayGet(pCols, c);
369

370
    if (bind[c].num != rowNum) {
33,753!
371
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
372
      goto _return;
×
373
    }
374

375
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
33,753!
376
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
33,753!
377
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
378
      goto _return;
×
379
    }
380

381
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
33,753✔
382
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
168✔
383
      if (code) {
168!
384
        goto _return;
×
385
      }
386
      pBind = &ncharBind;
168✔
387
    } else {
388
      pBind = bind + c;
33,585✔
389
    }
390

391
    pBindInfos[c].columnId = pColSchema->colId;
33,753✔
392
    pBindInfos[c].bind = pBind;
33,753✔
393
    pBindInfos[c].type = pColSchema->type;
33,753✔
394
  }
395

396
  code = tRowBuildFromBind(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols, &pDataBlock->ordered,
5,624✔
397
                           &pDataBlock->duplicateTs);
398

399
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
5,629!
400

401
_return:
5,629✔
402

403
  taosMemoryFree(ncharBind.buffer);
5,629!
404
  taosMemoryFree(ncharBind.length);
5,626!
405

406
  return code;
5,630✔
407
}
408

409
int32_t qBindStmtColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
42,751✔
410
                           void* charsetCxt) {
411
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
42,751✔
412
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
42,751✔
413
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
42,707✔
414
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
42,707✔
415
  int32_t          rowNum = bind->num;
42,707✔
416
  TAOS_MULTI_BIND  ncharBind = {0};
42,707✔
417
  TAOS_MULTI_BIND* pBind = NULL;
42,707✔
418
  int32_t          code = 0;
42,707✔
419

420
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
382,102✔
421
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
338,401✔
422
    SColData* pCol = taosArrayGet(pCols, c);
338,401✔
423

424
    if (bind[c].num != rowNum) {
338,123!
425
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
426
      goto _return;
×
427
    }
428

429
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
339,357!
430
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
338,815!
431
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
432
      goto _return;
×
433
    }
434

435
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
339,357✔
436
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
24,549✔
437
      if (code) {
24,655!
438
        goto _return;
×
439
      }
440
      pBind = &ncharBind;
24,655✔
441
    } else {
442
      pBind = bind + c;
314,808✔
443
    }
444

445
    int32_t bytes = 0;
339,463✔
446
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
339,463!
447
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
47,143!
448
        bytes = pColSchema->bytes - BLOBSTR_HEADER_SIZE;
×
449
      } else {
450
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
48,050✔
451
      }
452
    } else {
453
      bytes = -1;
292,320✔
454
    }
455
    code = tColDataAddValueByBind(pCol, pBind, bytes, initCtxAsText, checkWKB);
339,463✔
456
    if (code) {
339,396✔
457
      goto _return;
1✔
458
    }
459
  }
460

461
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
43,701!
462

463
_return:
43,701✔
464

465
  taosMemoryFree(ncharBind.buffer);
43,702!
466
  taosMemoryFree(ncharBind.length);
42,787!
467

468
  return code;
42,798✔
469
}
470

471
int32_t qBindStmtSingleColValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
×
472
                                int32_t colIdx, int32_t rowNum, void* charsetCxt) {
473
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
×
474
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
475
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
×
476
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
×
477
  SSchema*         pColSchema = &pSchema[boundInfo->pColIndex[colIdx]];
×
478
  SColData*        pCol = taosArrayGet(pCols, colIdx);
×
479
  TAOS_MULTI_BIND  ncharBind = {0};
×
480
  TAOS_MULTI_BIND* pBind = NULL;
×
481
  int32_t          code = 0;
×
482

483
  if (bind->num != rowNum) {
×
484
    return buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
485
  }
486

487
  // Column index exceeds the number of columns
488
  if (colIdx >= pCols->size && pCol == NULL) {
×
489
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
490
  }
491

492
  if (bind->buffer_type != pColSchema->type) {
×
493
    return buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
494
  }
495

496
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
×
497
    code = convertStmtNcharCol(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
×
498
    if (code) {
×
499
      goto _return;
×
500
    }
501
    pBind = &ncharBind;
×
502
  } else {
503
    pBind = bind;
×
504
  }
505
  
506
  code = tColDataAddValueByBind(pCol, pBind,
×
507
                                IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1,
×
508
                                initCtxAsText, checkWKB);
509

510
  parserDebug("stmt col %d bind %d rows data", colIdx, rowNum);
×
511

512
_return:
×
513

514
  taosMemoryFree(ncharBind.buffer);
×
515
  taosMemoryFree(ncharBind.length);
×
516

517
  return code;
×
518
}
519

520
int32_t qBindStmtTagsValue2(void* pBlock, void* boundTags, int64_t suid, const char* sTableName, char* tName,
189✔
521
                            TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen, void* charsetCxt,
522
                            SVCreateTbReq* pCreateTbReq) {
523
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
189✔
524
  SMsgBuf        pBuf = {.buf = msgBuf, .len = msgBufLen};
189✔
525
  int32_t        code = TSDB_CODE_SUCCESS;
189✔
526
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
189✔
527
  if (NULL == tags) {
189!
528
    return buildInvalidOperationMsg(&pBuf, "tags is null");
×
529
  }
530

531
  SArray* pTagArray;
532
  if (tags->parseredTags) {
189✔
533
    pTagArray = taosArrayDup(tags->parseredTags->pTagVals, NULL);
24✔
534
  } else {
535
    pTagArray = taosArrayInit(tags->numOfBound, sizeof(STagVal));
165✔
536
  }
537
  if (!pTagArray) {
189!
538
    return buildInvalidOperationMsg(&pBuf, "out of memory");
×
539
  }
540

541
  SArray* tagName;
542
  if (tags->parseredTags) {
189✔
543
    tagName = taosArrayDup(tags->parseredTags->STagNames, NULL);
24✔
544
  } else {
545
    tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
165✔
546
  }
547

548
  if (!tagName) {
189!
549
    code = buildInvalidOperationMsg(&pBuf, "out of memory");
×
550
    goto end;
×
551
  }
552

553
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
189✔
554

555
  bool  isJson = false;
189✔
556
  STag* pTag = NULL;
189✔
557

558
  int bindIdx = 0;
189✔
559
  for (int c = 0; c < tags->numOfBound; ++c) {
746✔
560
    if (bind == NULL) {
579✔
561
      break;
21✔
562
    }
563
    if (tags->parseredTags) {
558✔
564
      bool found = false;
264✔
565
      for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
720✔
566
        if (tags->parseredTags->pTagIndex[k] == tags->pColIndex[c]) {
504✔
567
          found = true;
48✔
568
          break;
48✔
569
        }
570
      }
571
      if (found) {
264✔
572
        continue;
48✔
573
      }
574
    }
575

576
    TAOS_STMT2_BIND bindData = bind[bindIdx++];
510✔
577

578
    if (bindData.is_null && bindData.is_null[0]) {
510!
579
      continue;
×
580
    }
581

582
    SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
510✔
583
    int32_t  colLen = pTagSchema->bytes;
510✔
584
    if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
510!
585
      if (IS_STR_DATA_BLOB(pTagSchema->type)) {
204!
586
        return TSDB_CODE_BLOB_NOT_SUPPORT_TAG;
×
587
      }
588
      if (!bindData.length) {
204!
589
        code = buildInvalidOperationMsg(&pBuf, "var tag length is null");
×
590
        goto end;
1✔
591
      }
592
      colLen = bindData.length[0];
204✔
593
      if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
204!
594
        code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
×
595
        goto end;
×
596
      }
597
    }
598
    if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
1,020!
599
      code = terrno;
×
600
      goto end;
×
601
    }
602
    if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
510!
603
      if (colLen > (TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
×
604
        code = buildSyntaxErrMsg(&pBuf, "json string too long than 4095", bindData.buffer);
×
605
        goto end;
×
606
      }
607

608
      isJson = true;
×
609
      char* tmp = taosMemoryCalloc(1, colLen + 1);
×
610
      if (!tmp) {
×
611
        code = terrno;
×
612
        goto end;
×
613
      }
614
      memcpy(tmp, bindData.buffer, colLen);
×
615
      code = parseJsontoTagData(tmp, pTagArray, &pTag, &pBuf, charsetCxt);
×
616
      taosMemoryFree(tmp);
×
617
      if (code != TSDB_CODE_SUCCESS) {
×
618
        goto end;
×
619
      }
620
    } else {
621
      STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
510✔
622
      //      strcpy(val.colName, pTagSchema->name);
623
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
510!
624
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
377✔
625
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
164✔
626
          code = initCtxAsText();
31✔
627
          if (code) {
31!
628
            qError("geometry init failed:%s", tstrerror(code));
×
629
            goto end;
1✔
630
          }
631
          code = checkWKB(bindData.buffer, colLen);
31✔
632
          if (code) {
31✔
633
            qError("stmt2 bind invalid geometry tag:%s, must be WKB format", (char*)bindData.buffer);
1!
634
            goto end;
1✔
635
          }
636
        }
637
        val.pData = (uint8_t*)bindData.buffer;
163✔
638
        val.nData = colLen;
163✔
639
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
346✔
640
        int32_t output = 0;
40✔
641
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
40!
642
        if (p == NULL) {
40!
643
          code = terrno;
×
644
          goto end;
×
645
        }
646
        if (!taosMbsToUcs4(bindData.buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
40!
647
          if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
648
            taosMemoryFree(p);
×
649
            code = generateSyntaxErrMsg(&pBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pTagSchema->name);
×
650
            goto end;
×
651
          }
652
          char buf[512] = {0};
×
653
          snprintf(buf, tListLen(buf), " taosMbsToUcs4 error:%s", strerror(terrno));
×
654
          taosMemoryFree(p);
×
655
          code = buildSyntaxErrMsg(&pBuf, buf, bindData.buffer);
×
656
          goto end;
×
657
        }
658
        val.pData = p;
40✔
659
        val.nData = output;
40✔
660
      } else {
661
        memcpy(&val.i64, bindData.buffer, colLen);
306✔
662
      }
663
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
509!
664
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
665
        goto end;
×
666
      }
667
      if (NULL == taosArrayPush(pTagArray, &val)) {
509!
668
        code = terrno;
×
669
        goto end;
×
670
      }
671
    }
672
  }
673

674
  if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) {
188!
675
    goto end;
×
676
  }
677

678
  if (pCreateTbReq) {
188✔
679
    code = insBuildCreateTbReq(pCreateTbReq, tName, pTag, suid, sTableName, tagName,
83✔
680
                               pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
83✔
681
    pTag = NULL;
83✔
682
    goto end;
83✔
683
  }
684

685
  if (NULL == pDataBlock->pData->pCreateTbReq) {
105✔
686
    pDataBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
8!
687
    if (NULL == pDataBlock->pData->pCreateTbReq) {
8!
688
      code = terrno;
×
689
      goto end;
×
690
    }
691
  } else {
692
    SVCreateTbReq* tmp = pDataBlock->pData->pCreateTbReq;
97✔
693
    taosMemoryFreeClear(tmp->name);
97!
694
    taosMemoryFreeClear(tmp->ctb.pTag);
97!
695
    taosMemoryFreeClear(tmp->ctb.stbName);
97!
696
    taosArrayDestroy(tmp->ctb.tagName);
97✔
697
    tmp->ctb.tagName = NULL;
97✔
698
  }
699

700
  code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName,
105✔
701
                             pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
105✔
702
  pTag = NULL;
105✔
703

704
end:
189✔
705
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
746✔
706
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
557✔
707
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
557✔
708
      taosMemoryFreeClear(p->pData);
40!
709
    }
710
  }
711
  taosArrayDestroy(pTagArray);
189✔
712
  taosArrayDestroy(tagName);
189✔
713
  taosMemoryFree(pTag);
189!
714

715
  return code;
189✔
716
}
717

718
static int32_t convertStmtStbNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst,
27✔
719
                                       void* charsetCxt) {
720
  int32_t       output = 0;
27✔
721
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
27✔
722

723
  dst->buffer = taosMemoryCalloc(src->num, max_buf_len);
27!
724
  if (NULL == dst->buffer) {
27!
725
    return terrno;
×
726
  }
727

728
  dst->length = taosMemoryCalloc(src->num, sizeof(int32_t));
27!
729
  if (NULL == dst->length) {
27!
730
    taosMemoryFreeClear(dst->buffer);
×
731
    return terrno;
×
732
  }
733

734
  char* src_buf = src->buffer;
27✔
735
  char* dst_buf = dst->buffer;
27✔
736
  for (int32_t i = 0; i < src->num; ++i) {
3,043✔
737
    if (src->is_null && src->is_null[i]) {
3,016!
738
      continue;
×
739
    }
740

741
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
3,016!
742
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
743
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
744
      }
745
      char buf[512] = {0};
×
746
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
747
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
748
    }
749

750
    dst->length[i] = output;
3,016✔
751
    src_buf += src->length[i];
3,016✔
752
    dst_buf += output;
3,016✔
753
  }
754

755
  dst->buffer_type = src->buffer_type;
27✔
756
  dst->is_null = src->is_null;
27✔
757
  dst->num = src->num;
27✔
758

759
  return TSDB_CODE_SUCCESS;
27✔
760
}
761

762
int32_t qBindStmtStbColsValue2(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
7,488✔
763
                               int32_t msgBufLen, STSchema** pTSchema, SBindInfo2* pBindInfos, void* charsetCxt,
764
                               SBlobRow2** ppBlob) {
765
  STableDataCxt*  pDataBlock = (STableDataCxt*)pBlock;
7,488✔
766
  SSchema*        pSchema = getTableColumnSchema(pDataBlock->pMeta);
7,488✔
767
  SBoundColInfo*  boundInfo = &pDataBlock->boundColsInfo;
7,488✔
768
  SMsgBuf         pBuf = {.buf = msgBuf, .len = msgBufLen};
7,488✔
769
  int32_t         rowNum = bind->num;
7,488✔
770
  SArray*         ncharBinds = NULL;
7,488✔
771
  TAOS_STMT2_BIND ncharBind = {0};
7,488✔
772
  int32_t         code = 0;
7,488✔
773
  int16_t         lastColId = -1;
7,488✔
774
  bool            colInOrder = true;
7,488✔
775
  int             ncharColNums = 0;
7,488✔
776
  int32_t         bindIdx = 0;
7,488✔
777
  int8_t          hasBlob = 0;
7,488✔
778
  if (NULL == pTSchema || NULL == *pTSchema) {
7,488✔
779
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
105✔
780
  }
781

782
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
118,208✔
783
    if (TSDB_DATA_TYPE_NCHAR == pSchema[boundInfo->pColIndex[c]].type) {
110,719✔
784
      ncharColNums++;
27✔
785
    }
786
  }
787
  if (ncharColNums > 0) {
7,489✔
788
    ncharBinds = taosArrayInit(ncharColNums, sizeof(ncharBind));
27✔
789
    if (!ncharBinds) {
27!
790
      code = terrno;
×
791
      goto _return;
×
792
    }
793
  }
794

795
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
116,971✔
796
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
109,473✔
797
    if (pColSchema->colId <= lastColId) {
109,473✔
798
      colInOrder = false;
7✔
799
    } else {
800
      lastColId = pColSchema->colId;
109,466✔
801
    }
802

803
    if (parsedCols) {
109,473✔
804
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
132✔
805
      if (pParsedVal) {
132✔
806
        pBindInfos[c].columnId = pColSchema->colId;
24✔
807
        pBindInfos[c].type = pColSchema->type;
24✔
808
        pBindInfos[c].bytes = pColSchema->bytes;
24✔
809
        continue;
24✔
810
      }
811
    }
812

813
    TAOS_STMT2_BIND bindData = bind[bindIdx];
109,449✔
814

815
    if (bindData.num != rowNum) {
109,449!
816
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
817
      goto _return;
×
818
    }
819

820
    if ((!(rowNum == 1 && bindData.is_null && *bindData.is_null)) &&
109,449!
821
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
109,453!
822
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
823
      goto _return;
×
824
    }
825

826
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
109,449✔
827
      code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
27✔
828
      if (code) {
27!
829
        goto _return;
×
830
      }
831
      if (!taosArrayPush(ncharBinds, &ncharBind)) {
27!
832
        code = terrno;
×
833
        goto _return;
×
834
      }
835
      pBindInfos[c].bind = taosArrayGetLast(ncharBinds);
27✔
836
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
109,422✔
837
      code = initCtxAsText();
13✔
838
      if (code) {
13!
839
        qError("geometry init failed:%s", tstrerror(code));
×
840
        goto _return;
×
841
      }
842
      uint8_t* buf = bindData.buffer;
13✔
843
      for (int j = 0; j < bindData.num; j++) {
29✔
844
        if (bindData.is_null && bindData.is_null[j]) {
16✔
845
          continue;
1✔
846
        }
847
        code = checkWKB(buf, bindData.length[j]);
15✔
848
        if (code) {
15!
849
          qError("stmt2 interlace mode geometry data[%d]:{%s},length:%d must be in WKB format", c, buf,
×
850
                 bindData.length[j]);
851
          goto _return;
×
852
        }
853
        buf += bindData.length[j];
15✔
854
      }
855
      pBindInfos[c].bind = bind + bindIdx;
13✔
856
    } else {
857
      if (IS_STR_DATA_BLOB(pColSchema->type)) hasBlob = 1;
109,409!
858
      pBindInfos[c].bind = bind + bindIdx;
109,409✔
859
    }
860

861
    pBindInfos[c].columnId = pColSchema->colId;
109,449✔
862
    pBindInfos[c].type = pColSchema->type;
109,449✔
863
    pBindInfos[c].bytes = pColSchema->bytes;
109,449✔
864

865
    bindIdx++;
109,449✔
866
  }
867

868
  if (hasBlob == 0) {
7,498✔
869
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pCols,
7,494✔
870
                              &pDataBlock->ordered, &pDataBlock->duplicateTs);
871
  } else {
872
    code = tBlobRowCreate(1024, 1, ppBlob);
4✔
873
    code = tRowBuildFromBind2WithBlob(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols,
×
874
                                      &pDataBlock->ordered, &pDataBlock->duplicateTs, *ppBlob);
875
  }
876

877
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
7,497!
878

879
_return:
7,497✔
880
  if (ncharBinds) {
7,497✔
881
    for (int i = 0; i < TARRAY_SIZE(ncharBinds); ++i) {
54✔
882
      TAOS_STMT2_BIND* ncBind = TARRAY_DATA(ncharBinds);
27✔
883
      taosMemoryFree(ncBind[i].buffer);
27!
884
      taosMemoryFree(ncBind[i].length);
27!
885
    }
886
    taosArrayDestroy(ncharBinds);
27✔
887
  }
888

889
  return code;
7,494✔
890
}
891

892
static int32_t convertStmtNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst,
24✔
893
                                    void* charsetCxt) {
894
  int32_t       output = 0;
24✔
895
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
24✔
896

897
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
24✔
898
  // if (dst->buffer_length < newBuflen) {
899
  dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
24!
900
  if (NULL == dst->buffer) {
24!
901
    return terrno;
×
902
  }
903
  //}
904

905
  if (NULL == dst->length) {
24✔
906
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
17!
907
    if (NULL == dst->length) {
17!
908
      taosMemoryFreeClear(dst->buffer);
×
909
      return terrno;
×
910
    }
911
  }
912

913
  // dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
914
  char* src_buf = src->buffer;
24✔
915
  char* dst_buf = dst->buffer;
24✔
916
  for (int32_t i = 0; i < src->num; ++i) {
129✔
917
    if (src->is_null && src->is_null[i]) {
105!
918
      continue;
×
919
    }
920

921
    /*if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
922
      (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output)) {*/
923
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
105!
924
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
925
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
926
      }
927
      char buf[512] = {0};
×
928
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
929
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
930
    }
931

932
    dst->length[i] = output;
105✔
933
    src_buf += src->length[i];
105✔
934
    dst_buf += output;
105✔
935
  }
936

937
  dst->buffer_type = src->buffer_type;
24✔
938
  dst->is_null = src->is_null;
24✔
939
  dst->num = src->num;
24✔
940

941
  return TSDB_CODE_SUCCESS;
24✔
942
}
943

944
int32_t qBindStmtColsValue2(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
125✔
945
                            int32_t msgBufLen, void* charsetCxt) {
946
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
125✔
947
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
125✔
948
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
125✔
949
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
125✔
950
  int32_t          rowNum = bind->num;
125✔
951
  TAOS_STMT2_BIND  ncharBind = {0};
125✔
952
  TAOS_STMT2_BIND* pBind = NULL;
125✔
953
  int32_t          code = 0;
125✔
954
  int32_t          bindIdx = 0;
125✔
955

956
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
494✔
957
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
369✔
958
    SColData* pCol = taosArrayGet(pCols, c);
369✔
959
    if (pCol == NULL || pColSchema == NULL) {
369!
960
      code = buildInvalidOperationMsg(&pBuf, "get column schema or column data failed");
×
961
      goto _return;
×
962
    }
963

964
    if (boundInfo->pColIndex[c] == 0) {
369✔
965
      pCol->cflag |= COL_IS_KEY;
125✔
966
    }
967

968
    if (parsedCols) {
369✔
969
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
132✔
970
      if (pParsedVal) {
132✔
971
        for (int row = 0; row < rowNum; row++) {
48✔
972
          code = tColDataAppendValue(pCol, pParsedVal);
24✔
973
          if (code) {
24!
974
            goto _return;
×
975
          }
976
        }
977
        continue;
24✔
978
      }
979
    }
980
    TAOS_STMT2_BIND bindData = bind[bindIdx];
345✔
981
    if (bindData.num != rowNum) {
345!
982
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
983
      goto _return;
×
984
    }
985

986
    if ((!(rowNum == 1 && bindData.is_null && *bindData.is_null)) &&
345!
987
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
345!
988
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
989
      goto _return;
×
990
    }
991

992
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
345✔
993
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
22✔
994
      if (code) {
22!
995
        goto _return;
×
996
      }
997
      pBind = &ncharBind;
22✔
998
    } else {
999
      pBind = bind + bindIdx;
323✔
1000
    }
1001
    int8_t  isBlob = 0;
345✔
1002
    int32_t bytes = -1;
345✔
1003
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
345!
1004
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
141!
1005
        isBlob = 1;
1✔
1006
        bytes = TSDB_MAX_BLOB_LEN;
1✔
1007
      } else {
1008
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
140✔
1009
      }
1010
    }
1011
    if (isBlob == 0) {
345✔
1012
      code = tColDataAddValueByBind2(pCol, pBind, bytes, initCtxAsText, checkWKB);
344✔
1013
    } else {
1014
      code = tColDataAddValueByBind2WithBlob(pCol, pBind, bytes, pDataBlock->pData->pBlobRow);
1✔
1015
    }
1016

1017
    if (code) {
345!
1018
      goto _return;
×
1019
    }
1020
    bindIdx++;
345✔
1021
  }
1022

1023
  parserDebug("stmt2 all %d columns bind %d rows data as col format", boundInfo->numOfBound, rowNum);
125!
1024

1025
_return:
125✔
1026

1027
  taosMemoryFree(ncharBind.buffer);
125!
1028
  taosMemoryFree(ncharBind.length);
125!
1029

1030
  return code;
125✔
1031
}
1032

1033
int32_t qBindStmtSingleColValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen,
6✔
1034
                                 int32_t colIdx, int32_t rowNum, void* charsetCxt) {
1035
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
6✔
1036
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
6✔
1037
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
6✔
1038
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
6✔
1039
  SSchema*         pColSchema = &pSchema[boundInfo->pColIndex[colIdx]];
6✔
1040
  SColData*        pCol = taosArrayGet(pCols, colIdx);
6✔
1041
  TAOS_STMT2_BIND  ncharBind = {0};
6✔
1042
  TAOS_STMT2_BIND* pBind = NULL;
6✔
1043
  int32_t          code = 0;
6✔
1044

1045
  if (bind->num != rowNum) {
6!
1046
    return buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1047
  }
1048

1049
  // Column index exceeds the number of columns
1050
  if (colIdx >= pCols->size && pCol == NULL) {
6!
1051
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
1052
  }
1053

1054
  if (bind->buffer_type != pColSchema->type) {
6!
1055
    return buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
1056
  }
1057

1058
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
6!
1059
    code = convertStmtNcharCol2(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
×
1060
    if (code) {
×
1061
      goto _return;
×
1062
    }
1063
    pBind = &ncharBind;
×
1064
  } else {
1065
    pBind = bind;
6✔
1066
  }
1067

1068
  int32_t bytes = -1;
6✔
1069
  int8_t  hasBlob = 0;
6✔
1070
  if (IS_VAR_DATA_TYPE(pColSchema->type)) {
6!
1071
    if (IS_STR_DATA_BLOB(pColSchema->type)) {
3!
1072
      bytes = TSDB_MAX_BLOB_LEN;
×
1073
      hasBlob = 1;
×
1074
    } else {
1075
      bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
3✔
1076
    }
1077
  } else {
1078
    bytes = -1;
3✔
1079
  }
1080

1081
  if (hasBlob) {
6!
1082
    code = tColDataAddValueByBind2WithBlob(pCol, pBind, bytes, pDataBlock->pData->pBlobRow);
×
1083
  } else {
1084
    code = tColDataAddValueByBind2(pCol, pBind, bytes, initCtxAsText, checkWKB);
6✔
1085
  }
1086

1087
  parserDebug("stmt col %d bind %d rows data", colIdx, rowNum);
6!
1088

1089
_return:
6✔
1090

1091
  taosMemoryFree(ncharBind.buffer);
6!
1092
  taosMemoryFree(ncharBind.length);
6!
1093

1094
  return code;
6✔
1095
}
1096

1097
int32_t qBindStmt2RowValue(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
2✔
1098
                           int32_t msgBufLen, STSchema** pTSchema, SBindInfo2* pBindInfos, void* charsetCxt) {
1099
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
2✔
1100
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
2✔
1101
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
2✔
1102
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
2✔
1103
  int32_t          rowNum = bind->num;
2✔
1104
  TAOS_STMT2_BIND  ncharBind = {0};
2✔
1105
  TAOS_STMT2_BIND* pBind = NULL;
2✔
1106
  int32_t          code = 0;
2✔
1107
  int16_t          lastColId = -1;
2✔
1108
  bool             colInOrder = true;
2✔
1109

1110
  if (NULL == pTSchema || NULL == *pTSchema) {
2!
1111
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
1✔
1112
  }
1113

1114
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
24✔
1115
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
22✔
1116
    if (pColSchema->colId <= lastColId) {
22!
1117
      colInOrder = false;
×
1118
    } else {
1119
      lastColId = pColSchema->colId;
22✔
1120
    }
1121

1122
    if (bind[c].num != rowNum) {
22!
1123
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1124
      goto _return;
×
1125
    }
1126

1127
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
22!
1128
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
22!
1129
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
1130
      goto _return;
×
1131
    }
1132

1133
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
22✔
1134
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
2✔
1135
      if (code) {
2!
1136
        goto _return;
×
1137
      }
1138
      pBindInfos[c].bind = &ncharBind;
2✔
1139
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
20✔
1140
      code = initCtxAsText();
2✔
1141
      if (code) {
2!
1142
        qError("geometry init failed:%s", tstrerror(code));
×
1143
        goto _return;
×
1144
      }
1145
      uint8_t* buf = bind[c].buffer;
2✔
1146
      for (int j = 0; j < bind[c].num; j++) {
4✔
1147
        if (bind[c].is_null && bind[c].is_null[j]) {
2!
1148
          continue;
×
1149
        }
1150
        code = checkWKB(buf, bind[c].length[j]);
2✔
1151
        if (code) {
2!
1152
          qError("stmt2 row bind geometry data[%d]:{%s},length:%d must be in WKB format", c, buf, bind[c].length[j]);
×
1153
          goto _return;
×
1154
        }
1155
        buf += bind[c].length[j];
2✔
1156
      }
1157
      pBindInfos[c].bind = bind + c;
2✔
1158
    } else {
1159
      pBindInfos[c].bind = bind + c;
18✔
1160
    }
1161

1162
    pBindInfos[c].columnId = pColSchema->colId;
22✔
1163
    pBindInfos[c].type = pColSchema->type;
22✔
1164
    pBindInfos[c].bytes = pColSchema->bytes;
22✔
1165

1166
    if (code) {
22!
1167
      goto _return;
×
1168
    }
1169
  }
1170

1171
  pDataBlock->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
2✔
1172
  if (pDataBlock->pData->pCreateTbReq != NULL) {
2!
1173
    pDataBlock->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
2✔
1174
  }
1175

1176
  code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pCols,
2✔
1177
                            &pDataBlock->ordered, &pDataBlock->duplicateTs);
1178
  qDebug("stmt2 all %d columns bind %d rows data as row format", boundInfo->numOfBound, rowNum);
2!
1179

1180
_return:
2✔
1181

1182
  taosMemoryFree(ncharBind.buffer);
2!
1183
  taosMemoryFree(ncharBind.length);
2!
1184

1185
  return code;
2✔
1186
}
1187

1188
int32_t buildBoundFields(int32_t numOfBound, int16_t* boundColumns, SSchema* pSchema, int32_t* fieldNum,
7✔
1189
                         TAOS_FIELD_E** fields, uint8_t timePrec) {
1190
  if (fields != NULL) {
7✔
1191
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
6!
1192
    if (NULL == *fields) {
6!
1193
      return terrno;
×
1194
    }
1195

1196
    SSchema* schema = &pSchema[boundColumns[0]];
6✔
1197
    if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
6✔
1198
      (*fields)[0].precision = timePrec;
5✔
1199
    }
1200

1201
    for (int32_t i = 0; i < numOfBound; ++i) {
28✔
1202
      schema = &pSchema[boundColumns[i]];
22✔
1203
      tstrncpy((*fields)[i].name, schema->name, 65);
22✔
1204
      (*fields)[i].type = schema->type;
22✔
1205
      (*fields)[i].bytes = calcTypeBytesFromSchemaBytes(schema->type, schema->bytes, true);
22✔
1206
    }
1207
  }
1208

1209
  *fieldNum = numOfBound;
7✔
1210

1211
  return TSDB_CODE_SUCCESS;
7✔
1212
}
1213

1214
int32_t buildStbBoundFields(SBoundColInfo boundColsInfo, SSchema* pSchema, int32_t* fieldNum, TAOS_FIELD_ALL** fields,
47✔
1215
                            STableMeta* pMeta, void* boundTags, SSHashObj* parsedCols, uint8_t tbNameFlag) {
1216
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
47✔
1217
  bool           hastag = (tags != NULL) && !(tbNameFlag & IS_FIXED_TAG);
47!
1218
  bool           hasPreBindTbname =
47✔
1219
      (tbNameFlag & IS_FIXED_VALUE) == 0 && ((tbNameFlag & USING_CLAUSE) != 0 || pMeta->tableType == TSDB_NORMAL_TABLE);
47!
1220
  int32_t numOfBound = boundColsInfo.numOfBound + (hasPreBindTbname ? 1 : 0);
47✔
1221
  if (hastag) {
47✔
1222
    numOfBound += tags->mixTagsCols ? 0 : tags->numOfBound;
28✔
1223
  }
1224

1225
  // Adjust the number of bound fields if there are parsed tags or parsed columns
1226
  if (tags->parseredTags) {
47✔
1227
    numOfBound -= tags->parseredTags->numOfTags;
2✔
1228
  }
1229
  if (parsedCols) {
47✔
1230
    numOfBound -= tSimpleHashGetSize(parsedCols);
4✔
1231
  }
1232

1233
  int32_t idx = 0;
47✔
1234
  if (fields != NULL) {
47!
1235
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_ALL));
47!
1236
    if (NULL == *fields) {
47!
1237
      return terrno;
×
1238
    }
1239

1240
    if (hasPreBindTbname) {
47✔
1241
      (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
31✔
1242
      tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
31✔
1243
      (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
31✔
1244
      (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
31✔
1245
      idx++;
31✔
1246
    }
1247

1248
    if (hastag && tags->numOfBound > 0 && !tags->mixTagsCols) {
47✔
1249
      SSchema* tagSchema = getTableTagSchema(pMeta);
13✔
1250

1251
      for (int32_t i = 0; i < tags->numOfBound; ++i) {
52✔
1252
        SSchema* schema = &tagSchema[tags->pColIndex[i]];
39✔
1253

1254
        if (tags->parseredTags && tags->parseredTags->numOfTags > 0) {
39!
1255
          int32_t tag_idx = schema->colId - 1 - pMeta->tableInfo.numOfColumns;
2✔
1256
          bool    found = false;
2✔
1257
          for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
3✔
1258
            if (tags->parseredTags->pTagIndex[k] == tag_idx) {
2✔
1259
              found = true;
1✔
1260
              break;
1✔
1261
            }
1262
          }
1263
          if (found) {
2✔
1264
            continue;
1✔
1265
          }
1266
        }
1267

1268
        (*fields)[idx].field_type = TAOS_FIELD_TAG;
38✔
1269

1270
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[i].name));
38✔
1271
        (*fields)[idx].type = schema->type;
38✔
1272
        (*fields)[idx].bytes = schema->bytes;
38✔
1273
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
38✔
1274
          (*fields)[idx].precision = pMeta->tableInfo.precision;
1✔
1275
        }
1276
        idx++;
38✔
1277
      }
1278
    }
1279

1280
    if (boundColsInfo.numOfBound > 0) {
47!
1281
      SSchema* schema = &pSchema[boundColsInfo.pColIndex[0]];
47✔
1282

1283
      for (int32_t i = 0; i < boundColsInfo.numOfBound; ++i) {
214✔
1284
        int16_t idxCol = boundColsInfo.pColIndex[i];
167✔
1285

1286
        if (idxCol == pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags) {
167✔
1287
          (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
9✔
1288
          tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
9✔
1289
          (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
9✔
1290
          (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
9✔
1291

1292
          idx++;
9✔
1293
          continue;
9✔
1294
        } else if (idxCol < pMeta->tableInfo.numOfColumns) {
158✔
1295
          if (parsedCols && tSimpleHashGet(parsedCols, &pSchema[idxCol].colId, sizeof(int16_t))) {
130✔
1296
            continue;
5✔
1297
          }
1298
          (*fields)[idx].field_type = TAOS_FIELD_COL;
125✔
1299
        } else {
1300
          if (tags->parseredTags && tags->parseredTags->numOfTags > 0) {
28!
1301
            int32_t tag_idx = idxCol - pMeta->tableInfo.numOfColumns;
2✔
1302
            bool    found = false;
2✔
1303
            for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
3✔
1304
              if (tags->parseredTags->pTagIndex[k] == tag_idx) {
2✔
1305
                found = true;
1✔
1306
                break;
1✔
1307
              }
1308
            }
1309
            if (found) {
2✔
1310
              continue;
1✔
1311
            }
1312
          }
1313
          (*fields)[idx].field_type = TAOS_FIELD_TAG;
27✔
1314
        }
1315

1316
        schema = &pSchema[idxCol];
152✔
1317
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[idx].name));
152✔
1318
        (*fields)[idx].type = schema->type;
152✔
1319
        (*fields)[idx].bytes = schema->bytes;
152✔
1320
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
152✔
1321
          (*fields)[idx].precision = pMeta->tableInfo.precision;
46✔
1322
        }
1323
        idx++;
152✔
1324
      }
1325
    }
1326
  }
1327

1328
  *fieldNum = numOfBound;
47✔
1329

1330
  return TSDB_CODE_SUCCESS;
47✔
1331
}
1332

1333
int32_t qBuildStmtTagFields(void* pBlock, void* boundTags, int32_t* fieldNum, TAOS_FIELD_E** fields) {
1✔
1334
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
1✔
1335
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
1✔
1336
  if (NULL == tags) {
1!
1337
    return TSDB_CODE_APP_ERROR;
×
1338
  }
1339

1340
  if (pDataBlock->pMeta->tableType != TSDB_SUPER_TABLE && pDataBlock->pMeta->tableType != TSDB_CHILD_TABLE) {
1!
1341
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1342
  }
1343

1344
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
1✔
1345
  if (tags->numOfBound <= 0) {
1!
1346
    *fieldNum = 0;
×
1347
    *fields = NULL;
×
1348

1349
    return TSDB_CODE_SUCCESS;
×
1350
  }
1351

1352
  CHECK_CODE(buildBoundFields(tags->numOfBound, tags->pColIndex, pSchema, fieldNum, fields, 0));
1!
1353

1354
  return TSDB_CODE_SUCCESS;
1✔
1355
}
1356

1357
int32_t qBuildStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields) {
6✔
1358
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
6✔
1359
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
6✔
1360
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
6!
1361
    *fieldNum = 0;
×
1362
    if (fields != NULL) {
×
1363
      *fields = NULL;
×
1364
    }
1365

1366
    return TSDB_CODE_SUCCESS;
×
1367
  }
1368

1369
  CHECK_CODE(buildBoundFields(pDataBlock->boundColsInfo.numOfBound, pDataBlock->boundColsInfo.pColIndex, pSchema,
6!
1370
                              fieldNum, fields, pDataBlock->pMeta->tableInfo.precision));
1371

1372
  return TSDB_CODE_SUCCESS;
6✔
1373
}
1374

1375
int32_t qBuildStmtStbColFields(void* pBlock, void* boundTags, SSHashObj* parsedCols, uint8_t tbNameFlag,
47✔
1376
                               int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
1377
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
47✔
1378
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
47✔
1379
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
47!
1380
    *fieldNum = 0;
×
1381
    if (fields != NULL) {
×
1382
      *fields = NULL;
×
1383
    }
1384

1385
    return TSDB_CODE_SUCCESS;
×
1386
  }
1387

1388
  CHECK_CODE(buildStbBoundFields(pDataBlock->boundColsInfo, pSchema, fieldNum, fields, pDataBlock->pMeta, boundTags,
47!
1389
                                 parsedCols, tbNameFlag));
1390

1391
  return TSDB_CODE_SUCCESS;
47✔
1392
}
1393

1394
int32_t qResetStmtColumns(SArray* pCols, bool deepClear) {
×
1395
  int32_t colNum = taosArrayGetSize(pCols);
×
1396

1397
  for (int32_t i = 0; i < colNum; ++i) {
×
1398
    SColData* pCol = (SColData*)taosArrayGet(pCols, i);
×
1399
    if (pCol == NULL) {
×
1400
      parserError("qResetStmtColumns column:%d is NULL", i);
×
1401
      return terrno;
×
1402
    }
1403
    if (deepClear) {
×
1404
      tColDataDeepClear(pCol);
×
1405
    } else {
1406
      tColDataClear(pCol);
×
1407
    }
1408
  }
1409

1410
  return TSDB_CODE_SUCCESS;
×
1411
}
1412

1413
int32_t qResetStmtDataBlock(STableDataCxt* block, bool deepClear) {
824✔
1414
  int32_t        code = 0;
824✔
1415
  STableDataCxt* pBlock = (STableDataCxt*)block;
824✔
1416
  int32_t        colNum = taosArrayGetSize(pBlock->pData->aCol);
824✔
1417

1418
  int8_t flag = 0;
824✔
1419
  for (int32_t i = 0; i < colNum; ++i) {
6,850✔
1420
    flag = pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
6,030✔
1421
    if (pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
6,030✔
1422
      SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i);
6,029✔
1423
      if (pCol == NULL) {
6,030!
1424
        parserError("qResetStmtDataBlock column:%d is NULL", i);
×
1425
        return terrno;
×
1426
      }
1427
      if (deepClear) {
6,030✔
1428
        tColDataDeepClear(pCol);
2,184✔
1429

1430
      } else {
1431
        tColDataClear(pCol);
3,846✔
1432
      }
1433

1434
    } else {
1435
      pBlock->pData->aRowP = taosArrayInit(20, POINTER_BYTES);
1✔
1436
    }
1437
  }
1438

1439
  tBlobRowDestroy(pBlock->pData->pBlobRow);
820✔
1440
  if (block->hasBlob) {
824✔
1441
    code = tBlobRowCreate(1024, flag, &pBlock->pData->pBlobRow);
2✔
1442
  }
1443

1444
  return code;
824✔
1445
}
1446

1447
int32_t qCloneStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, bool reset) {
294✔
1448
  int32_t code = 0;
294✔
1449

1450
  *pDst = taosMemoryCalloc(1, sizeof(STableDataCxt));
294!
1451
  if (NULL == *pDst) {
310!
1452
    return terrno;
×
1453
  }
1454

1455
  STableDataCxt* pNewCxt = (STableDataCxt*)*pDst;
310✔
1456
  STableDataCxt* pCxt = (STableDataCxt*)pSrc;
310✔
1457
  pNewCxt->hasBlob = pSrc->hasBlob;
310✔
1458
  pNewCxt->pSchema = NULL;
310✔
1459
  pNewCxt->pValues = NULL;
310✔
1460

1461
  if (pCxt->pMeta) {
310!
1462
    void* pNewMeta = taosMemoryMalloc(TABLE_META_SIZE(pCxt->pMeta));
310!
1463
    if (NULL == pNewMeta) {
309!
1464
      insDestroyTableDataCxt(*pDst);
×
1465
      return terrno;
×
1466
    }
1467
    memcpy(pNewMeta, pCxt->pMeta, TABLE_META_SIZE(pCxt->pMeta));
309!
1468
    pNewCxt->pMeta = pNewMeta;
309✔
1469
  }
1470

1471
  memcpy(&pNewCxt->boundColsInfo, &pCxt->boundColsInfo, sizeof(pCxt->boundColsInfo));
309✔
1472
  pNewCxt->boundColsInfo.pColIndex = NULL;
309✔
1473

1474
  if (pCxt->boundColsInfo.pColIndex) {
309!
1475
    void* pNewColIdx = taosMemoryMalloc(pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
309!
1476
    if (NULL == pNewColIdx) {
310!
1477
      insDestroyTableDataCxt(*pDst);
×
1478
      return terrno;
×
1479
    }
1480
    memcpy(pNewColIdx, pCxt->boundColsInfo.pColIndex,
310✔
1481
           pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
310✔
1482
    pNewCxt->boundColsInfo.pColIndex = pNewColIdx;
310✔
1483
  }
1484

1485
  if (pCxt->pData) {
310!
1486
    int8_t         flag = 1;
310✔
1487
    SSubmitTbData* pNewTb = (SSubmitTbData*)taosMemoryMalloc(sizeof(SSubmitTbData));
310!
1488
    if (NULL == pNewTb) {
310!
1489
      insDestroyTableDataCxt(*pDst);
×
1490
      return terrno;
×
1491
    }
1492

1493
    memcpy(pNewTb, pCxt->pData, sizeof(*pCxt->pData));
310✔
1494
    pNewTb->pCreateTbReq = NULL;
310✔
1495
    if (pNewTb->pBlobRow != NULL) {
310✔
1496
      flag = pNewTb->pBlobRow->type;
1✔
1497
    }
1498
    pNewTb->pBlobRow = NULL;
310✔
1499

1500
    pNewTb->aCol = taosArrayDup(pCxt->pData->aCol, NULL);
310✔
1501
    if (NULL == pNewTb->aCol) {
310!
1502
      insDestroyTableDataCxt(*pDst);
×
1503
      return terrno;
×
1504
    }
1505

1506
    if (pNewCxt->hasBlob) {
310✔
1507
      tBlobRowCreate(1024, flag, &pNewTb->pBlobRow);
1✔
1508
    }
1509

1510
    pNewCxt->pData = pNewTb;
310✔
1511

1512
    if (reset) {
310✔
1513
      code = qResetStmtDataBlock(*pDst, true);
226✔
1514
    }
1515
  }
1516

1517
  return code;
310✔
1518
}
1519

1520
int32_t qRebuildStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, uint64_t uid, uint64_t suid, int32_t vgId,
84✔
1521
                              bool rebuildCreateTb) {
1522
  int32_t code = qCloneStmtDataBlock(pDst, pSrc, false);
84✔
1523
  if (code) {
84!
1524
    return code;
×
1525
  }
1526

1527
  STableDataCxt* pBlock = (STableDataCxt*)*pDst;
84✔
1528
  if (pBlock->pMeta) {
84!
1529
    pBlock->pMeta->uid = uid;
84✔
1530
    pBlock->pMeta->vgId = vgId;
84✔
1531
    pBlock->pMeta->suid = suid;
84✔
1532
  }
1533

1534
  pBlock->pData->suid = suid;
84✔
1535
  pBlock->pData->uid = uid;
84✔
1536

1537
  if (rebuildCreateTb && NULL == pBlock->pData->pCreateTbReq) {
84!
1538
    pBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
64!
1539
    if (NULL == pBlock->pData->pCreateTbReq) {
64!
1540
      return terrno;
×
1541
    }
1542
  }
1543

1544
  return TSDB_CODE_SUCCESS;
84✔
1545
}
1546

1547
STableMeta* qGetTableMetaInDataBlock(STableDataCxt* pDataBlock) { return ((STableDataCxt*)pDataBlock)->pMeta; }
1,228✔
1548

1549
void qDestroyStmtDataBlock(STableDataCxt* pBlock) {
1,733✔
1550
  if (pBlock == NULL) {
1,733✔
1551
    return;
654✔
1552
  }
1553

1554
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
1,079✔
1555
  insDestroyTableDataCxt(pDataBlock);
1,079✔
1556
}
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