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

taosdata / TDengine / #5016

03 Apr 2026 03:59PM UTC coverage: 72.299% (+0.01%) from 72.289%
#5016

push

travis-ci

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

4055 of 5985 new or added lines in 68 files covered. (67.75%)

13126 existing lines in 156 files now uncovered.

257424 of 356056 relevant lines covered (72.3%)

133108577.02 hits per line

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

76.64
/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,177,426✔
35
  if (pDataBlock == NULL || pDataBlock->pData == NULL) {
7,177,426✔
36
    qError("stmt qCloneCurrentTbData input is null, maybe not bind data before execute");
135✔
37
    return TSDB_CODE_TSC_STMT_API_ERROR;
99✔
38
  }
39
  *pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
7,183,437✔
40
  if (NULL == *pData) {
7,171,843✔
41
    return terrno;
×
42
  }
43

44
  int8_t         flag = 1;
7,175,721✔
45
  SSubmitTbData* pNew = *pData;
7,175,721✔
46

47
  *pNew = *pDataBlock->pData;
7,177,121✔
48
  pNew->pBlobSet = NULL;
7,183,066✔
49

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

62
  int32_t colNum = taosArrayGetSize(pNew->aCol);
7,183,436✔
63
  for (int32_t i = 0; i < colNum; ++i) {
29,275,961✔
64
    if (pDataBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
22,082,987✔
65
      SColData* pCol = (SColData*)taosArrayGet(pNew->aCol, i);
22,088,042✔
66
      tColDataDeepClear(pCol);
22,086,852✔
67
    } else {
68
      pNew->aCol = taosArrayInit(20, POINTER_BYTES);
198✔
69
      if (pNew->aCol == NULL) {
198✔
70
        code = terrno;
×
71
        taosMemoryFreeClear(*pData);
×
72
        return code;
×
73
      }
74
    }
75
  }
76

77
  return code;
7,192,974✔
78
}
79

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

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

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

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

100
  if (pStmt->freeArrayFunc) {
887,933✔
101
    pStmt->freeArrayFunc(pVgDataBlocks);
888,364✔
102
  }
103
  return code;
888,270✔
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,177,432✔
128
  int32_t             code = TSDB_CODE_SUCCESS;
7,177,432✔
129
  SArray*             pVgDataBlocks = NULL;
7,177,432✔
130
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
7,181,715✔
131

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

137
  if (TSDB_CODE_SUCCESS == code) {
7,185,218✔
138
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks, false);
7,186,668✔
139
  }
140

141
  if (pStmt->freeArrayFunc) {
7,182,921✔
142
    pStmt->freeArrayFunc(pVgDataBlocks);
7,189,767✔
143
  }
144
  return code;
7,165,023✔
145
}
146

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

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

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

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

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

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

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

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

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

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

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

289
end:
7,015✔
290
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
26,366✔
291
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
19,351✔
292
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
19,351✔
293
      taosMemoryFreeClear(p->pData);
951✔
294
    }
295
  }
296
  taosArrayDestroy(pTagArray);
7,015✔
297
  taosArrayDestroy(tagName);
7,015✔
298
  taosMemoryFree(pTag);
7,015✔
299

300
  return code;
7,015✔
301
}
302

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

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

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

324
  for (int32_t i = 0; i < src->num; ++i) {
839,717✔
325
    if (src->is_null && src->is_null[i]) {
833,536✔
326
      continue;
313,956✔
327
    }
328

329
    if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
519,619✔
330
                       (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output,
519,580✔
331
                       charsetCxt)) {
332
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
39✔
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;
519,580✔
341
  }
342

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

347
  return TSDB_CODE_SUCCESS;
6,181✔
348
}
349

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

363
  if (NULL == pTSchema || NULL == *pTSchema) {
2,259,430✔
364
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
11,073✔
365
  }
366

367
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
11,525,073✔
368
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
9,266,991✔
369
    if (pColSchema->colId <= lastColId) {
9,269,643✔
370
      colInOrder = false;
×
371
    } else {
372
      lastColId = pColSchema->colId;
9,267,935✔
373
    }
374
    // SColData* pCol = taosArrayGet(pCols, c);
375

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

387
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
9,272,103✔
388
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
2,290✔
389
      if (code) {
2,290✔
390
        goto _return;
×
391
      }
392
      pBind = &ncharBind;
2,290✔
393
    } else {
394
      pBind = bind + c;
9,260,873✔
395
    }
396

397
    pBindInfos[c].columnId = pColSchema->colId;
9,267,423✔
398
    pBindInfos[c].bind = pBind;
9,269,704✔
399
    pBindInfos[c].type = pColSchema->type;
9,271,156✔
400
  }
401

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

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

407
_return:
2,256,825✔
408

409
  taosMemoryFree(ncharBind.buffer);
2,257,289✔
410
  taosMemoryFree(ncharBind.length);
2,258,444✔
411

412
  return code;
2,258,732✔
413
}
414

415
int32_t qBindStmtColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
7,067,658✔
416
                           void* charsetCxt) {
417
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
7,067,658✔
418
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
7,067,658✔
419
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
7,078,559✔
420
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
7,075,436✔
421
  int32_t          rowNum = bind->num;
7,075,089✔
422
  TAOS_MULTI_BIND  ncharBind = {0};
7,073,282✔
423
  TAOS_MULTI_BIND* pBind = NULL;
7,069,680✔
424
  int32_t          code = 0;
7,069,680✔
425

426
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
28,380,972✔
427
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
21,301,453✔
428
    SColData* pCol = taosArrayGet(pCols, c);
21,307,017✔
429

430
    if (bind[c].num != rowNum) {
21,313,718✔
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,313,347✔
436
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
21,308,673✔
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,310,200✔
442
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
3,811✔
443
      if (code) {
3,811✔
444
        goto _return;
×
445
      }
446
      pBind = &ncharBind;
3,811✔
447
    } else {
448
      pBind = bind + c;
21,307,514✔
449
    }
450

451
    int32_t bytes = 0;
21,305,294✔
452
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
21,305,294✔
453
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
20,590✔
454
        bytes = pColSchema->bytes - BLOBSTR_HEADER_SIZE;
×
455
      } else {
456
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
19,609✔
457
      }
458
    } else {
459
      bytes = -1;
21,276,208✔
460
    }
461
    code = tColDataAddValueByBind(pCol, pBind, bytes, initCtxAsText, checkWKB);
21,295,817✔
462
    if (code) {
21,315,453✔
463
      goto _return;
4,161✔
464
    }
465
  }
466

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

469
_return:
7,088,691✔
470

471
  taosMemoryFree(ncharBind.buffer);
7,088,443✔
472
  taosMemoryFree(ncharBind.length);
7,087,378✔
473

474
  return code;
7,087,737✔
475
}
476

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

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

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

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

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

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

537
_return:
495✔
538

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

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

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

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

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

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

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

633
  int bindIdx = 0;
546,015✔
634
  for (int c = 0; c < tags->numOfBound; ++c) {
1,655,250✔
635
    if (bind == NULL) {
1,111,192✔
636
      break;
2,079✔
637
    }
638
    if (tags->parseredTags) {
1,109,113✔
639
      bool found = false;
26,136✔
640
      for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
71,280✔
641
        if (tags->parseredTags->pTagIndex[k] == tags->pColIndex[c]) {
49,896✔
642
          found = true;
4,752✔
643
          break;
4,752✔
644
        }
645
      }
646
      if (found) {
26,136✔
647
        continue;
4,752✔
648
      }
649
    }
650

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

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

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

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

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

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

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

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

785
end:
546,275✔
786
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
1,655,562✔
787
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
1,109,274✔
788
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
1,109,287✔
789
      taosMemoryFreeClear(p->pData);
4,158✔
790
    }
791
  }
792
  taosArrayDestroy(pTagArray);
545,820✔
793
  taosArrayDestroy(tagName);
546,314✔
794
  taosMemoryFree(pTag);
546,210✔
795

796
  return code;
546,210✔
797
}
798

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

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

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

815
  char* src_buf = src->buffer;
2,514✔
816
  char* dst_buf = dst->buffer;
2,514✔
817
  for (int32_t i = 0; i < src->num; ++i) {
45,276✔
818
    if (src->is_null && src->is_null[i]) {
42,762✔
819
      continue;
99✔
820
    }
821

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

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

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

845
  return TSDB_CODE_SUCCESS;
2,514✔
846
}
847

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

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

883
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
9,222,508✔
884
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
8,126,148✔
885
    if (pColSchema->colId <= lastColId) {
8,124,947✔
886
      colInOrder = false;
693✔
887
    } else {
888
      lastColId = pColSchema->colId;
8,130,665✔
889
    }
890

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

901
    TAOS_STMT2_BIND bindData = bind[bindIdx];
8,133,430✔
902

903
    if (bindData.num != rowNum) {
8,129,053✔
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,129,053✔
909
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
8,122,505✔
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,131,451✔
916
      code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
2,514✔
917
      TAOS_CHECK_GOTO(code, &lino, _return);
2,514✔
918

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

948
    pBindInfos[c].columnId = pColSchema->colId;
8,119,629✔
949
    pBindInfos[c].type = pColSchema->type;
8,120,103✔
950
    pBindInfos[c].bytes = pColSchema->bytes;
8,123,778✔
951

952
    bindIdx++;
8,118,084✔
953
  }
954

955
  if (hasBlob == 0) {
1,101,669✔
956
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pSchemaExt, pCols,
1,101,487✔
957
                              &pDataBlock->ordered, &pDataBlock->duplicateTs);
958
    TAOS_CHECK_GOTO(code, &lino, _return);
1,101,804✔
959
  } else {
960
    code = tBlobSetCreate(1024, 1, ppBlob);
182✔
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);
43✔
966
  }
967

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

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

983
  return code;
1,101,519✔
984
}
985

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

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

999
  if (NULL == dst->length) {
28,419,960✔
1000
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
28,620,960✔
1001
    if (NULL == dst->length) {
28,926,055✔
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;
28,864,311✔
1009
  char* dst_buf = dst->buffer;
28,676,326✔
1010
  for (int32_t i = 0; i < src->num; ++i) {
57,669,494✔
1011
    if (src->is_null && src->is_null[i]) {
28,832,027✔
1012
      continue;
1,426,429✔
1013
    }
1014

1015
    if (src->length[i] == 0) {
27,412,235✔
1016
      dst->length[i] = 0;
21,410✔
1017
      continue;
21,410✔
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)) {
27,233,513✔
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;
26,875,652✔
1032
    src_buf += src->length[i];
27,375,727✔
1033
    dst_buf += output;
27,353,567✔
1034
  }
1035

1036
  dst->buffer_type = src->buffer_type;
28,878,398✔
1037
  dst->is_null = src->is_null;
28,919,662✔
1038
  dst->num = src->num;
28,798,617✔
1039

1040
  return TSDB_CODE_SUCCESS;
28,824,990✔
1041
}
1042

1043
int32_t qBindStmtColsValue2(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
53,910,136✔
1044
                            int32_t msgBufLen, void* charsetCxt) {
1045
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
53,910,136✔
1046
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
53,910,136✔
1047
  SSchemaExt*      pExtSchema = getTableColumnExtSchema(pDataBlock->pMeta);
54,639,966✔
1048
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
54,308,145✔
1049
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
54,600,250✔
1050
  int32_t          rowNum = bind->num;
54,634,968✔
1051
  TAOS_STMT2_BIND  ncharBind = {0};
54,616,663✔
1052
  TAOS_STMT2_BIND* pBind = NULL;
54,372,130✔
1053
  int32_t          code = 0;
54,372,130✔
1054
  int32_t          lino = 0;
54,372,130✔
1055
  int32_t          bindIdx = 0;
54,372,130✔
1056

1057
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
579,977,375✔
1058
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
524,226,637✔
1059
    SColData* pCol = taosArrayGet(pCols, c);
517,953,304✔
1060
    if (pCol == NULL || pColSchema == NULL) {
511,728,377✔
1061
      code = buildInvalidOperationMsg(&pBuf, "get column schema or column data failed");
×
1062
      goto _return;
×
1063
    }
1064

1065
    if (boundInfo->pColIndex[c] == 0) {
515,066,441✔
1066
      pCol->cflag |= COL_IS_KEY;
54,822,128✔
1067
    }
1068

1069
    if (parsedCols) {
516,153,295✔
1070
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
13,959✔
1071
      if (pParsedVal) {
13,959✔
1072
        for (int row = 0; row < rowNum; row++) {
5,544✔
1073
          code = tColDataAppendValue(pCol, pParsedVal);
2,871✔
1074
          if (code) {
2,871✔
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,673✔
1080
      }
1081
    }
1082
    TAOS_STMT2_BIND bindData = bind[bindIdx];
516,150,622✔
1083
    if (bindData.num != rowNum) {
514,707,552✔
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)) &&
514,707,552✔
1089
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
497,786,338✔
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) {
518,434,895✔
1096
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
28,769,517✔
1097
      if (code) {
28,277,590✔
1098
        goto _return;
×
1099
      }
1100
      pBind = &ncharBind;
28,277,590✔
1101
    } else {
1102
      pBind = bind + bindIdx;
484,039,801✔
1103
    }
1104
    int8_t  isBlob = 0;
519,613,173✔
1105
    int32_t bytes = -1;
519,613,173✔
1106
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
519,613,173✔
1107
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
38,311,203✔
1108
        isBlob = 1;
201✔
1109
        bytes = TSDB_MAX_BLOB_LEN;
201✔
1110
      } else {
1111
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
58,539,870✔
1112
      }
1113
    }
1114

1115
    if (pColSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
534,691,357✔
1116
      code = tColDataAddValueByBind2WithGeos(pCol, pBind, bytes, initCtxAsText, checkWKB);
151,583✔
1117
    } else if (isBlob == 1) {
526,557,234✔
1118
      if (pDataBlock->pData->pBlobSet == NULL) {
201✔
1119
        code = tBlobSetCreate(1024, 1, &pDataBlock->pData->pBlobSet);
173✔
1120
        if (code) {
173✔
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);
201✔
1126
    } else if (IS_DECIMAL_TYPE(pColSchema->type)) {
526,558,062✔
1127
      if (pExtSchema == NULL) {
110,545✔
1128
        code = buildInvalidOperationMsg(&pBuf, "decimal column ext schema is null");
×
1129
        goto _return;
×
1130
      }
1131
      uint8_t precision = 0, scale = 0;
110,545✔
1132
      decimalFromTypeMod(pExtSchema[c].typeMod, &precision, &scale);
110,545✔
1133
      code = tColDataAddValueByBind2WithDecimal(pCol, pBind, bytes, precision, scale);
1,029✔
1134
    } else {
1135
      code = tColDataAddValueByBind2(pCol, pBind, bytes);
519,337,320✔
1136
    }
1137

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

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

1147
_return:
54,811,267✔
1148

1149
  taosMemoryFree(ncharBind.buffer);
54,704,739✔
1150
  taosMemoryFree(ncharBind.length);
54,671,120✔
1151

1152
  return code;
54,819,813✔
1153
}
1154

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

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

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

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

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

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

1236
_return:
594✔
1237

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

1241
  return code;
594✔
1242
}
1243

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

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

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

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

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

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

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

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

1337
_return:
198✔
1338

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

1342
  return code;
198✔
1343
}
1344

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

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

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

1366
  *fieldNum = numOfBound;
693✔
1367

1368
  return TSDB_CODE_SUCCESS;
693✔
1369
}
1370

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1511
  return TSDB_CODE_SUCCESS;
5,370✔
1512
}
1513

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

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

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

1535
  return TSDB_CODE_SUCCESS;
99✔
1536
}
1537

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

1553
  return TSDB_CODE_SUCCESS;
594✔
1554
}
1555

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

1566
    return TSDB_CODE_SUCCESS;
×
1567
  }
1568

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

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

1593
  *fieldNum = numOfBound;
495✔
1594

1595
  return TSDB_CODE_SUCCESS;
495✔
1596
}
1597

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

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

1642
  int8_t flag = 0;
14,271,969✔
1643
  for (int32_t i = 0; i < colNum; ++i) {
57,741,918✔
1644
    flag = pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
43,472,755✔
1645
    if (pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
43,478,390✔
1646
      SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i);
43,490,966✔
1647
      if (pCol == NULL) {
43,491,118✔
1648
        parserError("qResetStmtDataBlock column:%d is NULL", i);
×
1649
        return terrno;
×
1650
      }
1651
      if (deepClear) {
43,491,144✔
1652
        tColDataDeepClear(pCol);
21,401,792✔
1653

1654
      } else {
1655
        tColDataClear(pCol);
22,089,352✔
1656
      }
1657

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

1663
  tBlobSetDestroy(pBlock->pData->pBlobSet);
14,269,163✔
1664
  pBlock->pData->pBlobSet = NULL;
14,257,233✔
1665
  return code;
14,263,666✔
1666
}
1667

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

1671
  *pDst = taosMemoryCalloc(1, sizeof(STableDataCxt));
7,083,049✔
1672
  if (NULL == *pDst) {
7,082,006✔
1673
    return terrno;
×
1674
  }
1675

1676
  STableDataCxt* pNewCxt = (STableDataCxt*)*pDst;
7,084,088✔
1677
  STableDataCxt* pCxt = (STableDataCxt*)pSrc;
7,084,088✔
1678
  pNewCxt->hasBlob = pSrc->hasBlob;
7,084,088✔
1679
  pNewCxt->pSchema = NULL;
7,088,647✔
1680
  pNewCxt->pValues = NULL;
7,087,958✔
1681

1682
  if (pCxt->pMeta) {
7,087,864✔
1683
    pNewCxt->pMeta = tableMetaDup(pCxt->pMeta);
7,088,623✔
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,087,618✔
1694
  pNewCxt->boundColsInfo.pColIndex = NULL;
7,090,794✔
1695

1696
  if (pCxt->boundColsInfo.pColIndex) {
7,088,688✔
1697
    void* pNewColIdx = taosMemoryMalloc(pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
7,087,982✔
1698
    if (NULL == pNewColIdx) {
7,083,806✔
1699
      insDestroyTableDataCxt(*pDst);
×
1700
      return terrno;
×
1701
    }
1702
    memcpy(pNewColIdx, pCxt->boundColsInfo.pColIndex,
7,086,247✔
1703
           pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
7,083,806✔
1704
    pNewCxt->boundColsInfo.pColIndex = pNewColIdx;
7,086,606✔
1705
  }
1706

1707
  if (pCxt->pData) {
7,088,664✔
1708
    int8_t         flag = 1;
7,086,247✔
1709
    SSubmitTbData* pNewTb = (SSubmitTbData*)taosMemoryMalloc(sizeof(SSubmitTbData));
7,086,247✔
1710
    if (NULL == pNewTb) {
7,078,900✔
1711
      insDestroyTableDataCxt(*pDst);
×
1712
      return terrno;
×
1713
    }
1714

1715
    memcpy(pNewTb, pCxt->pData, sizeof(*pCxt->pData));
7,078,900✔
1716
    pNewTb->pCreateTbReq = NULL;
7,080,647✔
1717
    if (pNewTb->pBlobSet != NULL) {
7,086,618✔
1718
      flag = pNewTb->pBlobSet->type;
×
1719
    }
1720
    pNewTb->pBlobSet = NULL;
7,088,006✔
1721

1722
    pNewTb->aCol = taosArrayDup(pCxt->pData->aCol, NULL);
7,085,565✔
1723
    if (NULL == pNewTb->aCol) {
7,086,929✔
1724
      insDestroyTableDataCxt(*pDst);
×
1725
      return terrno;
×
1726
    }
1727

1728
    pNewCxt->pData = pNewTb;
7,080,300✔
1729

1730
    if (reset) {
7,082,035✔
1731
      code = qResetStmtDataBlock(*pDst, true);
7,076,464✔
1732
    }
1733
  }
1734

1735
  return code;
7,089,370✔
1736
}
1737

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

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

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

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

1762
  return TSDB_CODE_SUCCESS;
9,424✔
1763
}
1764

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

1767
void qDestroyStmtDataBlock(STableDataCxt* pBlock) {
21,492,323✔
1768
  if (pBlock == NULL) {
21,492,323✔
1769
    return;
7,203,604✔
1770
  }
1771

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