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

taosdata / TDengine / #4821

22 Oct 2025 06:02AM UTC coverage: 61.242% (-0.1%) from 61.353%
#4821

push

travis-ci

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

fix(stream): reset tableScan operator (#33225)

156089 of 324573 branches covered (48.09%)

Branch coverage included in aggregate %.

184 of 244 new or added lines in 9 files covered. (75.41%)

789 existing lines in 124 files now uncovered.

207891 of 269759 relevant lines covered (77.07%)

244003752.29 hits per line

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

63.9
/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) {
9,963,942✔
35
  if (pDataBlock == NULL || pDataBlock->pData == NULL) {
9,963,942!
36
    qError("stmt qCloneCurrentTbData input is null, maybe not bind data before execute");
65!
37
    return TSDB_CODE_TSC_STMT_API_ERROR;
65✔
38
  }
39
  *pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
9,966,400!
40
  if (NULL == *pData) {
9,963,877!
41
    return terrno;
×
42
  }
43

44
  int8_t         flag = 1;
9,964,718✔
45
  SSubmitTbData* pNew = *pData;
9,964,718✔
46

47
  *pNew = *pDataBlock->pData;
9,965,559✔
48
  pNew->pBlobSet = NULL;
9,965,559✔
49

50
  int32_t code = cloneSVreateTbReq(pDataBlock->pData->pCreateTbReq, &pNew->pCreateTbReq);
9,965,559✔
51
  if (TSDB_CODE_SUCCESS != code) {
9,962,195!
52
    taosMemoryFreeClear(*pData);
×
53
    return code;
×
54
  }
55
  pNew->aCol = taosArrayDup(pDataBlock->pData->aCol, NULL);
9,962,195✔
56
  if (!pNew->aCol) {
9,966,400!
57
    code = terrno;
×
58
    taosMemoryFreeClear(*pData);
×
59
    return code;
×
60
  }
61

62
  int32_t colNum = taosArrayGetSize(pNew->aCol);
9,965,559✔
63
  for (int32_t i = 0; i < colNum; ++i) {
46,638,832✔
64
    if (pDataBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
36,672,432✔
65
      SColData* pCol = (SColData*)taosArrayGet(pNew->aCol, i);
36,673,143✔
66
      tColDataDeepClear(pCol);
36,671,858✔
67
    } else {
68
      pNew->aCol = taosArrayInit(20, POINTER_BYTES);
130✔
69
      if (pNew->aCol == NULL) {
130!
70
        code = terrno;
×
71
        taosMemoryFreeClear(*pData);
×
72
        return code;
×
73
      }
74
    }
75
  }
76

77
  return code;
9,966,400✔
78
}
79

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

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

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

96
  if (TSDB_CODE_SUCCESS == code) {
12,239,136!
97
    code = insBuildVgDataBlocks(pAllVgHash, pVgDataBlocks, &pStmt->pDataBlocks, true);
12,241,714✔
98
  }
99

100
  if (pStmt->freeArrayFunc) {
12,236,750!
101
    pStmt->freeArrayFunc(pVgDataBlocks);
12,241,714✔
102
  }
103
  return code;
12,240,521✔
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) {
9,963,036✔
128
  int32_t             code = TSDB_CODE_SUCCESS;
9,963,036✔
129
  SArray*             pVgDataBlocks = NULL;
9,963,036✔
130
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
9,965,559✔
131

132
  // merge according to vgId
133
  if (taosHashGetSize(pBlockHash) > 0) {
9,966,400!
134
    code = insMergeTableDataCxt(pBlockHash, &pVgDataBlocks, true);
9,963,877✔
135
  }
136

137
  if (TSDB_CODE_SUCCESS == code) {
9,965,559!
138
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks, false);
9,965,559✔
139
  }
140

141
  if (pStmt->freeArrayFunc) {
9,964,383!
142
    pStmt->freeArrayFunc(pVgDataBlocks);
9,965,061✔
143
  }
144
  return code;
9,956,316✔
145
}
146

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

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

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

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

170
  bool  isJson = false;
16,371✔
171
  STag* pTag = NULL;
16,371✔
172

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

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

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

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

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

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

290
end:
16,371✔
291
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
62,934✔
292
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
46,563✔
293
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
46,563✔
294
      taosMemoryFreeClear(p->pData);
2,788!
295
    }
296
  }
297
  taosArrayDestroy(pTagArray);
16,371✔
298
  taosArrayDestroy(tagName);
16,371✔
299
  taosMemoryFree(pTag);
16,371!
300

301
  return code;
16,371✔
302
}
303

304
int32_t convertStmtNcharCol(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_MULTI_BIND* src, TAOS_MULTI_BIND* dst,
118,918,868✔
305
                            void* charsetCxt) {
306
  int32_t output = 0;
118,918,868✔
307
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
118,930,178✔
308
  if (dst->buffer_length < newBuflen) {
118,933,583!
309
    dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
120,478,966!
310
    if (NULL == dst->buffer) {
120,807,413!
311
      return terrno;
×
312
    }
313
  }
314

315
  if (NULL == dst->length) {
119,265,434✔
316
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
119,385,955!
317
    if (NULL == dst->length) {
119,270,133!
318
      taosMemoryFreeClear(dst->buffer);
×
319
      return terrno;
×
320
    }
321
  }
322

323
  dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
119,146,195✔
324

325
  for (int32_t i = 0; i < src->num; ++i) {
259,662,776✔
326
    if (src->is_null && src->is_null[i]) {
139,358,700✔
327
      continue;
5,982,154✔
328
    }
329

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

341
    dst->length[i] = output;
134,526,517✔
342
  }
343

344
  dst->buffer_type = src->buffer_type;
120,295,084✔
345
  dst->is_null = src->is_null;
120,300,688✔
346
  dst->num = src->num;
120,297,316✔
347

348
  return TSDB_CODE_SUCCESS;
120,301,823✔
349
}
350

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

364
  if (NULL == pTSchema || NULL == *pTSchema) {
11,740,526!
365
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
96,751✔
366
  }
367

368
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
71,896,293✔
369
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
60,152,763✔
370
    if (pColSchema->colId <= lastColId) {
60,162,327!
371
      colInOrder = false;
×
372
    } else {
373
      lastColId = pColSchema->colId;
60,156,407✔
374
    }
375
    // SColData* pCol = taosArrayGet(pCols, c);
376

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

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

388
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
60,161,163✔
389
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
199,740✔
390
      if (code) {
199,740!
391
        goto _return;
×
392
      }
393
      pBind = &ncharBind;
199,740✔
394
    } else {
395
      pBind = bind + c;
59,959,019✔
396
    }
397

398
    pBindInfos[c].columnId = pColSchema->colId;
60,155,199✔
399
    pBindInfos[c].bind = pBind;
60,162,365✔
400
    pBindInfos[c].type = pColSchema->type;
60,157,525✔
401
  }
402

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

406
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
11,738,774!
407

408
_return:
11,738,774✔
409

410
  taosMemoryFree(ncharBind.buffer);
11,738,774!
411
  taosMemoryFree(ncharBind.length);
11,740,072!
412

413
  return code;
11,736,121✔
414
}
415

416
int32_t qBindStmtColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
240,237,980✔
417
                           void* charsetCxt) {
418
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
240,237,980✔
419
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
240,237,980✔
420
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
243,058,497✔
421
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
243,097,103✔
422
  int32_t          rowNum = bind->num;
243,125,166✔
423
  TAOS_MULTI_BIND  ncharBind = {0};
243,225,625✔
424
  TAOS_MULTI_BIND* pBind = NULL;
242,879,468✔
425
  int32_t          code = 0;
242,879,468✔
426

427
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
2,147,483,647✔
428
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
2,147,483,647✔
429
    SColData* pCol = taosArrayGet(pCols, c);
2,147,483,647✔
430

431
    if (bind[c].num != rowNum) {
2,147,483,647✔
432
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
5,605,376✔
433
      goto _return;
×
434
    }
435

436
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
2,147,483,647✔
437
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
2,145,380,470!
438
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
439
      goto _return;
×
440
    }
441

442
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
2,147,483,647✔
443
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
119,794,548✔
444
      if (code) {
120,142,685!
445
        goto _return;
×
446
      }
447
      pBind = &ncharBind;
120,142,685✔
448
    } else {
449
      pBind = bind + c;
2,085,564,143✔
450
    }
451

452
    int32_t bytes = 0;
2,147,483,647✔
453
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
2,147,483,647!
454
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
203,957,200✔
455
        bytes = pColSchema->bytes - BLOBSTR_HEADER_SIZE;
4,364✔
456
      } else {
457
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
239,548,737✔
458
      }
459
    } else {
460
      bytes = -1;
2,002,511,207✔
461
    }
462
    code = tColDataAddValueByBind(pCol, pBind, bytes, initCtxAsText, checkWKB);
2,147,483,647✔
463
    if (code) {
2,147,483,647✔
464
      goto _return;
9,425✔
465
    }
466
  }
467

468
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
218,518,946✔
469

470
_return:
218,528,306✔
471

472
  taosMemoryFree(ncharBind.buffer);
218,336,408!
473
  taosMemoryFree(ncharBind.length);
243,433,607!
474

475
  return code;
243,160,427✔
476
}
477

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

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

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

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

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

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

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

538
_return:
325✔
539

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

543
  return code;
325✔
544
}
545

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

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

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

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

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

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

587
_return:
5,460✔
588

589
  taosMemoryFree(ncharBind.buffer);
5,460!
590
  taosMemoryFree(ncharBind.length);
5,460!
591

592
  return code;
5,460✔
593
}
594

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

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

616
  SArray* tagName;
617
  if (tags->parseredTags) {
14,435✔
618
    tagName = taosArrayDup(tags->parseredTags->STagNames, NULL);
1,560✔
619
  } else {
620
    tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
12,875✔
621
  }
622

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

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

630
  bool  isJson = false;
14,435✔
631
  STag* pTag = NULL;
14,435✔
632

633
  int bindIdx = 0;
14,435✔
634
  for (int c = 0; c < tags->numOfBound; ++c) {
54,290✔
635
    if (bind == NULL) {
41,285✔
636
      break;
1,365✔
637
    }
638
    if (tags->parseredTags) {
39,920✔
639
      bool found = false;
17,160✔
640
      for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
46,800✔
641
        if (tags->parseredTags->pTagIndex[k] == tags->pColIndex[c]) {
32,760✔
642
          found = true;
3,120✔
643
          break;
3,120✔
644
        }
645
      }
646
      if (found) {
17,160✔
647
        continue;
3,120✔
648
      }
649
    }
650

651
    TAOS_STMT2_BIND bindData = bind[bindIdx++];
36,800✔
652

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

657
    SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
36,800✔
658
    int32_t  colLen = pTagSchema->bytes;
36,800✔
659
    if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
36,800!
660
      if (IS_STR_DATA_BLOB(pTagSchema->type)) {
14,890!
661
        return TSDB_CODE_BLOB_NOT_SUPPORT_TAG;
×
662
      }
663
      if (!bindData.length) {
14,890!
664
        code = buildInvalidOperationMsg(&pBuf, "var tag length is null");
×
665
        goto end;
65✔
666
      }
667
      colLen = bindData.length[0];
14,890✔
668
      if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
14,890!
669
        code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
×
670
        goto end;
×
671
      }
672
    }
673
    if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
73,600!
674
      code = terrno;
×
675
      goto end;
×
676
    }
677
    if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
36,800!
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};
36,800✔
697
      //      strcpy(val.colName, pTagSchema->name);
698
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
36,800!
699
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
26,655✔
700
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
12,160✔
701
          code = initCtxAsText();
2,015✔
702
          if (code) {
2,015!
703
            qError("geometry init failed:%s", tstrerror(code));
×
704
            goto end;
65✔
705
          }
706
          code = checkWKB(bindData.buffer, colLen);
2,015✔
707
          if (code) {
2,015✔
708
            qError("stmt2 bind invalid geometry tag:%s, must be WKB format", (char*)bindData.buffer);
65!
709
            goto end;
65✔
710
          }
711
        }
712
        val.pData = (uint8_t*)bindData.buffer;
12,095✔
713
        val.nData = colLen;
12,095✔
714
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
24,640✔
715
        int32_t output = 0;
2,730✔
716
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
2,730!
717
        if (p == NULL) {
2,730!
718
          code = terrno;
×
719
          goto end;
×
720
        }
721
        if (colLen != 0) {
2,730✔
722
          if (!taosMbsToUcs4(bindData.buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
2,600!
723
            if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
724
              taosMemoryFree(p);
×
725
              code = generateSyntaxErrMsg(&pBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pTagSchema->name);
×
726
              goto end;
×
727
            }
728
            char buf[512] = {0};
×
729
            snprintf(buf, tListLen(buf), " taosMbsToUcs4 error:%s", strerror(terrno));
×
730
            taosMemoryFree(p);
×
731
            code = buildSyntaxErrMsg(&pBuf, buf, bindData.buffer);
×
732
            goto end;
×
733
          }
734
        }
735
        val.pData = p;
2,730✔
736
        val.nData = output;
2,730✔
737

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

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

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

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

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

782
end:
14,435✔
783
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
54,290✔
784
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
39,855✔
785
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
39,855✔
786
      taosMemoryFreeClear(p->pData);
2,730!
787
    }
788
  }
789
  taosArrayDestroy(pTagArray);
14,435✔
790
  taosArrayDestroy(tagName);
14,435✔
791
  taosMemoryFree(pTag);
14,435!
792

793
  return code;
14,435✔
794
}
795

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

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

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

812
  char* src_buf = src->buffer;
5,192✔
813
  char* dst_buf = dst->buffer;
5,192✔
814
  for (int32_t i = 0; i < src->num; ++i) {
3,574,662✔
815
    if (src->is_null && src->is_null[i]) {
3,569,470✔
816
      continue;
65✔
817
    }
818

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

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

833
    dst->length[i] = output;
3,569,340✔
834
    src_buf += src->length[i];
3,569,340✔
835
    dst_buf += output;
3,569,340✔
836
  }
837

838
  dst->buffer_type = src->buffer_type;
5,192✔
839
  dst->is_null = src->is_null;
5,192✔
840
  dst->num = src->num;
5,192✔
841

842
  return TSDB_CODE_SUCCESS;
5,192✔
843
}
844

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

867
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
142,987,104✔
868
    if (TSDB_DATA_TYPE_NCHAR == pSchema[boundInfo->pColIndex[c]].type) {
134,003,933✔
869
      ncharColNums++;
5,192✔
870
    }
871
  }
872
  if (ncharColNums > 0) {
8,989,138✔
873
    ncharBinds = taosArrayInit(ncharColNums, sizeof(ncharBind));
5,192✔
874
    if (!ncharBinds) {
5,192!
875
      code = terrno;
×
876
      TAOS_CHECK_GOTO(code, &lino, _return);
×
877
    }
878
  }
879

880
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
143,072,640✔
881
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
134,071,213✔
882
    if (pColSchema->colId <= lastColId) {
134,075,989✔
883
      colInOrder = false;
455✔
884
    } else {
885
      lastColId = pColSchema->colId;
134,070,758✔
886
    }
887

888
    if (parsedCols) {
134,086,734✔
889
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
8,580✔
890
      if (pParsedVal) {
8,580✔
891
        pBindInfos[c].columnId = pColSchema->colId;
1,560✔
892
        pBindInfos[c].type = pColSchema->type;
1,560✔
893
        pBindInfos[c].bytes = pColSchema->bytes;
1,560✔
894
        continue;
1,560✔
895
      }
896
    }
897

898
    TAOS_STMT2_BIND bindData = bind[bindIdx];
134,085,174✔
899

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

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

911
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
134,013,535✔
912
      code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
5,192✔
913
      TAOS_CHECK_GOTO(code, &lino, _return);
5,192!
914

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

944
    pBindInfos[c].columnId = pColSchema->colId;
134,045,833✔
945
    pBindInfos[c].type = pColSchema->type;
134,069,707✔
946
    pBindInfos[c].bytes = pColSchema->bytes;
134,029,110✔
947

948
    bindIdx++;
134,081,591✔
949
  }
950

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

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

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

966
_return:
8,988,166✔
967
  if (ncharBinds) {
8,988,296✔
968
    for (int i = 0; i < TARRAY_SIZE(ncharBinds); ++i) {
10,384✔
969
      TAOS_STMT2_BIND* ncBind = TARRAY_DATA(ncharBinds);
5,192✔
970
      taosMemoryFree(ncBind[i].buffer);
5,192!
971
      taosMemoryFree(ncBind[i].length);
5,192!
972
    }
973
    taosArrayDestroy(ncharBinds);
5,192✔
974
  }
975
  if (code != 0) {
8,988,296✔
976
    parserError("stmt2 failed to bind stb col at lino %d since %s", lino, tstrerror(code));
130!
977
  }
978

979
  return code;
8,988,179✔
980
}
981

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

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

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

1003
  // dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
1004
  char* src_buf = src->buffer;
1,430✔
1005
  char* dst_buf = dst->buffer;
1,430✔
1006
  for (int32_t i = 0; i < src->num; ++i) {
6,435✔
1007
    if (src->is_null && src->is_null[i]) {
5,005✔
1008
      continue;
65✔
1009
    }
1010

1011
    if (src->length[i] == 0) {
4,940✔
1012
      dst->length[i] = 0;
715✔
1013
      continue;
715✔
1014
    }
1015

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

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

1032
  dst->buffer_type = src->buffer_type;
1,430✔
1033
  dst->is_null = src->is_null;
1,430✔
1034
  dst->num = src->num;
1,430✔
1035

1036
  return TSDB_CODE_SUCCESS;
1,430✔
1037
}
1038

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

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

1061
    if (boundInfo->pColIndex[c] == 0) {
28,280✔
1062
      pCol->cflag |= COL_IS_KEY;
9,410✔
1063
    }
1064

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

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

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

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

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

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

1142
_return:
9,410✔
1143

1144
  taosMemoryFree(ncharBind.buffer);
9,410!
1145
  taosMemoryFree(ncharBind.length);
9,410!
1146

1147
  return code;
9,410✔
1148
}
1149

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

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

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

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

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

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

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

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

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

1230
_return:
390✔
1231

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

1235
  return code;
390✔
1236
}
1237

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

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

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

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

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

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

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

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

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

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

1330
_return:
130✔
1331

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

1335
  return code;
130✔
1336
}
1337

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

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

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

1359
  *fieldNum = numOfBound;
455✔
1360

1361
  return TSDB_CODE_SUCCESS;
455✔
1362
}
1363

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

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

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

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

1398
    if (hastag && tags->numOfBound > 0 && !tags->mixTagsCols) {
3,462!
1399
      SSchema* tagSchema = getTableTagSchema(pMeta);
1,057✔
1400

1401
      for (int32_t i = 0; i < tags->numOfBound; ++i) {
3,886✔
1402
        SSchema* schema = &tagSchema[tags->pColIndex[i]];
2,829✔
1403

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

1418
        (*fields)[idx].field_type = TAOS_FIELD_TAG;
2,764✔
1419

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

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

1433
      for (int32_t i = 0; i < boundColsInfo.numOfBound; ++i) {
15,945✔
1434
        int16_t idxCol = boundColsInfo.pColIndex[i];
12,483✔
1435

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

1442
          idx++;
715✔
1443
          continue;
715✔
1444
        } else if (idxCol < pMeta->tableInfo.numOfColumns) {
11,768✔
1445
          if (parsedCols && tSimpleHashGet(parsedCols, &pSchema[idxCol].colId, sizeof(int16_t))) {
9,688✔
1446
            continue;
325✔
1447
          }
1448
          (*fields)[idx].field_type = TAOS_FIELD_COL;
9,363✔
1449
        } else {
1450
          if (tags->parseredTags && tags->parseredTags->numOfTags > 0) {
2,080!
1451
            int32_t tag_idx = idxCol - pMeta->tableInfo.numOfColumns;
130✔
1452
            bool    found = false;
130✔
1453
            for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
195✔
1454
              if (tags->parseredTags->pTagIndex[k] == tag_idx) {
130✔
1455
                found = true;
65✔
1456
                break;
65✔
1457
              }
1458
            }
1459
            if (found) {
130✔
1460
              continue;
65✔
1461
            }
1462
          }
1463
          (*fields)[idx].field_type = TAOS_FIELD_TAG;
2,015✔
1464
        }
1465

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

1481
  *fieldNum = numOfBound;
3,462✔
1482

1483
  return TSDB_CODE_SUCCESS;
3,462✔
1484
}
1485

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

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

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

1502
    return TSDB_CODE_SUCCESS;
×
1503
  }
1504

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

1507
  return TSDB_CODE_SUCCESS;
65✔
1508
}
1509

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

1519
    return TSDB_CODE_SUCCESS;
×
1520
  }
1521

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

1525
  return TSDB_CODE_SUCCESS;
390✔
1526
}
1527

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

1538
    return TSDB_CODE_SUCCESS;
×
1539
  }
1540

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

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

1565
  *fieldNum = numOfBound;
325✔
1566

1567
  return TSDB_CODE_SUCCESS;
325✔
1568
}
1569

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

1580
    return TSDB_CODE_SUCCESS;
×
1581
  }
1582

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

1586
  return TSDB_CODE_SUCCESS;
3,462✔
1587
}
1588

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

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

1605
  return TSDB_CODE_SUCCESS;
×
1606
}
1607

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

1613
  int8_t flag = 0;
18,602,059✔
1614
  for (int32_t i = 0; i < colNum; ++i) {
83,351,162✔
1615
    flag = pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
64,751,660✔
1616
    if (pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
64,754,554✔
1617
      SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i);
64,756,615✔
1618
      if (pCol == NULL) {
64,760,676!
UNCOV
1619
        parserError("qResetStmtDataBlock column:%d is NULL", i);
×
UNCOV
1620
        return terrno;
×
1621
      }
1622
      if (deepClear) {
64,760,703✔
1623
        tColDataDeepClear(pCol);
28,094,434✔
1624

1625
      } else {
1626
        tColDataClear(pCol);
36,666,269✔
1627
      }
1628

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

1634
  tBlobSetDestroy(pBlock->pData->pBlobSet);
18,599,502✔
1635
  pBlock->pData->pBlobSet = NULL;
18,600,160✔
1636
  return code;
18,601,445✔
1637
}
1638

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

1642
  *pDst = taosMemoryCalloc(1, sizeof(STableDataCxt));
8,643,609!
1643
  if (NULL == *pDst) {
8,643,139!
1644
    return terrno;
×
1645
  }
1646

1647
  STableDataCxt* pNewCxt = (STableDataCxt*)*pDst;
8,643,139✔
1648
  STableDataCxt* pCxt = (STableDataCxt*)pSrc;
8,644,343✔
1649
  pNewCxt->hasBlob = pSrc->hasBlob;
8,644,343✔
1650
  pNewCxt->pSchema = NULL;
8,646,025✔
1651
  pNewCxt->pValues = NULL;
8,644,813✔
1652

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

1664
  memcpy(&pNewCxt->boundColsInfo, &pCxt->boundColsInfo, sizeof(pCxt->boundColsInfo));
8,643,972!
1665
  pNewCxt->boundColsInfo.pColIndex = NULL;
8,646,025✔
1666

1667
  if (pCxt->boundColsInfo.pColIndex) {
8,645,184!
1668
    void* pNewColIdx = taosMemoryMalloc(pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
8,646,025!
1669
    if (NULL == pNewColIdx) {
8,644,835!
1670
      insDestroyTableDataCxt(*pDst);
×
1671
      return terrno;
×
1672
    }
1673
    memcpy(pNewColIdx, pCxt->boundColsInfo.pColIndex,
8,641,457!
1674
           pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
8,644,835✔
1675
    pNewCxt->boundColsInfo.pColIndex = pNewColIdx;
8,644,821✔
1676
  }
1677

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

1686
    memcpy(pNewTb, pCxt->pData, sizeof(*pCxt->pData));
8,644,835!
1687
    pNewTb->pCreateTbReq = NULL;
8,646,025✔
1688
    if (pNewTb->pBlobSet != NULL) {
8,644,835!
1689
      flag = pNewTb->pBlobSet->type;
×
1690
    }
1691
    pNewTb->pBlobSet = NULL;
8,646,025✔
1692

1693
    pNewTb->aCol = taosArrayDup(pCxt->pData->aCol, NULL);
8,643,972✔
1694
    if (NULL == pNewTb->aCol) {
8,646,025!
1695
      insDestroyTableDataCxt(*pDst);
×
1696
      return terrno;
×
1697
    }
1698

1699
    pNewCxt->pData = pNewTb;
8,645,184✔
1700

1701
    if (reset) {
8,645,184✔
1702
      code = qResetStmtDataBlock(*pDst, true);
8,636,839✔
1703
    }
1704
  }
1705

1706
  return code;
8,645,067✔
1707
}
1708

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

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

1723
  pBlock->pData->suid = suid;
9,186✔
1724
  pBlock->pData->uid = uid;
9,186✔
1725

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

1733
  return TSDB_CODE_SUCCESS;
9,186✔
1734
}
1735

1736
STableMeta* qGetTableMetaInDataBlock(STableDataCxt* pDataBlock) { return ((STableDataCxt*)pDataBlock)->pMeta; }
19,889,808✔
1737

1738
void qDestroyStmtDataBlock(STableDataCxt* pBlock) {
28,774,399✔
1739
  if (pBlock == NULL) {
28,774,399✔
1740
    return;
10,015,504✔
1741
  }
1742

1743
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
18,758,895✔
1744
  insDestroyTableDataCxt(pDataBlock);
18,758,895✔
1745
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc