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

taosdata / TDengine / #5044

06 May 2026 02:35AM UTC coverage: 73.169% (+0.06%) from 73.107%
#5044

push

travis-ci

web-flow
feat: [6659794715] cpu limit (#35153)

244 of 275 new or added lines in 23 files covered. (88.73%)

526 existing lines in 141 files now uncovered.

277745 of 379596 relevant lines covered (73.17%)

133740972.66 hits per line

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

76.91
/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,311,897✔
35
  if (pDataBlock == NULL || pDataBlock->pData == NULL) {
7,311,897✔
36
    qError("stmt qCloneCurrentTbData input is null, maybe not bind data before execute");
5,218✔
37
    return TSDB_CODE_TSC_STMT_API_ERROR;
100✔
38
  }
39
  *pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
7,308,338✔
40
  if (NULL == *pData) {
7,305,154✔
41
    return terrno;
×
42
  }
43

44
  int8_t         flag = 1;
7,310,088✔
45
  SSubmitTbData* pNew = *pData;
7,310,088✔
46

47
  *pNew = *pDataBlock->pData;
7,310,438✔
48
  pNew->pBlobSet = NULL;
7,313,255✔
49

50
  int32_t code = cloneSVreateTbReq(pDataBlock->pData->pCreateTbReq, &pNew->pCreateTbReq);
7,315,389✔
51
  if (TSDB_CODE_SUCCESS != code) {
7,311,869✔
52
    taosMemoryFreeClear(*pData);
×
53
    return code;
×
54
  }
55
  pNew->aCol = taosArrayDup(pDataBlock->pData->aCol, NULL);
7,311,869✔
56
  if (!pNew->aCol) {
7,317,008✔
57
    code = terrno;
×
58
    taosMemoryFreeClear(*pData);
×
59
    return code;
×
60
  }
61

62
  int32_t colNum = taosArrayGetSize(pNew->aCol);
7,310,981✔
63
  for (int32_t i = 0; i < colNum; ++i) {
29,831,005✔
64
    if (pDataBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
22,514,310✔
65
      SColData* pCol = (SColData*)taosArrayGet(pNew->aCol, i);
22,516,367✔
66
      tColDataDeepClear(pCol);
22,515,184✔
67
    } else {
68
      pNew->aCol = taosArrayInit(20, POINTER_BYTES);
200✔
69
      if (pNew->aCol == NULL) {
200✔
70
        code = terrno;
×
71
        taosMemoryFreeClear(*pData);
×
72
        return code;
×
73
      }
74
    }
75
  }
76

77
  return code;
7,316,695✔
78
}
79

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

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

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

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

100
  if (pStmt->freeArrayFunc) {
763,634✔
101
    pStmt->freeArrayFunc(pVgDataBlocks);
763,791✔
102
  }
103
  return code;
763,580✔
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,312,106✔
128
  int32_t             code = TSDB_CODE_SUCCESS;
7,312,106✔
129
  SArray*             pVgDataBlocks = NULL;
7,312,106✔
130
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
7,313,531✔
131

132
  // merge according to vgId
133
  if (taosHashGetSize(pBlockHash) > 0) {
7,314,231✔
134
    code = insMergeTableDataCxt(pBlockHash, &pVgDataBlocks, true);
7,313,252✔
135
  }
136

137
  if (TSDB_CODE_SUCCESS == code) {
7,314,339✔
138
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks, false);
7,314,706✔
139
  }
140

141
  if (pStmt->freeArrayFunc) {
7,312,760✔
142
    pStmt->freeArrayFunc(pVgDataBlocks);
7,313,793✔
143
  }
144
  return code;
7,301,575✔
145
}
146

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

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

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

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

170
  bool  isJson = false;
7,409✔
171
  STag* pTag = NULL;
7,409✔
172

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

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

201
      isJson = true;
110✔
202
      char* tmp = taosMemoryCalloc(1, colLen + 1);
110✔
203
      if (!tmp) {
110✔
204
        code = terrno;
×
205
        goto end;
×
206
      }
207
      memcpy(tmp, bind[c].buffer, colLen);
110✔
208
      code = parseJsontoTagData(tmp, pTagArray, &pTag, &pBuf, charsetCxt);
110✔
209
      taosMemoryFree(tmp);
110✔
210
      if (code != TSDB_CODE_SUCCESS) {
110✔
211
        goto end;
×
212
      }
213
    } else {
214
      STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
20,465✔
215
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
20,465✔
216
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
17,942✔
217
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
2,724✔
218
          code = initCtxAsText();
201✔
219
          if (code) {
201✔
220
            qError("geometry init failed:%s", tstrerror(code));
×
221
            goto end;
×
222
          }
223
          code = checkWKB(bind[c].buffer, colLen);
201✔
224
          if (code) {
201✔
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;
2,724✔
230
        val.nData = colLen;
2,724✔
231
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
17,741✔
232
        int32_t output = 0;
947✔
233
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
947✔
234
        if (p == NULL) {
947✔
235
          code = terrno;
×
236
          goto end;
×
237
        }
238
        if (!taosMbsToUcs4(bind[c].buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
947✔
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;
947✔
251
        val.nData = output;
947✔
252
      } else {
253
        memcpy(&val.i64, bind[c].buffer, colLen);
16,794✔
254
      }
255
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
20,465✔
256
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
257
        goto end;
×
258
      }
259
      if (NULL == taosArrayPush(pTagArray, &val)) {
20,465✔
260
        code = terrno;
×
261
        goto end;
×
262
      }
263
    }
264
  }
265

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

270
  if (NULL == pDataBlock->pData->pCreateTbReq) {
7,409✔
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;
7,409✔
278
    taosMemoryFreeClear(tmp->name);
7,409✔
279
    taosMemoryFreeClear(tmp->ctb.pTag);
7,409✔
280
    taosMemoryFreeClear(tmp->ctb.stbName);
7,409✔
281
    taosArrayDestroy(tmp->ctb.tagName);
7,409✔
282
    tmp->ctb.tagName = NULL;
7,409✔
283
  }
284

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

289
end:
7,409✔
290
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
27,974✔
291
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
20,565✔
292
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
20,565✔
293
      taosMemoryFreeClear(p->pData);
1,047✔
294
    }
295
  }
296
  taosArrayDestroy(pTagArray);
7,409✔
297
  taosArrayDestroy(tagName);
7,409✔
298
  taosMemoryFree(pTag);
7,409✔
299

300
  return code;
7,409✔
301
}
302

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

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

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

324
  for (int32_t i = 0; i < src->num; ++i) {
829,962✔
325
    if (src->is_null && src->is_null[i]) {
823,889✔
326
      continue;
331,398✔
327
    }
328

329
    if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
492,491✔
330
                       (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output,
492,491✔
331
                       charsetCxt)) {
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;
492,491✔
341
  }
342

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

347
  return TSDB_CODE_SUCCESS;
6,073✔
348
}
349

350
int32_t qBindStmtStbColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
1,707,448✔
351
                              STSchema** pTSchema, SBindInfo* pBindInfos, void* charsetCxt) {
352
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
1,707,448✔
353
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
1,707,448✔
354
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
1,720,424✔
355
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
1,720,336✔
356
  int32_t          rowNum = bind->num;
1,720,688✔
357
  TAOS_MULTI_BIND  ncharBind = {0};
1,720,644✔
358
  TAOS_MULTI_BIND* pBind = NULL;
1,721,264✔
359
  int32_t          code = 0;
1,721,264✔
360
  int16_t          lastColId = -1;
1,721,264✔
361
  bool             colInOrder = true;
1,721,264✔
362

363
  if (NULL == pTSchema || NULL == *pTSchema) {
1,721,264✔
364
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
7,079✔
365
  }
366

367
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
8,880,883✔
368
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
7,159,584✔
369
    if (pColSchema->colId <= lastColId) {
7,160,860✔
370
      colInOrder = false;
×
371
    } else {
372
      lastColId = pColSchema->colId;
7,162,232✔
373
    }
374
    // SColData* pCol = taosArrayGet(pCols, c);
375

376
    if (bind[c].num != rowNum) {
7,158,016✔
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)) &&
7,160,520✔
382
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
7,160,313✔
383
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
384
      goto _return;
×
385
    }
386

387
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
7,160,084✔
388
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
1,820✔
389
      if (code) {
1,820✔
390
        goto _return;
×
391
      }
392
      pBind = &ncharBind;
1,820✔
393
    } else {
394
      pBind = bind + c;
7,158,396✔
395
    }
396

397
    pBindInfos[c].columnId = pColSchema->colId;
7,157,752✔
398
    pBindInfos[c].bind = pBind;
7,161,084✔
399
    pBindInfos[c].type = pColSchema->type;
7,159,904✔
400
  }
401

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

405
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
1,722,360✔
406

407
_return:
1,722,360✔
408

409
  taosMemoryFree(ncharBind.buffer);
1,723,416✔
410
  taosMemoryFree(ncharBind.length);
1,720,776✔
411

412
  return code;
1,722,228✔
413
}
414

415
int32_t qBindStmtColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
7,190,051✔
416
                           void* charsetCxt) {
417
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
7,190,051✔
418
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
7,190,051✔
419
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
7,193,602✔
420
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
7,193,935✔
421
  int32_t          rowNum = bind->num;
7,194,302✔
422
  TAOS_MULTI_BIND  ncharBind = {0};
7,195,087✔
423
  TAOS_MULTI_BIND* pBind = NULL;
7,191,468✔
424
  int32_t          code = 0;
7,191,468✔
425

426
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
28,846,336✔
427
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
21,654,629✔
428
    SColData* pCol = taosArrayGet(pCols, c);
21,655,329✔
429

430
    if (bind[c].num != rowNum) {
21,657,096✔
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)) &&
21,654,962✔
436
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
21,650,086✔
437
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
438
      goto _return;
×
439
    }
440

441
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
21,656,729✔
442
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
4,153✔
443
      if (code) {
4,153✔
444
        goto _return;
×
445
      }
446
      pBind = &ncharBind;
4,153✔
447
    } else {
448
      pBind = bind + c;
21,654,010✔
449
    }
450

451
    int32_t bytes = 0;
21,652,512✔
452
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
21,652,512✔
453
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
24,727✔
454
        bytes = pColSchema->bytes - BLOBSTR_HEADER_SIZE;
×
455
      } else {
456
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
21,125✔
457
      }
458
    } else {
459
      bytes = -1;
21,627,034✔
460
    }
461
    code = tColDataAddValueByBind(pCol, pBind, bytes, initCtxAsText, checkWKB);
21,648,159✔
462
    if (code) {
21,659,199✔
463
      goto _return;
4,331✔
464
    }
465
  }
466

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

469
_return:
7,199,573✔
470

471
  taosMemoryFree(ncharBind.buffer);
7,200,024✔
472
  taosMemoryFree(ncharBind.length);
7,199,338✔
473

474
  return code;
7,198,988✔
475
}
476

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

490
  for (int c = 0; c < numOfBound; ++c) {
2,929✔
491
    if (tSimpleHashGet(parsedCols, &c, sizeof(c))) {
2,424✔
492
      continue;
404✔
493
    }
494
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[actualIndex]];
2,020✔
495
    SColData* pCol = taosArrayGet(pCols, actualIndex);
2,020✔
496

497
    if (bind[c].num != rowNum) {
2,020✔
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)) &&
2,020✔
503
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
2,020✔
504
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
505
      goto _return;
×
506
    }
507

508
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
2,020✔
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;
2,020✔
516
    }
517

518
    int32_t bytes = 0;
2,020✔
519
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
2,020✔
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;
2,020✔
527
    }
528
    code = tColDataAddValueByBind(pCol, pBind, bytes, initCtxAsText, checkWKB);
2,020✔
529
    if (code) {
2,020✔
530
      goto _return;
×
531
    }
532
    actualIndex++;
2,020✔
533
  }
534

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

537
_return:
505✔
538

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

542
  return code;
505✔
543
}
544

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

557
  if (bind->num != rowNum) {
1,400✔
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,400✔
563
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
564
  }
565

566
  if (bind->buffer_type != pColSchema->type) {
1,400✔
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,400✔
572
    code = convertStmtNcharCol(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
100✔
573
    if (code) {
100✔
574
      goto _return;
×
575
    }
576
    pBind = &ncharBind;
100✔
577
  } else {
578
    pBind = bind;
1,300✔
579
  }
580

581
  code = tColDataAddValueByBind(pCol, pBind,
1,400✔
582
                                IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1,
1,400✔
583
                                initCtxAsText, checkWKB);
584

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

587
_return:
1,400✔
588

589
  taosMemoryFree(ncharBind.buffer);
1,400✔
590
  taosMemoryFree(ncharBind.length);
1,400✔
591

592
  return code;
1,400✔
593
}
594

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

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

616
  SArray* tagName;
617
  if (tags->parseredTags) {
546,212✔
618
    tagName = taosArrayDup(tags->parseredTags->STagNames, NULL);
2,400✔
619
  } else {
620
    tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
543,812✔
621
  }
622

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

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

630
  bool  isJson = false;
546,134✔
631
  STag* pTag = NULL;
546,134✔
632

633
  int bindIdx = 0;
546,134✔
634
  for (int c = 0; c < tags->numOfBound; ++c) {
1,655,420✔
635
    if (bind == NULL) {
1,111,512✔
636
      break;
2,100✔
637
    }
638
    if (tags->parseredTags) {
1,109,412✔
639
      bool found = false;
26,400✔
640
      for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
72,000✔
641
        if (tags->parseredTags->pTagIndex[k] == tags->pColIndex[c]) {
50,400✔
642
          found = true;
4,800✔
643
          break;
4,800✔
644
        }
645
      }
646
      if (found) {
26,400✔
647
        continue;
4,800✔
648
      }
649
    }
650

651
    TAOS_STMT2_BIND bindData = bind[bindIdx++];
1,104,612✔
652

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

657
    SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
1,104,612✔
658
    int32_t  colLen = pTagSchema->bytes;
1,104,612✔
659
    if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
1,104,612✔
660
      if (IS_STR_DATA_BLOB(pTagSchema->type)) {
546,751✔
661
        return TSDB_CODE_BLOB_NOT_SUPPORT_TAG;
×
662
      }
663
      if (!bindData.length) {
546,751✔
664
        code = buildInvalidOperationMsg(&pBuf, "var tag length is null");
×
665
        goto end;
100✔
666
      }
667
      colLen = bindData.length[0];
546,751✔
668
      if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
546,751✔
669
        code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
×
670
        goto end;
×
671
      }
672
    }
673
    if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
2,209,341✔
674
      code = terrno;
×
675
      goto end;
×
676
    }
677
    if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
1,104,729✔
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);
×
UNCOV
692
      if (code != TSDB_CODE_SUCCESS) {
×
693
        goto end;
×
694
      }
695
    } else {
696
      STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
1,104,729✔
697
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
1,104,729✔
698
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
565,560✔
699
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
542,321✔
700
          code = initCtxAsText();
3,100✔
701
          if (code) {
3,100✔
702
            qError("geometry init failed:%s", tstrerror(code));
×
703
            goto end;
100✔
704
          }
705
          code = checkWKB(bindData.buffer, colLen);
3,100✔
706
          if (code) {
3,100✔
707
            qError("stmt2 bind invalid geometry tag:%s, must be WKB format", (char*)bindData.buffer);
87✔
708
            goto end;
100✔
709
          }
710
        }
711
        val.pData = (uint8_t*)bindData.buffer;
542,286✔
712
        val.nData = colLen;
542,286✔
713
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
562,408✔
714
        int32_t output = 0;
4,300✔
715
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
4,300✔
716
        if (p == NULL) {
4,300✔
717
          code = terrno;
×
718
          goto end;
×
719
        }
720
        if (colLen != 0) {
4,300✔
721
          if (!taosMbsToUcs4(bindData.buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
4,100✔
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;
4,300✔
735
        val.nData = output;
4,300✔
736

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

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

759
  if (pCreateTbReq) {
546,060✔
760
    code = insBuildCreateTbReq(pCreateTbReq, tName, pTag, suid, sTableName, tagName,
534,135✔
761
                               pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
534,135✔
762
    pTag = NULL;
534,148✔
763
    goto end;
534,148✔
764
  }
765

766
  if (NULL == pDataBlock->pData->pCreateTbReq) {
11,925✔
767
    pDataBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
1,316✔
768
    if (NULL == pDataBlock->pData->pCreateTbReq) {
1,316✔
769
      code = terrno;
×
770
      goto end;
×
771
    }
772
  } else {
773
    SVCreateTbReq* tmp = pDataBlock->pData->pCreateTbReq;
10,609✔
774
    taosMemoryFreeClear(tmp->name);
10,609✔
775
    taosMemoryFreeClear(tmp->ctb.pTag);
10,609✔
776
    taosMemoryFreeClear(tmp->ctb.stbName);
10,609✔
777
    taosArrayDestroy(tmp->ctb.tagName);
10,609✔
778
    tmp->ctb.tagName = NULL;
10,674✔
779
  }
780

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

785
end:
546,238✔
786
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
1,655,758✔
787
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
1,109,507✔
788
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
1,109,520✔
789
      taosMemoryFreeClear(p->pData);
4,300✔
790
    }
791
  }
792
  taosArrayDestroy(pTagArray);
546,004✔
793
  taosArrayDestroy(tagName);
546,225✔
794
  taosMemoryFree(pTag);
546,160✔
795

796
  return code;
546,134✔
797
}
798

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

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

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

815
  char* src_buf = src->buffer;
2,530✔
816
  char* dst_buf = dst->buffer;
2,530✔
817
  for (int32_t i = 0; i < src->num; ++i) {
36,330✔
818
    if (src->is_null && src->is_null[i]) {
33,800✔
819
      continue;
100✔
820
    }
821

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

827
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
33,600✔
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;
33,600✔
837
    src_buf += src->length[i];
33,600✔
838
    dst_buf += output;
33,600✔
839
  }
840

841
  dst->buffer_type = src->buffer_type;
2,530✔
842
  dst->is_null = src->is_null;
2,530✔
843
  dst->num = src->num;
2,530✔
844

845
  return TSDB_CODE_SUCCESS;
2,530✔
846
}
847

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

870
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
9,104,598✔
871
    if (TSDB_DATA_TYPE_NCHAR == pSchema[boundInfo->pColIndex[c]].type) {
8,064,023✔
872
      ncharColNums++;
2,530✔
873
    }
874
  }
875
  if (ncharColNums > 0) {
1,043,400✔
876
    ncharBinds = taosArrayInit(ncharColNums, sizeof(ncharBind));
2,530✔
877
    if (!ncharBinds) {
2,530✔
UNCOV
878
      code = terrno;
×
879
      TAOS_CHECK_GOTO(code, &lino, _return);
×
880
    }
881
  }
882

883
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
9,115,013✔
884
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
7,973,589✔
885
    if (pColSchema->colId <= lastColId) {
8,068,567✔
886
      colInOrder = false;
700✔
887
    } else {
888
      lastColId = pColSchema->colId;
8,070,815✔
889
    }
890

891
    if (parsedCols) {
8,070,318✔
892
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
14,400✔
893
      if (pParsedVal) {
14,400✔
894
        pBindInfos[c].columnId = pColSchema->colId;
3,000✔
895
        pBindInfos[c].type = pColSchema->type;
3,000✔
896
        pBindInfos[c].bytes = pColSchema->bytes;
3,000✔
897
        continue;
3,000✔
898
      }
899
    }
900

901
    TAOS_STMT2_BIND bindData = bind[bindIdx];
8,067,318✔
902

903
    if (bindData.num != rowNum) {
8,066,605✔
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)) &&
8,066,605✔
909
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
8,065,758✔
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) {
8,066,271✔
916
      code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
2,530✔
917
      TAOS_CHECK_GOTO(code, &lino, _return);
2,530✔
918

919
      if (!taosArrayPush(ncharBinds, &ncharBind)) {
2,530✔
920
        code = terrno;
×
921
        goto _return;
×
922
      }
923
      pBindInfos[c].bind = taosArrayGetLast(ncharBinds);
2,530✔
924
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
8,062,782✔
925
      code = initCtxAsText();
1,300✔
926
      if (code) {
1,300✔
927
        parserError("stmt2 bind geometry init failed, ErrCode: 0x%x", code);
×
928
        goto _return;
×
929
      }
930
      uint8_t* buf = bindData.buffer;
1,300✔
931
      for (int j = 0; j < bindData.num; j++) {
2,900✔
932
        if (bindData.is_null && bindData.is_null[j]) {
1,600✔
933
          continue;
100✔
934
        }
935
        code = checkWKB(buf, bindData.length[j]);
1,500✔
936
        if (code) {
1,500✔
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];
1,500✔
941
      }
942
      pBindInfos[c].bind = bind + bindIdx;
1,300✔
943
    } else {
944
      if (IS_STR_DATA_BLOB(pColSchema->type)) hasBlob = 1;
8,061,781✔
945
      pBindInfos[c].bind = bind + bindIdx;
8,061,526✔
946
    }
947

948
    pBindInfos[c].columnId = pColSchema->colId;
8,064,609✔
949
    pBindInfos[c].type = pColSchema->type;
8,066,598✔
950
    pBindInfos[c].bytes = pColSchema->bytes;
8,067,758✔
951

952
    bindIdx++;
8,068,453✔
953
  }
954

955
  if (hasBlob == 0) {
1,043,753✔
956
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pSchemaExt, pCols,
1,043,510✔
957
                              &pDataBlock->ordered, &pDataBlock->duplicateTs);
958
    TAOS_CHECK_GOTO(code, &lino, _return);
1,043,615✔
959
  } else {
960
    code = tBlobSetCreate(1024, 1, ppBlob);
243✔
961
    TAOS_CHECK_GOTO(code, &lino, _return);
100✔
962

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

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

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

983
  return code;
1,043,717✔
984
}
985

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

991
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
43,793,540✔
992
  // if (dst->buffer_length < newBuflen) {
993
  dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
44,175,824✔
994
  if (NULL == dst->buffer) {
42,982,657✔
995
    return terrno;
×
996
  }
997
  //}
998

999
  if (NULL == dst->length) {
43,232,554✔
1000
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
43,470,342✔
1001
    if (NULL == dst->length) {
43,637,917✔
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;
43,574,976✔
1009
  char* dst_buf = dst->buffer;
43,550,782✔
1010
  for (int32_t i = 0; i < src->num; ++i) {
87,446,246✔
1011
    if (src->is_null && src->is_null[i]) {
43,316,574✔
1012
      continue;
2,150,142✔
1013
    }
1014

1015
    if (src->length[i] == 0) {
41,510,295✔
1016
      dst->length[i] = 0;
7,060✔
1017
      continue;
7,060✔
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)) {
41,742,760✔
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;
40,840,157✔
1032
    src_buf += src->length[i];
41,211,167✔
1033
    dst_buf += output;
41,831,290✔
1034
  }
1035

1036
  dst->buffer_type = src->buffer_type;
43,709,914✔
1037
  dst->is_null = src->is_null;
43,940,260✔
1038
  dst->num = src->num;
44,103,670✔
1039

1040
  return TSDB_CODE_SUCCESS;
43,651,891✔
1041
}
1042

1043
int32_t qBindStmtColsValue2(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
70,345,250✔
1044
                            int32_t msgBufLen, void* charsetCxt) {
1045
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
70,345,250✔
1046
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
70,345,250✔
1047
  SSchemaExt*      pExtSchema = getTableColumnExtSchema(pDataBlock->pMeta);
70,888,266✔
1048
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
71,922,168✔
1049
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
72,060,737✔
1050
  int32_t          rowNum = bind->num;
72,102,380✔
1051
  TAOS_STMT2_BIND  ncharBind = {0};
71,391,868✔
1052
  TAOS_STMT2_BIND* pBind = NULL;
70,741,256✔
1053
  int32_t          code = 0;
70,741,256✔
1054
  int32_t          lino = 0;
70,741,256✔
1055
  int32_t          bindIdx = 0;
70,741,256✔
1056

1057
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
828,819,979✔
1058
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
755,461,013✔
1059
    SColData* pCol = taosArrayGet(pCols, c);
753,119,454✔
1060
    if (pCol == NULL || pColSchema == NULL) {
738,915,421✔
1061
      code = buildInvalidOperationMsg(&pBuf, "get column schema or column data failed");
×
1062
      goto _return;
×
1063
    }
1064

1065
    if (boundInfo->pColIndex[c] == 0) {
741,255,977✔
1066
      pCol->cflag |= COL_IS_KEY;
71,088,857✔
1067
    }
1068

1069
    if (parsedCols) {
745,562,690✔
1070
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
14,100✔
1071
      if (pParsedVal) {
14,100✔
1072
        for (int row = 0; row < rowNum; row++) {
5,600✔
1073
          code = tColDataAppendValue(pCol, pParsedVal);
2,900✔
1074
          if (code) {
2,900✔
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;
2,700✔
1080
      }
1081
    }
1082
    TAOS_STMT2_BIND bindData = bind[bindIdx];
745,559,990✔
1083
    if (bindData.num != rowNum) {
749,505,111✔
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)) &&
749,505,111✔
1089
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
723,550,904✔
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) {
745,466,786✔
1096
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
43,761,330✔
1097
      if (code) {
43,173,461✔
1098
        goto _return;
×
1099
      }
1100
      pBind = &ncharBind;
43,173,461✔
1101
    } else {
1102
      pBind = bind + bindIdx;
708,514,582✔
1103
    }
1104
    int8_t  isBlob = 0;
750,832,825✔
1105
    int32_t bytes = -1;
750,832,825✔
1106
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
750,832,825✔
1107
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
70,997,024✔
1108
        isBlob = 1;
228✔
1109
        bytes = TSDB_MAX_BLOB_LEN;
228✔
1110
      } else {
1111
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
88,408,880✔
1112
      }
1113
    }
1114

1115
    if (pColSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
770,304,788✔
1116
      code = tColDataAddValueByBind2WithGeos(pCol, pBind, bytes, initCtxAsText, checkWKB);
2,085,260✔
1117
    } else if (isBlob == 1) {
755,924,328✔
1118
      if (pDataBlock->pData->pBlobSet == NULL) {
228✔
1119
        code = tBlobSetCreate(1024, 1, &pDataBlock->pData->pBlobSet);
193✔
1120
        if (code) {
193✔
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);
228✔
1126
    } else if (IS_DECIMAL_TYPE(pColSchema->type)) {
755,925,171✔
1127
      if (pExtSchema == NULL) {
751✔
1128
        code = buildInvalidOperationMsg(&pBuf, "decimal column ext schema is null");
×
1129
        goto _return;
×
1130
      }
1131
      uint8_t precision = 0, scale = 0;
751✔
1132
      decimalFromTypeMod(pExtSchema[c].typeMod, &precision, &scale);
751✔
1133
      code = tColDataAddValueByBind2WithDecimal(pCol, pBind, bytes, precision, scale);
1,071✔
1134
    } else {
1135
      code = tColDataAddValueByBind2(pCol, pBind, bytes);
755,699,430✔
1136
    }
1137

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

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

1147
_return:
71,252,260✔
1148

1149
  taosMemoryFree(ncharBind.buffer);
71,477,350✔
1150
  taosMemoryFree(ncharBind.length);
70,645,218✔
1151

1152
  return code;
71,221,526✔
1153
}
1154

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

1168
  if (bind->num != rowNum) {
600✔
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) {
600✔
1174
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
1175
  }
1176

1177
  if (bind->buffer_type != pColSchema->type) {
600✔
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) {
600✔
1183
    code = convertStmtNcharCol2(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
×
1184
    if (code) {
×
1185
      goto _return;
×
1186
    }
1187
    pBind = &ncharBind;
×
1188
  } else {
1189
    pBind = bind;
600✔
1190
  }
1191

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

1205
  if (hasBlob) {
600✔
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) {
600✔
1215
    code = tColDataAddValueByBind2WithGeos(pCol, pBind, bytes, initCtxAsText, checkWKB);
×
1216
  } else if (IS_DECIMAL_TYPE(pColSchema->type)) {
600✔
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);
600✔
1227
  }
1228

1229
  if (code) {
600✔
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);
600✔
1235

1236
_return:
600✔
1237

1238
  taosMemoryFree(ncharBind.buffer);
600✔
1239
  taosMemoryFree(ncharBind.length);
600✔
1240

1241
  return code;
600✔
1242
}
1243

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

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

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

1271
    if (bind[c].num != rowNum) {
2,200✔
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)) &&
2,200✔
1277
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
2,200✔
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) {
2,200✔
1284
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
200✔
1285
      if (code) {
200✔
1286
        goto _return;
×
1287
      }
1288
      pBindInfos[c].bind = &ncharBind;
200✔
1289
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
2,000✔
1290
      code = initCtxAsText();
200✔
1291
      if (code) {
200✔
1292
        qError("geometry init failed:%s", tstrerror(code));
×
1293
        goto _return;
×
1294
      }
1295
      uint8_t* buf = bind[c].buffer;
200✔
1296
      for (int j = 0; j < bind[c].num; j++) {
400✔
1297
        if (bind[c].is_null && bind[c].is_null[j]) {
200✔
1298
          continue;
×
1299
        }
1300
        code = checkWKB(buf, bind[c].length[j]);
200✔
1301
        if (code) {
200✔
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];
200✔
1306
      }
1307
      pBindInfos[c].bind = bind + c;
200✔
1308
    } else {
1309
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
1,800✔
1310
        hasBlob = 1;
×
1311
      }
1312
      pBindInfos[c].bind = bind + c;
1,800✔
1313
    }
1314

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

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

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

1329
  if (hasBlob == 0) {
200✔
1330
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pSchemaExt, pCols,
200✔
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);
200✔
1336

1337
_return:
200✔
1338

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

1342
  return code;
200✔
1343
}
1344

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

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

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

1366
  *fieldNum = numOfBound;
707✔
1367

1368
  return TSDB_CODE_SUCCESS;
707✔
1369
}
1370

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

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

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

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

1407
  if (hastag && tags->numOfBound > 0 && !tags->mixTagsCols) {
5,424✔
1408
    SSchema* tagSchema = getTableTagSchema(pMeta);
1,514✔
1409

1410
    for (int32_t i = 0; i < tags->numOfBound; ++i) {
5,642✔
1411
      SSchema* schema = &tagSchema[tags->pColIndex[i]];
4,128✔
1412

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

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

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

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

1447
    for (int32_t i = 0; i < boundColsInfo.numOfBound; ++i) {
25,250✔
1448
      int16_t idxCol = boundColsInfo.pColIndex[i];
19,826✔
1449

1450
      if (idxCol == pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags) {
19,826✔
1451
        if (fields != NULL) {
1,310✔
1452
          (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
1,310✔
1453
          tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
1,310✔
1454
          (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
1,310✔
1455
          (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
1,310✔
1456
        }
1457
        idx++;
1,310✔
1458
        continue;
1,310✔
1459
      } else if (idxCol < pMeta->tableInfo.numOfColumns) {
18,516✔
1460
        if (parsedCols && tSimpleHashGet(parsedCols, &pSchema[idxCol].colId, sizeof(int16_t))) {
14,896✔
1461
          continue;
500✔
1462
        }
1463
        if (fields != NULL) {
14,396✔
1464
          (*fields)[idx].field_type = TAOS_FIELD_COL;
14,396✔
1465
        }
1466
        if (placeholderOfCols != NULL) {
14,396✔
1467
          (*placeholderOfCols)++;
14,396✔
1468
        }
1469
      } else {
1470
        if (tags->parseredTags && tags->parseredTags->numOfTags > 0) {
3,620✔
1471
          int32_t tag_idx = idxCol - pMeta->tableInfo.numOfColumns;
200✔
1472
          bool    found = false;
200✔
1473
          for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
300✔
1474
            if (tags->parseredTags->pTagIndex[k] == tag_idx) {
200✔
1475
              found = true;
100✔
1476
              break;
100✔
1477
            }
1478
          }
1479
          if (found) {
200✔
1480
            continue;
100✔
1481
          }
1482
        }
1483
        if (fields != NULL) {
3,520✔
1484
          (*fields)[idx].field_type = TAOS_FIELD_TAG;
3,520✔
1485
        }
1486
        if (placeholderOfTags != NULL) {
3,520✔
1487
          (*placeholderOfTags)++;
3,520✔
1488
        }
1489
      }
1490

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

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

1511
  return TSDB_CODE_SUCCESS;
5,424✔
1512
}
1513

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

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

1525
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
101✔
1526
  if (tags->numOfBound <= 0) {
101✔
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));
101✔
1534

1535
  return TSDB_CODE_SUCCESS;
101✔
1536
}
1537

1538
int32_t qBuildStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields) {
606✔
1539
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
606✔
1540
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
606✔
1541
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
606✔
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,
606✔
1551
                              fieldNum, fields, pDataBlock->pMeta->tableInfo.precision));
1552

1553
  return TSDB_CODE_SUCCESS;
606✔
1554
}
1555

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

1566
    return TSDB_CODE_SUCCESS;
×
1567
  }
1568

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

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

1593
  *fieldNum = numOfBound;
505✔
1594

1595
  return TSDB_CODE_SUCCESS;
505✔
1596
}
1597

1598
int32_t qBuildStmtStbColFields(void* pBlock, void* boundTags, SSHashObj* parsedCols, uint8_t tbNameFlag,
5,424✔
1599
                               int32_t* fieldNum, TAOS_FIELD_ALL** fields, int32_t* placeholderOfTags,
1600
                               int32_t* placeholderOfCols) {
1601
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
5,424✔
1602
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
5,424✔
1603
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
5,424✔
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,
5,424✔
1613
                                 parsedCols, tbNameFlag, placeholderOfTags, placeholderOfCols));
1614

1615
  return TSDB_CODE_SUCCESS;
5,424✔
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) {
14,503,930✔
1638
  int32_t        code = 0;
14,503,930✔
1639
  STableDataCxt* pBlock = (STableDataCxt*)block;
14,503,930✔
1640
  int32_t        colNum = taosArrayGetSize(pBlock->pData->aCol);
14,503,930✔
1641

1642
  int8_t flag = 0;
14,507,144✔
1643
  for (int32_t i = 0; i < colNum; ++i) {
58,761,521✔
1644
    flag = pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
44,255,801✔
1645
    if (pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
44,258,958✔
1646
      SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i);
44,262,109✔
1647
      if (pCol == NULL) {
44,253,155✔
1648
        parserError("qResetStmtDataBlock column:%d is NULL", i);
12✔
1649
        return terrno;
12✔
1650
      }
1651
      if (deepClear) {
44,253,143✔
1652
        tColDataDeepClear(pCol);
21,735,275✔
1653

1654
      } else {
1655
        tColDataClear(pCol);
22,517,868✔
1656
      }
1657

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

1663
  tBlobSetDestroy(pBlock->pData->pBlobSet);
14,505,720✔
1664
  pBlock->pData->pBlobSet = NULL;
14,503,330✔
1665
  return code;
14,505,791✔
1666
}
1667

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

1671
  *pDst = taosMemoryCalloc(1, sizeof(STableDataCxt));
7,199,647✔
1672
  if (NULL == *pDst) {
7,199,665✔
1673
    return terrno;
×
1674
  }
1675

1676
  STableDataCxt* pNewCxt = (STableDataCxt*)*pDst;
7,200,015✔
1677
  STableDataCxt* pCxt = (STableDataCxt*)pSrc;
7,200,015✔
1678
  pNewCxt->hasBlob = pSrc->hasBlob;
7,200,015✔
1679
  pNewCxt->pSchema = NULL;
7,201,765✔
1680
  pNewCxt->pValues = NULL;
7,201,354✔
1681

1682
  if (pCxt->pMeta) {
7,201,398✔
1683
    pNewCxt->pMeta = tableMetaDup(pCxt->pMeta);
7,202,028✔
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,200,381✔
1694
  pNewCxt->boundColsInfo.pColIndex = NULL;
7,200,377✔
1695

1696
  if (pCxt->boundColsInfo.pColIndex) {
7,201,094✔
1697
    void* pNewColIdx = taosMemoryMalloc(pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
7,200,688✔
1698
    if (NULL == pNewColIdx) {
7,199,665✔
1699
      insDestroyTableDataCxt(*pDst);
×
1700
      return terrno;
×
1701
    }
1702
    memcpy(pNewColIdx, pCxt->boundColsInfo.pColIndex,
7,199,648✔
1703
           pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
7,199,665✔
1704
    pNewCxt->boundColsInfo.pColIndex = pNewColIdx;
7,200,715✔
1705
  }
1706

1707
  if (pCxt->pData) {
7,199,354✔
1708
    int8_t         flag = 1;
7,200,294✔
1709
    SSubmitTbData* pNewTb = (SSubmitTbData*)taosMemoryMalloc(sizeof(SSubmitTbData));
7,200,294✔
1710
    if (NULL == pNewTb) {
7,196,831✔
1711
      insDestroyTableDataCxt(*pDst);
×
1712
      return terrno;
×
1713
    }
1714

1715
    memcpy(pNewTb, pCxt->pData, sizeof(*pCxt->pData));
7,196,831✔
1716
    pNewTb->pCreateTbReq = NULL;
7,198,948✔
1717
    if (pNewTb->pBlobSet != NULL) {
7,200,698✔
1718
      flag = pNewTb->pBlobSet->type;
×
1719
    }
1720
    pNewTb->pBlobSet = NULL;
7,201,748✔
1721

1722
    pNewTb->aCol = taosArrayDup(pCxt->pData->aCol, NULL);
7,200,294✔
1723
    if (NULL == pNewTb->aCol) {
7,202,132✔
1724
      insDestroyTableDataCxt(*pDst);
×
1725
      return terrno;
×
1726
    }
1727

1728
    pNewCxt->pData = pNewTb;
7,200,015✔
1729

1730
    if (reset) {
7,199,617✔
1731
      code = qResetStmtDataBlock(*pDst, true);
7,191,738✔
1732
    }
1733
  }
1734

1735
  return code;
7,201,765✔
1736
}
1737

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

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

1752
  pBlock->pData->suid = suid;
9,646✔
1753
  pBlock->pData->uid = uid;
9,646✔
1754

1755
  if (rebuildCreateTb && NULL == pBlock->pData->pCreateTbReq) {
9,646✔
1756
    pBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
7,638✔
1757
    if (NULL == pBlock->pData->pCreateTbReq) {
7,638✔
1758
      return terrno;
×
1759
    }
1760
  }
1761

1762
  return TSDB_CODE_SUCCESS;
9,646✔
1763
}
1764

1765
STableMeta* qGetTableMetaInDataBlock(STableDataCxt* pDataBlock) { return ((STableDataCxt*)pDataBlock)->pMeta; }
14,626,321✔
1766

1767
void qDestroyStmtDataBlock(STableDataCxt* pBlock) {
21,854,990✔
1768
  if (pBlock == NULL) {
21,854,990✔
1769
    return;
7,333,738✔
1770
  }
1771

1772
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
14,521,252✔
1773
  insDestroyTableDataCxt(pDataBlock);
14,521,252✔
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