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

taosdata / TDengine / #5052

13 May 2026 12:00PM UTC coverage: 73.338% (-0.02%) from 73.358%
#5052

push

travis-ci

web-flow
feat: taosdump support stream backup/restore (#35326)

139 of 170 new or added lines in 3 files covered. (81.76%)

761 existing lines in 163 files now uncovered.

281469 of 383795 relevant lines covered (73.34%)

134502812.98 hits per line

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

77.09
/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) {
7,745,378✔
35
  if (pDataBlock == NULL || pDataBlock->pData == NULL) {
7,745,378✔
36
    qError("stmt qCloneCurrentTbData input is null, maybe not bind data before execute");
2,860✔
37
    return TSDB_CODE_TSC_STMT_API_ERROR;
198✔
38
  }
39
  *pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
7,744,031✔
40
  if (NULL == *pData) {
7,739,135✔
41
    return terrno;
×
42
  }
43

44
  int8_t         flag = 1;
7,741,825✔
45
  SSubmitTbData* pNew = *pData;
7,741,825✔
46

47
  *pNew = *pDataBlock->pData;
7,744,869✔
48
  pNew->pBlobSet = NULL;
7,746,264✔
49

50
  int32_t code = cloneSVreateTbReq(pDataBlock->pData->pCreateTbReq, &pNew->pCreateTbReq);
7,749,820✔
51
  if (TSDB_CODE_SUCCESS != code) {
7,745,207✔
52
    taosMemoryFreeClear(*pData);
×
53
    return code;
×
54
  }
55
  pNew->aCol = taosArrayDup(pDataBlock->pData->aCol, NULL);
7,745,207✔
56
  if (!pNew->aCol) {
7,752,804✔
57
    code = terrno;
×
58
    taosMemoryFreeClear(*pData);
×
59
    return code;
×
60
  }
61

62
  int32_t colNum = taosArrayGetSize(pNew->aCol);
7,746,610✔
63
  for (int32_t i = 0; i < colNum; ++i) {
31,770,166✔
64
    if (pDataBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
24,015,972✔
65
      SColData* pCol = (SColData*)taosArrayGet(pNew->aCol, i);
24,020,597✔
66
      tColDataDeepClear(pCol);
24,017,902✔
67
    } else {
68
      pNew->aCol = taosArrayInit(20, POINTER_BYTES);
396✔
69
      if (pNew->aCol == NULL) {
396✔
70
        code = terrno;
×
71
        taosMemoryFreeClear(*pData);
×
72
        return code;
×
73
      }
74
    }
75
  }
76

77
  return code;
7,754,194✔
78
}
79

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

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

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

96
  if (TSDB_CODE_SUCCESS == code) {
998,599✔
97
    code = insBuildVgDataBlocks(pAllVgHash, pVgDataBlocks, &pStmt->pDataBlocks, true);
998,564✔
98
  }
99

100
  if (pStmt->freeArrayFunc) {
998,599✔
101
    pStmt->freeArrayFunc(pVgDataBlocks);
998,577✔
102
  }
103
  return code;
998,387✔
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) {
7,745,199✔
128
  int32_t             code = TSDB_CODE_SUCCESS;
7,745,199✔
129
  SArray*             pVgDataBlocks = NULL;
7,745,199✔
130
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
7,749,311✔
131

132
  // merge according to vgId
133
  if (taosHashGetSize(pBlockHash) > 0) {
7,747,855✔
134
    code = insMergeTableDataCxt(pBlockHash, &pVgDataBlocks, true);
7,748,548✔
135
  }
136

137
  if (TSDB_CODE_SUCCESS == code) {
7,751,260✔
138
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks, false);
7,752,016✔
139
  }
140

141
  if (pStmt->freeArrayFunc) {
7,751,539✔
142
    pStmt->freeArrayFunc(pVgDataBlocks);
7,751,912✔
143
  }
144
  return code;
7,737,934✔
145
}
146

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

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

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

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

170
  bool  isJson = false;
8,581✔
171
  STag* pTag = NULL;
8,581✔
172

173
  for (int c = 0; c < tags->numOfBound; ++c) {
32,452✔
174
    if (bind[c].is_null && bind[c].is_null[0]) {
23,871✔
175
      continue;
×
176
    }
177

178
    SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
23,871✔
179
    int32_t  colLen = pTagSchema->bytes;
23,871✔
180
    if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
23,871✔
181
      if (!bind[c].length) {
4,927✔
182
        code = buildInvalidOperationMsg(&pBuf, "var tag length is null");
×
183
        goto end;
×
184
      }
185
      colLen = bind[c].length[0];
4,927✔
186
      if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
4,927✔
187
        code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
×
188
        goto end;
×
189
      }
190
    }
191
    if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
47,742✔
192
      code = terrno;
×
193
      goto end;
×
194
    }
195
    if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
23,871✔
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};
23,782✔
215
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
23,782✔
216
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
20,262✔
217
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
3,798✔
218
          code = initCtxAsText();
278✔
219
          if (code) {
278✔
220
            qError("geometry init failed:%s", tstrerror(code));
×
221
            goto end;
×
222
          }
223
          code = checkWKB(bind[c].buffer, colLen);
278✔
224
          if (code) {
278✔
225
            qError("stmt bind invalid geometry tag:%s, must be WKB format", (char*)bind[c].buffer);
×
226
            goto end;
×
227
          }
228
        }
229
        val.pData = (uint8_t*)bind[c].buffer;
3,798✔
230
        val.nData = colLen;
3,798✔
231
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
19,984✔
232
        int32_t output = 0;
1,040✔
233
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
1,040✔
234
        if (p == NULL) {
1,040✔
235
          code = terrno;
×
236
          goto end;
×
237
        }
238
        if (!taosMbsToUcs4(bind[c].buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
1,040✔
239
          if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
240
            taosMemoryFree(p);
×
241
            code = generateSyntaxErrMsg(&pBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pTagSchema->name);
×
242
            goto end;
×
243
          }
244
          char buf[512] = {0};
×
245
          snprintf(buf, tListLen(buf), " taosMbsToUcs4 error:%s", strerror(terrno));
×
246
          taosMemoryFree(p);
×
247
          code = buildSyntaxErrMsg(&pBuf, buf, bind[c].buffer);
×
248
          goto end;
×
249
        }
250
        val.pData = p;
1,040✔
251
        val.nData = output;
1,040✔
252
      } else {
253
        memcpy(&val.i64, bind[c].buffer, colLen);
18,944✔
254
      }
255
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
23,782✔
256
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
257
        goto end;
×
258
      }
259
      if (NULL == taosArrayPush(pTagArray, &val)) {
23,782✔
260
        code = terrno;
×
261
        goto end;
×
262
      }
263
    }
264
  }
265

266
  if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) {
8,581✔
267
    goto end;
×
268
  }
269

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

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

289
end:
8,581✔
290
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
32,443✔
291
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
23,862✔
292
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
23,862✔
293
      taosMemoryFreeClear(p->pData);
1,120✔
294
    }
295
  }
296
  taosArrayDestroy(pTagArray);
8,581✔
297
  taosArrayDestroy(tagName);
8,581✔
298
  taosMemoryFree(pTag);
8,581✔
299

300
  return code;
8,581✔
301
}
302

303
int32_t convertStmtNcharCol(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_MULTI_BIND* src, TAOS_MULTI_BIND* dst,
6,417✔
304
                            void* charsetCxt) {
305
  int32_t output = 0;
6,417✔
306
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
6,417✔
307
  if (dst->buffer_length < newBuflen) {
6,417✔
308
    dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
6,417✔
309
    if (NULL == dst->buffer) {
6,417✔
310
      return terrno;
×
311
    }
312
  }
313

314
  if (NULL == dst->length) {
6,417✔
315
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
6,417✔
316
    if (NULL == dst->length) {
6,417✔
317
      taosMemoryFreeClear(dst->buffer);
×
318
      return terrno;
×
319
    }
320
  }
321

322
  dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
6,417✔
323

324
  for (int32_t i = 0; i < src->num; ++i) {
871,530✔
325
    if (src->is_null && src->is_null[i]) {
865,113✔
326
      continue;
344,736✔
327
    }
328

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

340
    dst->length[i] = output;
520,377✔
341
  }
342

343
  dst->buffer_type = src->buffer_type;
6,417✔
344
  dst->is_null = src->is_null;
6,417✔
345
  dst->num = src->num;
6,417✔
346

347
  return TSDB_CODE_SUCCESS;
6,417✔
348
}
349

350
int32_t qBindStmtStbColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
2,523,886✔
351
                              STSchema** pTSchema, SBindInfo* pBindInfos, void* charsetCxt) {
352
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
2,523,886✔
353
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
2,523,886✔
354
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
2,532,530✔
355
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
2,533,306✔
356
  int32_t          rowNum = bind->num;
2,533,500✔
357
  TAOS_MULTI_BIND  ncharBind = {0};
2,534,611✔
358
  TAOS_MULTI_BIND* pBind = NULL;
2,534,871✔
359
  int32_t          code = 0;
2,534,871✔
360
  int16_t          lastColId = -1;
2,534,871✔
361
  bool             colInOrder = true;
2,534,871✔
362

363
  if (NULL == pTSchema || NULL == *pTSchema) {
2,534,871✔
364
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
14,060✔
365
  }
366

367
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
13,071,723✔
368
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
10,531,425✔
369
    if (pColSchema->colId <= lastColId) {
10,534,147✔
370
      colInOrder = false;
×
371
    } else {
372
      lastColId = pColSchema->colId;
10,536,143✔
373
    }
374
    // SColData* pCol = taosArrayGet(pCols, c);
375

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

381
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
10,534,006✔
382
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
10,526,271✔
383
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
384
      goto _return;
×
385
    }
386

387
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
10,537,585✔
388
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
1,862✔
389
      if (code) {
1,862✔
390
        goto _return;
×
391
      }
392
      pBind = &ncharBind;
1,862✔
393
    } else {
394
      pBind = bind + c;
10,532,733✔
395
    }
396

397
    pBindInfos[c].columnId = pColSchema->colId;
10,529,915✔
398
    pBindInfos[c].bind = pBind;
10,543,304✔
399
    pBindInfos[c].type = pColSchema->type;
10,538,854✔
400
  }
401

402
  code = tRowBuildFromBind(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols, &pDataBlock->ordered,
2,544,736✔
403
                           &pDataBlock->duplicateTs);
404

405
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
2,535,526✔
406

407
_return:
2,535,526✔
408

409
  taosMemoryFree(ncharBind.buffer);
2,538,837✔
410
  taosMemoryFree(ncharBind.length);
2,537,406✔
411

412
  return code;
2,536,704✔
413
}
414

415
int32_t qBindStmtColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
7,585,564✔
416
                           void* charsetCxt) {
417
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
7,585,564✔
418
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
7,585,564✔
419
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
7,589,698✔
420
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
7,591,573✔
421
  int32_t          rowNum = bind->num;
7,592,319✔
422
  TAOS_MULTI_BIND  ncharBind = {0};
7,588,166✔
423
  TAOS_MULTI_BIND* pBind = NULL;
7,588,186✔
424
  int32_t          code = 0;
7,588,186✔
425

426
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
30,442,510✔
427
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
22,852,074✔
428
    SColData* pCol = taosArrayGet(pCols, c);
22,852,124✔
429

430
    if (bind[c].num != rowNum) {
22,855,148✔
431
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
432
      goto _return;
×
433
    }
434

435
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
22,853,656✔
436
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
22,848,604✔
437
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
438
      goto _return;
×
439
    }
440

441
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
22,852,537✔
442
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
4,475✔
443
      if (code) {
4,475✔
444
        goto _return;
×
445
      }
446
      pBind = &ncharBind;
4,475✔
447
    } else {
448
      pBind = bind + c;
22,850,673✔
449
    }
450

451
    int32_t bytes = 0;
22,845,430✔
452
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
22,845,430✔
453
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
22,406✔
454
        bytes = pColSchema->bytes - BLOBSTR_HEADER_SIZE;
×
455
      } else {
456
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
22,759✔
457
      }
458
    } else {
459
      bytes = -1;
22,826,007✔
460
    }
461
    code = tColDataAddValueByBind(pCol, pBind, bytes, initCtxAsText, checkWKB);
22,848,766✔
462
    if (code) {
22,858,902✔
463
      goto _return;
4,578✔
464
    }
465
  }
466

467
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
7,593,380✔
468

469
_return:
7,597,760✔
470

471
  taosMemoryFree(ncharBind.buffer);
7,594,964✔
472
  taosMemoryFree(ncharBind.length);
7,595,696✔
473

474
  return code;
7,594,587✔
475
}
476

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

490
  for (int c = 0; c < numOfBound; ++c) {
5,742✔
491
    if (tSimpleHashGet(parsedCols, &c, sizeof(c))) {
4,752✔
492
      continue;
792✔
493
    }
494
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[actualIndex]];
3,960✔
495
    SColData* pCol = taosArrayGet(pCols, actualIndex);
3,960✔
496

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

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

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

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

535
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
990✔
536

537
_return:
990✔
538

539
  taosMemoryFree(ncharBind.buffer);
990✔
540
  taosMemoryFree(ncharBind.length);
990✔
541

542
  return code;
990✔
543
}
544

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

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

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

566
  if (bind->buffer_type != pColSchema->type) {
1,120✔
567
    return buildInvalidOperationMsgExt(&pBuf, "column type:%d mis-match with buffer type:%d", pColSchema->type,
×
568
                                       bind->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,
567,323✔
596
                            TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen, void* charsetCxt,
597
                            SVCreateTbReq* pCreateTbReq) {
598
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
567,323✔
599
  SMsgBuf        pBuf = {.buf = msgBuf, .len = msgBufLen};
567,323✔
600
  int32_t        code = TSDB_CODE_SUCCESS;
567,323✔
601
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
567,323✔
602
  if (NULL == tags) {
567,323✔
603
    return buildInvalidOperationMsg(&pBuf, "tags is null");
×
604
  }
605

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

616
  SArray* tagName;
617
  if (tags->parseredTags) {
567,362✔
618
    tagName = taosArrayDup(tags->parseredTags->STagNames, NULL);
4,752✔
619
  } else {
620
    tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
562,610✔
621
  }
622

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

628
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
567,323✔
629

630
  bool  isJson = false;
567,388✔
631
  STag* pTag = NULL;
567,388✔
632

633
  int bindIdx = 0;
567,388✔
634
  for (int c = 0; c < tags->numOfBound; ++c) {
1,735,960✔
635
    if (bind == NULL) {
1,173,084✔
636
      break;
4,158✔
637
    }
638
    if (tags->parseredTags) {
1,168,926✔
639
      bool found = false;
52,272✔
640
      for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
142,560✔
641
        if (tags->parseredTags->pTagIndex[k] == tags->pColIndex[c]) {
99,792✔
642
          found = true;
9,504✔
643
          break;
9,504✔
644
        }
645
      }
646
      if (found) {
52,272✔
647
        continue;
9,504✔
648
      }
649
    }
650

651
    TAOS_STMT2_BIND bindData = bind[bindIdx++];
1,159,422✔
652

653
    if (bindData.is_null && bindData.is_null[0]) {
1,159,422✔
654
      continue;
×
655
    }
656

657
    SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
1,159,422✔
658
    int32_t  colLen = pTagSchema->bytes;
1,159,422✔
659
    if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
1,159,422✔
660
      if (IS_STR_DATA_BLOB(pTagSchema->type)) {
568,300✔
661
        return TSDB_CODE_BLOB_NOT_SUPPORT_TAG;
×
662
      }
663
      if (!bindData.length) {
568,300✔
664
        code = buildInvalidOperationMsg(&pBuf, "var tag length is null");
×
665
        goto end;
198✔
666
      }
667
      colLen = bindData.length[0];
568,300✔
668
      if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
568,300✔
669
        code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
×
670
        goto end;
×
671
      }
672
    }
673
    if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
2,318,649✔
674
      code = terrno;
×
675
      goto end;
×
676
    }
677
    if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
1,159,227✔
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) {
39✔
693
        goto end;
×
694
      }
695
    } else {
696
      STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
1,159,227✔
697
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
1,159,227✔
698
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
605,774✔
699
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
559,617✔
700
          code = initCtxAsText();
6,138✔
701
          if (code) {
6,138✔
702
            qError("geometry init failed:%s", tstrerror(code));
×
703
            goto end;
198✔
704
          }
705
          code = checkWKB(bindData.buffer, colLen);
6,138✔
706
          if (code) {
6,138✔
707
            qError("stmt2 bind invalid geometry tag:%s, must be WKB format", (char*)bindData.buffer);
172✔
708
            goto end;
198✔
709
          }
710
        }
711
        val.pData = (uint8_t*)bindData.buffer;
559,549✔
712
        val.nData = colLen;
559,549✔
713
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
599,610✔
714
        int32_t output = 0;
8,514✔
715
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
8,514✔
716
        if (p == NULL) {
8,514✔
717
          code = terrno;
×
718
          goto end;
×
719
        }
720
        if (colLen != 0) {
8,514✔
721
          if (!taosMbsToUcs4(bindData.buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
8,118✔
722
            if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
723
              taosMemoryFree(p);
×
724
              code = generateSyntaxErrMsg(&pBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pTagSchema->name);
×
725
              goto end;
×
726
            }
727
            char buf[512] = {0};
×
728
            snprintf(buf, tListLen(buf), " taosMbsToUcs4 error:%s", strerror(terrno));
×
729
            taosMemoryFree(p);
×
730
            code = buildSyntaxErrMsg(&pBuf, buf, bindData.buffer);
×
731
            goto end;
×
732
          }
733
        }
734
        val.pData = p;
8,514✔
735
        val.nData = output;
8,514✔
736

737
      } else {
738
        uint8_t* buf = (uint8_t*)bindData.buffer;
591,096✔
739
        if (TSDB_DATA_TYPE_BOOL == pTagSchema->type && *buf > 1) {
591,096✔
740
          *buf = 1;
990✔
741
        }
742
        memcpy(&val.i64, buf, colLen);
591,096✔
743
      }
744
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
1,159,159✔
745
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
746
        goto end;
×
747
      }
748
      if (NULL == taosArrayPush(pTagArray, &val)) {
1,159,029✔
749
        code = terrno;
×
750
        goto end;
×
751
      }
752
    }
753
  }
754

755
  if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) {
567,034✔
756
    goto end;
×
757
  }
758

759
  if (pCreateTbReq) {
567,099✔
760
    code = insBuildCreateTbReq(pCreateTbReq, tName, pTag, suid, sTableName, tagName,
543,626✔
761
                               pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
543,626✔
762
    pTag = NULL;
543,626✔
763
    goto end;
543,626✔
764
  }
765

766
  if (NULL == pDataBlock->pData->pCreateTbReq) {
23,473✔
767
    pDataBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
2,590✔
768
    if (NULL == pDataBlock->pData->pCreateTbReq) {
2,590✔
769
      code = terrno;
×
770
      goto end;
×
771
    }
772
  } else {
773
    SVCreateTbReq* tmp = pDataBlock->pData->pCreateTbReq;
20,883✔
774
    taosMemoryFreeClear(tmp->name);
20,883✔
775
    taosMemoryFreeClear(tmp->ctb.pTag);
20,883✔
776
    taosMemoryFreeClear(tmp->ctb.stbName);
20,883✔
777
    taosArrayDestroy(tmp->ctb.tagName);
20,883✔
778
    tmp->ctb.tagName = NULL;
20,961✔
779
  }
780

781
  code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName,
23,551✔
782
                             pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
23,551✔
783
  pTag = NULL;
23,551✔
784

785
end:
567,375✔
786
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
1,736,012✔
787
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
1,168,624✔
788
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
1,168,637✔
789
      taosMemoryFreeClear(p->pData);
8,514✔
790
    }
791
  }
792
  taosArrayDestroy(pTagArray);
567,284✔
793
  taosArrayDestroy(tagName);
567,427✔
794
  taosMemoryFree(pTag);
567,427✔
795

796
  return code;
567,362✔
797
}
798

799
static int32_t convertStmtStbNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst,
4,980✔
800
                                       void* charsetCxt) {
801
  int32_t       output = 0;
4,980✔
802
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
4,980✔
803

804
  dst->buffer = taosMemoryCalloc(src->num, max_buf_len);
4,980✔
805
  if (NULL == dst->buffer) {
4,980✔
806
    return terrno;
×
807
  }
808

809
  dst->length = taosMemoryCalloc(src->num, sizeof(int32_t));
4,980✔
810
  if (NULL == dst->length) {
4,980✔
811
    taosMemoryFreeClear(dst->buffer);
×
812
    return terrno;
×
813
  }
814

815
  char* src_buf = src->buffer;
4,980✔
816
  char* dst_buf = dst->buffer;
4,980✔
817
  for (int32_t i = 0; i < src->num; ++i) {
42,504✔
818
    if (src->is_null && src->is_null[i]) {
37,524✔
819
      continue;
198✔
820
    }
821

822
    if (src->length[i] == 0) {
37,326✔
823
      dst->length[i] = 0;
198✔
824
      continue;
198✔
825
    }
826

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

836
    dst->length[i] = output;
37,128✔
837
    src_buf += src->length[i];
37,128✔
838
    dst_buf += output;
37,128✔
839
  }
840

841
  dst->buffer_type = src->buffer_type;
4,980✔
842
  dst->is_null = src->is_null;
4,980✔
843
  dst->num = src->num;
4,980✔
844

845
  return TSDB_CODE_SUCCESS;
4,980✔
846
}
847

848
int32_t qBindStmtStbColsValue2(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
1,222,654✔
849
                               int32_t msgBufLen, STSchema** pTSchema, SBindInfo2* pBindInfos, void* charsetCxt,
850
                               SBlobSet** ppBlob) {
851
  STableDataCxt*  pDataBlock = (STableDataCxt*)pBlock;
1,222,654✔
852
  SSchema*        pSchema = getTableColumnSchema(pDataBlock->pMeta);
1,222,654✔
853
  SSchemaExt*     pSchemaExt = getTableColumnExtSchema(pDataBlock->pMeta);
1,222,646✔
854
  SBoundColInfo*  boundInfo = &pDataBlock->boundColsInfo;
1,222,689✔
855
  SMsgBuf         pBuf = {.buf = msgBuf, .len = msgBufLen};
1,222,689✔
856
  int32_t         rowNum = bind->num;
1,222,689✔
857
  SArray*         ncharBinds = NULL;
1,222,689✔
858
  TAOS_STMT2_BIND ncharBind = {0};
1,222,689✔
859
  int32_t         code = 0;
1,222,689✔
860
  int16_t         lastColId = -1;
1,222,689✔
861
  bool            colInOrder = true;
1,222,689✔
862
  int             ncharColNums = 0;
1,222,689✔
863
  int32_t         bindIdx = 0;
1,222,689✔
864
  int8_t          hasBlob = 0;
1,222,689✔
865
  int32_t         lino = 0;
1,222,689✔
866
  if (NULL == pTSchema || NULL == *pTSchema) {
1,222,747✔
867
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
14,177✔
868
  }
869

870
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
11,798,242✔
871
    if (TSDB_DATA_TYPE_NCHAR == pSchema[boundInfo->pColIndex[c]].type) {
10,591,013✔
872
      ncharColNums++;
4,980✔
873
    }
874
  }
875
  if (ncharColNums > 0) {
1,222,818✔
876
    ncharBinds = taosArrayInit(ncharColNums, sizeof(ncharBind));
4,980✔
877
    if (!ncharBinds) {
4,980✔
UNCOV
878
      code = terrno;
×
879
      TAOS_CHECK_GOTO(code, &lino, _return);
×
880
    }
881
  }
882

883
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
11,838,108✔
884
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
10,612,099✔
885
    if (pColSchema->colId <= lastColId) {
10,615,009✔
886
      colInOrder = false;
1,386✔
887
    } else {
888
      lastColId = pColSchema->colId;
10,617,625✔
889
    }
890

891
    if (parsedCols) {
10,617,241✔
892
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
28,512✔
893
      if (pParsedVal) {
28,512✔
894
        pBindInfos[c].columnId = pColSchema->colId;
5,940✔
895
        pBindInfos[c].type = pColSchema->type;
5,940✔
896
        pBindInfos[c].bytes = pColSchema->bytes;
5,940✔
897
        continue;
5,940✔
898
      }
899
    }
900

901
    TAOS_STMT2_BIND bindData = bind[bindIdx];
10,611,301✔
902

903
    if (bindData.num != rowNum) {
10,600,542✔
904
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
905
      goto _return;
×
906
    }
907

908
    if ((!(rowNum == 1 && bindData.is_null && *bindData.is_null)) &&
10,600,542✔
909
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
10,600,454✔
910
      code = buildInvalidOperationMsgExt(&pBuf, "bind[%d] column type:%d mis-match with buffer type:%d", bindIdx,
×
911
                                         pColSchema->type, bindData.buffer_type);
×
912
      goto _return;
×
913
    }
914

915
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
10,598,099✔
916
      code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
4,980✔
917
      TAOS_CHECK_GOTO(code, &lino, _return);
4,980✔
918

919
      if (!taosArrayPush(ncharBinds, &ncharBind)) {
4,980✔
920
        code = terrno;
×
921
        goto _return;
×
922
      }
923
      pBindInfos[c].bind = taosArrayGetLast(ncharBinds);
4,980✔
924
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
10,594,811✔
925
      code = initCtxAsText();
2,574✔
926
      if (code) {
2,574✔
927
        parserError("stmt2 bind geometry init failed, ErrCode: 0x%x", code);
×
928
        goto _return;
×
929
      }
930
      uint8_t* buf = bindData.buffer;
2,574✔
931
      for (int j = 0; j < bindData.num; j++) {
5,742✔
932
        if (bindData.is_null && bindData.is_null[j]) {
3,168✔
933
          continue;
198✔
934
        }
935
        code = checkWKB(buf, bindData.length[j]);
2,970✔
936
        if (code) {
2,970✔
937
          parserError("stmt2 interlace mode geometry data col:%d, row:%d must be in WKB format", c, j);
×
938
          goto _return;
×
939
        }
940
        buf += bindData.length[j];
2,970✔
941
      }
942
      pBindInfos[c].bind = bind + bindIdx;
2,574✔
943
    } else {
944
      if (IS_STR_DATA_BLOB(pColSchema->type)) hasBlob = 1;
10,590,872✔
945
      pBindInfos[c].bind = bind + bindIdx;
10,591,066✔
946
    }
947

948
    pBindInfos[c].columnId = pColSchema->colId;
10,592,197✔
949
    pBindInfos[c].type = pColSchema->type;
10,600,773✔
950
    pBindInfos[c].bytes = pColSchema->bytes;
10,605,936✔
951

952
    bindIdx++;
10,609,265✔
953
  }
954

955
  if (hasBlob == 0) {
1,222,824✔
956
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pSchemaExt, pCols,
1,222,548✔
957
                              &pDataBlock->ordered, &pDataBlock->duplicateTs);
958
    TAOS_CHECK_GOTO(code, &lino, _return);
1,222,823✔
959
  } else {
960
    code = tBlobSetCreate(1024, 1, ppBlob);
276✔
961
    TAOS_CHECK_GOTO(code, &lino, _return);
198✔
962

963
    code = tRowBuildFromBind2WithBlob(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pSchemaExt,
198✔
964
                                      pCols, &pDataBlock->ordered, &pDataBlock->duplicateTs, *ppBlob);
965
    TAOS_CHECK_GOTO(code, &lino, _return);
256✔
966
  }
967

968
  parserDebug("stmt2 all %d columns bind %d rows data as row format", boundInfo->numOfBound, rowNum);
1,222,683✔
969

970
_return:
1,222,683✔
971
  if (ncharBinds) {
1,222,956✔
972
    for (int i = 0; i < TARRAY_SIZE(ncharBinds); ++i) {
9,960✔
973
      TAOS_STMT2_BIND* ncBind = TARRAY_DATA(ncharBinds);
4,980✔
974
      taosMemoryFree(ncBind[i].buffer);
4,980✔
975
      taosMemoryFree(ncBind[i].length);
4,980✔
976
    }
977
    taosArrayDestroy(ncharBinds);
4,980✔
978
  }
979
  if (code != 0) {
1,222,956✔
980
    parserError("stmt2 failed to bind stb col at lino %d since %s", lino, tstrerror(code));
396✔
981
  }
982

983
  return code;
1,222,917✔
984
}
985

986
static int32_t convertStmtNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst,
30,161✔
987
                                    void* charsetCxt) {
988
  int32_t       output = 0;
30,161✔
989
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
30,161✔
990

991
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
30,161✔
992
  // if (dst->buffer_length < newBuflen) {
993
  dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
30,161✔
994
  if (NULL == dst->buffer) {
30,161✔
995
    return terrno;
×
996
  }
997
  //}
998

999
  if (NULL == dst->length) {
30,161✔
1000
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
29,171✔
1001
    if (NULL == dst->length) {
29,171✔
1002
      taosMemoryFreeClear(dst->buffer);
×
1003
      return terrno;
×
1004
    }
1005
  }
1006

1007
  // dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
1008
  char* src_buf = src->buffer;
30,161✔
1009
  char* dst_buf = dst->buffer;
30,136✔
1010
  for (int32_t i = 0; i < src->num; ++i) {
69,895,195✔
1011
    if (src->is_null && src->is_null[i]) {
69,858,227✔
1012
      continue;
6,185,946✔
1013
    }
1014

1015
    if (src->length[i] == 0) {
63,686,224✔
1016
      dst->length[i] = 0;
2,646,856✔
1017
      continue;
2,646,787✔
1018
    }
1019

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

1031
    dst->length[i] = output;
61,035,059✔
1032
    src_buf += src->length[i];
61,020,798✔
1033
    dst_buf += output;
61,043,636✔
1034
  }
1035

1036
  dst->buffer_type = src->buffer_type;
30,161✔
1037
  dst->is_null = src->is_null;
30,161✔
1038
  dst->num = src->num;
30,161✔
1039

1040
  return TSDB_CODE_SUCCESS;
30,161✔
1041
}
1042

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

1057
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
1,610,226✔
1058
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
1,395,011✔
1059
    SColData* pCol = taosArrayGet(pCols, c);
1,395,048✔
1060
    if (pCol == NULL || pColSchema == NULL) {
1,395,072✔
UNCOV
1061
      code = buildInvalidOperationMsg(&pBuf, "get column schema or column data failed");
×
1062
      goto _return;
×
1063
    }
1064

1065
    if (boundInfo->pColIndex[c] == 0) {
1,395,072✔
1066
      pCol->cflag |= COL_IS_KEY;
215,234✔
1067
    }
1068

1069
    if (parsedCols) {
1,395,081✔
1070
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
27,918✔
1071
      if (pParsedVal) {
27,918✔
1072
        for (int row = 0; row < rowNum; row++) {
11,088✔
1073
          code = tColDataAppendValue(pCol, pParsedVal);
5,742✔
1074
          if (code) {
5,742✔
1075
            parserError("stmt2 failed to add fixed value col:%d ,type:%d, ErrCode: 0x%x", c, pColSchema->type, code);
×
1076
            goto _return;
×
1077
          }
1078
        }
1079
        continue;
5,346✔
1080
      }
1081
    }
1082
    TAOS_STMT2_BIND bindData = bind[bindIdx];
1,389,735✔
1083
    if (bindData.num != rowNum) {
1,388,937✔
1084
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1085
      goto _return;
×
1086
    }
1087

1088
    if ((!(rowNum == 1 && bindData.is_null && *bindData.is_null)) &&
1,388,937✔
1089
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
1,381,034✔
1090
      code = buildInvalidOperationMsgExt(&pBuf, "bind[%d] column type:%d mis-match with buffer type:%d", bindIdx,
×
1091
                                         pColSchema->type, bindData.buffer_type);
×
1092
      goto _return;
×
1093
    }
1094

1095
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
1,389,586✔
1096
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
29,765✔
1097
      if (code) {
29,765✔
1098
        goto _return;
×
1099
      }
1100
      pBind = &ncharBind;
29,765✔
1101
    } else {
1102
      pBind = bind + bindIdx;
1,359,798✔
1103
    }
1104
    int8_t  isBlob = 0;
1,389,629✔
1105
    int32_t bytes = -1;
1,389,629✔
1106
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
1,389,629✔
1107
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
97,087✔
1108
        isBlob = 1;
402✔
1109
        bytes = TSDB_MAX_BLOB_LEN;
402✔
1110
      } else {
1111
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
96,951✔
1112
      }
1113
    }
1114

1115
    if (pColSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
1,389,430✔
1116
      code = tColDataAddValueByBind2WithGeos(pCol, pBind, bytes, initCtxAsText, checkWKB);
5,931✔
1117
    } else if (isBlob == 1) {
1,383,474✔
1118
      if (pDataBlock->pData->pBlobSet == NULL) {
402✔
1119
        code = tBlobSetCreate(1024, 1, &pDataBlock->pData->pBlobSet);
402✔
1120
        if (code) {
402✔
1121
          parserError("stmt2 failed to create blob set, ErrCode: 0x%x", code);
×
1122
          goto _return;
×
1123
        }
1124
      }
1125
      code = tColDataAddValueByBind2WithBlob(pCol, pBind, bytes, pDataBlock->pData->pBlobSet);
402✔
1126
    } else if (IS_DECIMAL_TYPE(pColSchema->type)) {
1,389,108✔
1127
      if (pExtSchema == NULL) {
6,116✔
1128
        code = buildInvalidOperationMsg(&pBuf, "decimal column ext schema is null");
×
1129
        goto _return;
×
1130
      }
1131
      uint8_t precision = 0, scale = 0;
6,116✔
1132
      decimalFromTypeMod(pExtSchema[c].typeMod, &precision, &scale);
6,116✔
1133
      code = tColDataAddValueByBind2WithDecimal(pCol, pBind, bytes, precision, scale);
6,036✔
1134
    } else {
1135
      code = tColDataAddValueByBind2(pCol, pBind, bytes);
1,376,803✔
1136
    }
1137

1138
    if (code) {
1,389,726✔
1139
      parserError("stmt2 failed to add col:%d ,type:%d, ErrCode: 0x%x", c, pColSchema->type, code);
×
1140
      goto _return;
×
1141
    }
1142
    bindIdx++;
1,389,726✔
1143
  }
1144

1145
  parserDebug("stmt2 all %d columns bind %d rows data as col format", boundInfo->numOfBound, rowNum);
215,517✔
1146

1147
_return:
215,517✔
1148

1149
  taosMemoryFree(ncharBind.buffer);
215,517✔
1150
  taosMemoryFree(ncharBind.length);
215,517✔
1151

1152
  return code;
215,495✔
1153
}
1154

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

1168
  if (bind->num != rowNum) {
1,188✔
1169
    return buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1170
  }
1171

1172
  // Column index exceeds the number of columns
1173
  if (colIdx >= pCols->size && pCol == NULL) {
1,188✔
1174
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
1175
  }
1176

1177
  if (bind->buffer_type != pColSchema->type) {
1,188✔
1178
    return buildInvalidOperationMsgExt(&pBuf, "column type:%d mis-match with buffer type:%d", pColSchema->type,
×
1179
                                       bind->buffer_type);
1180
  }
1181

1182
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
1,188✔
1183
    code = convertStmtNcharCol2(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
×
1184
    if (code) {
×
1185
      goto _return;
×
1186
    }
1187
    pBind = &ncharBind;
×
1188
  } else {
1189
    pBind = bind;
1,188✔
1190
  }
1191

1192
  int32_t bytes = -1;
1,188✔
1193
  int8_t  hasBlob = 0;
1,188✔
1194
  if (IS_VAR_DATA_TYPE(pColSchema->type)) {
1,188✔
1195
    if (IS_STR_DATA_BLOB(pColSchema->type)) {
594✔
1196
      bytes = TSDB_MAX_BLOB_LEN;
×
1197
      hasBlob = 1;
×
1198
    } else {
1199
      bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
594✔
1200
    }
1201
  } else {
1202
    bytes = -1;
594✔
1203
  }
1204

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

1229
  if (code) {
1,188✔
1230
    parserError("stmt2 failed to add single col:%d ,type:%d, ErrCode: 0x%x", colIdx, pColSchema->type, code);
×
1231
    goto _return;
×
1232
  }
1233

1234
  parserDebug("stmt col %d bind %d rows data", colIdx, rowNum);
1,188✔
1235

1236
_return:
1,188✔
1237

1238
  taosMemoryFree(ncharBind.buffer);
1,188✔
1239
  taosMemoryFree(ncharBind.length);
1,188✔
1240

1241
  return code;
1,188✔
1242
}
1243

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

1259
  if (NULL == pTSchema || NULL == *pTSchema) {
396✔
1260
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
198✔
1261
  }
1262

1263
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
4,752✔
1264
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
4,356✔
1265
    if (pColSchema->colId <= lastColId) {
4,356✔
1266
      colInOrder = false;
×
1267
    } else {
1268
      lastColId = pColSchema->colId;
4,356✔
1269
    }
1270

1271
    if (bind[c].num != rowNum) {
4,356✔
1272
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1273
      goto _return;
×
1274
    }
1275

1276
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
4,356✔
1277
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
4,356✔
1278
      code = buildInvalidOperationMsgExt(&pBuf, "bind[%d] column type:%d mis-match with buffer type:%d", c,
×
1279
                                         pColSchema->type, bind[c].buffer_type);
×
1280
      goto _return;
×
1281
    }
1282

1283
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
4,356✔
1284
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
396✔
1285
      if (code) {
396✔
1286
        goto _return;
×
1287
      }
1288
      pBindInfos[c].bind = &ncharBind;
396✔
1289
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
3,960✔
1290
      code = initCtxAsText();
396✔
1291
      if (code) {
396✔
1292
        qError("geometry init failed:%s", tstrerror(code));
×
1293
        goto _return;
×
1294
      }
1295
      uint8_t* buf = bind[c].buffer;
396✔
1296
      for (int j = 0; j < bind[c].num; j++) {
792✔
1297
        if (bind[c].is_null && bind[c].is_null[j]) {
396✔
1298
          continue;
×
1299
        }
1300
        code = checkWKB(buf, bind[c].length[j]);
396✔
1301
        if (code) {
396✔
1302
          qError("stmt2 row bind geometry data[%d]:{%s},length:%d must be in WKB format", c, buf, bind[c].length[j]);
×
1303
          goto _return;
×
1304
        }
1305
        buf += bind[c].length[j];
396✔
1306
      }
1307
      pBindInfos[c].bind = bind + c;
396✔
1308
    } else {
1309
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
3,564✔
1310
        hasBlob = 1;
×
1311
      }
1312
      pBindInfos[c].bind = bind + c;
3,564✔
1313
    }
1314

1315
    pBindInfos[c].columnId = pColSchema->colId;
4,356✔
1316
    pBindInfos[c].type = pColSchema->type;
4,356✔
1317
    pBindInfos[c].bytes = pColSchema->bytes;
4,356✔
1318

1319
    if (code) {
4,356✔
1320
      goto _return;
×
1321
    }
1322
  }
1323

1324
  pDataBlock->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
396✔
1325
  if (pDataBlock->pData->pCreateTbReq != NULL) {
396✔
1326
    pDataBlock->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
396✔
1327
  }
1328

1329
  if (hasBlob == 0) {
396✔
1330
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pSchemaExt, pCols,
396✔
1331
                              &pDataBlock->ordered, &pDataBlock->duplicateTs);
1332
  } else {
1333
    code = TSDB_CODE_BLOB_NOT_SUPPORT;
×
1334
  }
1335
  qDebug("stmt2 all %d columns bind %d rows data as row format", boundInfo->numOfBound, rowNum);
396✔
1336

1337
_return:
396✔
1338

1339
  taosMemoryFree(ncharBind.buffer);
396✔
1340
  taosMemoryFree(ncharBind.length);
396✔
1341

1342
  return code;
396✔
1343
}
1344

1345
int32_t buildBoundFields(int32_t numOfBound, int16_t* boundColumns, SSchema* pSchema, int32_t* fieldNum,
1,386✔
1346
                         TAOS_FIELD_E** fields, uint8_t timePrec) {
1347
  if (fields != NULL) {
1,386✔
1348
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
1,188✔
1349
    if (NULL == *fields) {
1,188✔
1350
      return terrno;
×
1351
    }
1352

1353
    SSchema* schema = &pSchema[boundColumns[0]];
1,188✔
1354
    if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
1,188✔
1355
      (*fields)[0].precision = timePrec;
990✔
1356
    }
1357

1358
    for (int32_t i = 0; i < numOfBound; ++i) {
5,544✔
1359
      schema = &pSchema[boundColumns[i]];
4,356✔
1360
      tstrncpy((*fields)[i].name, schema->name, 65);
4,356✔
1361
      (*fields)[i].type = schema->type;
4,356✔
1362
      (*fields)[i].bytes = calcTypeBytesFromSchemaBytes(schema->type, schema->bytes, true);
4,356✔
1363
    }
1364
  }
1365

1366
  *fieldNum = numOfBound;
1,386✔
1367

1368
  return TSDB_CODE_SUCCESS;
1,386✔
1369
}
1370

1371
int32_t buildStbBoundFields(SBoundColInfo boundColsInfo, SSchema* pSchema, int32_t* fieldNum, TAOS_FIELD_ALL** fields,
10,716✔
1372
                            STableMeta* pMeta, void* boundTags, SSHashObj* parsedCols, uint8_t tbNameFlag,
1373
                            int32_t* placeholderOfTags, int32_t* placeholderOfCols) {
1374
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
10,716✔
1375
  bool           hastag = (tags != NULL) && !(tbNameFlag & IS_FIXED_TAG);
10,716✔
1376
  bool           hasPreBindTbname =
10,716✔
1377
      (tbNameFlag & IS_FIXED_VALUE) == 0 && ((tbNameFlag & USING_CLAUSE) != 0 || pMeta->tableType == TSDB_NORMAL_TABLE);
10,716✔
1378
  int32_t numOfBound = boundColsInfo.numOfBound + (hasPreBindTbname ? 1 : 0);
10,716✔
1379
  if (hastag) {
10,716✔
1380
    numOfBound += tags->mixTagsCols ? 0 : tags->numOfBound;
7,152✔
1381
  }
1382

1383
  // Adjust the number of bound fields if there are parsed tags or parsed columns
1384
  if (tags && tags->parseredTags) {
10,716✔
1385
    numOfBound -= tags->parseredTags->numOfTags;
396✔
1386
  }
1387
  if (parsedCols) {
10,716✔
1388
    numOfBound -= tSimpleHashGetSize(parsedCols);
792✔
1389
  }
1390

1391
  int32_t idx = 0;
10,716✔
1392
  if (fields != NULL) {
10,716✔
1393
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_ALL));
10,716✔
1394
    if (NULL == *fields) {
10,716✔
1395
      return terrno;
×
1396
    }
1397
  }
1398

1399
  if (hasPreBindTbname && fields != NULL) {
10,716✔
1400
    (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
6,351✔
1401
    tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
6,351✔
1402
    (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
6,351✔
1403
    (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
6,351✔
1404
    idx++;
6,351✔
1405
  }
1406

1407
  if (hastag && tags->numOfBound > 0 && !tags->mixTagsCols) {
10,716✔
1408
    SSchema* tagSchema = getTableTagSchema(pMeta);
2,985✔
1409

1410
    for (int32_t i = 0; i < tags->numOfBound; ++i) {
11,133✔
1411
      SSchema* schema = &tagSchema[tags->pColIndex[i]];
8,148✔
1412

1413
      if (tags->parseredTags && tags->parseredTags->numOfTags > 0) {
8,148✔
1414
        int32_t tag_idx = schema->colId - 1 - pMeta->tableInfo.numOfColumns;
396✔
1415
        bool    found = false;
396✔
1416
        for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
594✔
1417
          if (tags->parseredTags->pTagIndex[k] == tag_idx) {
396✔
1418
            found = true;
198✔
1419
            break;
198✔
1420
          }
1421
        }
1422
        if (found) {
396✔
1423
          continue;
198✔
1424
        }
1425
      }
1426

1427
      if (placeholderOfTags != NULL) {
7,950✔
1428
        (*placeholderOfTags)++;
7,950✔
1429
      }
1430

1431
      if (fields != NULL) {
7,950✔
1432
        (*fields)[idx].field_type = TAOS_FIELD_TAG;
7,950✔
1433
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[i].name));
7,950✔
1434
        (*fields)[idx].type = schema->type;
7,950✔
1435
        (*fields)[idx].bytes = schema->bytes;
7,950✔
1436
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
7,950✔
1437
          (*fields)[idx].precision = pMeta->tableInfo.precision;
198✔
1438
        }
1439
      }
1440
      idx++;
7,950✔
1441
    }
1442
  }
1443

1444
  if (boundColsInfo.numOfBound > 0) {
10,716✔
1445
    SSchema* schema = &pSchema[boundColsInfo.pColIndex[0]];
10,716✔
1446

1447
    for (int32_t i = 0; i < boundColsInfo.numOfBound; ++i) {
49,845✔
1448
      int16_t idxCol = boundColsInfo.pColIndex[i];
39,129✔
1449

1450
      if (idxCol == pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags) {
39,129✔
1451
        if (fields != NULL) {
2,583✔
1452
          (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
2,583✔
1453
          tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
2,583✔
1454
          (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
2,583✔
1455
          (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
2,583✔
1456
        }
1457
        idx++;
2,583✔
1458
        continue;
2,583✔
1459
      } else if (idxCol < pMeta->tableInfo.numOfColumns) {
36,546✔
1460
        if (parsedCols && tSimpleHashGet(parsedCols, &pSchema[idxCol].colId, sizeof(int16_t))) {
29,400✔
1461
          continue;
990✔
1462
        }
1463
        if (fields != NULL) {
28,410✔
1464
          (*fields)[idx].field_type = TAOS_FIELD_COL;
28,410✔
1465
        }
1466
        if (placeholderOfCols != NULL) {
28,410✔
1467
          (*placeholderOfCols)++;
28,410✔
1468
        }
1469
      } else {
1470
        if (tags->parseredTags && tags->parseredTags->numOfTags > 0) {
7,146✔
1471
          int32_t tag_idx = idxCol - pMeta->tableInfo.numOfColumns;
396✔
1472
          bool    found = false;
396✔
1473
          for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
594✔
1474
            if (tags->parseredTags->pTagIndex[k] == tag_idx) {
396✔
1475
              found = true;
198✔
1476
              break;
198✔
1477
            }
1478
          }
1479
          if (found) {
396✔
1480
            continue;
198✔
1481
          }
1482
        }
1483
        if (fields != NULL) {
6,948✔
1484
          (*fields)[idx].field_type = TAOS_FIELD_TAG;
6,948✔
1485
        }
1486
        if (placeholderOfTags != NULL) {
6,948✔
1487
          (*placeholderOfTags)++;
6,948✔
1488
        }
1489
      }
1490

1491
      if (fields != NULL) {
35,358✔
1492
        schema = &pSchema[idxCol];
35,358✔
1493
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[idx].name));
35,358✔
1494
        (*fields)[idx].type = schema->type;
35,358✔
1495
        (*fields)[idx].bytes = schema->bytes;
35,358✔
1496
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
35,358✔
1497
          (*fields)[idx].precision = pMeta->tableInfo.precision;
10,518✔
1498
        }
1499
        if (TSDB_DATA_TYPE_DECIMAL64 == schema->type || TSDB_DATA_TYPE_DECIMAL == schema->type) {
35,358✔
1500
          decimalFromTypeMod(pMeta->schemaExt[idxCol].typeMod, &(*fields)[idx].precision, &(*fields)[idx].scale);
1,188✔
1501
        }
1502
      }
1503
      idx++;
35,358✔
1504
    }
1505
  }
1506

1507
  if (fieldNum != NULL) {
10,716✔
1508
    *fieldNum = numOfBound;
10,716✔
1509
  }
1510

1511
  return TSDB_CODE_SUCCESS;
10,716✔
1512
}
1513

1514
int32_t qBuildStmtTagFields(void* pBlock, void* boundTags, int32_t* fieldNum, TAOS_FIELD_E** fields) {
198✔
1515
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
198✔
1516
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
198✔
1517
  if (NULL == tags) {
198✔
1518
    return TSDB_CODE_APP_ERROR;
×
1519
  }
1520

1521
  if (pDataBlock->pMeta->tableType != TSDB_SUPER_TABLE && pDataBlock->pMeta->tableType != TSDB_CHILD_TABLE) {
198✔
1522
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1523
  }
1524

1525
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
198✔
1526
  if (tags->numOfBound <= 0) {
198✔
1527
    *fieldNum = 0;
×
1528
    *fields = NULL;
×
1529

1530
    return TSDB_CODE_SUCCESS;
×
1531
  }
1532

1533
  CHECK_CODE(buildBoundFields(tags->numOfBound, tags->pColIndex, pSchema, fieldNum, fields, 0));
198✔
1534

1535
  return TSDB_CODE_SUCCESS;
198✔
1536
}
1537

1538
int32_t qBuildStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields) {
1,188✔
1539
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
1,188✔
1540
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
1,188✔
1541
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
1,188✔
1542
    *fieldNum = 0;
×
1543
    if (fields != NULL) {
×
1544
      *fields = NULL;
×
1545
    }
1546

1547
    return TSDB_CODE_SUCCESS;
×
1548
  }
1549

1550
  CHECK_CODE(buildBoundFields(pDataBlock->boundColsInfo.numOfBound, pDataBlock->boundColsInfo.pColIndex, pSchema,
1,188✔
1551
                              fieldNum, fields, pDataBlock->pMeta->tableInfo.precision));
1552

1553
  return TSDB_CODE_SUCCESS;
1,188✔
1554
}
1555

1556
int32_t qBuildUpdateStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields, SSHashObj* parsedCols) {
990✔
1557
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
990✔
1558
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
990✔
1559
  int32_t        numOfBound = pDataBlock->boundColsInfo.numOfBound + tSimpleHashGetSize(parsedCols);
990✔
1560
  if (numOfBound <= 0) {
990✔
1561
    *fieldNum = 0;
×
1562
    if (fields != NULL) {
×
1563
      *fields = NULL;
×
1564
    }
1565

1566
    return TSDB_CODE_SUCCESS;
×
1567
  }
1568

1569
  if (fields != NULL) {
990✔
1570
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
990✔
1571
    if (NULL == *fields) {
990✔
1572
      return terrno;
×
1573
    }
1574

1575
    int32_t actualIdx = 0;
990✔
1576
    for (int32_t i = 0; i < numOfBound; ++i) {
5,742✔
1577
      SSchema* schema;
1578
      int32_t* idx = (int32_t*)tSimpleHashGet(parsedCols, &i, sizeof(int32_t));
4,752✔
1579
      if (idx) {
4,752✔
1580
        schema = &pSchema[*idx];
792✔
1581
      } else {
1582
        schema = &pSchema[pDataBlock->boundColsInfo.pColIndex[actualIdx++]];
3,960✔
1583
      }
1584
      tstrncpy((*fields)[i].name, schema->name, 65);
4,752✔
1585
      (*fields)[i].type = schema->type;
4,752✔
1586
      (*fields)[i].bytes = calcTypeBytesFromSchemaBytes(schema->type, schema->bytes, true);
4,752✔
1587
      if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
4,752✔
1588
        (*fields)[i].precision = pDataBlock->pMeta->tableInfo.precision;
990✔
1589
      }
1590
    }
1591
  }
1592

1593
  *fieldNum = numOfBound;
990✔
1594

1595
  return TSDB_CODE_SUCCESS;
990✔
1596
}
1597

1598
int32_t qBuildStmtStbColFields(void* pBlock, void* boundTags, SSHashObj* parsedCols, uint8_t tbNameFlag,
10,716✔
1599
                               int32_t* fieldNum, TAOS_FIELD_ALL** fields, int32_t* placeholderOfTags,
1600
                               int32_t* placeholderOfCols) {
1601
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
10,716✔
1602
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
10,716✔
1603
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
10,716✔
1604
    *fieldNum = 0;
×
1605
    if (fields != NULL) {
×
1606
      *fields = NULL;
×
1607
    }
1608

1609
    return TSDB_CODE_SUCCESS;
×
1610
  }
1611

1612
  CHECK_CODE(buildStbBoundFields(pDataBlock->boundColsInfo, pSchema, fieldNum, fields, pDataBlock->pMeta, boundTags,
10,716✔
1613
                                 parsedCols, tbNameFlag, placeholderOfTags, placeholderOfCols));
1614

1615
  return TSDB_CODE_SUCCESS;
10,716✔
1616
}
1617

1618
int32_t qResetStmtColumns(SArray* pCols, bool deepClear) {
×
1619
  int32_t colNum = taosArrayGetSize(pCols);
×
1620

1621
  for (int32_t i = 0; i < colNum; ++i) {
×
1622
    SColData* pCol = (SColData*)taosArrayGet(pCols, i);
×
1623
    if (pCol == NULL) {
×
1624
      parserError("qResetStmtColumns column:%d is NULL", i);
×
1625
      return terrno;
×
1626
    }
1627
    if (deepClear) {
×
1628
      tColDataDeepClear(pCol);
×
1629
    } else {
1630
      tColDataClear(pCol);
×
1631
    }
1632
  }
1633

1634
  return TSDB_CODE_SUCCESS;
×
1635
}
1636

1637
int32_t qResetStmtDataBlock(STableDataCxt* block, bool deepClear) {
15,347,236✔
1638
  int32_t        code = 0;
15,347,236✔
1639
  STableDataCxt* pBlock = (STableDataCxt*)block;
15,347,236✔
1640
  int32_t        colNum = taosArrayGetSize(pBlock->pData->aCol);
15,347,236✔
1641

1642
  int8_t flag = 0;
15,351,550✔
1643
  for (int32_t i = 0; i < colNum; ++i) {
62,449,080✔
1644
    flag = pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
47,094,705✔
1645
    if (pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
47,101,200✔
1646
      SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i);
47,102,011✔
1647
      if (pCol == NULL) {
47,102,712✔
1648
        parserError("qResetStmtDataBlock column:%d is NULL", i);
10✔
1649
        return terrno;
10✔
1650
      }
1651
      if (deepClear) {
47,102,729✔
1652
        tColDataDeepClear(pCol);
23,077,556✔
1653

1654
      } else {
1655
        tColDataClear(pCol);
24,025,173✔
1656
      }
1657

1658
    } else {
1659
      pBlock->pData->aRowP = taosArrayInit(20, POINTER_BYTES);
198✔
1660
    }
1661
  }
1662

1663
  tBlobSetDestroy(pBlock->pData->pBlobSet);
15,354,375✔
1664
  pBlock->pData->pBlobSet = NULL;
15,353,332✔
1665
  return code;
15,356,459✔
1666
}
1667

1668
int32_t qCloneStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, bool reset) {
7,644,277✔
1669
  int32_t code = 0;
7,644,277✔
1670

1671
  *pDst = taosMemoryCalloc(1, sizeof(STableDataCxt));
7,644,277✔
1672
  if (NULL == *pDst) {
7,643,521✔
1673
    return terrno;
×
1674
  }
1675

1676
  STableDataCxt* pNewCxt = (STableDataCxt*)*pDst;
7,645,386✔
1677
  STableDataCxt* pCxt = (STableDataCxt*)pSrc;
7,645,386✔
1678
  pNewCxt->hasBlob = pSrc->hasBlob;
7,645,386✔
1679
  pNewCxt->pSchema = NULL;
7,647,644✔
1680
  pNewCxt->pValues = NULL;
7,644,934✔
1681

1682
  if (pCxt->pMeta) {
7,645,327✔
1683
    pNewCxt->pMeta = tableMetaDup(pCxt->pMeta);
7,647,260✔
1684
    // void* pNewMeta = taosMemoryMalloc(TABLE_META_SIZE(pCxt->pMeta));
1685
    // if (NULL == pNewMeta) {
1686
    //   insDestroyTableDataCxt(*pDst);
1687
    //   return terrno;
1688
    // }
1689
    // memcpy(pNewMeta, pCxt->pMeta, TABLE_META_SIZE(pCxt->pMeta));
1690
    // pNewCxt->pMeta = pNewMeta;
1691
  }
1692

1693
  memcpy(&pNewCxt->boundColsInfo, &pCxt->boundColsInfo, sizeof(pCxt->boundColsInfo));
7,646,905✔
1694
  pNewCxt->boundColsInfo.pColIndex = NULL;
7,645,701✔
1695

1696
  if (pCxt->boundColsInfo.pColIndex) {
7,647,222✔
1697
    void* pNewColIdx = taosMemoryMalloc(pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
7,645,008✔
1698
    if (NULL == pNewColIdx) {
7,643,487✔
1699
      insDestroyTableDataCxt(*pDst);
×
1700
      return terrno;
×
1701
    }
1702
    memcpy(pNewColIdx, pCxt->boundColsInfo.pColIndex,
7,644,351✔
1703
           pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
7,643,487✔
1704
    pNewCxt->boundColsInfo.pColIndex = pNewColIdx;
7,646,962✔
1705
  }
1706

1707
  if (pCxt->pData) {
7,649,858✔
1708
    int8_t         flag = 1;
7,644,704✔
1709
    SSubmitTbData* pNewTb = (SSubmitTbData*)taosMemoryMalloc(sizeof(SSubmitTbData));
7,644,704✔
1710
    if (NULL == pNewTb) {
7,640,890✔
1711
      insDestroyTableDataCxt(*pDst);
×
1712
      return terrno;
×
1713
    }
1714

1715
    memcpy(pNewTb, pCxt->pData, sizeof(*pCxt->pData));
7,640,890✔
1716
    pNewTb->pCreateTbReq = NULL;
7,643,079✔
1717
    if (pNewTb->pBlobSet != NULL) {
7,645,336✔
1718
      flag = pNewTb->pBlobSet->type;
41✔
1719
    }
1720
    pNewTb->pBlobSet = NULL;
7,642,754✔
1721

1722
    pNewTb->aCol = taosArrayDup(pCxt->pData->aCol, NULL);
7,642,655✔
1723
    if (NULL == pNewTb->aCol) {
7,648,078✔
1724
      insDestroyTableDataCxt(*pDst);
×
1725
      return terrno;
×
1726
    }
1727

1728
    pNewCxt->pData = pNewTb;
7,643,498✔
1729

1730
    if (reset) {
7,643,802✔
1731
      code = qResetStmtDataBlock(*pDst, true);
7,600,939✔
1732
    }
1733
  }
1734

1735
  return code;
7,649,953✔
1736
}
1737

1738
int32_t qRebuildStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, uint64_t uid, uint64_t suid, int32_t vgId,
47,430✔
1739
                              bool rebuildCreateTb) {
1740
  int32_t code = qCloneStmtDataBlock(pDst, pSrc, false);
47,430✔
1741
  if (code) {
47,453✔
1742
    return code;
×
1743
  }
1744

1745
  STableDataCxt* pBlock = (STableDataCxt*)*pDst;
47,453✔
1746
  if (pBlock->pMeta) {
47,453✔
1747
    pBlock->pMeta->uid = uid;
47,453✔
1748
    pBlock->pMeta->vgId = vgId;
47,453✔
1749
    pBlock->pMeta->suid = suid;
47,453✔
1750
  }
1751

1752
  pBlock->pData->suid = suid;
47,453✔
1753
  pBlock->pData->uid = uid;
47,428✔
1754

1755
  if (rebuildCreateTb && NULL == pBlock->pData->pCreateTbReq) {
47,428✔
1756
    pBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
14,208✔
1757
    if (NULL == pBlock->pData->pCreateTbReq) {
14,208✔
1758
      return terrno;
×
1759
    }
1760
  }
1761

1762
  return TSDB_CODE_SUCCESS;
47,428✔
1763
}
1764

1765
STableMeta* qGetTableMetaInDataBlock(STableDataCxt* pDataBlock) { return ((STableDataCxt*)pDataBlock)->pMeta; }
15,528,255✔
1766

1767
void qDestroyStmtDataBlock(STableDataCxt* pBlock) {
23,174,983✔
1768
  if (pBlock == NULL) {
23,174,983✔
1769
    return;
7,768,580✔
1770
  }
1771

1772
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
15,406,403✔
1773
  insDestroyTableDataCxt(pDataBlock);
15,406,403✔
1774
}
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