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

taosdata / TDengine / #5011

03 Apr 2026 03:59PM UTC coverage: 72.3% (+0.008%) from 72.292%
#5011

push

travis-ci

web-flow
merge: from main to 3.0 branch #35067

4053 of 5985 new or added lines in 68 files covered. (67.72%)

732 existing lines in 143 files now uncovered.

257430 of 356056 relevant lines covered (72.3%)

131834103.52 hits per line

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

76.55
/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) {
6,977,300✔
35
  if (pDataBlock == NULL || pDataBlock->pData == NULL) {
6,977,300✔
36
    qError("stmt qCloneCurrentTbData input is null, maybe not bind data before execute");
3,591✔
37
    return TSDB_CODE_TSC_STMT_API_ERROR;
195✔
38
  }
39
  *pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
6,974,731✔
40
  if (NULL == *pData) {
6,970,643✔
41
    return terrno;
×
42
  }
43

44
  int8_t         flag = 1;
6,971,665✔
45
  SSubmitTbData* pNew = *pData;
6,971,665✔
46

47
  *pNew = *pDataBlock->pData;
6,971,665✔
48
  pNew->pBlobSet = NULL;
6,978,127✔
49

50
  int32_t code = cloneSVreateTbReq(pDataBlock->pData->pCreateTbReq, &pNew->pCreateTbReq);
6,967,915✔
51
  if (TSDB_CODE_SUCCESS != code) {
6,967,899✔
52
    taosMemoryFreeClear(*pData);
×
53
    return code;
×
54
  }
55
  pNew->aCol = taosArrayDup(pDataBlock->pData->aCol, NULL);
6,967,899✔
56
  if (!pNew->aCol) {
6,983,897✔
57
    code = terrno;
×
58
    taosMemoryFreeClear(*pData);
×
59
    return code;
×
60
  }
61

62
  int32_t colNum = taosArrayGetSize(pNew->aCol);
6,979,479✔
63
  for (int32_t i = 0; i < colNum; ++i) {
28,460,709✔
64
    if (pDataBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
21,475,444✔
65
      SColData* pCol = (SColData*)taosArrayGet(pNew->aCol, i);
21,474,946✔
66
      tColDataDeepClear(pCol);
21,473,340✔
67
    } else {
68
      pNew->aCol = taosArrayInit(20, POINTER_BYTES);
390✔
69
      if (pNew->aCol == NULL) {
390✔
70
        code = terrno;
×
71
        taosMemoryFreeClear(*pData);
×
72
        return code;
×
73
      }
74
    }
75
  }
76

77
  return code;
6,985,265✔
78
}
79

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

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

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

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

100
  if (pStmt->freeArrayFunc) {
812,906✔
101
    pStmt->freeArrayFunc(pVgDataBlocks);
813,121✔
102
  }
103
  return code;
812,835✔
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) {
6,976,759✔
128
  int32_t             code = TSDB_CODE_SUCCESS;
6,976,759✔
129
  SArray*             pVgDataBlocks = NULL;
6,976,759✔
130
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
6,980,517✔
131

132
  // merge according to vgId
133
  if (taosHashGetSize(pBlockHash) > 0) {
6,979,149✔
134
    code = insMergeTableDataCxt(pBlockHash, &pVgDataBlocks, true);
6,981,201✔
135
  }
136

137
  if (TSDB_CODE_SUCCESS == code) {
6,981,193✔
138
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks, false);
6,982,207✔
139
  }
140

141
  if (pStmt->freeArrayFunc) {
6,977,897✔
142
    pStmt->freeArrayFunc(pVgDataBlocks);
6,982,323✔
143
  }
144
  return code;
6,963,855✔
145
}
146

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

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

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

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

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

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

178
    SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
22,541✔
179
    int32_t  colLen = pTagSchema->bytes;
22,541✔
180
    if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
22,541✔
181
      if (!bind[c].length) {
4,798✔
182
        code = buildInvalidOperationMsg(&pBuf, "var tag length is null");
×
183
        goto end;
×
184
      }
185
      colLen = bind[c].length[0];
4,798✔
186
      if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
4,798✔
187
        code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
×
188
        goto end;
×
189
      }
190
    }
191
    if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
45,082✔
192
      code = terrno;
×
193
      goto end;
×
194
    }
195
    if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
22,541✔
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};
22,452✔
215
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
22,452✔
216
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
19,003✔
217
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
3,725✔
218
          code = initCtxAsText();
276✔
219
          if (code) {
276✔
220
            qError("geometry init failed:%s", tstrerror(code));
×
221
            goto end;
×
222
          }
223
          code = checkWKB(bind[c].buffer, colLen);
276✔
224
          if (code) {
276✔
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,725✔
230
        val.nData = colLen;
3,725✔
231
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
18,727✔
232
        int32_t output = 0;
984✔
233
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
984✔
234
        if (p == NULL) {
984✔
235
          code = terrno;
×
236
          goto end;
×
237
        }
238
        if (!taosMbsToUcs4(bind[c].buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
984✔
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;
984✔
251
        val.nData = output;
984✔
252
      } else {
253
        memcpy(&val.i64, bind[c].buffer, colLen);
17,743✔
254
      }
255
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
22,452✔
256
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
257
        goto end;
×
258
      }
259
      if (NULL == taosArrayPush(pTagArray, &val)) {
22,452✔
260
        code = terrno;
×
261
        goto end;
×
262
      }
263
    }
264
  }
265

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

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

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

289
end:
8,095✔
290
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
30,627✔
291
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
22,532✔
292
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
22,532✔
293
      taosMemoryFreeClear(p->pData);
1,064✔
294
    }
295
  }
296
  taosArrayDestroy(pTagArray);
8,095✔
297
  taosArrayDestroy(tagName);
8,095✔
298
  taosMemoryFree(pTag);
8,095✔
299

300
  return code;
8,095✔
301
}
302

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

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

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

324
  for (int32_t i = 0; i < src->num; ++i) {
816,035✔
325
    if (src->is_null && src->is_null[i]) {
809,944✔
326
      continue;
317,034✔
327
    }
328

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

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

347
  return TSDB_CODE_SUCCESS;
6,091✔
348
}
349

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

363
  if (NULL == pTSchema || NULL == *pTSchema) {
1,939,356✔
364
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
5,859✔
365
  }
366

367
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
9,952,249✔
368
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
8,012,327✔
369
    if (pColSchema->colId <= lastColId) {
8,011,343✔
370
      colInOrder = false;
×
371
    } else {
372
      lastColId = pColSchema->colId;
8,011,713✔
373
    }
374
    // SColData* pCol = taosArrayGet(pCols, c);
375

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

387
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
8,013,759✔
388
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
1,978✔
389
      if (code) {
1,978✔
390
        goto _return;
×
391
      }
392
      pBind = &ncharBind;
1,978✔
393
    } else {
394
      pBind = bind + c;
8,006,697✔
395
    }
396

397
    pBindInfos[c].columnId = pColSchema->colId;
8,013,089✔
398
    pBindInfos[c].bind = pBind;
8,010,089✔
399
    pBindInfos[c].type = pColSchema->type;
8,012,039✔
400
  }
401

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

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

407
_return:
1,938,799✔
408

409
  taosMemoryFree(ncharBind.buffer);
1,939,435✔
410
  taosMemoryFree(ncharBind.length);
1,939,325✔
411

412
  return code;
1,938,883✔
413
}
414

415
int32_t qBindStmtColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
6,863,769✔
416
                           void* charsetCxt) {
417
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
6,863,769✔
418
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
6,863,769✔
419
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
6,865,145✔
420
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
6,861,773✔
421
  int32_t          rowNum = bind->num;
6,864,501✔
422
  TAOS_MULTI_BIND  ncharBind = {0};
6,864,123✔
423
  TAOS_MULTI_BIND* pBind = NULL;
6,860,341✔
424
  int32_t          code = 0;
6,860,341✔
425

426
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
27,540,837✔
427
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
20,674,172✔
428
    SColData* pCol = taosArrayGet(pCols, c);
20,678,252✔
429

430
    if (bind[c].num != rowNum) {
20,680,642✔
UNCOV
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)) &&
20,677,944✔
436
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
20,673,307✔
437
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
438
      goto _return;
×
439
    }
440

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

451
    int32_t bytes = 0;
20,676,906✔
452
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
20,676,906✔
453
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
30,576✔
454
        bytes = pColSchema->bytes - BLOBSTR_HEADER_SIZE;
×
455
      } else {
456
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
20,452✔
457
      }
458
    } else {
459
      bytes = -1;
20,644,898✔
460
    }
461
    code = tColDataAddValueByBind(pCol, pBind, bytes, initCtxAsText, checkWKB);
20,665,350✔
462
    if (code) {
20,684,712✔
463
      goto _return;
4,216✔
464
    }
465
  }
466

467
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
6,869,095✔
468

469
_return:
6,873,115✔
470

471
  taosMemoryFree(ncharBind.buffer);
6,872,297✔
472
  taosMemoryFree(ncharBind.length);
6,871,615✔
473

474
  return code;
6,872,975✔
475
}
476

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

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

497
    if (bind[c].num != rowNum) {
3,920✔
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,920✔
503
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
3,920✔
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,920✔
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,920✔
516
    }
517

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

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

537
_return:
980✔
538

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

542
  return code;
980✔
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,
566,398✔
596
                            TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen, void* charsetCxt,
597
                            SVCreateTbReq* pCreateTbReq) {
598
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
566,398✔
599
  SMsgBuf        pBuf = {.buf = msgBuf, .len = msgBufLen};
566,398✔
600
  int32_t        code = TSDB_CODE_SUCCESS;
566,398✔
601
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
566,398✔
602
  if (NULL == tags) {
566,398✔
603
    return buildInvalidOperationMsg(&pBuf, "tags is null");
×
604
  }
605

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

616
  SArray* tagName;
617
  if (tags->parseredTags) {
566,424✔
618
    tagName = taosArrayDup(tags->parseredTags->STagNames, NULL);
4,680✔
619
  } else {
620
    tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
561,744✔
621
  }
622

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

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

630
  bool  isJson = false;
566,411✔
631
  STag* pTag = NULL;
566,411✔
632

633
  int bindIdx = 0;
566,411✔
634
  for (int c = 0; c < tags->numOfBound; ++c) {
1,732,968✔
635
    if (bind == NULL) {
1,170,730✔
636
      break;
4,095✔
637
    }
638
    if (tags->parseredTags) {
1,166,635✔
639
      bool found = false;
51,480✔
640
      for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
140,400✔
641
        if (tags->parseredTags->pTagIndex[k] == tags->pColIndex[c]) {
98,280✔
642
          found = true;
9,360✔
643
          break;
9,360✔
644
        }
645
      }
646
      if (found) {
51,480✔
647
        continue;
9,360✔
648
      }
649
    }
650

651
    TAOS_STMT2_BIND bindData = bind[bindIdx++];
1,157,275✔
652

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

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

683
      isJson = true;
×
684
      char* tmp = taosMemoryCalloc(1, colLen + 1);
×
685
      if (!tmp) {
×
686
        code = terrno;
×
687
        goto end;
×
688
      }
689
      memcpy(tmp, bindData.buffer, colLen);
×
690
      code = parseJsontoTagData(tmp, pTagArray, &pTag, &pBuf, charsetCxt);
×
691
      taosMemoryFree(tmp);
×
692
      if (code != TSDB_CODE_SUCCESS) {
×
693
        goto end;
×
694
      }
695
    } else {
696
      STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
1,157,418✔
697
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
1,157,418✔
698
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
604,293✔
699
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
559,287✔
700
          code = initCtxAsText();
6,045✔
701
          if (code) {
6,045✔
702
            qError("geometry init failed:%s", tstrerror(code));
×
703
            goto end;
195✔
704
          }
705
          code = checkWKB(bindData.buffer, colLen);
6,045✔
706
          if (code) {
6,045✔
707
            qError("stmt2 bind invalid geometry tag:%s, must be WKB format", (char*)bindData.buffer);
169✔
708
            goto end;
195✔
709
          }
710
        }
711
        val.pData = (uint8_t*)bindData.buffer;
559,131✔
712
        val.nData = colLen;
559,131✔
713
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
598,131✔
714
        int32_t output = 0;
8,190✔
715
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
8,190✔
716
        if (p == NULL) {
8,190✔
717
          code = terrno;
×
718
          goto end;
×
719
        }
720
        if (colLen != 0) {
8,190✔
721
          if (!taosMbsToUcs4(bindData.buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
7,800✔
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,190✔
735
        val.nData = output;
8,190✔
736

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

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

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

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

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

785
end:
566,554✔
786
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
1,733,085✔
787
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
1,166,596✔
788
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
1,166,531✔
789
      taosMemoryFreeClear(p->pData);
8,190✔
790
    }
791
  }
792
  taosArrayDestroy(pTagArray);
566,229✔
793
  taosArrayDestroy(tagName);
566,606✔
794
  taosMemoryFree(pTag);
566,567✔
795

796
  return code;
566,567✔
797
}
798

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

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

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

815
  char* src_buf = src->buffer;
4,908✔
816
  char* dst_buf = dst->buffer;
4,908✔
817
  for (int32_t i = 0; i < src->num; ++i) {
45,318✔
818
    if (src->is_null && src->is_null[i]) {
40,410✔
819
      continue;
195✔
820
    }
821

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

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

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

845
  return TSDB_CODE_SUCCESS;
4,908✔
846
}
847

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

870
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
9,132,948✔
871
    if (TSDB_DATA_TYPE_NCHAR == pSchema[boundInfo->pColIndex[c]].type) {
8,054,696✔
872
      ncharColNums++;
4,908✔
873
    }
874
  }
875
  if (ncharColNums > 0) {
1,081,471✔
876
    ncharBinds = taosArrayInit(ncharColNums, sizeof(ncharBind));
4,908✔
877
    if (!ncharBinds) {
4,908✔
878
      code = terrno;
×
879
      TAOS_CHECK_GOTO(code, &lino, _return);
×
880
    }
881
  }
882

883
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
9,135,509✔
884
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
8,054,239✔
885
    if (pColSchema->colId <= lastColId) {
8,054,110✔
886
      colInOrder = false;
1,365✔
887
    } else {
888
      lastColId = pColSchema->colId;
8,051,413✔
889
    }
890

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

901
    TAOS_STMT2_BIND bindData = bind[bindIdx];
8,049,016✔
902

903
    if (bindData.num != rowNum) {
8,048,320✔
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,048,320✔
909
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
8,046,796✔
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,049,160✔
916
      code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
4,908✔
917
      TAOS_CHECK_GOTO(code, &lino, _return);
4,908✔
918

919
      if (!taosArrayPush(ncharBinds, &ncharBind)) {
4,908✔
920
        code = terrno;
×
921
        goto _return;
×
922
      }
923
      pBindInfos[c].bind = taosArrayGetLast(ncharBinds);
4,908✔
924
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
8,043,160✔
925
      code = initCtxAsText();
2,535✔
926
      if (code) {
2,535✔
927
        parserError("stmt2 bind geometry init failed, ErrCode: 0x%x", code);
×
928
        goto _return;
×
929
      }
930
      uint8_t* buf = bindData.buffer;
2,535✔
931
      for (int j = 0; j < bindData.num; j++) {
5,655✔
932
        if (bindData.is_null && bindData.is_null[j]) {
3,120✔
933
          continue;
195✔
934
        }
935
        code = checkWKB(buf, bindData.length[j]);
2,925✔
936
        if (code) {
2,925✔
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,925✔
941
      }
942
      pBindInfos[c].bind = bind + bindIdx;
2,535✔
943
    } else {
944
      if (IS_STR_DATA_BLOB(pColSchema->type)) hasBlob = 1;
8,035,834✔
945
      pBindInfos[c].bind = bind + bindIdx;
8,033,359✔
946
    }
947

948
    pBindInfos[c].columnId = pColSchema->colId;
8,046,685✔
949
    pBindInfos[c].type = pColSchema->type;
8,047,468✔
950
    pBindInfos[c].bytes = pColSchema->bytes;
8,048,131✔
951

952
    bindIdx++;
8,047,927✔
953
  }
954

955
  if (hasBlob == 0) {
1,081,774✔
956
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pSchemaExt, pCols,
1,081,574✔
957
                              &pDataBlock->ordered, &pDataBlock->duplicateTs);
958
    TAOS_CHECK_GOTO(code, &lino, _return);
1,081,453✔
959
  } else {
960
    code = tBlobSetCreate(1024, 1, ppBlob);
200✔
961
    TAOS_CHECK_GOTO(code, &lino, _return);
×
962

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

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

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

983
  return code;
1,081,677✔
984
}
985

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

991
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
25,114,580✔
992
  // if (dst->buffer_length < newBuflen) {
993
  dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
25,209,178✔
994
  if (NULL == dst->buffer) {
24,942,285✔
995
    return terrno;
×
996
  }
997
  //}
998

999
  if (NULL == dst->length) {
24,795,783✔
1000
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
25,074,040✔
1001
    if (NULL == dst->length) {
25,260,301✔
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;
25,248,113✔
1009
  char* dst_buf = dst->buffer;
25,112,947✔
1010
  for (int32_t i = 0; i < src->num; ++i) {
50,491,120✔
1011
    if (src->is_null && src->is_null[i]) {
25,205,973✔
1012
      continue;
1,220,168✔
1013
    }
1014

1015
    if (src->length[i] == 0) {
24,016,699✔
1016
      dst->length[i] = 0;
21,828✔
1017
      continue;
21,828✔
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)) {
23,688,015✔
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;
23,805,946✔
1032
    src_buf += src->length[i];
23,936,892✔
1033
    dst_buf += output;
24,021,783✔
1034
  }
1035

1036
  dst->buffer_type = src->buffer_type;
25,268,497✔
1037
  dst->is_null = src->is_null;
25,276,128✔
1038
  dst->num = src->num;
25,132,304✔
1039

1040
  return TSDB_CODE_SUCCESS;
25,236,542✔
1041
}
1042

1043
int32_t qBindStmtColsValue2(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
50,805,297✔
1044
                            int32_t msgBufLen, void* charsetCxt) {
1045
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
50,805,297✔
1046
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
50,805,297✔
1047
  SSchemaExt*      pExtSchema = getTableColumnExtSchema(pDataBlock->pMeta);
51,556,111✔
1048
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
51,891,981✔
1049
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
52,135,750✔
1050
  int32_t          rowNum = bind->num;
52,173,435✔
1051
  TAOS_STMT2_BIND  ncharBind = {0};
52,143,840✔
1052
  TAOS_STMT2_BIND* pBind = NULL;
51,935,930✔
1053
  int32_t          code = 0;
51,935,930✔
1054
  int32_t          lino = 0;
51,935,930✔
1055
  int32_t          bindIdx = 0;
51,935,930✔
1056

1057
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
547,925,696✔
1058
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
496,055,902✔
1059
    SColData* pCol = taosArrayGet(pCols, c);
494,513,862✔
1060
    if (pCol == NULL || pColSchema == NULL) {
490,288,512✔
UNCOV
1061
      code = buildInvalidOperationMsg(&pBuf, "get column schema or column data failed");
×
1062
      goto _return;
×
1063
    }
1064

1065
    if (boundInfo->pColIndex[c] == 0) {
493,618,245✔
1066
      pCol->cflag |= COL_IS_KEY;
51,743,818✔
1067
    }
1068

1069
    if (parsedCols) {
493,420,767✔
1070
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
27,495✔
1071
      if (pParsedVal) {
27,495✔
1072
        for (int row = 0; row < rowNum; row++) {
10,920✔
1073
          code = tColDataAppendValue(pCol, pParsedVal);
5,655✔
1074
          if (code) {
5,655✔
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,265✔
1080
      }
1081
    }
1082
    TAOS_STMT2_BIND bindData = bind[bindIdx];
493,415,502✔
1083
    if (bindData.num != rowNum) {
493,607,931✔
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)) &&
493,607,931✔
1089
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
478,993,013✔
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) {
493,501,594✔
1096
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
25,214,614✔
1097
      if (code) {
24,820,179✔
1098
        goto _return;
×
1099
      }
1100
      pBind = &ncharBind;
24,820,179✔
1101
    } else {
1102
      pBind = bind + bindIdx;
463,634,437✔
1103
    }
1104
    int8_t  isBlob = 0;
493,494,960✔
1105
    int32_t bytes = -1;
493,494,960✔
1106
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
493,494,960✔
1107
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
39,484,293✔
1108
        isBlob = 1;
213✔
1109
        bytes = TSDB_MAX_BLOB_LEN;
213✔
1110
      } else {
1111
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
51,840,963✔
1112
      }
1113
    }
1114

1115
    if (pColSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
498,590,659✔
1116
      code = tColDataAddValueByBind2WithGeos(pCol, pBind, bytes, initCtxAsText, checkWKB);
275,102✔
1117
    } else if (isBlob == 1) {
495,568,432✔
1118
      if (pDataBlock->pData->pBlobSet == NULL) {
213✔
1119
        code = tBlobSetCreate(1024, 1, &pDataBlock->pData->pBlobSet);
183✔
1120
        if (code) {
183✔
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);
213✔
1126
    } else if (IS_DECIMAL_TYPE(pColSchema->type)) {
495,570,034✔
1127
      if (pExtSchema == NULL) {
1,191✔
1128
        code = buildInvalidOperationMsg(&pBuf, "decimal column ext schema is null");
×
1129
        goto _return;
×
1130
      }
1131
      uint8_t precision = 0, scale = 0;
1,191✔
1132
      decimalFromTypeMod(pExtSchema[c].typeMod, &precision, &scale);
1,191✔
1133
      code = tColDataAddValueByBind2WithDecimal(pCol, pBind, bytes, precision, scale);
1,815✔
1134
    } else {
1135
      code = tColDataAddValueByBind2(pCol, pBind, bytes);
488,512,837✔
1136
    }
1137

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

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

1147
_return:
51,870,776✔
1148

1149
  taosMemoryFree(ncharBind.buffer);
51,695,809✔
1150
  taosMemoryFree(ncharBind.length);
51,585,828✔
1151

1152
  return code;
51,624,452✔
1153
}
1154

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

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

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

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

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

1229
  if (code) {
1,170✔
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,170✔
1235

1236
_return:
1,170✔
1237

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

1241
  return code;
1,170✔
1242
}
1243

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

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

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

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

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

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

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

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

1337
_return:
390✔
1338

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

1342
  return code;
390✔
1343
}
1344

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

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

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

1366
  *fieldNum = numOfBound;
1,372✔
1367

1368
  return TSDB_CODE_SUCCESS;
1,372✔
1369
}
1370

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

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

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

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

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

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

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

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

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

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

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

1450
      if (idxCol == pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags) {
38,542✔
1451
        if (fields != NULL) {
2,544✔
1452
          (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
2,544✔
1453
          tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
2,544✔
1454
          (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
2,544✔
1455
          (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
2,544✔
1456
        }
1457
        idx++;
2,544✔
1458
        continue;
2,544✔
1459
      } else if (idxCol < pMeta->tableInfo.numOfColumns) {
35,998✔
1460
        if (parsedCols && tSimpleHashGet(parsedCols, &pSchema[idxCol].colId, sizeof(int16_t))) {
28,960✔
1461
          continue;
975✔
1462
        }
1463
        if (fields != NULL) {
27,985✔
1464
          (*fields)[idx].field_type = TAOS_FIELD_COL;
27,985✔
1465
        }
1466
        if (placeholderOfCols != NULL) {
27,985✔
1467
          (*placeholderOfCols)++;
27,985✔
1468
        }
1469
      } else {
1470
        if (tags->parseredTags && tags->parseredTags->numOfTags > 0) {
7,038✔
1471
          int32_t tag_idx = idxCol - pMeta->tableInfo.numOfColumns;
390✔
1472
          bool    found = false;
390✔
1473
          for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
585✔
1474
            if (tags->parseredTags->pTagIndex[k] == tag_idx) {
390✔
1475
              found = true;
195✔
1476
              break;
195✔
1477
            }
1478
          }
1479
          if (found) {
390✔
1480
            continue;
195✔
1481
          }
1482
        }
1483
        if (fields != NULL) {
6,843✔
1484
          (*fields)[idx].field_type = TAOS_FIELD_TAG;
6,843✔
1485
        }
1486
        if (placeholderOfTags != NULL) {
6,843✔
1487
          (*placeholderOfTags)++;
6,843✔
1488
        }
1489
      }
1490

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

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

1511
  return TSDB_CODE_SUCCESS;
10,555✔
1512
}
1513

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

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

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

1535
  return TSDB_CODE_SUCCESS;
196✔
1536
}
1537

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

1553
  return TSDB_CODE_SUCCESS;
1,176✔
1554
}
1555

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

1566
    return TSDB_CODE_SUCCESS;
×
1567
  }
1568

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

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

1593
  *fieldNum = numOfBound;
980✔
1594

1595
  return TSDB_CODE_SUCCESS;
980✔
1596
}
1597

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

1615
  return TSDB_CODE_SUCCESS;
10,555✔
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) {
13,846,650✔
1638
  int32_t        code = 0;
13,846,650✔
1639
  STableDataCxt* pBlock = (STableDataCxt*)block;
13,846,650✔
1640
  int32_t        colNum = taosArrayGetSize(pBlock->pData->aCol);
13,846,650✔
1641

1642
  int8_t flag = 0;
13,854,939✔
1643
  for (int32_t i = 0; i < colNum; ++i) {
56,087,991✔
1644
    flag = pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
42,237,986✔
1645
    if (pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
42,244,523✔
1646
      SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i);
42,248,504✔
1647
      if (pCol == NULL) {
42,251,631✔
UNCOV
1648
        parserError("qResetStmtDataBlock column:%d is NULL", i);
×
UNCOV
1649
        return terrno;
×
1650
      }
1651
      if (deepClear) {
42,251,631✔
1652
        tColDataDeepClear(pCol);
20,771,755✔
1653

1654
      } else {
1655
        tColDataClear(pCol);
21,479,876✔
1656
      }
1657

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

1663
  tBlobSetDestroy(pBlock->pData->pBlobSet);
13,850,005✔
1664
  pBlock->pData->pBlobSet = NULL;
13,839,371✔
1665
  return code;
13,843,484✔
1666
}
1667

1668
int32_t qCloneStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, bool reset) {
6,880,975✔
1669
  int32_t code = 0;
6,880,975✔
1670

1671
  *pDst = taosMemoryCalloc(1, sizeof(STableDataCxt));
6,880,975✔
1672
  if (NULL == *pDst) {
6,879,055✔
1673
    return terrno;
×
1674
  }
1675

1676
  STableDataCxt* pNewCxt = (STableDataCxt*)*pDst;
6,881,075✔
1677
  STableDataCxt* pCxt = (STableDataCxt*)pSrc;
6,882,781✔
1678
  pNewCxt->hasBlob = pSrc->hasBlob;
6,882,781✔
1679
  pNewCxt->pSchema = NULL;
6,885,501✔
1680
  pNewCxt->pValues = NULL;
6,884,125✔
1681

1682
  if (pCxt->pMeta) {
6,884,471✔
1683
    pNewCxt->pMeta = tableMetaDup(pCxt->pMeta);
6,883,111✔
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));
6,886,177✔
1694
  pNewCxt->boundColsInfo.pColIndex = NULL;
6,886,499✔
1695

1696
  if (pCxt->boundColsInfo.pColIndex) {
6,883,095✔
1697
    void* pNewColIdx = taosMemoryMalloc(pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
6,884,777✔
1698
    if (NULL == pNewColIdx) {
6,880,721✔
1699
      insDestroyTableDataCxt(*pDst);
×
1700
      return terrno;
×
1701
    }
1702
    memcpy(pNewColIdx, pCxt->boundColsInfo.pColIndex,
6,883,111✔
1703
           pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
6,880,721✔
1704
    pNewCxt->boundColsInfo.pColIndex = pNewColIdx;
6,883,441✔
1705
  }
1706

1707
  if (pCxt->pData) {
6,889,259✔
1708
    int8_t         flag = 1;
6,881,397✔
1709
    SSubmitTbData* pNewTb = (SSubmitTbData*)taosMemoryMalloc(sizeof(SSubmitTbData));
6,881,397✔
1710
    if (NULL == pNewTb) {
6,877,985✔
1711
      insDestroyTableDataCxt(*pDst);
×
1712
      return terrno;
×
1713
    }
1714

1715
    memcpy(pNewTb, pCxt->pData, sizeof(*pCxt->pData));
6,877,985✔
1716
    pNewTb->pCreateTbReq = NULL;
6,879,691✔
1717
    if (pNewTb->pBlobSet != NULL) {
6,886,185✔
1718
      flag = pNewTb->pBlobSet->type;
×
1719
    }
1720
    pNewTb->pBlobSet = NULL;
6,883,111✔
1721

1722
    pNewTb->aCol = taosArrayDup(pCxt->pData->aCol, NULL);
6,884,463✔
1723
    if (NULL == pNewTb->aCol) {
6,885,501✔
1724
      insDestroyTableDataCxt(*pDst);
×
1725
      return terrno;
×
1726
    }
1727

1728
    pNewCxt->pData = pNewTb;
6,880,061✔
1729

1730
    if (reset) {
6,883,441✔
1731
      code = qResetStmtDataBlock(*pDst, true);
6,864,146✔
1732
    }
1733
  }
1734

1735
  return code;
6,881,408✔
1736
}
1737

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

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

1752
  pBlock->pData->suid = suid;
17,935✔
1753
  pBlock->pData->uid = uid;
17,935✔
1754

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

1762
  return TSDB_CODE_SUCCESS;
17,935✔
1763
}
1764

1765
STableMeta* qGetTableMetaInDataBlock(STableDataCxt* pDataBlock) { return ((STableDataCxt*)pDataBlock)->pMeta; }
13,971,870✔
1766

1767
void qDestroyStmtDataBlock(STableDataCxt* pBlock) {
20,886,433✔
1768
  if (pBlock == NULL) {
20,886,433✔
1769
    return;
7,003,962✔
1770
  }
1771

1772
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
13,882,471✔
1773
  insDestroyTableDataCxt(pDataBlock);
13,882,471✔
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