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

taosdata / TDengine / #4720

08 Sep 2025 08:43AM UTC coverage: 58.139% (-0.6%) from 58.762%
#4720

push

travis-ci

web-flow
Merge pull request #32881 from taosdata/enh/add-new-windows-ci

fix(ci): update workflow reference to use new Windows CI YAML

133181 of 292179 branches covered (45.58%)

Branch coverage included in aggregate %.

201691 of 283811 relevant lines covered (71.07%)

5442780.71 hits per line

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

63.47
/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) {
10,908✔
35
  *pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
10,908!
36
  if (NULL == *pData) {
10,912!
37
    return terrno;
×
38
  }
39

40
  int8_t         flag = 1;
10,912✔
41
  SSubmitTbData* pNew = *pData;
10,912✔
42

43
  *pNew = *pDataBlock->pData;
10,912✔
44
  pNew->pBlobSet = NULL;
10,912✔
45

46
  int32_t code = cloneSVreateTbReq(pDataBlock->pData->pCreateTbReq, &pNew->pCreateTbReq);
10,912✔
47
  if (TSDB_CODE_SUCCESS != code) {
10,922!
48
    taosMemoryFreeClear(*pData);
×
49
    return code;
×
50
  }
51
  pNew->aCol = taosArrayDup(pDataBlock->pData->aCol, NULL);
10,922✔
52
  if (!pNew->aCol) {
10,924!
53
    code = terrno;
×
54
    taosMemoryFreeClear(*pData);
×
55
    return code;
×
56
  }
57

58
  int32_t colNum = taosArrayGetSize(pNew->aCol);
10,924✔
59
  for (int32_t i = 0; i < colNum; ++i) {
47,312✔
60
    if (pDataBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
36,392✔
61
      SColData* pCol = (SColData*)taosArrayGet(pNew->aCol, i);
36,390✔
62
      tColDataDeepClear(pCol);
36,398✔
63
    } else {
64
      pNew->aCol = taosArrayInit(20, POINTER_BYTES);
2✔
65
      if (pNew->aCol == NULL) {
2!
66
        code = terrno;
×
67
        taosMemoryFreeClear(*pData);
×
68
        return code;
×
69
      }
70
    }
71
  }
72

73
  return code;
10,920✔
74
}
75

76
int32_t qAppendStmtTableOutput(SQuery* pQuery, SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
5,629✔
77
                               SStbInterlaceInfo* pBuildInfo) {
78
  // merge according to vgId
79
  return insAppendStmtTableDataCxt(pAllVgHash, pTbData, pTbCtx, pBuildInfo);
5,629✔
80
}
81

82
int32_t qAppendStmt2TableOutput(SQuery* pQuery, SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
7,501✔
83
                                SStbInterlaceInfo* pBuildInfo, SVCreateTbReq* ctbReq) {
84
  // merge according to vgId
85
  return insAppendStmt2TableDataCxt(pAllVgHash, pTbData, pTbCtx, pBuildInfo, ctbReq);
7,501✔
86
}
87

88
int32_t qBuildStmtFinOutput(SQuery* pQuery, SHashObj* pAllVgHash, SArray* pVgDataBlocks) {
9,066✔
89
  int32_t             code = TSDB_CODE_SUCCESS;
9,066✔
90
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
9,066✔
91

92
  if (TSDB_CODE_SUCCESS == code) {
9,066!
93
    code = insBuildVgDataBlocks(pAllVgHash, pVgDataBlocks, &pStmt->pDataBlocks, true);
9,070✔
94
  }
95

96
  if (pStmt->freeArrayFunc) {
9,065!
97
    pStmt->freeArrayFunc(pVgDataBlocks);
9,069✔
98
  }
99
  return code;
9,067✔
100
}
101

102
/*
103
int32_t qBuildStmtOutputFromTbList(SQuery* pQuery, SHashObj* pVgHash, SArray* pBlockList, STableDataCxt* pTbCtx, int32_t
104
tbNum) { int32_t             code = TSDB_CODE_SUCCESS; SArray*             pVgDataBlocks = NULL; SVnodeModifyOpStmt*
105
pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
106

107
  // merge according to vgId
108
  if (tbNum > 0) {
109
    code = insMergeStmtTableDataCxt(pTbCtx, pBlockList, &pVgDataBlocks, true, tbNum);
110
  }
111

112
  if (TSDB_CODE_SUCCESS == code) {
113
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks);
114
  }
115

116
  if (pStmt->freeArrayFunc) {
117
    pStmt->freeArrayFunc(pVgDataBlocks);
118
  }
119
  return code;
120
}
121
*/
122

123
int32_t qBuildStmtOutput(SQuery* pQuery, SHashObj* pVgHash, SHashObj* pBlockHash) {
10,904✔
124
  int32_t             code = TSDB_CODE_SUCCESS;
10,904✔
125
  SArray*             pVgDataBlocks = NULL;
10,904✔
126
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
10,904✔
127

128
  // merge according to vgId
129
  if (taosHashGetSize(pBlockHash) > 0) {
10,904!
130
    code = insMergeTableDataCxt(pBlockHash, &pVgDataBlocks, true);
10,924✔
131
  }
132

133
  if (TSDB_CODE_SUCCESS == code) {
10,926!
134
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks, false);
10,929✔
135
  }
136

137
  if (pStmt->freeArrayFunc) {
10,911!
138
    pStmt->freeArrayFunc(pVgDataBlocks);
10,927✔
139
  }
140
  return code;
10,925✔
141
}
142

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

153
  SArray* pTagArray = taosArrayInit(tags->numOfBound, sizeof(STagVal));
11✔
154
  if (!pTagArray) {
11!
155
    return buildInvalidOperationMsg(&pBuf, "out of memory");
×
156
  }
157

158
  SArray* tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
11✔
159
  if (!tagName) {
11!
160
    code = buildInvalidOperationMsg(&pBuf, "out of memory");
×
161
    goto end;
×
162
  }
163

164
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
11✔
165

166
  bool  isJson = false;
11✔
167
  STag* pTag = NULL;
11✔
168

169
  for (int c = 0; c < tags->numOfBound; ++c) {
42✔
170
    if (bind[c].is_null && bind[c].is_null[0]) {
31!
171
      continue;
×
172
    }
173

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

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

263
  if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) {
11!
264
    goto end;
×
265
  }
266

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

282
  code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName,
11✔
283
                             pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
11✔
284
  pTag = NULL;
11✔
285

286
end:
11✔
287
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
42✔
288
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
31✔
289
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
31✔
290
      taosMemoryFreeClear(p->pData);
1!
291
    }
292
  }
293
  taosArrayDestroy(pTagArray);
11✔
294
  taosArrayDestroy(tagName);
11✔
295
  taosMemoryFree(pTag);
11!
296

297
  return code;
11✔
298
}
299

300
int32_t convertStmtNcharCol(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_MULTI_BIND* src, TAOS_MULTI_BIND* dst,
24,698✔
301
                            void* charsetCxt) {
302
  int32_t output = 0;
24,698✔
303
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
24,698✔
304
  if (dst->buffer_length < newBuflen) {
24,698!
305
    dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
24,708!
306
    if (NULL == dst->buffer) {
24,759!
307
      return terrno;
×
308
    }
309
  }
310

311
  if (NULL == dst->length) {
24,749!
312
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
24,769!
313
    if (NULL == dst->length) {
24,813!
314
      taosMemoryFreeClear(dst->buffer);
×
315
      return terrno;
×
316
    }
317
  }
318

319
  dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
24,793✔
320

321
  for (int32_t i = 0; i < src->num; ++i) {
64,967✔
322
    if (src->is_null && src->is_null[i]) {
40,075✔
323
      continue;
687✔
324
    }
325

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

337
    dst->length[i] = output;
39,487✔
338
  }
339

340
  dst->buffer_type = src->buffer_type;
24,892✔
341
  dst->is_null = src->is_null;
24,892✔
342
  dst->num = src->num;
24,892✔
343

344
  return TSDB_CODE_SUCCESS;
24,892✔
345
}
346

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

360
  if (NULL == pTSchema || NULL == *pTSchema) {
5,629!
361
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
76✔
362
  }
363

364
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
39,348✔
365
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
33,718✔
366
    if (pColSchema->colId <= lastColId) {
33,718!
367
      colInOrder = false;
×
368
    } else {
369
      lastColId = pColSchema->colId;
33,718✔
370
    }
371
    // SColData* pCol = taosArrayGet(pCols, c);
372

373
    if (bind[c].num != rowNum) {
33,718!
374
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
375
      goto _return;
×
376
    }
377

378
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
33,718!
379
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
33,718!
380
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
381
      goto _return;
×
382
    }
383

384
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
33,718✔
385
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
168✔
386
      if (code) {
168!
387
        goto _return;
×
388
      }
389
      pBind = &ncharBind;
168✔
390
    } else {
391
      pBind = bind + c;
33,550✔
392
    }
393

394
    pBindInfos[c].columnId = pColSchema->colId;
33,718✔
395
    pBindInfos[c].bind = pBind;
33,718✔
396
    pBindInfos[c].type = pColSchema->type;
33,718✔
397
  }
398

399
  code = tRowBuildFromBind(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols, &pDataBlock->ordered,
5,630✔
400
                           &pDataBlock->duplicateTs);
401

402
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
5,630!
403

404
_return:
5,630✔
405

406
  taosMemoryFree(ncharBind.buffer);
5,630!
407
  taosMemoryFree(ncharBind.length);
5,630!
408

409
  return code;
5,629✔
410
}
411

412
int32_t qBindStmtColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
80,621✔
413
                           void* charsetCxt) {
414
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
80,621✔
415
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
80,621✔
416
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
80,394✔
417
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
80,394✔
418
  int32_t          rowNum = bind->num;
80,394✔
419
  TAOS_MULTI_BIND  ncharBind = {0};
80,394✔
420
  TAOS_MULTI_BIND* pBind = NULL;
80,394✔
421
  int32_t          code = 0;
80,394✔
422

423
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
589,548✔
424
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
504,606✔
425
    SColData* pCol = taosArrayGet(pCols, c);
504,606✔
426

427
    if (bind[c].num != rowNum) {
503,353!
428
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
429
      goto _return;
×
430
    }
431

432
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
505,369!
433
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
504,613!
434
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
435
      goto _return;
×
436
    }
437

438
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
505,369✔
439
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
24,543✔
440
      if (code) {
24,678!
441
        goto _return;
×
442
      }
443
      pBind = &ncharBind;
24,678✔
444
    } else {
445
      pBind = bind + c;
480,826✔
446
    }
447

448
    int32_t bytes = 0;
505,504✔
449
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
505,504!
450
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
46,925!
451
        bytes = pColSchema->bytes - BLOBSTR_HEADER_SIZE;
×
452
      } else {
453
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
48,030✔
454
      }
455
    } else {
456
      bytes = -1;
458,579✔
457
    }
458
    code = tColDataAddValueByBind(pCol, pBind, bytes, initCtxAsText, checkWKB);
505,504✔
459
    if (code) {
509,155✔
460
      goto _return;
1✔
461
    }
462
  }
463

464
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
84,942!
465

466
_return:
84,942✔
467

468
  taosMemoryFree(ncharBind.buffer);
84,943!
469
  taosMemoryFree(ncharBind.length);
80,568!
470

471
  return code;
80,963✔
472
}
473

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

487
  for (int c = 0; c < numOfBound; ++c) {
29✔
488
    if (tSimpleHashGet(parsedCols, &c, sizeof(c))) {
24✔
489
      continue;
4✔
490
    }
491
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[actualIndex]];
20✔
492
    SColData* pCol = taosArrayGet(pCols, actualIndex);
20✔
493

494
    if (bind[c].num != rowNum) {
20!
495
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
496
      goto _return;
×
497
    }
498

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

505
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
20!
506
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
×
507
      if (code) {
×
508
        goto _return;
×
509
      }
510
      pBind = &ncharBind;
×
511
    } else {
512
      pBind = bind + c;
20✔
513
    }
514

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

532
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
5!
533

534
_return:
5✔
535

536
  taosMemoryFree(ncharBind.buffer);
5!
537
  taosMemoryFree(ncharBind.length);
5!
538

539
  return code;
5✔
540
}
541

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

554
  if (bind->num != rowNum) {
×
555
    return buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
556
  }
557

558
  // Column index exceeds the number of columns
559
  if (colIdx >= pCols->size && pCol == NULL) {
×
560
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
561
  }
562

563
  if (bind->buffer_type != pColSchema->type) {
×
564
    return buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
565
  }
566

567
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
×
568
    code = convertStmtNcharCol(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
×
569
    if (code) {
×
570
      goto _return;
×
571
    }
572
    pBind = &ncharBind;
×
573
  } else {
574
    pBind = bind;
×
575
  }
576
  
577
  code = tColDataAddValueByBind(pCol, pBind,
×
578
                                IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1,
×
579
                                initCtxAsText, checkWKB);
580

581
  parserDebug("stmt col %d bind %d rows data", colIdx, rowNum);
×
582

583
_return:
×
584

585
  taosMemoryFree(ncharBind.buffer);
×
586
  taosMemoryFree(ncharBind.length);
×
587

588
  return code;
×
589
}
590

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

602
  SArray* pTagArray;
603
  if (tags->parseredTags) {
193✔
604
    pTagArray = taosArrayDup(tags->parseredTags->pTagVals, NULL);
24✔
605
  } else {
606
    pTagArray = taosArrayInit(tags->numOfBound, sizeof(STagVal));
169✔
607
  }
608
  if (!pTagArray) {
193!
609
    return buildInvalidOperationMsg(&pBuf, "out of memory");
×
610
  }
611

612
  SArray* tagName;
613
  if (tags->parseredTags) {
193✔
614
    tagName = taosArrayDup(tags->parseredTags->STagNames, NULL);
24✔
615
  } else {
616
    tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
169✔
617
  }
618

619
  if (!tagName) {
193!
620
    code = buildInvalidOperationMsg(&pBuf, "out of memory");
×
621
    goto end;
×
622
  }
623

624
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
193✔
625

626
  bool  isJson = false;
193✔
627
  STag* pTag = NULL;
193✔
628

629
  int bindIdx = 0;
193✔
630
  for (int c = 0; c < tags->numOfBound; ++c) {
758✔
631
    if (bind == NULL) {
587✔
632
      break;
21✔
633
    }
634
    if (tags->parseredTags) {
566✔
635
      bool found = false;
264✔
636
      for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
720✔
637
        if (tags->parseredTags->pTagIndex[k] == tags->pColIndex[c]) {
504✔
638
          found = true;
48✔
639
          break;
48✔
640
        }
641
      }
642
      if (found) {
264✔
643
        continue;
48✔
644
      }
645
    }
646

647
    TAOS_STMT2_BIND bindData = bind[bindIdx++];
518✔
648

649
    if (bindData.is_null && bindData.is_null[0]) {
518!
650
      continue;
×
651
    }
652

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

679
      isJson = true;
×
680
      char* tmp = taosMemoryCalloc(1, colLen + 1);
×
681
      if (!tmp) {
×
682
        code = terrno;
×
683
        goto end;
×
684
      }
685
      memcpy(tmp, bindData.buffer, colLen);
×
686
      code = parseJsontoTagData(tmp, pTagArray, &pTag, &pBuf, charsetCxt);
×
687
      taosMemoryFree(tmp);
×
688
      if (code != TSDB_CODE_SUCCESS) {
×
689
        goto end;
×
690
      }
691
    } else {
692
      STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
518✔
693
      //      strcpy(val.colName, pTagSchema->name);
694
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
518!
695
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
381✔
696
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
168✔
697
          code = initCtxAsText();
31✔
698
          if (code) {
31!
699
            qError("geometry init failed:%s", tstrerror(code));
×
700
            goto end;
1✔
701
          }
702
          code = checkWKB(bindData.buffer, colLen);
31✔
703
          if (code) {
31✔
704
            qError("stmt2 bind invalid geometry tag:%s, must be WKB format", (char*)bindData.buffer);
1!
705
            goto end;
1✔
706
          }
707
        }
708
        val.pData = (uint8_t*)bindData.buffer;
167✔
709
        val.nData = colLen;
167✔
710
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
350✔
711
        int32_t output = 0;
40✔
712
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
40!
713
        if (p == NULL) {
40!
714
          code = terrno;
×
715
          goto end;
×
716
        }
717
        if (!taosMbsToUcs4(bindData.buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
40!
718
          if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
719
            taosMemoryFree(p);
×
720
            code = generateSyntaxErrMsg(&pBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pTagSchema->name);
×
721
            goto end;
×
722
          }
723
          char buf[512] = {0};
×
724
          snprintf(buf, tListLen(buf), " taosMbsToUcs4 error:%s", strerror(terrno));
×
725
          taosMemoryFree(p);
×
726
          code = buildSyntaxErrMsg(&pBuf, buf, bindData.buffer);
×
727
          goto end;
×
728
        }
729
        val.pData = p;
40✔
730
        val.nData = output;
40✔
731
      } else {
732
        memcpy(&val.i64, bindData.buffer, colLen);
310✔
733
      }
734
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
517!
735
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
736
        goto end;
×
737
      }
738
      if (NULL == taosArrayPush(pTagArray, &val)) {
517!
739
        code = terrno;
×
740
        goto end;
×
741
      }
742
    }
743
  }
744

745
  if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) {
192!
746
    goto end;
×
747
  }
748

749
  if (pCreateTbReq) {
192✔
750
    code = insBuildCreateTbReq(pCreateTbReq, tName, pTag, suid, sTableName, tagName,
85✔
751
                               pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
85✔
752
    pTag = NULL;
85✔
753
    goto end;
85✔
754
  }
755

756
  if (NULL == pDataBlock->pData->pCreateTbReq) {
107✔
757
    pDataBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
8!
758
    if (NULL == pDataBlock->pData->pCreateTbReq) {
8!
759
      code = terrno;
×
760
      goto end;
×
761
    }
762
  } else {
763
    SVCreateTbReq* tmp = pDataBlock->pData->pCreateTbReq;
99✔
764
    taosMemoryFreeClear(tmp->name);
99!
765
    taosMemoryFreeClear(tmp->ctb.pTag);
99!
766
    taosMemoryFreeClear(tmp->ctb.stbName);
99!
767
    taosArrayDestroy(tmp->ctb.tagName);
99✔
768
    tmp->ctb.tagName = NULL;
99✔
769
  }
770

771
  code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName,
107✔
772
                             pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
107✔
773
  pTag = NULL;
107✔
774

775
end:
193✔
776
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
758✔
777
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
565✔
778
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
565✔
779
      taosMemoryFreeClear(p->pData);
40!
780
    }
781
  }
782
  taosArrayDestroy(pTagArray);
193✔
783
  taosArrayDestroy(tagName);
193✔
784
  taosMemoryFree(pTag);
193!
785

786
  return code;
193✔
787
}
788

789
static int32_t convertStmtStbNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst,
27✔
790
                                       void* charsetCxt) {
791
  int32_t       output = 0;
27✔
792
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
27✔
793

794
  dst->buffer = taosMemoryCalloc(src->num, max_buf_len);
27!
795
  if (NULL == dst->buffer) {
27!
796
    return terrno;
×
797
  }
798

799
  dst->length = taosMemoryCalloc(src->num, sizeof(int32_t));
27!
800
  if (NULL == dst->length) {
27!
801
    taosMemoryFreeClear(dst->buffer);
×
802
    return terrno;
×
803
  }
804

805
  char* src_buf = src->buffer;
27✔
806
  char* dst_buf = dst->buffer;
27✔
807
  for (int32_t i = 0; i < src->num; ++i) {
3,062✔
808
    if (src->is_null && src->is_null[i]) {
3,004!
809
      continue;
×
810
    }
811

812
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
3,004!
813
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
814
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
815
      }
816
      char buf[512] = {0};
×
817
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
818
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
819
    }
820

821
    dst->length[i] = output;
3,035✔
822
    src_buf += src->length[i];
3,035✔
823
    dst_buf += output;
3,035✔
824
  }
825

826
  dst->buffer_type = src->buffer_type;
58✔
827
  dst->is_null = src->is_null;
58✔
828
  dst->num = src->num;
58✔
829

830
  return TSDB_CODE_SUCCESS;
58✔
831
}
832

833
int32_t qBindStmtStbColsValue2(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
7,493✔
834
                               int32_t msgBufLen, STSchema** pTSchema, SBindInfo2* pBindInfos, void* charsetCxt,
835
                               SBlobSet** ppBlob) {
836
  STableDataCxt*  pDataBlock = (STableDataCxt*)pBlock;
7,493✔
837
  SSchema*        pSchema = getTableColumnSchema(pDataBlock->pMeta);
7,493✔
838
  SBoundColInfo*  boundInfo = &pDataBlock->boundColsInfo;
7,493✔
839
  SMsgBuf         pBuf = {.buf = msgBuf, .len = msgBufLen};
7,493✔
840
  int32_t         rowNum = bind->num;
7,493✔
841
  SArray*         ncharBinds = NULL;
7,493✔
842
  TAOS_STMT2_BIND ncharBind = {0};
7,493✔
843
  int32_t         code = 0;
7,493✔
844
  int16_t         lastColId = -1;
7,493✔
845
  bool            colInOrder = true;
7,493✔
846
  int             ncharColNums = 0;
7,493✔
847
  int32_t         bindIdx = 0;
7,493✔
848
  int8_t          hasBlob = 0;
7,493✔
849
  int32_t         lino = 0;
7,493✔
850
  if (NULL == pTSchema || NULL == *pTSchema) {
7,493!
851
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
105✔
852
  }
853

854
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
118,294✔
855
    if (TSDB_DATA_TYPE_NCHAR == pSchema[boundInfo->pColIndex[c]].type) {
110,797✔
856
      ncharColNums++;
27✔
857
    }
858
  }
859
  if (ncharColNums > 0) {
7,497✔
860
    ncharBinds = taosArrayInit(ncharColNums, sizeof(ncharBind));
27✔
861
    if (!ncharBinds) {
27!
862
      code = terrno;
×
863
      goto _return;
×
864
    }
865
  }
866

867
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
117,391✔
868
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
109,893✔
869
    if (pColSchema->colId <= lastColId) {
109,893✔
870
      colInOrder = false;
7✔
871
    } else {
872
      lastColId = pColSchema->colId;
109,886✔
873
    }
874

875
    if (parsedCols) {
109,893✔
876
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
132✔
877
      if (pParsedVal) {
132✔
878
        pBindInfos[c].columnId = pColSchema->colId;
24✔
879
        pBindInfos[c].type = pColSchema->type;
24✔
880
        pBindInfos[c].bytes = pColSchema->bytes;
24✔
881
        continue;
24✔
882
      }
883
    }
884

885
    TAOS_STMT2_BIND bindData = bind[bindIdx];
109,869✔
886

887
    if (bindData.num != rowNum) {
109,869!
888
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
889
      goto _return;
×
890
    }
891

892
    if ((!(rowNum == 1 && bindData.is_null && *bindData.is_null)) &&
109,869!
893
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
109,878!
894
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
895
      goto _return;
×
896
    }
897

898
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
109,869✔
899
      code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
27✔
900
      if (code) {
27!
901
        goto _return;
×
902
      }
903
      if (!taosArrayPush(ncharBinds, &ncharBind)) {
27!
904
        code = terrno;
×
905
        goto _return;
×
906
      }
907
      pBindInfos[c].bind = taosArrayGetLast(ncharBinds);
27✔
908
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
109,842✔
909
      code = initCtxAsText();
13✔
910
      if (code) {
13!
911
        qError("geometry init failed:%s", tstrerror(code));
×
912
        goto _return;
×
913
      }
914
      uint8_t* buf = bindData.buffer;
13✔
915
      for (int j = 0; j < bindData.num; j++) {
29✔
916
        if (bindData.is_null && bindData.is_null[j]) {
16✔
917
          continue;
1✔
918
        }
919
        code = checkWKB(buf, bindData.length[j]);
15✔
920
        if (code) {
15!
921
          qError("stmt2 interlace mode geometry data[%d]:{%s},length:%d must be in WKB format", c, buf,
×
922
                 bindData.length[j]);
923
          goto _return;
×
924
        }
925
        buf += bindData.length[j];
15✔
926
      }
927
      pBindInfos[c].bind = bind + bindIdx;
13✔
928
    } else {
929
      if (IS_STR_DATA_BLOB(pColSchema->type)) hasBlob = 1;
109,829!
930
      pBindInfos[c].bind = bind + bindIdx;
109,829✔
931
    }
932

933
    pBindInfos[c].columnId = pColSchema->colId;
109,869✔
934
    pBindInfos[c].type = pColSchema->type;
109,869✔
935
    pBindInfos[c].bytes = pColSchema->bytes;
109,869✔
936

937
    bindIdx++;
109,869✔
938
  }
939

940
  if (hasBlob == 0) {
7,498!
941
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pCols,
7,501✔
942
                              &pDataBlock->ordered, &pDataBlock->duplicateTs);
943
  } else {
944
    code = tBlobSetCreate(1024, 1, ppBlob);
×
945
    TAOS_CHECK_GOTO(code, &lino, _return);
×
946

947
    code = tRowBuildFromBind2WithBlob(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols,
×
948
                                      &pDataBlock->ordered, &pDataBlock->duplicateTs, *ppBlob);
949
    TAOS_CHECK_GOTO(code, &lino, _return);
×
950
  }
951

952
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
7,496!
953

954
_return:
7,496✔
955
  if (ncharBinds) {
7,496✔
956
    for (int i = 0; i < TARRAY_SIZE(ncharBinds); ++i) {
54✔
957
      TAOS_STMT2_BIND* ncBind = TARRAY_DATA(ncharBinds);
27✔
958
      taosMemoryFree(ncBind[i].buffer);
27!
959
      taosMemoryFree(ncBind[i].length);
27!
960
    }
961
    taosArrayDestroy(ncharBinds);
27✔
962
  }
963
  if (code != 0) {
7,496✔
964
    parserDebug("stmt2 failed to bind at lino %d since %s", lino, tstrerror(code));
1!
965
  }
966

967
  return code;
7,496✔
968
}
969

970
static int32_t convertStmtNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst,
24✔
971
                                    void* charsetCxt) {
972
  int32_t       output = 0;
24✔
973
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
24✔
974

975
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
24✔
976
  // if (dst->buffer_length < newBuflen) {
977
  dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
24!
978
  if (NULL == dst->buffer) {
24!
979
    return terrno;
×
980
  }
981
  //}
982

983
  if (NULL == dst->length) {
24✔
984
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
17!
985
    if (NULL == dst->length) {
17!
986
      taosMemoryFreeClear(dst->buffer);
×
987
      return terrno;
×
988
    }
989
  }
990

991
  // dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
992
  char* src_buf = src->buffer;
24✔
993
  char* dst_buf = dst->buffer;
24✔
994
  for (int32_t i = 0; i < src->num; ++i) {
129✔
995
    if (src->is_null && src->is_null[i]) {
105!
996
      continue;
×
997
    }
998

999
    /*if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
1000
      (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output)) {*/
1001
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
105!
1002
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
1003
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
1004
      }
1005
      char buf[512] = {0};
×
1006
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
1007
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
1008
    }
1009

1010
    dst->length[i] = output;
105✔
1011
    src_buf += src->length[i];
105✔
1012
    dst_buf += output;
105✔
1013
  }
1014

1015
  dst->buffer_type = src->buffer_type;
24✔
1016
  dst->is_null = src->is_null;
24✔
1017
  dst->num = src->num;
24✔
1018

1019
  return TSDB_CODE_SUCCESS;
24✔
1020
}
1021

1022
int32_t qBindStmtColsValue2(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
128✔
1023
                            int32_t msgBufLen, void* charsetCxt) {
1024
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
128✔
1025
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
128✔
1026
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
128✔
1027
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
128✔
1028
  int32_t          rowNum = bind->num;
128✔
1029
  TAOS_STMT2_BIND  ncharBind = {0};
128✔
1030
  TAOS_STMT2_BIND* pBind = NULL;
128✔
1031
  int32_t          code = 0;
128✔
1032
  int32_t          lino = 0;
128✔
1033
  int32_t          bindIdx = 0;
128✔
1034

1035
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
503✔
1036
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
375✔
1037
    SColData* pCol = taosArrayGet(pCols, c);
375✔
1038
    if (pCol == NULL || pColSchema == NULL) {
375!
1039
      code = buildInvalidOperationMsg(&pBuf, "get column schema or column data failed");
×
1040
      goto _return;
×
1041
    }
1042

1043
    if (boundInfo->pColIndex[c] == 0) {
375✔
1044
      pCol->cflag |= COL_IS_KEY;
128✔
1045
    }
1046

1047
    if (parsedCols) {
375✔
1048
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
132✔
1049
      if (pParsedVal) {
132✔
1050
        for (int row = 0; row < rowNum; row++) {
48✔
1051
          code = tColDataAppendValue(pCol, pParsedVal);
24✔
1052
          if (code) {
24!
1053
            goto _return;
×
1054
          }
1055
        }
1056
        continue;
24✔
1057
      }
1058
    }
1059
    TAOS_STMT2_BIND bindData = bind[bindIdx];
351✔
1060
    if (bindData.num != rowNum) {
351!
1061
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1062
      goto _return;
×
1063
    }
1064

1065
    if ((!(rowNum == 1 && bindData.is_null && *bindData.is_null)) &&
351!
1066
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
351!
1067
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
1068
      goto _return;
×
1069
    }
1070

1071
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
351✔
1072
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
22✔
1073
      if (code) {
22!
1074
        goto _return;
×
1075
      }
1076
      pBind = &ncharBind;
22✔
1077
    } else {
1078
      pBind = bind + bindIdx;
329✔
1079
    }
1080
    int8_t  isBlob = 0;
351✔
1081
    int32_t bytes = -1;
351✔
1082
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
351!
1083
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
144!
1084
        isBlob = 1;
1✔
1085
        bytes = TSDB_MAX_BLOB_LEN;
1✔
1086
      } else {
1087
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
143✔
1088
      }
1089
    }
1090
    if (isBlob == 0) {
351✔
1091
      code = tColDataAddValueByBind2(pCol, pBind, bytes, initCtxAsText, checkWKB);
350✔
1092
    } else {
1093
      if (pDataBlock->pData->pBlobSet == NULL) {
1!
1094
        code = tBlobSetCreate(1024, 1, &pDataBlock->pData->pBlobSet);
1✔
1095
        TAOS_CHECK_GOTO(code, &lino, _return);
1!
1096
      }
1097
      code = tColDataAddValueByBind2WithBlob(pCol, pBind, bytes, pDataBlock->pData->pBlobSet);
1✔
1098
    }
1099

1100
    if (code) {
351!
1101
      goto _return;
×
1102
    }
1103
    bindIdx++;
351✔
1104
  }
1105

1106
  parserDebug("stmt2 all %d columns bind %d rows data as col format", boundInfo->numOfBound, rowNum);
128!
1107

1108
_return:
128✔
1109

1110
  taosMemoryFree(ncharBind.buffer);
128!
1111
  taosMemoryFree(ncharBind.length);
128!
1112
  if (code != 0) {
128!
1113
    parserDebug("stmt2 failed to bind col at lino %d since %s", lino, tstrerror(code));
×
1114
  }
1115

1116
  return code;
128✔
1117
}
1118

1119
int32_t qBindStmtSingleColValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen,
6✔
1120
                                 int32_t colIdx, int32_t rowNum, void* charsetCxt) {
1121
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
6✔
1122
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
6✔
1123
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
6✔
1124
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
6✔
1125
  SSchema*         pColSchema = &pSchema[boundInfo->pColIndex[colIdx]];
6✔
1126
  SColData*        pCol = taosArrayGet(pCols, colIdx);
6✔
1127
  TAOS_STMT2_BIND  ncharBind = {0};
6✔
1128
  TAOS_STMT2_BIND* pBind = NULL;
6✔
1129
  int32_t          code = 0;
6✔
1130
  int32_t          lino = 0;
6✔
1131

1132
  if (bind->num != rowNum) {
6!
1133
    return buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1134
  }
1135

1136
  // Column index exceeds the number of columns
1137
  if (colIdx >= pCols->size && pCol == NULL) {
6!
1138
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
1139
  }
1140

1141
  if (bind->buffer_type != pColSchema->type) {
6!
1142
    return buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
1143
  }
1144

1145
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
6!
1146
    code = convertStmtNcharCol2(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
×
1147
    if (code) {
×
1148
      goto _return;
×
1149
    }
1150
    pBind = &ncharBind;
×
1151
  } else {
1152
    pBind = bind;
6✔
1153
  }
1154

1155
  int32_t bytes = -1;
6✔
1156
  int8_t  hasBlob = 0;
6✔
1157
  if (IS_VAR_DATA_TYPE(pColSchema->type)) {
6!
1158
    if (IS_STR_DATA_BLOB(pColSchema->type)) {
3!
1159
      bytes = TSDB_MAX_BLOB_LEN;
×
1160
      hasBlob = 1;
×
1161
    } else {
1162
      bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
3✔
1163
    }
1164
  } else {
1165
    bytes = -1;
3✔
1166
  }
1167

1168
  if (hasBlob) {
6!
1169
    if (pDataBlock->pData->pBlobSet == NULL) {
×
1170
      code = tBlobSetCreate(1024, 1, &pDataBlock->pData->pBlobSet);
×
1171
      TAOS_CHECK_GOTO(code, &lino, _return);
×
1172
    }
1173
    code = tColDataAddValueByBind2WithBlob(pCol, pBind, bytes, pDataBlock->pData->pBlobSet);
×
1174
  } else {
1175
    code = tColDataAddValueByBind2(pCol, pBind, bytes, initCtxAsText, checkWKB);
6✔
1176
  }
1177

1178
  parserDebug("stmt col %d bind %d rows data", colIdx, rowNum);
6!
1179

1180
_return:
6✔
1181

1182
  taosMemoryFree(ncharBind.buffer);
6!
1183
  taosMemoryFree(ncharBind.length);
6!
1184
  if (code != 0) {
6!
1185
    parserDebug("stmt failed to parse at lino %d since %s", lino, tstrerror(code));
×
1186
  }
1187

1188
  return code;
6✔
1189
}
1190

1191
int32_t qBindStmt2RowValue(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
2✔
1192
                           int32_t msgBufLen, STSchema** pTSchema, SBindInfo2* pBindInfos, void* charsetCxt) {
1193
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
2✔
1194
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
2✔
1195
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
2✔
1196
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
2✔
1197
  int32_t          rowNum = bind->num;
2✔
1198
  TAOS_STMT2_BIND  ncharBind = {0};
2✔
1199
  TAOS_STMT2_BIND* pBind = NULL;
2✔
1200
  int32_t          code = 0;
2✔
1201
  int16_t          lastColId = -1;
2✔
1202
  bool             colInOrder = true;
2✔
1203
  int8_t           hasBlob = 0;
2✔
1204

1205
  if (NULL == pTSchema || NULL == *pTSchema) {
2!
1206
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
1✔
1207
  }
1208

1209
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
24✔
1210
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
22✔
1211
    if (pColSchema->colId <= lastColId) {
22!
1212
      colInOrder = false;
×
1213
    } else {
1214
      lastColId = pColSchema->colId;
22✔
1215
    }
1216

1217
    if (bind[c].num != rowNum) {
22!
1218
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1219
      goto _return;
×
1220
    }
1221

1222
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
22!
1223
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
22!
1224
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
1225
      goto _return;
×
1226
    }
1227

1228
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
22✔
1229
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
2✔
1230
      if (code) {
2!
1231
        goto _return;
×
1232
      }
1233
      pBindInfos[c].bind = &ncharBind;
2✔
1234
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
20✔
1235
      code = initCtxAsText();
2✔
1236
      if (code) {
2!
1237
        qError("geometry init failed:%s", tstrerror(code));
×
1238
        goto _return;
×
1239
      }
1240
      uint8_t* buf = bind[c].buffer;
2✔
1241
      for (int j = 0; j < bind[c].num; j++) {
4✔
1242
        if (bind[c].is_null && bind[c].is_null[j]) {
2!
1243
          continue;
×
1244
        }
1245
        code = checkWKB(buf, bind[c].length[j]);
2✔
1246
        if (code) {
2!
1247
          qError("stmt2 row bind geometry data[%d]:{%s},length:%d must be in WKB format", c, buf, bind[c].length[j]);
×
1248
          goto _return;
×
1249
        }
1250
        buf += bind[c].length[j];
2✔
1251
      }
1252
      pBindInfos[c].bind = bind + c;
2✔
1253
    } else {
1254
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
18!
1255
        hasBlob = 1;
×
1256
      }
1257
      pBindInfos[c].bind = bind + c;
18✔
1258
    }
1259

1260
    pBindInfos[c].columnId = pColSchema->colId;
22✔
1261
    pBindInfos[c].type = pColSchema->type;
22✔
1262
    pBindInfos[c].bytes = pColSchema->bytes;
22✔
1263

1264
    if (code) {
22!
1265
      goto _return;
×
1266
    }
1267
  }
1268

1269
  pDataBlock->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
2✔
1270
  if (pDataBlock->pData->pCreateTbReq != NULL) {
2!
1271
    pDataBlock->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
2✔
1272
  }
1273

1274
  if (hasBlob == 0) {
2!
1275
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pCols,
2✔
1276
                            &pDataBlock->ordered, &pDataBlock->duplicateTs);
1277
  } else {
1278
    code = TSDB_CODE_BLOB_NOT_SUPPORT;
×
1279
  }
1280
  qDebug("stmt2 all %d columns bind %d rows data as row format", boundInfo->numOfBound, rowNum);
2!
1281

1282
_return:
2✔
1283

1284
  taosMemoryFree(ncharBind.buffer);
2!
1285
  taosMemoryFree(ncharBind.length);
2!
1286

1287
  return code;
2✔
1288
}
1289

1290
int32_t buildBoundFields(int32_t numOfBound, int16_t* boundColumns, SSchema* pSchema, int32_t* fieldNum,
7✔
1291
                         TAOS_FIELD_E** fields, uint8_t timePrec) {
1292
  if (fields != NULL) {
7✔
1293
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
6!
1294
    if (NULL == *fields) {
6!
1295
      return terrno;
×
1296
    }
1297

1298
    SSchema* schema = &pSchema[boundColumns[0]];
6✔
1299
    if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
6✔
1300
      (*fields)[0].precision = timePrec;
5✔
1301
    }
1302

1303
    for (int32_t i = 0; i < numOfBound; ++i) {
28✔
1304
      schema = &pSchema[boundColumns[i]];
22✔
1305
      tstrncpy((*fields)[i].name, schema->name, 65);
22✔
1306
      (*fields)[i].type = schema->type;
22✔
1307
      (*fields)[i].bytes = calcTypeBytesFromSchemaBytes(schema->type, schema->bytes, true);
22✔
1308
    }
1309
  }
1310

1311
  *fieldNum = numOfBound;
7✔
1312

1313
  return TSDB_CODE_SUCCESS;
7✔
1314
}
1315

1316
int32_t buildStbBoundFields(SBoundColInfo boundColsInfo, SSchema* pSchema, int32_t* fieldNum, TAOS_FIELD_ALL** fields,
48✔
1317
                            STableMeta* pMeta, void* boundTags, SSHashObj* parsedCols, uint8_t tbNameFlag) {
1318
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
48✔
1319
  bool           hastag = (tags != NULL) && !(tbNameFlag & IS_FIXED_TAG);
48!
1320
  bool           hasPreBindTbname =
48✔
1321
      (tbNameFlag & IS_FIXED_VALUE) == 0 && ((tbNameFlag & USING_CLAUSE) != 0 || pMeta->tableType == TSDB_NORMAL_TABLE);
48!
1322
  int32_t numOfBound = boundColsInfo.numOfBound + (hasPreBindTbname ? 1 : 0);
48✔
1323
  if (hastag) {
48✔
1324
    numOfBound += tags->mixTagsCols ? 0 : tags->numOfBound;
30✔
1325
  }
1326

1327
  // Adjust the number of bound fields if there are parsed tags or parsed columns
1328
  if (tags->parseredTags) {
48✔
1329
    numOfBound -= tags->parseredTags->numOfTags;
2✔
1330
  }
1331
  if (parsedCols) {
48✔
1332
    numOfBound -= tSimpleHashGetSize(parsedCols);
4✔
1333
  }
1334

1335
  int32_t idx = 0;
48✔
1336
  if (fields != NULL) {
48!
1337
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_ALL));
48!
1338
    if (NULL == *fields) {
48!
1339
      return terrno;
×
1340
    }
1341

1342
    if (hasPreBindTbname) {
48✔
1343
      (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
30✔
1344
      tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
30✔
1345
      (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
30✔
1346
      (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
30✔
1347
      idx++;
30✔
1348
    }
1349

1350
    if (hastag && tags->numOfBound > 0 && !tags->mixTagsCols) {
48✔
1351
      SSchema* tagSchema = getTableTagSchema(pMeta);
13✔
1352

1353
      for (int32_t i = 0; i < tags->numOfBound; ++i) {
52✔
1354
        SSchema* schema = &tagSchema[tags->pColIndex[i]];
39✔
1355

1356
        if (tags->parseredTags && tags->parseredTags->numOfTags > 0) {
39!
1357
          int32_t tag_idx = schema->colId - 1 - pMeta->tableInfo.numOfColumns;
2✔
1358
          bool    found = false;
2✔
1359
          for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
3✔
1360
            if (tags->parseredTags->pTagIndex[k] == tag_idx) {
2✔
1361
              found = true;
1✔
1362
              break;
1✔
1363
            }
1364
          }
1365
          if (found) {
2✔
1366
            continue;
1✔
1367
          }
1368
        }
1369

1370
        (*fields)[idx].field_type = TAOS_FIELD_TAG;
38✔
1371

1372
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[i].name));
38✔
1373
        (*fields)[idx].type = schema->type;
38✔
1374
        (*fields)[idx].bytes = schema->bytes;
38✔
1375
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
38✔
1376
          (*fields)[idx].precision = pMeta->tableInfo.precision;
1✔
1377
        }
1378
        idx++;
38✔
1379
      }
1380
    }
1381

1382
    if (boundColsInfo.numOfBound > 0) {
48!
1383
      SSchema* schema = &pSchema[boundColsInfo.pColIndex[0]];
48✔
1384

1385
      for (int32_t i = 0; i < boundColsInfo.numOfBound; ++i) {
224✔
1386
        int16_t idxCol = boundColsInfo.pColIndex[i];
176✔
1387

1388
        if (idxCol == pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags) {
176✔
1389
          (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
11✔
1390
          tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
11✔
1391
          (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
11✔
1392
          (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
11✔
1393

1394
          idx++;
11✔
1395
          continue;
11✔
1396
        } else if (idxCol < pMeta->tableInfo.numOfColumns) {
165✔
1397
          if (parsedCols && tSimpleHashGet(parsedCols, &pSchema[idxCol].colId, sizeof(int16_t))) {
133✔
1398
            continue;
5✔
1399
          }
1400
          (*fields)[idx].field_type = TAOS_FIELD_COL;
128✔
1401
        } else {
1402
          if (tags->parseredTags && tags->parseredTags->numOfTags > 0) {
32!
1403
            int32_t tag_idx = idxCol - pMeta->tableInfo.numOfColumns;
2✔
1404
            bool    found = false;
2✔
1405
            for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
3✔
1406
              if (tags->parseredTags->pTagIndex[k] == tag_idx) {
2✔
1407
                found = true;
1✔
1408
                break;
1✔
1409
              }
1410
            }
1411
            if (found) {
2✔
1412
              continue;
1✔
1413
            }
1414
          }
1415
          (*fields)[idx].field_type = TAOS_FIELD_TAG;
31✔
1416
        }
1417

1418
        schema = &pSchema[idxCol];
159✔
1419
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[idx].name));
159✔
1420
        (*fields)[idx].type = schema->type;
159✔
1421
        (*fields)[idx].bytes = schema->bytes;
159✔
1422
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
159✔
1423
          (*fields)[idx].precision = pMeta->tableInfo.precision;
47✔
1424
        }
1425
        idx++;
159✔
1426
      }
1427
    }
1428
  }
1429

1430
  *fieldNum = numOfBound;
48✔
1431

1432
  return TSDB_CODE_SUCCESS;
48✔
1433
}
1434

1435
int32_t qBuildStmtTagFields(void* pBlock, void* boundTags, int32_t* fieldNum, TAOS_FIELD_E** fields) {
1✔
1436
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
1✔
1437
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
1✔
1438
  if (NULL == tags) {
1!
1439
    return TSDB_CODE_APP_ERROR;
×
1440
  }
1441

1442
  if (pDataBlock->pMeta->tableType != TSDB_SUPER_TABLE && pDataBlock->pMeta->tableType != TSDB_CHILD_TABLE) {
1!
1443
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1444
  }
1445

1446
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
1✔
1447
  if (tags->numOfBound <= 0) {
1!
1448
    *fieldNum = 0;
×
1449
    *fields = NULL;
×
1450

1451
    return TSDB_CODE_SUCCESS;
×
1452
  }
1453

1454
  CHECK_CODE(buildBoundFields(tags->numOfBound, tags->pColIndex, pSchema, fieldNum, fields, 0));
1!
1455

1456
  return TSDB_CODE_SUCCESS;
1✔
1457
}
1458

1459
int32_t qBuildStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields) {
6✔
1460
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
6✔
1461
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
6✔
1462
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
6!
1463
    *fieldNum = 0;
×
1464
    if (fields != NULL) {
×
1465
      *fields = NULL;
×
1466
    }
1467

1468
    return TSDB_CODE_SUCCESS;
×
1469
  }
1470

1471
  CHECK_CODE(buildBoundFields(pDataBlock->boundColsInfo.numOfBound, pDataBlock->boundColsInfo.pColIndex, pSchema,
6!
1472
                              fieldNum, fields, pDataBlock->pMeta->tableInfo.precision));
1473

1474
  return TSDB_CODE_SUCCESS;
6✔
1475
}
1476

1477
int32_t qBuildUpdateStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields, SSHashObj* parsedCols) {
5✔
1478
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
5✔
1479
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
5✔
1480
  int32_t        numOfBound = pDataBlock->boundColsInfo.numOfBound + tSimpleHashGetSize(parsedCols);
5✔
1481
  if (numOfBound <= 0) {
5!
1482
    *fieldNum = 0;
×
1483
    if (fields != NULL) {
×
1484
      *fields = NULL;
×
1485
    }
1486

1487
    return TSDB_CODE_SUCCESS;
×
1488
  }
1489

1490
  if (fields != NULL) {
5!
1491
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
5!
1492
    if (NULL == *fields) {
5!
1493
      return terrno;
×
1494
    }
1495

1496
    SSchema* schema = &pSchema[pDataBlock->boundColsInfo.pColIndex[0]];
5✔
1497
    if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
5!
1498
      (*fields)[0].precision = pDataBlock->pMeta->tableInfo.precision;
×
1499
    }
1500

1501
    int32_t actualIdx = 0;
5✔
1502
    for (int32_t i = 0; i < numOfBound; ++i) {
29✔
1503
      SSchema* schema;
1504
      int32_t* idx = (int32_t*)tSimpleHashGet(parsedCols, &i, sizeof(int32_t));
24✔
1505
      if (idx) {
24✔
1506
        schema = &pSchema[*idx];
4✔
1507
      } else {
1508
        schema = &pSchema[pDataBlock->boundColsInfo.pColIndex[actualIdx++]];
20✔
1509
      }
1510
      tstrncpy((*fields)[i].name, schema->name, 65);
24✔
1511
      (*fields)[i].type = schema->type;
24✔
1512
      (*fields)[i].bytes = calcTypeBytesFromSchemaBytes(schema->type, schema->bytes, true);
24✔
1513
    }
1514
  }
1515

1516
  *fieldNum = numOfBound;
5✔
1517

1518
  return TSDB_CODE_SUCCESS;
5✔
1519
}
1520

1521
int32_t qBuildStmtStbColFields(void* pBlock, void* boundTags, SSHashObj* parsedCols, uint8_t tbNameFlag,
48✔
1522
                               int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
1523
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
48✔
1524
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
48✔
1525
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
48!
1526
    *fieldNum = 0;
×
1527
    if (fields != NULL) {
×
1528
      *fields = NULL;
×
1529
    }
1530

1531
    return TSDB_CODE_SUCCESS;
×
1532
  }
1533

1534
  CHECK_CODE(buildStbBoundFields(pDataBlock->boundColsInfo, pSchema, fieldNum, fields, pDataBlock->pMeta, boundTags,
48!
1535
                                 parsedCols, tbNameFlag));
1536

1537
  return TSDB_CODE_SUCCESS;
48✔
1538
}
1539

1540
int32_t qResetStmtColumns(SArray* pCols, bool deepClear) {
×
1541
  int32_t colNum = taosArrayGetSize(pCols);
×
1542

1543
  for (int32_t i = 0; i < colNum; ++i) {
×
1544
    SColData* pCol = (SColData*)taosArrayGet(pCols, i);
×
1545
    if (pCol == NULL) {
×
1546
      parserError("qResetStmtColumns column:%d is NULL", i);
×
1547
      return terrno;
×
1548
    }
1549
    if (deepClear) {
×
1550
      tColDataDeepClear(pCol);
×
1551
    } else {
1552
      tColDataClear(pCol);
×
1553
    }
1554
  }
1555

1556
  return TSDB_CODE_SUCCESS;
×
1557
}
1558

1559
int32_t qResetStmtDataBlock(STableDataCxt* block, bool deepClear) {
21,136✔
1560
  int32_t        code = 0;
21,136✔
1561
  STableDataCxt* pBlock = (STableDataCxt*)block;
21,136✔
1562
  int32_t        colNum = taosArrayGetSize(pBlock->pData->aCol);
21,136✔
1563

1564
  int8_t flag = 0;
21,146✔
1565
  for (int32_t i = 0; i < colNum; ++i) {
89,648✔
1566
    flag = pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
68,511✔
1567
    if (pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
68,511✔
1568
      SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i);
68,510✔
1569
      if (pCol == NULL) {
68,504!
1570
        parserError("qResetStmtDataBlock column:%d is NULL", i);
×
1571
        return terrno;
×
1572
      }
1573
      if (deepClear) {
68,527✔
1574
        tColDataDeepClear(pCol);
32,128✔
1575

1576
      } else {
1577
        tColDataClear(pCol);
36,399✔
1578
      }
1579

1580
    } else {
1581
      pBlock->pData->aRowP = taosArrayInit(20, POINTER_BYTES);
1✔
1582
    }
1583
  }
1584

1585
  tBlobSetDestroy(pBlock->pData->pBlobSet);
21,137✔
1586
  pBlock->pData->pBlobSet = NULL;
21,128✔
1587
  return code;
21,128✔
1588
}
1589

1590
int32_t qCloneStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, bool reset) {
10,288✔
1591
  int32_t code = 0;
10,288✔
1592

1593
  *pDst = taosMemoryCalloc(1, sizeof(STableDataCxt));
10,288!
1594
  if (NULL == *pDst) {
10,302!
1595
    return terrno;
×
1596
  }
1597

1598
  STableDataCxt* pNewCxt = (STableDataCxt*)*pDst;
10,302✔
1599
  STableDataCxt* pCxt = (STableDataCxt*)pSrc;
10,302✔
1600
  pNewCxt->hasBlob = pSrc->hasBlob;
10,302✔
1601
  pNewCxt->pSchema = NULL;
10,302✔
1602
  pNewCxt->pValues = NULL;
10,302✔
1603

1604
  if (pCxt->pMeta) {
10,302!
1605
    void* pNewMeta = taosMemoryMalloc(TABLE_META_SIZE(pCxt->pMeta));
10,303!
1606
    if (NULL == pNewMeta) {
10,304!
1607
      insDestroyTableDataCxt(*pDst);
×
1608
      return terrno;
×
1609
    }
1610
    memcpy(pNewMeta, pCxt->pMeta, TABLE_META_SIZE(pCxt->pMeta));
10,304✔
1611
    pNewCxt->pMeta = pNewMeta;
10,304✔
1612
  }
1613

1614
  memcpy(&pNewCxt->boundColsInfo, &pCxt->boundColsInfo, sizeof(pCxt->boundColsInfo));
10,303✔
1615
  pNewCxt->boundColsInfo.pColIndex = NULL;
10,303✔
1616

1617
  if (pCxt->boundColsInfo.pColIndex) {
10,303!
1618
    void* pNewColIdx = taosMemoryMalloc(pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
10,315!
1619
    if (NULL == pNewColIdx) {
10,312!
1620
      insDestroyTableDataCxt(*pDst);
×
1621
      return terrno;
×
1622
    }
1623
    memcpy(pNewColIdx, pCxt->boundColsInfo.pColIndex,
10,312✔
1624
           pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
10,312✔
1625
    pNewCxt->boundColsInfo.pColIndex = pNewColIdx;
10,312✔
1626
  }
1627

1628
  if (pCxt->pData) {
10,300!
1629
    int8_t         flag = 1;
10,302✔
1630
    SSubmitTbData* pNewTb = (SSubmitTbData*)taosMemoryMalloc(sizeof(SSubmitTbData));
10,302!
1631
    if (NULL == pNewTb) {
10,301!
1632
      insDestroyTableDataCxt(*pDst);
×
1633
      return terrno;
×
1634
    }
1635

1636
    memcpy(pNewTb, pCxt->pData, sizeof(*pCxt->pData));
10,301✔
1637
    pNewTb->pCreateTbReq = NULL;
10,301✔
1638
    if (pNewTb->pBlobSet != NULL) {
10,301✔
1639
      flag = pNewTb->pBlobSet->type;
1✔
1640
    }
1641
    pNewTb->pBlobSet = NULL;
10,301✔
1642

1643
    pNewTb->aCol = taosArrayDup(pCxt->pData->aCol, NULL);
10,301✔
1644
    if (NULL == pNewTb->aCol) {
10,312!
1645
      insDestroyTableDataCxt(*pDst);
×
1646
      return terrno;
×
1647
    }
1648

1649
    pNewCxt->pData = pNewTb;
10,312✔
1650

1651
    if (reset) {
10,312✔
1652
      code = qResetStmtDataBlock(*pDst, true);
10,227✔
1653
    }
1654
  }
1655

1656
  return code;
10,301✔
1657
}
1658

1659
int32_t qRebuildStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, uint64_t uid, uint64_t suid, int32_t vgId,
85✔
1660
                              bool rebuildCreateTb) {
1661
  int32_t code = qCloneStmtDataBlock(pDst, pSrc, false);
85✔
1662
  if (code) {
85!
1663
    return code;
×
1664
  }
1665

1666
  STableDataCxt* pBlock = (STableDataCxt*)*pDst;
85✔
1667
  if (pBlock->pMeta) {
85!
1668
    pBlock->pMeta->uid = uid;
85✔
1669
    pBlock->pMeta->vgId = vgId;
85✔
1670
    pBlock->pMeta->suid = suid;
85✔
1671
  }
1672

1673
  pBlock->pData->suid = suid;
85✔
1674
  pBlock->pData->uid = uid;
85✔
1675

1676
  if (rebuildCreateTb && NULL == pBlock->pData->pCreateTbReq) {
85!
1677
    pBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
65!
1678
    if (NULL == pBlock->pData->pCreateTbReq) {
65!
1679
      return terrno;
×
1680
    }
1681
  }
1682

1683
  return TSDB_CODE_SUCCESS;
85✔
1684
}
1685

1686
STableMeta* qGetTableMetaInDataBlock(STableDataCxt* pDataBlock) { return ((STableDataCxt*)pDataBlock)->pMeta; }
21,898✔
1687

1688
void qDestroyStmtDataBlock(STableDataCxt* pBlock) {
32,415✔
1689
  if (pBlock == NULL) {
32,415✔
1690
    return;
10,998✔
1691
  }
1692

1693
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
21,417✔
1694
  insDestroyTableDataCxt(pDataBlock);
21,417✔
1695
}
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

© 2025 Coveralls, Inc