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

taosdata / TDengine / #4667

14 Aug 2025 01:04PM UTC coverage: 59.532% (-0.6%) from 60.112%
#4667

push

travis-ci

web-flow
fix(query): fix order by column check of union operator (#32524)

136437 of 292055 branches covered (46.72%)

Branch coverage included in aggregate %.

1 of 13 new or added lines in 1 file covered. (7.69%)

2683 existing lines in 164 files now uncovered.

206730 of 284385 relevant lines covered (72.69%)

4603587.36 hits per line

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

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

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

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

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

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

73
  return code;
10,852✔
74
}
75

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

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

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

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

96
  if (pStmt->freeArrayFunc) {
9,010!
97
    pStmt->freeArrayFunc(pVgDataBlocks);
9,014✔
98
  }
99
  return code;
9,009✔
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,844✔
124
  int32_t             code = TSDB_CODE_SUCCESS;
10,844✔
125
  SArray*             pVgDataBlocks = NULL;
10,844✔
126
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
10,844✔
127

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

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

137
  if (pStmt->freeArrayFunc) {
10,863✔
138
    pStmt->freeArrayFunc(pVgDataBlocks);
10,859✔
139
  }
140
  return code;
10,860✔
141
}
142

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

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

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

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

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

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

174
    SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
11✔
175
    int32_t  colLen = pTagSchema->bytes;
11✔
176
    if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
11!
177
      if (!bind[c].length) {
3!
178
        code = buildInvalidOperationMsg(&pBuf, "var tag length is null");
×
179
        goto end;
×
180
      }
181
      colLen = bind[c].length[0];
3✔
182
      if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
3!
183
        code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
×
184
        goto end;
×
185
      }
186
    }
187
    if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
22!
188
      code = terrno;
×
189
      goto end;
×
190
    }
191
    if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
11!
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};
11✔
211
      //      strcpy(val.colName, pTagSchema->name);
212
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
11!
213
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
10✔
214
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
2✔
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;
2✔
227
        val.nData = colLen;
2✔
228
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
9✔
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);
8✔
251
      }
252
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
11!
253
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
254
        goto end;
×
255
      }
256
      if (NULL == taosArrayPush(pTagArray, &val)) {
11!
257
        code = terrno;
×
258
        goto end;
×
259
      }
260
    }
261
  }
262

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

267
  if (NULL == pDataBlock->pData->pCreateTbReq) {
1!
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;
1✔
275
    taosMemoryFreeClear(tmp->name);
1!
276
    taosMemoryFreeClear(tmp->ctb.pTag);
1!
277
    taosMemoryFreeClear(tmp->ctb.stbName);
1!
278
    taosArrayDestroy(tmp->ctb.tagName);
1✔
279
    tmp->ctb.tagName = NULL;
1✔
280
  }
281

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

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

297
  return code;
1✔
298
}
299

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

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

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

321
  for (int32_t i = 0; i < src->num; ++i) {
64,807✔
322
    if (src->is_null && src->is_null[i]) {
39,973✔
323
      continue;
569✔
324
    }
325

326
    if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
39,549✔
327
                       (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output,
39,404✔
328
                       charsetCxt)) {
329
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
3!
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,546✔
338
  }
339

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

344
  return TSDB_CODE_SUCCESS;
24,834✔
345
}
346

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

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

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

373
    if (bind[c].num != rowNum) {
33,785!
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,785!
379
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
33,785!
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,785✔
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,617✔
392
    }
393

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

399
  code = tRowBuildFromBind(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols, &pDataBlock->ordered,
5,628✔
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,629!
408

409
  return code;
5,628✔
410
}
411

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

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

427
    if (bind[c].num != rowNum) {
504,604!
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)) &&
506,170!
433
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
505,533!
434
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
435
      goto _return;
×
436
    }
437

438
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
506,170✔
439
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
24,544✔
440
      if (code) {
24,569!
441
        goto _return;
×
442
      }
443
      pBind = &ncharBind;
24,569✔
444
    } else {
445
      pBind = bind + c;
481,626✔
446
    }
447

448
    int32_t bytes = 0;
506,195✔
449
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
506,195!
450
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
47,265!
451
        bytes = pColSchema->bytes - BLOBSTR_HEADER_SIZE;
×
452
      } else {
453
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
48,023✔
454
      }
455
    } else {
456
      bytes = -1;
458,930✔
457
    }
458
    code = tColDataAddValueByBind(pCol, pBind, bytes, initCtxAsText, checkWKB);
506,195✔
459
    if (code) {
508,446✔
460
      goto _return;
1✔
461
    }
462
  }
463

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

466
_return:
83,510✔
467

468
  taosMemoryFree(ncharBind.buffer);
83,511!
469
  taosMemoryFree(ncharBind.length);
80,042!
470

471
  return code;
80,495✔
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

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

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

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

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

UNCOV
624
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
×
625

UNCOV
626
  bool  isJson = false;
×
UNCOV
627
  STag* pTag = NULL;
×
628

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

UNCOV
647
    TAOS_STMT2_BIND bindData = bind[bindIdx++];
×
648

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

UNCOV
653
    SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
×
UNCOV
654
    int32_t  colLen = pTagSchema->bytes;
×
UNCOV
655
    if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
×
UNCOV
656
      if (IS_STR_DATA_BLOB(pTagSchema->type)) {
×
657
        return TSDB_CODE_BLOB_NOT_SUPPORT_TAG;
×
658
      }
UNCOV
659
      if (!bindData.length) {
×
660
        code = buildInvalidOperationMsg(&pBuf, "var tag length is null");
×
UNCOV
661
        goto end;
×
662
      }
UNCOV
663
      colLen = bindData.length[0];
×
UNCOV
664
      if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
×
665
        code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
×
666
        goto end;
×
667
      }
668
    }
UNCOV
669
    if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
×
670
      code = terrno;
×
671
      goto end;
×
672
    }
UNCOV
673
    if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
×
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 {
UNCOV
692
      STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
×
693
      //      strcpy(val.colName, pTagSchema->name);
UNCOV
694
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
×
UNCOV
695
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
×
UNCOV
696
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
×
UNCOV
697
          code = initCtxAsText();
×
UNCOV
698
          if (code) {
×
699
            qError("geometry init failed:%s", tstrerror(code));
×
UNCOV
700
            goto end;
×
701
          }
UNCOV
702
          code = checkWKB(bindData.buffer, colLen);
×
UNCOV
703
          if (code) {
×
UNCOV
704
            qError("stmt2 bind invalid geometry tag:%s, must be WKB format", (char*)bindData.buffer);
×
UNCOV
705
            goto end;
×
706
          }
707
        }
UNCOV
708
        val.pData = (uint8_t*)bindData.buffer;
×
UNCOV
709
        val.nData = colLen;
×
UNCOV
710
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
×
UNCOV
711
        int32_t output = 0;
×
UNCOV
712
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
×
UNCOV
713
        if (p == NULL) {
×
714
          code = terrno;
×
715
          goto end;
×
716
        }
UNCOV
717
        if (!taosMbsToUcs4(bindData.buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
×
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
        }
UNCOV
729
        val.pData = p;
×
UNCOV
730
        val.nData = output;
×
731
      } else {
UNCOV
732
        memcpy(&val.i64, bindData.buffer, colLen);
×
733
      }
UNCOV
734
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
×
735
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
736
        goto end;
×
737
      }
UNCOV
738
      if (NULL == taosArrayPush(pTagArray, &val)) {
×
739
        code = terrno;
×
740
        goto end;
×
741
      }
742
    }
743
  }
744

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

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

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

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

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

UNCOV
786
  return code;
×
787
}
788

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

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

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

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

812
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
2,907!
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;
2,908✔
822
    src_buf += src->length[i];
2,908✔
823
    dst_buf += output;
2,908✔
824
  }
825

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

830
  return TSDB_CODE_SUCCESS;
4✔
831
}
832

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

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

867
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
117,420✔
868
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
110,044✔
869
    if (pColSchema->colId <= lastColId) {
110,044!
UNCOV
870
      colInOrder = false;
×
871
    } else {
872
      lastColId = pColSchema->colId;
110,044✔
873
    }
874

875
    if (parsedCols) {
110,044!
UNCOV
876
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
×
UNCOV
877
      if (pParsedVal) {
×
UNCOV
878
        pBindInfos[c].columnId = pColSchema->colId;
×
UNCOV
879
        pBindInfos[c].type = pColSchema->type;
×
UNCOV
880
        pBindInfos[c].bytes = pColSchema->bytes;
×
UNCOV
881
        continue;
×
882
      }
883
    }
884

885
    TAOS_STMT2_BIND bindData = bind[bindIdx];
110,044✔
886

887
    if (bindData.num != rowNum) {
110,044!
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)) &&
110,044✔
893
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
110,038!
894
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
895
      goto _return;
×
896
    }
897

898
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
110,044✔
899
      code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
3✔
900
      if (code) {
3!
901
        goto _return;
×
902
      }
903
      if (!taosArrayPush(ncharBinds, &ncharBind)) {
3!
904
        code = terrno;
×
905
        goto _return;
×
906
      }
907
      pBindInfos[c].bind = taosArrayGetLast(ncharBinds);
3✔
908
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
110,041!
UNCOV
909
      code = initCtxAsText();
×
UNCOV
910
      if (code) {
×
911
        qError("geometry init failed:%s", tstrerror(code));
×
912
        goto _return;
×
913
      }
UNCOV
914
      uint8_t* buf = bindData.buffer;
×
UNCOV
915
      for (int j = 0; j < bindData.num; j++) {
×
UNCOV
916
        if (bindData.is_null && bindData.is_null[j]) {
×
UNCOV
917
          continue;
×
918
        }
UNCOV
919
        code = checkWKB(buf, bindData.length[j]);
×
UNCOV
920
        if (code) {
×
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
        }
UNCOV
925
        buf += bindData.length[j];
×
926
      }
UNCOV
927
      pBindInfos[c].bind = bind + bindIdx;
×
928
    } else {
929
      if (IS_STR_DATA_BLOB(pColSchema->type)) hasBlob = 1;
110,041!
930
      pBindInfos[c].bind = bind + bindIdx;
110,041✔
931
    }
932

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

937
    bindIdx++;
110,044✔
938
  }
939

940
  if (hasBlob == 0) {
7,376✔
941
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pCols,
7,375✔
942
                              &pDataBlock->ordered, &pDataBlock->duplicateTs);
943
  } else {
944
    code = tBlobSetCreate(1024, 1, ppBlob);
1✔
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,373!
953

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

967
  return code;
7,373✔
968
}
969

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

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

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

991
  // dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
UNCOV
992
  char* src_buf = src->buffer;
×
UNCOV
993
  char* dst_buf = dst->buffer;
×
UNCOV
994
  for (int32_t i = 0; i < src->num; ++i) {
×
UNCOV
995
    if (src->is_null && src->is_null[i]) {
×
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)) {*/
UNCOV
1001
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
×
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

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

UNCOV
1015
  dst->buffer_type = src->buffer_type;
×
UNCOV
1016
  dst->is_null = src->is_null;
×
UNCOV
1017
  dst->num = src->num;
×
1018

UNCOV
1019
  return TSDB_CODE_SUCCESS;
×
1020
}
1021

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

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

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

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

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

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

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

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

UNCOV
1108
_return:
×
1109

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

UNCOV
1116
  return code;
×
1117
}
1118

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

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

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

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

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

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

UNCOV
1168
  if (hasBlob) {
×
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 {
UNCOV
1175
    code = tColDataAddValueByBind2(pCol, pBind, bytes, initCtxAsText, checkWKB);
×
1176
  }
1177

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

UNCOV
1180
_return:
×
1181

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

UNCOV
1188
  return code;
×
1189
}
1190

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

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

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

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

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

UNCOV
1228
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
×
UNCOV
1229
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
×
UNCOV
1230
      if (code) {
×
1231
        goto _return;
×
1232
      }
UNCOV
1233
      pBindInfos[c].bind = &ncharBind;
×
UNCOV
1234
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
×
UNCOV
1235
      code = initCtxAsText();
×
UNCOV
1236
      if (code) {
×
1237
        qError("geometry init failed:%s", tstrerror(code));
×
1238
        goto _return;
×
1239
      }
UNCOV
1240
      uint8_t* buf = bind[c].buffer;
×
UNCOV
1241
      for (int j = 0; j < bind[c].num; j++) {
×
UNCOV
1242
        if (bind[c].is_null && bind[c].is_null[j]) {
×
1243
          continue;
×
1244
        }
UNCOV
1245
        code = checkWKB(buf, bind[c].length[j]);
×
UNCOV
1246
        if (code) {
×
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
        }
UNCOV
1250
        buf += bind[c].length[j];
×
1251
      }
UNCOV
1252
      pBindInfos[c].bind = bind + c;
×
1253
    } else {
UNCOV
1254
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
×
1255
        hasBlob = 1;
×
1256
      }
UNCOV
1257
      pBindInfos[c].bind = bind + c;
×
1258
    }
1259

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

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

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

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

UNCOV
1282
_return:
×
1283

UNCOV
1284
  taosMemoryFree(ncharBind.buffer);
×
UNCOV
1285
  taosMemoryFree(ncharBind.length);
×
1286

UNCOV
1287
  return code;
×
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

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

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

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

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

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

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

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

UNCOV
1370
        (*fields)[idx].field_type = TAOS_FIELD_TAG;
×
1371

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

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

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

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

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

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

UNCOV
1430
  *fieldNum = numOfBound;
×
1431

UNCOV
1432
  return TSDB_CODE_SUCCESS;
×
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

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

1531
    return TSDB_CODE_SUCCESS;
×
1532
  }
1533

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

UNCOV
1537
  return TSDB_CODE_SUCCESS;
×
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,010✔
1560
  int32_t        code = 0;
21,010✔
1561
  STableDataCxt* pBlock = (STableDataCxt*)block;
21,010✔
1562
  int32_t        colNum = taosArrayGetSize(pBlock->pData->aCol);
21,010✔
1563

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

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

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

1585
  tBlobSetDestroy(pBlock->pData->pBlobSet);
20,994✔
1586
  pBlock->pData->pBlobSet = NULL;
20,992✔
1587
  return code;
20,992✔
1588
}
1589

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

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

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

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

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

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

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

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

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

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

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

1656
  return code;
10,149✔
1657
}
1658

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

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

UNCOV
1673
  pBlock->pData->suid = suid;
×
UNCOV
1674
  pBlock->pData->uid = uid;
×
1675

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

UNCOV
1683
  return TSDB_CODE_SUCCESS;
×
1684
}
1685

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

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

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

© 2026 Coveralls, Inc