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

taosdata / TDengine / #4815

17 Oct 2025 06:47AM UTC coverage: 61.177% (-0.03%) from 61.206%
#4815

push

travis-ci

web-flow
Merge pull request #33289 from taosdata/3.0

enh: Code Optimization (#33283)

155629 of 324369 branches covered (47.98%)

Branch coverage included in aggregate %.

207706 of 269535 relevant lines covered (77.06%)

127615938.53 hits per line

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

63.65
/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) {
1,004,322✔
35
  if (pDataBlock == NULL || pDataBlock->pData == NULL) {
1,004,322!
36
    qError("stmt qCloneCurrentTbData input is null, maybe not bind data before execute");
×
37
    return TSDB_CODE_TSC_STMT_API_ERROR;
3✔
38
  }
39
  *pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
1,006,441!
40
  if (NULL == *pData) {
1,005,928!
41
    return terrno;
×
42
  }
43

44
  int8_t         flag = 1;
1,004,543✔
45
  SSubmitTbData* pNew = *pData;
1,004,543✔
46

47
  *pNew = *pDataBlock->pData;
1,005,092✔
48
  pNew->pBlobSet = NULL;
1,006,213✔
49

50
  int32_t code = cloneSVreateTbReq(pDataBlock->pData->pCreateTbReq, &pNew->pCreateTbReq);
1,005,229✔
51
  if (TSDB_CODE_SUCCESS != code) {
1,004,663!
52
    taosMemoryFreeClear(*pData);
×
53
    return code;
×
54
  }
55
  pNew->aCol = taosArrayDup(pDataBlock->pData->aCol, NULL);
1,004,663✔
56
  if (!pNew->aCol) {
1,005,778!
57
    code = terrno;
×
58
    taosMemoryFreeClear(*pData);
×
59
    return code;
×
60
  }
61

62
  int32_t colNum = taosArrayGetSize(pNew->aCol);
1,006,213✔
63
  for (int32_t i = 0; i < colNum; ++i) {
5,834,328✔
64
    if (pDataBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
4,827,992✔
65
      SColData* pCol = (SColData*)taosArrayGet(pNew->aCol, i);
4,828,877✔
66
      tColDataDeepClear(pCol);
4,828,499✔
67
    } else {
68
      pNew->aCol = taosArrayInit(20, POINTER_BYTES);
6✔
69
      if (pNew->aCol == NULL) {
6!
70
        code = terrno;
×
71
        taosMemoryFreeClear(*pData);
×
72
        return code;
×
73
      }
74
    }
75
  }
76

77
  return code;
1,006,336✔
78
}
79

80
int32_t qAppendStmtTableOutput(SQuery* pQuery, SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
5,826,255✔
81
                               SStbInterlaceInfo* pBuildInfo) {
82
  // merge according to vgId
83
  return insAppendStmtTableDataCxt(pAllVgHash, pTbData, pTbCtx, pBuildInfo);
5,826,255✔
84
}
85

86
int32_t qAppendStmt2TableOutput(SQuery* pQuery, SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
3,001,722✔
87
                                SStbInterlaceInfo* pBuildInfo, SVCreateTbReq* ctbReq) {
88
  // merge according to vgId
89
  return insAppendStmt2TableDataCxt(pAllVgHash, pTbData, pTbCtx, pBuildInfo, ctbReq);
3,001,722✔
90
}
91

92
int32_t qBuildStmtFinOutput(SQuery* pQuery, SHashObj* pAllVgHash, SArray* pVgDataBlocks) {
4,532,130✔
93
  int32_t             code = TSDB_CODE_SUCCESS;
4,532,130✔
94
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
4,532,130✔
95

96
  if (TSDB_CODE_SUCCESS == code) {
4,534,142✔
97
    code = insBuildVgDataBlocks(pAllVgHash, pVgDataBlocks, &pStmt->pDataBlocks, true);
4,533,873✔
98
  }
99

100
  if (pStmt->freeArrayFunc) {
4,534,603✔
101
    pStmt->freeArrayFunc(pVgDataBlocks);
4,533,928✔
102
  }
103
  return code;
4,534,334✔
104
}
105

106
/*
107
int32_t qBuildStmtOutputFromTbList(SQuery* pQuery, SHashObj* pVgHash, SArray* pBlockList, STableDataCxt* pTbCtx, int32_t
108
tbNum) { int32_t             code = TSDB_CODE_SUCCESS; SArray*             pVgDataBlocks = NULL; SVnodeModifyOpStmt*
109
pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
110

111
  // merge according to vgId
112
  if (tbNum > 0) {
113
    code = insMergeStmtTableDataCxt(pTbCtx, pBlockList, &pVgDataBlocks, true, tbNum);
114
  }
115

116
  if (TSDB_CODE_SUCCESS == code) {
117
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks);
118
  }
119

120
  if (pStmt->freeArrayFunc) {
121
    pStmt->freeArrayFunc(pVgDataBlocks);
122
  }
123
  return code;
124
}
125
*/
126

127
int32_t qBuildStmtOutput(SQuery* pQuery, SHashObj* pVgHash, SHashObj* pBlockHash) {
1,005,041✔
128
  int32_t             code = TSDB_CODE_SUCCESS;
1,005,041✔
129
  SArray*             pVgDataBlocks = NULL;
1,005,041✔
130
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
1,005,269✔
131

132
  // merge according to vgId
133
  if (taosHashGetSize(pBlockHash) > 0) {
1,006,217!
134
    code = insMergeTableDataCxt(pBlockHash, &pVgDataBlocks, true);
1,005,497✔
135
  }
136

137
  if (TSDB_CODE_SUCCESS == code) {
1,005,550!
138
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks, false);
1,005,664✔
139
  }
140

141
  if (pStmt->freeArrayFunc) {
1,005,990!
142
    pStmt->freeArrayFunc(pVgDataBlocks);
1,006,332✔
143
  }
144
  return code;
1,005,151✔
145
}
146

147
int32_t qBindStmtTagsValue(void* pBlock, void* boundTags, int64_t suid, const char* sTableName, char* tName,
6,821✔
148
                           TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen, void* charsetCxt) {
149
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
6,821✔
150
  SMsgBuf        pBuf = {.buf = msgBuf, .len = msgBufLen};
6,821✔
151
  int32_t        code = TSDB_CODE_SUCCESS;
6,821✔
152
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
6,821✔
153
  if (NULL == tags) {
6,821!
154
    return TSDB_CODE_APP_ERROR;
×
155
  }
156

157
  SArray* pTagArray = taosArrayInit(tags->numOfBound, sizeof(STagVal));
6,821✔
158
  if (!pTagArray) {
6,821!
159
    return buildInvalidOperationMsg(&pBuf, "out of memory");
×
160
  }
161

162
  SArray* tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
6,821✔
163
  if (!tagName) {
6,821!
164
    code = buildInvalidOperationMsg(&pBuf, "out of memory");
×
165
    goto end;
×
166
  }
167

168
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
6,821✔
169

170
  bool  isJson = false;
6,821✔
171
  STag* pTag = NULL;
6,821✔
172

173
  for (int c = 0; c < tags->numOfBound; ++c) {
26,139✔
174
    if (bind[c].is_null && bind[c].is_null[0]) {
19,318!
175
      continue;
×
176
    }
177

178
    SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
19,318✔
179
    int32_t  colLen = pTagSchema->bytes;
19,318✔
180
    if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
19,318!
181
      if (!bind[c].length) {
2,549!
182
        code = buildInvalidOperationMsg(&pBuf, "var tag length is null");
×
183
        goto end;
×
184
      }
185
      colLen = bind[c].length[0];
2,549✔
186
      if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
2,549!
187
        code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
×
188
        goto end;
×
189
      }
190
    }
191
    if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
38,636!
192
      code = terrno;
×
193
      goto end;
×
194
    }
195
    if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
19,318✔
196
      if (colLen > (TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
89!
197
        code = buildSyntaxErrMsg(&pBuf, "json string too long than 4095", bind[c].buffer);
×
198
        goto end;
×
199
      }
200

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

267
  if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) {
6,821!
268
    goto end;
×
269
  }
270

271
  if (NULL == pDataBlock->pData->pCreateTbReq) {
6,821!
272
    pDataBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
273
    if (NULL == pDataBlock->pData->pCreateTbReq) {
×
274
      code = terrno;
×
275
      goto end;
×
276
    }
277
  } else {
278
    SVCreateTbReq* tmp = pDataBlock->pData->pCreateTbReq;
6,821✔
279
    taosMemoryFreeClear(tmp->name);
6,821!
280
    taosMemoryFreeClear(tmp->ctb.pTag);
6,821!
281
    taosMemoryFreeClear(tmp->ctb.stbName);
6,821!
282
    taosArrayDestroy(tmp->ctb.tagName);
6,821✔
283
    tmp->ctb.tagName = NULL;
6,821✔
284
  }
285

286
  code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName,
6,821✔
287
                             pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
6,821✔
288
  pTag = NULL;
6,821✔
289

290
end:
6,821✔
291
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
26,130✔
292
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
19,309✔
293
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
19,309✔
294
      taosMemoryFreeClear(p->pData);
998!
295
    }
296
  }
297
  taosArrayDestroy(pTagArray);
6,821✔
298
  taosArrayDestroy(tagName);
6,821✔
299
  taosMemoryFree(pTag);
6,821!
300

301
  return code;
6,821✔
302
}
303

304
int32_t convertStmtNcharCol(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_MULTI_BIND* src, TAOS_MULTI_BIND* dst,
65,728,488✔
305
                            void* charsetCxt) {
306
  int32_t output = 0;
65,728,488✔
307
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
65,732,615✔
308
  if (dst->buffer_length < newBuflen) {
65,736,737✔
309
    dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
67,595,747!
310
    if (NULL == dst->buffer) {
66,890,702!
311
      return terrno;
×
312
    }
313
  }
314

315
  if (NULL == dst->length) {
65,028,252!
316
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
67,603,507!
317
    if (NULL == dst->length) {
68,828,924!
318
      taosMemoryFreeClear(dst->buffer);
×
319
      return terrno;
×
320
    }
321
  }
322

323
  dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
66,251,949✔
324

325
  for (int32_t i = 0; i < src->num; ++i) {
140,461,623✔
326
    if (src->is_null && src->is_null[i]) {
73,907,322✔
327
      continue;
3,531,110✔
328
    }
329

330
    if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
70,405,332!
331
                       (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output,
70,377,640✔
332
                       charsetCxt)) {
333
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
334
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
335
      }
336
      char buf[512] = {0};
×
337
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
338
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
339
    }
340

341
    dst->length[i] = output;
70,672,372✔
342
  }
343

344
  dst->buffer_type = src->buffer_type;
66,561,232✔
345
  dst->is_null = src->is_null;
66,562,265✔
346
  dst->num = src->num;
66,562,953✔
347

348
  return TSDB_CODE_SUCCESS;
66,555,390✔
349
}
350

351
int32_t qBindStmtStbColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
5,818,347✔
352
                              STSchema** pTSchema, SBindInfo* pBindInfos, void* charsetCxt) {
353
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
5,818,347✔
354
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
5,818,347✔
355
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
5,819,057✔
356
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
5,819,057✔
357
  int32_t          rowNum = bind->num;
5,819,057✔
358
  TAOS_MULTI_BIND  ncharBind = {0};
5,819,426✔
359
  TAOS_MULTI_BIND* pBind = NULL;
5,819,423✔
360
  int32_t          code = 0;
5,819,423✔
361
  int16_t          lastColId = -1;
5,819,423✔
362
  bool             colInOrder = true;
5,819,423✔
363

364
  if (NULL == pTSchema || NULL == *pTSchema) {
5,819,423!
365
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
32,137✔
366
  }
367

368
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
33,392,305✔
369
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
27,571,598✔
370
    if (pColSchema->colId <= lastColId) {
27,570,386!
371
      colInOrder = false;
×
372
    } else {
373
      lastColId = pColSchema->colId;
27,566,771✔
374
    }
375
    // SColData* pCol = taosArrayGet(pCols, c);
376

377
    if (bind[c].num != rowNum) {
27,572,010!
378
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
379
      goto _return;
×
380
    }
381

382
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
27,572,010!
383
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
27,573,089!
384
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
385
      goto _return;
×
386
    }
387

388
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
27,571,235✔
389
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
66,610✔
390
      if (code) {
66,610!
391
        goto _return;
×
392
      }
393
      pBind = &ncharBind;
66,610✔
394
    } else {
395
      pBind = bind + c;
27,505,434✔
396
    }
397

398
    pBindInfos[c].columnId = pColSchema->colId;
27,570,792✔
399
    pBindInfos[c].bind = pBind;
27,571,641✔
400
    pBindInfos[c].type = pColSchema->type;
27,570,866✔
401
  }
402

403
  code = tRowBuildFromBind(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols, &pDataBlock->ordered,
5,821,479✔
404
                           &pDataBlock->duplicateTs);
405

406
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
5,821,488!
407

408
_return:
5,821,488✔
409

410
  taosMemoryFree(ncharBind.buffer);
5,821,488!
411
  taosMemoryFree(ncharBind.length);
5,822,621!
412

413
  return code;
5,820,979✔
414
}
415

416
int32_t qBindStmtColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
115,288,945✔
417
                           void* charsetCxt) {
418
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
115,288,945✔
419
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
115,288,945✔
420
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
115,741,938✔
421
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
115,775,753✔
422
  int32_t          rowNum = bind->num;
115,798,628✔
423
  TAOS_MULTI_BIND  ncharBind = {0};
115,893,352✔
424
  TAOS_MULTI_BIND* pBind = NULL;
115,386,101✔
425
  int32_t          code = 0;
115,386,101✔
426

427
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
1,254,430,310✔
428
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
1,160,102,324✔
429
    SColData* pCol = taosArrayGet(pCols, c);
1,160,547,235✔
430

431
    if (bind[c].num != rowNum) {
1,135,898,822!
432
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
433
      goto _return;
×
434
    }
435

436
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
1,167,397,310✔
437
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
1,131,738,524!
438
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
439
      goto _return;
×
440
    }
441

442
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
1,168,636,303✔
443
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
67,308,976✔
444
      if (code) {
66,994,592!
445
        goto _return;
×
446
      }
447
      pBind = &ncharBind;
66,994,592✔
448
    } else {
449
      pBind = bind + c;
1,101,116,722✔
450
    }
451

452
    int32_t bytes = 0;
1,168,436,670✔
453
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
1,168,436,670!
454
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
125,540,573!
455
        bytes = pColSchema->bytes - BLOBSTR_HEADER_SIZE;
3,091✔
456
      } else {
457
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
134,659,462✔
458
      }
459
    } else {
460
      bytes = -1;
1,042,485,618✔
461
    }
462
    code = tColDataAddValueByBind(pCol, pBind, bytes, initCtxAsText, checkWKB);
1,168,311,541✔
463
    if (code) {
1,139,048,820✔
464
      goto _return;
4,611✔
465
    }
466
  }
467

468
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
93,861,993✔
469

470
_return:
93,866,601✔
471

472
  taosMemoryFree(ncharBind.buffer);
93,531,980!
473
  taosMemoryFree(ncharBind.length);
116,595,829!
474

475
  return code;
118,184,728✔
476
}
477

478
int32_t qBindUpdateStmtColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
15✔
479
                                 void* charsetCxt, SSHashObj* parsedCols) {
480
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
15✔
481
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
15✔
482
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
15✔
483
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
15✔
484
  int32_t          rowNum = bind->num;
15✔
485
  TAOS_MULTI_BIND  ncharBind = {0};
15✔
486
  TAOS_MULTI_BIND* pBind = NULL;
15✔
487
  int32_t          code = 0;
15✔
488
  int32_t          actualIndex = 0;
15✔
489
  int32_t          numOfBound = boundInfo->numOfBound + tSimpleHashGetSize(parsedCols);
15✔
490

491
  for (int c = 0; c < numOfBound; ++c) {
87✔
492
    if (tSimpleHashGet(parsedCols, &c, sizeof(c))) {
72✔
493
      continue;
12✔
494
    }
495
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[actualIndex]];
60✔
496
    SColData* pCol = taosArrayGet(pCols, actualIndex);
60✔
497

498
    if (bind[c].num != rowNum) {
60!
499
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
500
      goto _return;
×
501
    }
502

503
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
60!
504
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
60!
505
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
506
      goto _return;
×
507
    }
508

509
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
60!
510
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
×
511
      if (code) {
×
512
        goto _return;
×
513
      }
514
      pBind = &ncharBind;
×
515
    } else {
516
      pBind = bind + c;
60✔
517
    }
518

519
    int32_t bytes = 0;
60✔
520
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
60!
521
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
×
522
        bytes = pColSchema->bytes - BLOBSTR_HEADER_SIZE;
×
523
      } else {
524
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
×
525
      }
526
    } else {
527
      bytes = -1;
60✔
528
    }
529
    code = tColDataAddValueByBind(pCol, pBind, bytes, initCtxAsText, checkWKB);
60✔
530
    if (code) {
60!
531
      goto _return;
×
532
    }
533
    actualIndex++;
60✔
534
  }
535

536
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
15!
537

538
_return:
15✔
539

540
  taosMemoryFree(ncharBind.buffer);
15!
541
  taosMemoryFree(ncharBind.length);
15!
542

543
  return code;
15✔
544
}
545

546
int32_t qBindStmtSingleColValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
1,120✔
547
                                int32_t colIdx, int32_t rowNum, void* charsetCxt) {
548
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
1,120✔
549
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
1,120✔
550
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
1,120✔
551
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
1,120✔
552
  SSchema*         pColSchema = &pSchema[boundInfo->pColIndex[colIdx]];
1,120✔
553
  SColData*        pCol = taosArrayGet(pCols, colIdx);
1,120✔
554
  TAOS_MULTI_BIND  ncharBind = {0};
1,120✔
555
  TAOS_MULTI_BIND* pBind = NULL;
1,120✔
556
  int32_t          code = 0;
1,120✔
557

558
  if (bind->num != rowNum) {
1,120!
559
    return buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
560
  }
561

562
  // Column index exceeds the number of columns
563
  if (colIdx >= pCols->size && pCol == NULL) {
1,120!
564
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
565
  }
566

567
  if (bind->buffer_type != pColSchema->type) {
1,120!
568
    return buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
569
  }
570

571
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
1,120✔
572
    code = convertStmtNcharCol(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
80✔
573
    if (code) {
80!
574
      goto _return;
×
575
    }
576
    pBind = &ncharBind;
80✔
577
  } else {
578
    pBind = bind;
1,040✔
579
  }
580
  
581
  code = tColDataAddValueByBind(pCol, pBind,
1,120✔
582
                                IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1,
1,120!
583
                                initCtxAsText, checkWKB);
584

585
  parserDebug("stmt col %d bind %d rows data", colIdx, rowNum);
1,120!
586

587
_return:
1,120✔
588

589
  taosMemoryFree(ncharBind.buffer);
1,120!
590
  taosMemoryFree(ncharBind.length);
1,120!
591

592
  return code;
1,120✔
593
}
594

595
int32_t qBindStmtTagsValue2(void* pBlock, void* boundTags, int64_t suid, const char* sTableName, char* tName,
879✔
596
                            TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen, void* charsetCxt,
597
                            SVCreateTbReq* pCreateTbReq) {
598
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
879✔
599
  SMsgBuf        pBuf = {.buf = msgBuf, .len = msgBufLen};
879✔
600
  int32_t        code = TSDB_CODE_SUCCESS;
879✔
601
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
879✔
602
  if (NULL == tags) {
879!
603
    return buildInvalidOperationMsg(&pBuf, "tags is null");
×
604
  }
605

606
  SArray* pTagArray;
607
  if (tags->parseredTags) {
879✔
608
    pTagArray = taosArrayDup(tags->parseredTags->pTagVals, NULL);
72✔
609
  } else {
610
    pTagArray = taosArrayInit(tags->numOfBound, sizeof(STagVal));
807✔
611
  }
612
  if (!pTagArray) {
879!
613
    return buildInvalidOperationMsg(&pBuf, "out of memory");
×
614
  }
615

616
  SArray* tagName;
617
  if (tags->parseredTags) {
879✔
618
    tagName = taosArrayDup(tags->parseredTags->STagNames, NULL);
72✔
619
  } else {
620
    tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
807✔
621
  }
622

623
  if (!tagName) {
879!
624
    code = buildInvalidOperationMsg(&pBuf, "out of memory");
×
625
    goto end;
×
626
  }
627

628
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
879✔
629

630
  bool  isJson = false;
879✔
631
  STag* pTag = NULL;
879✔
632

633
  int bindIdx = 0;
879✔
634
  for (int c = 0; c < tags->numOfBound; ++c) {
3,144✔
635
    if (bind == NULL) {
2,331✔
636
      break;
63✔
637
    }
638
    if (tags->parseredTags) {
2,268✔
639
      bool found = false;
792✔
640
      for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
2,160✔
641
        if (tags->parseredTags->pTagIndex[k] == tags->pColIndex[c]) {
1,512✔
642
          found = true;
144✔
643
          break;
144✔
644
        }
645
      }
646
      if (found) {
792✔
647
        continue;
144✔
648
      }
649
    }
650

651
    TAOS_STMT2_BIND bindData = bind[bindIdx++];
2,124✔
652

653
    if (bindData.is_null && bindData.is_null[0]) {
2,124!
654
      continue;
×
655
    }
656

657
    SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
2,124✔
658
    int32_t  colLen = pTagSchema->bytes;
2,124✔
659
    if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
2,124!
660
      if (IS_STR_DATA_BLOB(pTagSchema->type)) {
900!
661
        return TSDB_CODE_BLOB_NOT_SUPPORT_TAG;
×
662
      }
663
      if (!bindData.length) {
900!
664
        code = buildInvalidOperationMsg(&pBuf, "var tag length is null");
×
665
        goto end;
3✔
666
      }
667
      colLen = bindData.length[0];
900✔
668
      if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
900!
669
        code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
×
670
        goto end;
×
671
      }
672
    }
673
    if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
4,248!
674
      code = terrno;
×
675
      goto end;
×
676
    }
677
    if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
2,124!
678
      if (colLen > (TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
×
679
        code = buildSyntaxErrMsg(&pBuf, "json string too long than 4095", bindData.buffer);
×
680
        goto end;
×
681
      }
682

683
      isJson = true;
×
684
      char* tmp = taosMemoryCalloc(1, colLen + 1);
×
685
      if (!tmp) {
×
686
        code = terrno;
×
687
        goto end;
×
688
      }
689
      memcpy(tmp, bindData.buffer, colLen);
×
690
      code = parseJsontoTagData(tmp, pTagArray, &pTag, &pBuf, charsetCxt);
×
691
      taosMemoryFree(tmp);
×
692
      if (code != TSDB_CODE_SUCCESS) {
×
693
        goto end;
×
694
      }
695
    } else {
696
      STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
2,124✔
697
      //      strcpy(val.colName, pTagSchema->name);
698
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
2,124!
699
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
1,443✔
700
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
774✔
701
          code = initCtxAsText();
93✔
702
          if (code) {
93!
703
            qError("geometry init failed:%s", tstrerror(code));
×
704
            goto end;
3✔
705
          }
706
          code = checkWKB(bindData.buffer, colLen);
93✔
707
          if (code) {
93✔
708
            qError("stmt2 bind invalid geometry tag:%s, must be WKB format", (char*)bindData.buffer);
3!
709
            goto end;
3✔
710
          }
711
        }
712
        val.pData = (uint8_t*)bindData.buffer;
771✔
713
        val.nData = colLen;
771✔
714
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
1,350✔
715
        int32_t output = 0;
126✔
716
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
126!
717
        if (p == NULL) {
126!
718
          code = terrno;
×
719
          goto end;
×
720
        }
721
        if (colLen != 0) {
126✔
722
          if (!taosMbsToUcs4(bindData.buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
120!
723
            if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
724
              taosMemoryFree(p);
×
725
              code = generateSyntaxErrMsg(&pBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pTagSchema->name);
×
726
              goto end;
×
727
            }
728
            char buf[512] = {0};
×
729
            snprintf(buf, tListLen(buf), " taosMbsToUcs4 error:%s", strerror(terrno));
×
730
            taosMemoryFree(p);
×
731
            code = buildSyntaxErrMsg(&pBuf, buf, bindData.buffer);
×
732
            goto end;
×
733
          }
734
        }
735
        val.pData = p;
126✔
736
        val.nData = output;
126✔
737

738
      } else {
739
        memcpy(&val.i64, bindData.buffer, colLen);
1,224!
740
      }
741
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
2,121!
742
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
743
        goto end;
×
744
      }
745
      if (NULL == taosArrayPush(pTagArray, &val)) {
2,121!
746
        code = terrno;
×
747
        goto end;
×
748
      }
749
    }
750
  }
751

752
  if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) {
876!
753
    goto end;
×
754
  }
755

756
  if (pCreateTbReq) {
876✔
757
    code = insBuildCreateTbReq(pCreateTbReq, tName, pTag, suid, sTableName, tagName,
363✔
758
                               pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
363✔
759
    pTag = NULL;
363✔
760
    goto end;
363✔
761
  }
762

763
  if (NULL == pDataBlock->pData->pCreateTbReq) {
513✔
764
    pDataBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
48!
765
    if (NULL == pDataBlock->pData->pCreateTbReq) {
48!
766
      code = terrno;
×
767
      goto end;
×
768
    }
769
  } else {
770
    SVCreateTbReq* tmp = pDataBlock->pData->pCreateTbReq;
465✔
771
    taosMemoryFreeClear(tmp->name);
465!
772
    taosMemoryFreeClear(tmp->ctb.pTag);
465!
773
    taosMemoryFreeClear(tmp->ctb.stbName);
465!
774
    taosArrayDestroy(tmp->ctb.tagName);
465✔
775
    tmp->ctb.tagName = NULL;
465✔
776
  }
777

778
  code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName,
513✔
779
                             pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
513✔
780
  pTag = NULL;
513✔
781

782
end:
879✔
783
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
3,144✔
784
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
2,265✔
785
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
2,265✔
786
      taosMemoryFreeClear(p->pData);
126!
787
    }
788
  }
789
  taosArrayDestroy(pTagArray);
879✔
790
  taosArrayDestroy(tagName);
879✔
791
  taosMemoryFree(pTag);
879!
792

793
  return code;
879✔
794
}
795

796
static int32_t convertStmtStbNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst,
1,272✔
797
                                       void* charsetCxt) {
798
  int32_t       output = 0;
1,272✔
799
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
1,272✔
800

801
  dst->buffer = taosMemoryCalloc(src->num, max_buf_len);
1,272!
802
  if (NULL == dst->buffer) {
1,272!
803
    return terrno;
×
804
  }
805

806
  dst->length = taosMemoryCalloc(src->num, sizeof(int32_t));
1,272!
807
  if (NULL == dst->length) {
1,272!
808
    taosMemoryFreeClear(dst->buffer);
×
809
    return terrno;
×
810
  }
811

812
  char* src_buf = src->buffer;
1,272✔
813
  char* dst_buf = dst->buffer;
1,272✔
814
  for (int32_t i = 0; i < src->num; ++i) {
1,198,386✔
815
    if (src->is_null && src->is_null[i]) {
1,197,114✔
816
      continue;
3✔
817
    }
818

819
    if (src->length[i] == 0) {
1,197,111✔
820
      dst->length[i] = 0;
3✔
821
      continue;
3✔
822
    }
823

824
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
1,197,108!
825
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
826
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
827
      }
828
      char buf[512] = {0};
×
829
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
830
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
831
    }
832

833
    dst->length[i] = output;
1,197,108✔
834
    src_buf += src->length[i];
1,197,108✔
835
    dst_buf += output;
1,197,108✔
836
  }
837

838
  dst->buffer_type = src->buffer_type;
1,272✔
839
  dst->is_null = src->is_null;
1,272✔
840
  dst->num = src->num;
1,272✔
841

842
  return TSDB_CODE_SUCCESS;
1,272✔
843
}
844

845
int32_t qBindStmtStbColsValue2(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
2,997,313✔
846
                               int32_t msgBufLen, STSchema** pTSchema, SBindInfo2* pBindInfos, void* charsetCxt,
847
                               SBlobSet** ppBlob) {
848
  STableDataCxt*  pDataBlock = (STableDataCxt*)pBlock;
2,997,313✔
849
  SSchema*        pSchema = getTableColumnSchema(pDataBlock->pMeta);
2,997,313✔
850
  SSchemaExt*     pSchemaExt = getTableColumnExtSchema(pDataBlock->pMeta);
2,998,507✔
851
  SBoundColInfo*  boundInfo = &pDataBlock->boundColsInfo;
3,002,128✔
852
  SMsgBuf         pBuf = {.buf = msgBuf, .len = msgBufLen};
3,002,528✔
853
  int32_t         rowNum = bind->num;
3,002,528✔
854
  SArray*         ncharBinds = NULL;
3,002,528✔
855
  TAOS_STMT2_BIND ncharBind = {0};
3,002,528✔
856
  int32_t         code = 0;
3,002,528✔
857
  int16_t         lastColId = -1;
3,002,528✔
858
  bool            colInOrder = true;
3,002,528✔
859
  int             ncharColNums = 0;
3,002,528✔
860
  int32_t         bindIdx = 0;
3,002,528✔
861
  int8_t          hasBlob = 0;
3,002,528✔
862
  int32_t         lino = 0;
3,002,528✔
863
  if (NULL == pTSchema || NULL == *pTSchema) {
3,002,134✔
864
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
32,952✔
865
  }
866

867
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
48,320,300✔
868
    if (TSDB_DATA_TYPE_NCHAR == pSchema[boundInfo->pColIndex[c]].type) {
45,349,256✔
869
      ncharColNums++;
1,272✔
870
    }
871
  }
872
  if (ncharColNums > 0) {
3,002,443✔
873
    ncharBinds = taosArrayInit(ncharColNums, sizeof(ncharBind));
1,272✔
874
    if (!ncharBinds) {
1,272!
875
      code = terrno;
×
876
      TAOS_CHECK_GOTO(code, &lino, _return);
×
877
    }
878
  }
879

880
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
48,385,156✔
881
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
45,379,407✔
882
    if (pColSchema->colId <= lastColId) {
45,378,998✔
883
      colInOrder = false;
21✔
884
    } else {
885
      lastColId = pColSchema->colId;
45,385,434✔
886
    }
887

888
    if (parsedCols) {
45,388,664✔
889
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
396✔
890
      if (pParsedVal) {
396✔
891
        pBindInfos[c].columnId = pColSchema->colId;
72✔
892
        pBindInfos[c].type = pColSchema->type;
72✔
893
        pBindInfos[c].bytes = pColSchema->bytes;
72✔
894
        continue;
72✔
895
      }
896
    }
897

898
    TAOS_STMT2_BIND bindData = bind[bindIdx];
45,388,592✔
899

900
    if (bindData.num != rowNum) {
45,373,284!
901
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
902
      goto _return;
×
903
    }
904

905
    if ((!(rowNum == 1 && bindData.is_null && *bindData.is_null)) &&
45,373,284✔
906
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
45,364,497!
907
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
908
      goto _return;
×
909
    }
910

911
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
45,385,783✔
912
      code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
1,272✔
913
      TAOS_CHECK_GOTO(code, &lino, _return);
1,272!
914

915
      if (!taosArrayPush(ncharBinds, &ncharBind)) {
1,272!
916
        code = terrno;
×
917
        goto _return;
×
918
      }
919
      pBindInfos[c].bind = taosArrayGetLast(ncharBinds);
1,272✔
920
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
45,378,866✔
921
      code = initCtxAsText();
39✔
922
      if (code) {
39!
923
        parserError("stmt2 bind geometry init failed, ErrCode: 0x%x", code);
×
924
        goto _return;
×
925
      }
926
      uint8_t* buf = bindData.buffer;
39✔
927
      for (int j = 0; j < bindData.num; j++) {
87✔
928
        if (bindData.is_null && bindData.is_null[j]) {
48✔
929
          continue;
3✔
930
        }
931
        code = checkWKB(buf, bindData.length[j]);
45✔
932
        if (code) {
45!
933
          parserError("stmt2 interlace mode geometry data col:%d, row:%d must be in WKB format", c, j);
×
934
          goto _return;
×
935
        }
936
        buf += bindData.length[j];
45✔
937
      }
938
      pBindInfos[c].bind = bind + bindIdx;
39✔
939
    } else {
940
      if (IS_STR_DATA_BLOB(pColSchema->type)) hasBlob = 1;
45,369,167✔
941
      pBindInfos[c].bind = bind + bindIdx;
45,369,152✔
942
    }
943

944
    pBindInfos[c].columnId = pColSchema->colId;
45,361,630✔
945
    pBindInfos[c].type = pColSchema->type;
45,370,072✔
946
    pBindInfos[c].bytes = pColSchema->bytes;
45,382,559✔
947

948
    bindIdx++;
45,383,362✔
949
  }
950

951
  if (hasBlob == 0) {
3,002,528!
952
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pSchemaExt, pCols,
3,002,528✔
953
                              &pDataBlock->ordered, &pDataBlock->duplicateTs);
954
    TAOS_CHECK_GOTO(code, &lino, _return);
3,000,119✔
955
  } else {
956
    code = tBlobSetCreate(1024, 1, ppBlob);
×
957
    TAOS_CHECK_GOTO(code, &lino, _return);
×
958

959
    code = tRowBuildFromBind2WithBlob(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols,
×
960
                                      &pDataBlock->ordered, &pDataBlock->duplicateTs, *ppBlob);
961
    TAOS_CHECK_GOTO(code, &lino, _return);
3!
962
  }
963

964
  parserDebug("stmt2 all %d columns bind %d rows data as row format", boundInfo->numOfBound, rowNum);
2,997,716!
965

966
_return:
2,997,716✔
967
  if (ncharBinds) {
2,998,122✔
968
    for (int i = 0; i < TARRAY_SIZE(ncharBinds); ++i) {
2,544✔
969
      TAOS_STMT2_BIND* ncBind = TARRAY_DATA(ncharBinds);
1,272✔
970
      taosMemoryFree(ncBind[i].buffer);
1,272!
971
      taosMemoryFree(ncBind[i].length);
1,272!
972
    }
973
    taosArrayDestroy(ncharBinds);
1,272✔
974
  }
975
  if (code != 0) {
2,998,122✔
976
    parserError("stmt2 failed to bind stb col at lino %d since %s", lino, tstrerror(code));
6!
977
  }
978

979
  return code;
2,998,122✔
980
}
981

982
static int32_t convertStmtNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst,
66✔
983
                                    void* charsetCxt) {
984
  int32_t       output = 0;
66✔
985
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
66✔
986

987
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
66✔
988
  // if (dst->buffer_length < newBuflen) {
989
  dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
66!
990
  if (NULL == dst->buffer) {
66!
991
    return terrno;
×
992
  }
993
  //}
994

995
  if (NULL == dst->length) {
66✔
996
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
51!
997
    if (NULL == dst->length) {
51!
998
      taosMemoryFreeClear(dst->buffer);
×
999
      return terrno;
×
1000
    }
1001
  }
1002

1003
  // dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
1004
  char* src_buf = src->buffer;
66✔
1005
  char* dst_buf = dst->buffer;
66✔
1006
  for (int32_t i = 0; i < src->num; ++i) {
297✔
1007
    if (src->is_null && src->is_null[i]) {
231✔
1008
      continue;
3✔
1009
    }
1010

1011
    if (src->length[i] == 0) {
228✔
1012
      dst->length[i] = 0;
33✔
1013
      continue;
33✔
1014
    }
1015

1016
    /*if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
1017
      (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output)) {*/
1018
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
195!
1019
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
1020
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
1021
      }
1022
      char buf[512] = {0};
×
1023
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
1024
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
1025
    }
1026

1027
    dst->length[i] = output;
195✔
1028
    src_buf += src->length[i];
195✔
1029
    dst_buf += output;
195✔
1030
  }
1031

1032
  dst->buffer_type = src->buffer_type;
66✔
1033
  dst->is_null = src->is_null;
66✔
1034
  dst->num = src->num;
66✔
1035

1036
  return TSDB_CODE_SUCCESS;
66✔
1037
}
1038

1039
int32_t qBindStmtColsValue2(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
576✔
1040
                            int32_t msgBufLen, void* charsetCxt) {
1041
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
576✔
1042
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
576✔
1043
  SSchemaExt*      pExtSchema = getTableColumnExtSchema(pDataBlock->pMeta);
576✔
1044
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
576✔
1045
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
576✔
1046
  int32_t          rowNum = bind->num;
576✔
1047
  TAOS_STMT2_BIND  ncharBind = {0};
576✔
1048
  TAOS_STMT2_BIND* pBind = NULL;
576✔
1049
  int32_t          code = 0;
576✔
1050
  int32_t          lino = 0;
576✔
1051
  int32_t          bindIdx = 0;
576✔
1052

1053
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
2,448✔
1054
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
1,872✔
1055
    SColData* pCol = taosArrayGet(pCols, c);
1,872✔
1056
    if (pCol == NULL || pColSchema == NULL) {
1,872!
1057
      code = buildInvalidOperationMsg(&pBuf, "get column schema or column data failed");
×
1058
      goto _return;
×
1059
    }
1060

1061
    if (boundInfo->pColIndex[c] == 0) {
1,872✔
1062
      pCol->cflag |= COL_IS_KEY;
576✔
1063
    }
1064

1065
    if (parsedCols) {
1,872✔
1066
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
396✔
1067
      if (pParsedVal) {
396✔
1068
        for (int row = 0; row < rowNum; row++) {
144✔
1069
          code = tColDataAppendValue(pCol, pParsedVal);
72✔
1070
          if (code) {
72!
1071
            parserError("stmt2 failed to add fixed value col:%d ,type:%d, ErrCode: 0x%x", c, pColSchema->type, code);
×
1072
            goto _return;
×
1073
          }
1074
        }
1075
        continue;
72✔
1076
      }
1077
    }
1078
    TAOS_STMT2_BIND bindData = bind[bindIdx];
1,800✔
1079
    if (bindData.num != rowNum) {
1,800!
1080
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1081
      goto _return;
×
1082
    }
1083

1084
    if ((!(rowNum == 1 && bindData.is_null && *bindData.is_null)) &&
1,800!
1085
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
1,794!
1086
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
1087
      goto _return;
×
1088
    }
1089

1090
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
1,800✔
1091
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
60✔
1092
      if (code) {
60!
1093
        goto _return;
×
1094
      }
1095
      pBind = &ncharBind;
60✔
1096
    } else {
1097
      pBind = bind + bindIdx;
1,740✔
1098
    }
1099
    int8_t  isBlob = 0;
1,800✔
1100
    int32_t bytes = -1;
1,800✔
1101
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
1,800!
1102
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
423!
1103
        isBlob = 1;
×
1104
        bytes = TSDB_MAX_BLOB_LEN;
×
1105
      } else {
1106
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
423✔
1107
      }
1108
    }
1109

1110
    if (pColSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
1,800✔
1111
      code = tColDataAddValueByBind2WithGeos(pCol, pBind, bytes, initCtxAsText, checkWKB);
45✔
1112
    } else if (isBlob == 1) {
1,755!
1113
      if (pDataBlock->pData->pBlobSet == NULL) {
×
1114
        code = tBlobSetCreate(1024, 1, &pDataBlock->pData->pBlobSet);
×
1115
        if (code) {
×
1116
          parserError("stmt2 failed to create blob set, ErrCode: 0x%x", code);
×
1117
          goto _return;
×
1118
        }
1119
      }
1120
      code = tColDataAddValueByBind2WithBlob(pCol, pBind, bytes, pDataBlock->pData->pBlobSet);
×
1121
    } else if (IS_DECIMAL_TYPE(pColSchema->type)) {
1,779✔
1122
      if (pExtSchema == NULL) {
24!
1123
        code = buildInvalidOperationMsg(&pBuf, "decimal column ext schema is null");
×
1124
        goto _return;
×
1125
      }
1126
      uint8_t precision = 0, scale = 0;
24✔
1127
      decimalFromTypeMod(pExtSchema[c].typeMod, &precision, &scale);
24✔
1128
      code = tColDataAddValueByBind2WithDecimal(pCol, pBind, bytes, precision, scale);
24✔
1129
    } else {
1130
      code = tColDataAddValueByBind2(pCol, pBind, bytes);
1,731✔
1131
    }
1132

1133
    if (code) {
1,800!
1134
      parserError("stmt2 failed to add col:%d ,type:%d, ErrCode: 0x%x", c, pColSchema->type, code);
×
1135
      goto _return;
×
1136
    }
1137
    bindIdx++;
1,800✔
1138
  }
1139

1140
  parserDebug("stmt2 all %d columns bind %d rows data as col format", boundInfo->numOfBound, rowNum);
576!
1141

1142
_return:
576✔
1143

1144
  taosMemoryFree(ncharBind.buffer);
576!
1145
  taosMemoryFree(ncharBind.length);
576!
1146

1147
  return code;
576✔
1148
}
1149

1150
int32_t qBindStmtSingleColValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen,
18✔
1151
                                 int32_t colIdx, int32_t rowNum, void* charsetCxt) {
1152
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
18✔
1153
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
18✔
1154
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
18✔
1155
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
18✔
1156
  SSchema*         pColSchema = &pSchema[boundInfo->pColIndex[colIdx]];
18✔
1157
  SColData*        pCol = taosArrayGet(pCols, colIdx);
18✔
1158
  TAOS_STMT2_BIND  ncharBind = {0};
18✔
1159
  TAOS_STMT2_BIND* pBind = NULL;
18✔
1160
  int32_t          code = 0;
18✔
1161
  int32_t          lino = 0;
18✔
1162

1163
  if (bind->num != rowNum) {
18!
1164
    return buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1165
  }
1166

1167
  // Column index exceeds the number of columns
1168
  if (colIdx >= pCols->size && pCol == NULL) {
18!
1169
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
1170
  }
1171

1172
  if (bind->buffer_type != pColSchema->type) {
18!
1173
    return buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
1174
  }
1175

1176
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
18!
1177
    code = convertStmtNcharCol2(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
×
1178
    if (code) {
×
1179
      goto _return;
×
1180
    }
1181
    pBind = &ncharBind;
×
1182
  } else {
1183
    pBind = bind;
18✔
1184
  }
1185

1186
  int32_t bytes = -1;
18✔
1187
  int8_t  hasBlob = 0;
18✔
1188
  if (IS_VAR_DATA_TYPE(pColSchema->type)) {
18!
1189
    if (IS_STR_DATA_BLOB(pColSchema->type)) {
9!
1190
      bytes = TSDB_MAX_BLOB_LEN;
×
1191
      hasBlob = 1;
×
1192
    } else {
1193
      bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
9✔
1194
    }
1195
  } else {
1196
    bytes = -1;
9✔
1197
  }
1198

1199
  if (hasBlob) {
18!
1200
    if (pDataBlock->pData->pBlobSet == NULL) {
×
1201
      code = tBlobSetCreate(1024, 1, &pDataBlock->pData->pBlobSet);
×
1202
      if (code) {
×
1203
        parserError("stmt2 failed to create blob set, ErrCode: 0x%x", code);
×
1204
        goto _return;
×
1205
      }
1206
    }
1207
    code = tColDataAddValueByBind2WithBlob(pCol, pBind, bytes, pDataBlock->pData->pBlobSet);
×
1208
  } else if (pColSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
18!
1209
    code = tColDataAddValueByBind2WithGeos(pCol, pBind, bytes, initCtxAsText, checkWKB);
×
1210
  } else if (IS_DECIMAL_TYPE(pColSchema->type)) {
18!
1211
    SSchemaExt* pExtSchema = getTableColumnExtSchema(pDataBlock->pMeta);
×
1212
    if (pExtSchema == NULL) {
×
1213
      code = buildInvalidOperationMsg(&pBuf, "decimal column ext schema is null");
×
1214
      goto _return;
×
1215
    }
1216
    uint8_t precision = 0, scale = 0;
×
1217
    decimalFromTypeMod(pExtSchema->typeMod, &precision, &scale);
×
1218
    code = tColDataAddValueByBind2WithDecimal(pCol, pBind, bytes, precision, scale);
×
1219
  } else {
1220
    code = tColDataAddValueByBind2(pCol, pBind, bytes);
18✔
1221
  }
1222

1223
  if (code) {
18!
1224
    parserError("stmt2 failed to add single col:%d ,type:%d, ErrCode: 0x%x", colIdx, pColSchema->type, code);
×
1225
    goto _return;
×
1226
  }
1227

1228
  parserDebug("stmt col %d bind %d rows data", colIdx, rowNum);
18!
1229

1230
_return:
18✔
1231

1232
  taosMemoryFree(ncharBind.buffer);
18!
1233
  taosMemoryFree(ncharBind.length);
18!
1234

1235
  return code;
18✔
1236
}
1237

1238
int32_t qBindStmt2RowValue(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
6✔
1239
                           int32_t msgBufLen, STSchema** pTSchema, SBindInfo2* pBindInfos, void* charsetCxt) {
1240
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
6✔
1241
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
6✔
1242
  SSchemaExt*      pSchemaExt = getTableColumnExtSchema(pDataBlock->pMeta);
6✔
1243
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
6✔
1244
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
6✔
1245
  int32_t          rowNum = bind->num;
6✔
1246
  TAOS_STMT2_BIND  ncharBind = {0};
6✔
1247
  TAOS_STMT2_BIND* pBind = NULL;
6✔
1248
  int32_t          code = 0;
6✔
1249
  int16_t          lastColId = -1;
6✔
1250
  bool             colInOrder = true;
6✔
1251
  int8_t           hasBlob = 0;
6✔
1252

1253
  if (NULL == pTSchema || NULL == *pTSchema) {
6!
1254
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
3✔
1255
  }
1256

1257
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
72✔
1258
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
66✔
1259
    if (pColSchema->colId <= lastColId) {
66!
1260
      colInOrder = false;
×
1261
    } else {
1262
      lastColId = pColSchema->colId;
66✔
1263
    }
1264

1265
    if (bind[c].num != rowNum) {
66!
1266
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1267
      goto _return;
×
1268
    }
1269

1270
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
66!
1271
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
66!
1272
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
1273
      goto _return;
×
1274
    }
1275

1276
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
66✔
1277
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
6✔
1278
      if (code) {
6!
1279
        goto _return;
×
1280
      }
1281
      pBindInfos[c].bind = &ncharBind;
6✔
1282
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
60✔
1283
      code = initCtxAsText();
6✔
1284
      if (code) {
6!
1285
        qError("geometry init failed:%s", tstrerror(code));
×
1286
        goto _return;
×
1287
      }
1288
      uint8_t* buf = bind[c].buffer;
6✔
1289
      for (int j = 0; j < bind[c].num; j++) {
12✔
1290
        if (bind[c].is_null && bind[c].is_null[j]) {
6!
1291
          continue;
×
1292
        }
1293
        code = checkWKB(buf, bind[c].length[j]);
6✔
1294
        if (code) {
6!
1295
          qError("stmt2 row bind geometry data[%d]:{%s},length:%d must be in WKB format", c, buf, bind[c].length[j]);
×
1296
          goto _return;
×
1297
        }
1298
        buf += bind[c].length[j];
6✔
1299
      }
1300
      pBindInfos[c].bind = bind + c;
6✔
1301
    } else {
1302
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
54!
1303
        hasBlob = 1;
×
1304
      }
1305
      pBindInfos[c].bind = bind + c;
54✔
1306
    }
1307

1308
    pBindInfos[c].columnId = pColSchema->colId;
66✔
1309
    pBindInfos[c].type = pColSchema->type;
66✔
1310
    pBindInfos[c].bytes = pColSchema->bytes;
66✔
1311

1312
    if (code) {
66!
1313
      goto _return;
×
1314
    }
1315
  }
1316

1317
  pDataBlock->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
6✔
1318
  if (pDataBlock->pData->pCreateTbReq != NULL) {
6!
1319
    pDataBlock->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
6✔
1320
  }
1321

1322
  if (hasBlob == 0) {
6!
1323
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pSchemaExt, pCols,
6✔
1324
                              &pDataBlock->ordered, &pDataBlock->duplicateTs);
1325
  } else {
1326
    code = TSDB_CODE_BLOB_NOT_SUPPORT;
×
1327
  }
1328
  qDebug("stmt2 all %d columns bind %d rows data as row format", boundInfo->numOfBound, rowNum);
6!
1329

1330
_return:
6✔
1331

1332
  taosMemoryFree(ncharBind.buffer);
6!
1333
  taosMemoryFree(ncharBind.length);
6!
1334

1335
  return code;
6✔
1336
}
1337

1338
int32_t buildBoundFields(int32_t numOfBound, int16_t* boundColumns, SSchema* pSchema, int32_t* fieldNum,
21✔
1339
                         TAOS_FIELD_E** fields, uint8_t timePrec) {
1340
  if (fields != NULL) {
21✔
1341
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
18!
1342
    if (NULL == *fields) {
18!
1343
      return terrno;
×
1344
    }
1345

1346
    SSchema* schema = &pSchema[boundColumns[0]];
18✔
1347
    if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
18✔
1348
      (*fields)[0].precision = timePrec;
15✔
1349
    }
1350

1351
    for (int32_t i = 0; i < numOfBound; ++i) {
84✔
1352
      schema = &pSchema[boundColumns[i]];
66✔
1353
      tstrncpy((*fields)[i].name, schema->name, 65);
66!
1354
      (*fields)[i].type = schema->type;
66✔
1355
      (*fields)[i].bytes = calcTypeBytesFromSchemaBytes(schema->type, schema->bytes, true);
66✔
1356
    }
1357
  }
1358

1359
  *fieldNum = numOfBound;
21✔
1360

1361
  return TSDB_CODE_SUCCESS;
21✔
1362
}
1363

1364
int32_t buildStbBoundFields(SBoundColInfo boundColsInfo, SSchema* pSchema, int32_t* fieldNum, TAOS_FIELD_ALL** fields,
173✔
1365
                            STableMeta* pMeta, void* boundTags, SSHashObj* parsedCols, uint8_t tbNameFlag) {
1366
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
173✔
1367
  bool           hastag = (tags != NULL) && !(tbNameFlag & IS_FIXED_TAG);
173!
1368
  bool           hasPreBindTbname =
173✔
1369
      (tbNameFlag & IS_FIXED_VALUE) == 0 && ((tbNameFlag & USING_CLAUSE) != 0 || pMeta->tableType == TSDB_NORMAL_TABLE);
173!
1370
  int32_t numOfBound = boundColsInfo.numOfBound + (hasPreBindTbname ? 1 : 0);
173✔
1371
  if (hastag) {
173✔
1372
    numOfBound += tags->mixTagsCols ? 0 : tags->numOfBound;
119!
1373
  }
1374

1375
  // Adjust the number of bound fields if there are parsed tags or parsed columns
1376
  if (tags->parseredTags) {
173✔
1377
    numOfBound -= tags->parseredTags->numOfTags;
6✔
1378
  }
1379
  if (parsedCols) {
173✔
1380
    numOfBound -= tSimpleHashGetSize(parsedCols);
12✔
1381
  }
1382

1383
  int32_t idx = 0;
173✔
1384
  if (fields != NULL) {
173!
1385
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_ALL));
173!
1386
    if (NULL == *fields) {
173!
1387
      return terrno;
×
1388
    }
1389

1390
    if (hasPreBindTbname) {
173✔
1391
      (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
113✔
1392
      tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
113!
1393
      (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
113✔
1394
      (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
113✔
1395
      idx++;
113✔
1396
    }
1397

1398
    if (hastag && tags->numOfBound > 0 && !tags->mixTagsCols) {
173!
1399
      SSchema* tagSchema = getTableTagSchema(pMeta);
62✔
1400

1401
      for (int32_t i = 0; i < tags->numOfBound; ++i) {
219✔
1402
        SSchema* schema = &tagSchema[tags->pColIndex[i]];
157✔
1403

1404
        if (tags->parseredTags && tags->parseredTags->numOfTags > 0) {
157!
1405
          int32_t tag_idx = schema->colId - 1 - pMeta->tableInfo.numOfColumns;
6✔
1406
          bool    found = false;
6✔
1407
          for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
9✔
1408
            if (tags->parseredTags->pTagIndex[k] == tag_idx) {
6✔
1409
              found = true;
3✔
1410
              break;
3✔
1411
            }
1412
          }
1413
          if (found) {
6✔
1414
            continue;
3✔
1415
          }
1416
        }
1417

1418
        (*fields)[idx].field_type = TAOS_FIELD_TAG;
154✔
1419

1420
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[i].name));
154!
1421
        (*fields)[idx].type = schema->type;
154✔
1422
        (*fields)[idx].bytes = schema->bytes;
154✔
1423
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
154✔
1424
          (*fields)[idx].precision = pMeta->tableInfo.precision;
3✔
1425
        }
1426
        idx++;
154✔
1427
      }
1428
    }
1429

1430
    if (boundColsInfo.numOfBound > 0) {
173!
1431
      SSchema* schema = &pSchema[boundColsInfo.pColIndex[0]];
173✔
1432

1433
      for (int32_t i = 0; i < boundColsInfo.numOfBound; ++i) {
802✔
1434
        int16_t idxCol = boundColsInfo.pColIndex[i];
629✔
1435

1436
        if (idxCol == pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags) {
629✔
1437
          (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
33✔
1438
          tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
33!
1439
          (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
33✔
1440
          (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
33✔
1441

1442
          idx++;
33✔
1443
          continue;
33✔
1444
        } else if (idxCol < pMeta->tableInfo.numOfColumns) {
596✔
1445
          if (parsedCols && tSimpleHashGet(parsedCols, &pSchema[idxCol].colId, sizeof(int16_t))) {
500✔
1446
            continue;
15✔
1447
          }
1448
          (*fields)[idx].field_type = TAOS_FIELD_COL;
485✔
1449
        } else {
1450
          if (tags->parseredTags && tags->parseredTags->numOfTags > 0) {
96!
1451
            int32_t tag_idx = idxCol - pMeta->tableInfo.numOfColumns;
6✔
1452
            bool    found = false;
6✔
1453
            for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
9✔
1454
              if (tags->parseredTags->pTagIndex[k] == tag_idx) {
6✔
1455
                found = true;
3✔
1456
                break;
3✔
1457
              }
1458
            }
1459
            if (found) {
6✔
1460
              continue;
3✔
1461
            }
1462
          }
1463
          (*fields)[idx].field_type = TAOS_FIELD_TAG;
93✔
1464
        }
1465

1466
        schema = &pSchema[idxCol];
578✔
1467
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[idx].name));
578!
1468
        (*fields)[idx].type = schema->type;
578✔
1469
        (*fields)[idx].bytes = schema->bytes;
578✔
1470
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
578✔
1471
          (*fields)[idx].precision = pMeta->tableInfo.precision;
170✔
1472
        }
1473
        if (TSDB_DATA_TYPE_DECIMAL64 == schema->type || TSDB_DATA_TYPE_DECIMAL == schema->type) {
578✔
1474
          decimalFromTypeMod(pMeta->schemaExt[idxCol].typeMod, &(*fields)[idx].precision, &(*fields)[idx].scale);
18✔
1475
        }
1476
        idx++;
578✔
1477
      }
1478
    }
1479
  }
1480

1481
  *fieldNum = numOfBound;
173✔
1482

1483
  return TSDB_CODE_SUCCESS;
173✔
1484
}
1485

1486
int32_t qBuildStmtTagFields(void* pBlock, void* boundTags, int32_t* fieldNum, TAOS_FIELD_E** fields) {
3✔
1487
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
3✔
1488
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
3✔
1489
  if (NULL == tags) {
3!
1490
    return TSDB_CODE_APP_ERROR;
×
1491
  }
1492

1493
  if (pDataBlock->pMeta->tableType != TSDB_SUPER_TABLE && pDataBlock->pMeta->tableType != TSDB_CHILD_TABLE) {
3!
1494
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1495
  }
1496

1497
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
3✔
1498
  if (tags->numOfBound <= 0) {
3!
1499
    *fieldNum = 0;
×
1500
    *fields = NULL;
×
1501

1502
    return TSDB_CODE_SUCCESS;
×
1503
  }
1504

1505
  CHECK_CODE(buildBoundFields(tags->numOfBound, tags->pColIndex, pSchema, fieldNum, fields, 0));
3!
1506

1507
  return TSDB_CODE_SUCCESS;
3✔
1508
}
1509

1510
int32_t qBuildStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields) {
18✔
1511
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
18✔
1512
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
18✔
1513
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
18!
1514
    *fieldNum = 0;
×
1515
    if (fields != NULL) {
×
1516
      *fields = NULL;
×
1517
    }
1518

1519
    return TSDB_CODE_SUCCESS;
×
1520
  }
1521

1522
  CHECK_CODE(buildBoundFields(pDataBlock->boundColsInfo.numOfBound, pDataBlock->boundColsInfo.pColIndex, pSchema,
18!
1523
                              fieldNum, fields, pDataBlock->pMeta->tableInfo.precision));
1524

1525
  return TSDB_CODE_SUCCESS;
18✔
1526
}
1527

1528
int32_t qBuildUpdateStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields, SSHashObj* parsedCols) {
15✔
1529
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
15✔
1530
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
15✔
1531
  int32_t        numOfBound = pDataBlock->boundColsInfo.numOfBound + tSimpleHashGetSize(parsedCols);
15✔
1532
  if (numOfBound <= 0) {
15!
1533
    *fieldNum = 0;
×
1534
    if (fields != NULL) {
×
1535
      *fields = NULL;
×
1536
    }
1537

1538
    return TSDB_CODE_SUCCESS;
×
1539
  }
1540

1541
  if (fields != NULL) {
15!
1542
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
15!
1543
    if (NULL == *fields) {
15!
1544
      return terrno;
×
1545
    }
1546

1547
    int32_t actualIdx = 0;
15✔
1548
    for (int32_t i = 0; i < numOfBound; ++i) {
87✔
1549
      SSchema* schema;
1550
      int32_t* idx = (int32_t*)tSimpleHashGet(parsedCols, &i, sizeof(int32_t));
72✔
1551
      if (idx) {
72✔
1552
        schema = &pSchema[*idx];
12✔
1553
      } else {
1554
        schema = &pSchema[pDataBlock->boundColsInfo.pColIndex[actualIdx++]];
60✔
1555
      }
1556
      tstrncpy((*fields)[i].name, schema->name, 65);
72!
1557
      (*fields)[i].type = schema->type;
72✔
1558
      (*fields)[i].bytes = calcTypeBytesFromSchemaBytes(schema->type, schema->bytes, true);
72✔
1559
      if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
72✔
1560
        (*fields)[i].precision = pDataBlock->pMeta->tableInfo.precision;
15✔
1561
      }
1562
    }
1563
  }
1564

1565
  *fieldNum = numOfBound;
15✔
1566

1567
  return TSDB_CODE_SUCCESS;
15✔
1568
}
1569

1570
int32_t qBuildStmtStbColFields(void* pBlock, void* boundTags, SSHashObj* parsedCols, uint8_t tbNameFlag,
173✔
1571
                               int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
1572
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
173✔
1573
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
173✔
1574
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
173!
1575
    *fieldNum = 0;
×
1576
    if (fields != NULL) {
×
1577
      *fields = NULL;
×
1578
    }
1579

1580
    return TSDB_CODE_SUCCESS;
×
1581
  }
1582

1583
  CHECK_CODE(buildStbBoundFields(pDataBlock->boundColsInfo, pSchema, fieldNum, fields, pDataBlock->pMeta, boundTags,
173!
1584
                                 parsedCols, tbNameFlag));
1585

1586
  return TSDB_CODE_SUCCESS;
173✔
1587
}
1588

1589
int32_t qResetStmtColumns(SArray* pCols, bool deepClear) {
×
1590
  int32_t colNum = taosArrayGetSize(pCols);
×
1591

1592
  for (int32_t i = 0; i < colNum; ++i) {
×
1593
    SColData* pCol = (SColData*)taosArrayGet(pCols, i);
×
1594
    if (pCol == NULL) {
×
1595
      parserError("qResetStmtColumns column:%d is NULL", i);
×
1596
      return terrno;
×
1597
    }
1598
    if (deepClear) {
×
1599
      tColDataDeepClear(pCol);
×
1600
    } else {
1601
      tColDataClear(pCol);
×
1602
    }
1603
  }
1604

1605
  return TSDB_CODE_SUCCESS;
×
1606
}
1607

1608
int32_t qResetStmtDataBlock(STableDataCxt* block, bool deepClear) {
1,649,444✔
1609
  int32_t        code = 0;
1,649,444✔
1610
  STableDataCxt* pBlock = (STableDataCxt*)block;
1,649,444✔
1611
  int32_t        colNum = taosArrayGetSize(pBlock->pData->aCol);
1,649,444✔
1612

1613
  int8_t flag = 0;
1,650,638✔
1614
  for (int32_t i = 0; i < colNum; ++i) {
9,234,051✔
1615
    flag = pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
7,584,549✔
1616
    if (pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
7,584,891✔
1617
      SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i);
7,585,059✔
1618
      if (pCol == NULL) {
7,585,174!
1619
        parserError("qResetStmtDataBlock column:%d is NULL", i);
×
1620
        return terrno;
×
1621
      }
1622
      if (deepClear) {
7,585,311✔
1623
        tColDataDeepClear(pCol);
2,756,211✔
1624

1625
      } else {
1626
        tColDataClear(pCol);
4,829,100✔
1627
      }
1628

1629
    } else {
1630
      pBlock->pData->aRowP = taosArrayInit(20, POINTER_BYTES);
3✔
1631
    }
1632
  }
1633

1634
  tBlobSetDestroy(pBlock->pData->pBlobSet);
1,649,502✔
1635
  pBlock->pData->pBlobSet = NULL;
1,649,304✔
1636
  return code;
1,649,760✔
1637
}
1638

1639
int32_t qCloneStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, bool reset) {
642,967✔
1640
  int32_t code = 0;
642,967✔
1641

1642
  *pDst = taosMemoryCalloc(1, sizeof(STableDataCxt));
642,967!
1643
  if (NULL == *pDst) {
645,057!
1644
    return terrno;
×
1645
  }
1646

1647
  STableDataCxt* pNewCxt = (STableDataCxt*)*pDst;
645,171✔
1648
  STableDataCxt* pCxt = (STableDataCxt*)pSrc;
644,766✔
1649
  pNewCxt->hasBlob = pSrc->hasBlob;
644,766✔
1650
  pNewCxt->pSchema = NULL;
645,057✔
1651
  pNewCxt->pValues = NULL;
645,114✔
1652

1653
  if (pCxt->pMeta) {
645,285!
1654
    pNewCxt->pMeta = tableMetaDup(pCxt->pMeta);
645,342✔
1655
    // void* pNewMeta = taosMemoryMalloc(TABLE_META_SIZE(pCxt->pMeta));
1656
    // if (NULL == pNewMeta) {
1657
    //   insDestroyTableDataCxt(*pDst);
1658
    //   return terrno;
1659
    // }
1660
    // memcpy(pNewMeta, pCxt->pMeta, TABLE_META_SIZE(pCxt->pMeta));
1661
    // pNewCxt->pMeta = pNewMeta;
1662
  }
1663

1664
  memcpy(&pNewCxt->boundColsInfo, &pCxt->boundColsInfo, sizeof(pCxt->boundColsInfo));
645,130!
1665
  pNewCxt->boundColsInfo.pColIndex = NULL;
645,244✔
1666

1667
  if (pCxt->boundColsInfo.pColIndex) {
645,358✔
1668
    void* pNewColIdx = taosMemoryMalloc(pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
644,958!
1669
    if (NULL == pNewColIdx) {
645,130!
1670
      insDestroyTableDataCxt(*pDst);
×
1671
      return terrno;
×
1672
    }
1673
    memcpy(pNewColIdx, pCxt->boundColsInfo.pColIndex,
645,130!
1674
           pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
645,130✔
1675
    pNewCxt->boundColsInfo.pColIndex = pNewColIdx;
644,875✔
1676
  }
1677

1678
  if (pCxt->pData) {
645,103✔
1679
    int8_t         flag = 1;
644,761✔
1680
    SSubmitTbData* pNewTb = (SSubmitTbData*)taosMemoryMalloc(sizeof(SSubmitTbData));
644,761!
1681
    if (NULL == pNewTb) {
645,130!
1682
      insDestroyTableDataCxt(*pDst);
×
1683
      return terrno;
×
1684
    }
1685

1686
    memcpy(pNewTb, pCxt->pData, sizeof(*pCxt->pData));
645,130!
1687
    pNewTb->pCreateTbReq = NULL;
645,244✔
1688
    if (pNewTb->pBlobSet != NULL) {
644,787!
1689
      flag = pNewTb->pBlobSet->type;
×
1690
    }
1691
    pNewTb->pBlobSet = NULL;
644,844✔
1692

1693
    pNewTb->aCol = taosArrayDup(pCxt->pData->aCol, NULL);
644,704✔
1694
    if (NULL == pNewTb->aCol) {
644,993!
1695
      insDestroyTableDataCxt(*pDst);
×
1696
      return terrno;
×
1697
    }
1698

1699
    pNewCxt->pData = pNewTb;
644,879✔
1700

1701
    if (reset) {
644,993✔
1702
      code = qResetStmtDataBlock(*pDst, true);
644,211✔
1703
    }
1704
  }
1705

1706
  return code;
645,285✔
1707
}
1708

1709
int32_t qRebuildStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, uint64_t uid, uint64_t suid, int32_t vgId,
1,017✔
1710
                              bool rebuildCreateTb) {
1711
  int32_t code = qCloneStmtDataBlock(pDst, pSrc, false);
1,017✔
1712
  if (code) {
1,017!
1713
    return code;
×
1714
  }
1715

1716
  STableDataCxt* pBlock = (STableDataCxt*)*pDst;
1,017✔
1717
  if (pBlock->pMeta) {
1,017!
1718
    pBlock->pMeta->uid = uid;
1,017✔
1719
    pBlock->pMeta->vgId = vgId;
1,017✔
1720
    pBlock->pMeta->suid = suid;
1,017✔
1721
  }
1722

1723
  pBlock->pData->suid = suid;
1,017✔
1724
  pBlock->pData->uid = uid;
1,017✔
1725

1726
  if (rebuildCreateTb && NULL == pBlock->pData->pCreateTbReq) {
1,017!
1727
    pBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
957!
1728
    if (NULL == pBlock->pData->pCreateTbReq) {
957!
1729
      return terrno;
×
1730
    }
1731
  }
1732

1733
  return TSDB_CODE_SUCCESS;
1,017✔
1734
}
1735

1736
STableMeta* qGetTableMetaInDataBlock(STableDataCxt* pDataBlock) { return ((STableDataCxt*)pDataBlock)->pMeta; }
1,994,311✔
1737

1738
void qDestroyStmtDataBlock(STableDataCxt* pBlock) {
2,715,906✔
1739
  if (pBlock == NULL) {
2,715,906✔
1740
    return;
1,020,867✔
1741
  }
1742

1743
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
1,695,039✔
1744
  insDestroyTableDataCxt(pDataBlock);
1,695,039✔
1745
}
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