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

taosdata / TDengine / #4674

18 Aug 2025 07:58AM UTC coverage: 59.821% (+0.1%) from 59.715%
#4674

push

travis-ci

web-flow
test: update case desc (#32551)

136937 of 292075 branches covered (46.88%)

Branch coverage included in aggregate %.

207916 of 284395 relevant lines covered (73.11%)

4553289.94 hits per line

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

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

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

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

46
  int32_t code = cloneSVreateTbReq(pDataBlock->pData->pCreateTbReq, &pNew->pCreateTbReq);
10,850✔
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,855!
53
    code = terrno;
×
54
    taosMemoryFreeClear(*pData);
×
55
    return code;
×
56
  }
57

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

73
  return code;
10,844✔
74
}
75

76
int32_t qAppendStmtTableOutput(SQuery* pQuery, SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
5,602✔
77
                               SStbInterlaceInfo* pBuildInfo) {
78
  // merge according to vgId
79
  return insAppendStmtTableDataCxt(pAllVgHash, pTbData, pTbCtx, pBuildInfo);
5,602✔
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,000✔
89
  int32_t             code = TSDB_CODE_SUCCESS;
9,000✔
90
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
9,000✔
91

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

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

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

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

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

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

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

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

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

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

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

174
    SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
×
175
    int32_t  colLen = pTagSchema->bytes;
×
176
    if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
×
177
      if (!bind[c].length) {
×
178
        code = buildInvalidOperationMsg(&pBuf, "var tag length is null");
×
179
        goto end;
×
180
      }
181
      colLen = bind[c].length[0];
×
182
      if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
×
183
        code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
×
184
        goto end;
×
185
      }
186
    }
187
    if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
×
188
      code = terrno;
×
189
      goto end;
×
190
    }
191
    if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
×
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};
×
211
      //      strcpy(val.colName, pTagSchema->name);
212
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
×
213
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
×
214
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
×
215
          code = initCtxAsText();
×
216
          if (code) {
×
217
            qError("geometry init failed:%s", tstrerror(code));
×
218
            goto end;
×
219
          }
220
          code = checkWKB(bind[c].buffer, colLen);
×
221
          if (code) {
×
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;
×
227
        val.nData = colLen;
×
228
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
×
229
        int32_t output = 0;
×
230
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
×
231
        if (p == NULL) {
×
232
          code = terrno;
×
233
          goto end;
×
234
        }
235
        if (!taosMbsToUcs4(bind[c].buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
×
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;
×
248
        val.nData = output;
×
249
      } else {
250
        memcpy(&val.i64, bind[c].buffer, colLen);
×
251
      }
252
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
×
253
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
254
        goto end;
×
255
      }
256
      if (NULL == taosArrayPush(pTagArray, &val)) {
×
257
        code = terrno;
×
258
        goto end;
×
259
      }
260
    }
261
  }
262

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

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

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

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

297
  return code;
×
298
}
299

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

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

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

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

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

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

344
  return TSDB_CODE_SUCCESS;
24,841✔
345
}
346

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

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

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

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

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

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

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

404
_return:
5,599✔
405

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

409
  return code;
5,598✔
410
}
411

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

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

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

438
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
502,251✔
439
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
24,564✔
440
      if (code) {
24,606!
441
        goto _return;
×
442
      }
443
      pBind = &ncharBind;
24,606✔
444
    } else {
445
      pBind = bind + c;
477,687✔
446
    }
447

448
    int32_t bytes = 0;
502,293✔
449
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
502,293!
450
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
47,398!
451
        bytes = pColSchema->bytes - BLOBSTR_HEADER_SIZE;
×
452
      } else {
453
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
48,025✔
454
      }
455
    } else {
456
      bytes = -1;
454,895✔
457
    }
458
    code = tColDataAddValueByBind(pCol, pBind, bytes, initCtxAsText, checkWKB);
502,293✔
459
    if (code) {
505,355!
460
      goto _return;
×
461
    }
462
  }
463

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

466
_return:
84,279✔
467

468
  taosMemoryFree(ncharBind.buffer);
84,279!
469
  taosMemoryFree(ncharBind.length);
80,185!
470

471
  return code;
80,656✔
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,
×
592
                            TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen, void* charsetCxt,
593
                            SVCreateTbReq* pCreateTbReq) {
594
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
595
  SMsgBuf        pBuf = {.buf = msgBuf, .len = msgBufLen};
×
596
  int32_t        code = TSDB_CODE_SUCCESS;
×
597
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
×
598
  if (NULL == tags) {
×
599
    return buildInvalidOperationMsg(&pBuf, "tags is null");
×
600
  }
601

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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,997✔
808
    if (src->is_null && src->is_null[i]) {
2,993!
809
      continue;
×
810
    }
811

812
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
2,993!
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,994✔
822
    src_buf += src->length[i];
2,994✔
823
    dst_buf += output;
2,994✔
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,375✔
834
                               int32_t msgBufLen, STSchema** pTSchema, SBindInfo2* pBindInfos, void* charsetCxt,
835
                               SBlobSet** ppBlob) {
836
  STableDataCxt*  pDataBlock = (STableDataCxt*)pBlock;
7,375✔
837
  SSchema*        pSchema = getTableColumnSchema(pDataBlock->pMeta);
7,375✔
838
  SBoundColInfo*  boundInfo = &pDataBlock->boundColsInfo;
7,375✔
839
  SMsgBuf         pBuf = {.buf = msgBuf, .len = msgBufLen};
7,375✔
840
  int32_t         rowNum = bind->num;
7,375✔
841
  SArray*         ncharBinds = NULL;
7,375✔
842
  TAOS_STMT2_BIND ncharBind = {0};
7,375✔
843
  int32_t         code = 0;
7,375✔
844
  int16_t         lastColId = -1;
7,375✔
845
  bool            colInOrder = true;
7,375✔
846
  int             ncharColNums = 0;
7,375✔
847
  int32_t         bindIdx = 0;
7,375✔
848
  int8_t          hasBlob = 0;
7,375✔
849
  int32_t         lino = 0;
7,375✔
850
  if (NULL == pTSchema || NULL == *pTSchema) {
7,375!
851
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
72✔
852
  }
853

854
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
118,200✔
855
    if (TSDB_DATA_TYPE_NCHAR == pSchema[boundInfo->pColIndex[c]].type) {
110,826✔
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,577✔
868
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
110,199✔
869
    if (pColSchema->colId <= lastColId) {
110,199!
870
      colInOrder = false;
×
871
    } else {
872
      lastColId = pColSchema->colId;
110,199✔
873
    }
874

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

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

887
    if (bindData.num != rowNum) {
110,199!
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,199!
893
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
110,200!
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,199✔
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,196!
909
      code = initCtxAsText();
×
910
      if (code) {
×
911
        qError("geometry init failed:%s", tstrerror(code));
×
912
        goto _return;
×
913
      }
914
      uint8_t* buf = bindData.buffer;
×
915
      for (int j = 0; j < bindData.num; j++) {
×
916
        if (bindData.is_null && bindData.is_null[j]) {
×
917
          continue;
×
918
        }
919
        code = checkWKB(buf, bindData.length[j]);
×
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
        }
925
        buf += bindData.length[j];
×
926
      }
927
      pBindInfos[c].bind = bind + bindIdx;
×
928
    } else {
929
      if (IS_STR_DATA_BLOB(pColSchema->type)) hasBlob = 1;
110,196!
930
      pBindInfos[c].bind = bind + bindIdx;
110,196✔
931
    }
932

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

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

940
  if (hasBlob == 0) {
7,378✔
941
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pCols,
7,377✔
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,376!
953

954
_return:
7,376✔
955
  if (ncharBinds) {
7,376✔
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,376!
964
    parserDebug("stmt2 failed to bind at lino %d since %s", lino, tstrerror(code));
×
965
  }
966

967
  return code;
7,377✔
968
}
969

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

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

983
  if (NULL == dst->length) {
×
984
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
×
985
    if (NULL == dst->length) {
×
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;
×
993
  char* dst_buf = dst->buffer;
×
994
  for (int32_t i = 0; i < src->num; ++i) {
×
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)) {*/
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

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

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

1019
  return TSDB_CODE_SUCCESS;
×
1020
}
1021

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

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

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

1047
    if (parsedCols) {
×
1048
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
×
1049
      if (pParsedVal) {
×
1050
        for (int row = 0; row < rowNum; row++) {
×
1051
          code = tColDataAppendValue(pCol, pParsedVal);
×
1052
          if (code) {
×
1053
            goto _return;
×
1054
          }
1055
        }
1056
        continue;
×
1057
      }
1058
    }
1059
    TAOS_STMT2_BIND bindData = bind[bindIdx];
×
1060
    if (bindData.num != rowNum) {
×
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)) &&
×
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

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

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

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

1108
_return:
×
1109

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

1116
  return code;
×
1117
}
1118

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) {
1121
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
×
1122
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
1123
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
×
1124
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
×
1125
  SSchema*         pColSchema = &pSchema[boundInfo->pColIndex[colIdx]];
×
1126
  SColData*        pCol = taosArrayGet(pCols, colIdx);
×
1127
  TAOS_STMT2_BIND  ncharBind = {0};
×
1128
  TAOS_STMT2_BIND* pBind = NULL;
×
1129
  int32_t          code = 0;
×
1130
  int32_t          lino = 0;
×
1131

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
1137
  if (colIdx >= pCols->size && pCol == NULL) {
×
1138
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
1139
  }
1140

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

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 {
1152
    pBind = bind;
×
1153
  }
1154

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

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

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

1180
_return:
×
1181

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

1188
  return code;
×
1189
}
1190

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) {
1193
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
×
1194
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
1195
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
×
1196
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
×
1197
  int32_t          rowNum = bind->num;
×
1198
  TAOS_STMT2_BIND  ncharBind = {0};
×
1199
  TAOS_STMT2_BIND* pBind = NULL;
×
1200
  int32_t          code = 0;
×
1201
  int16_t          lastColId = -1;
×
1202
  bool             colInOrder = true;
×
1203
  int8_t           hasBlob = 0;
×
1204

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

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

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

1222
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
×
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

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

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

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

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

1274
  if (hasBlob == 0) {
×
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
  }
1280
  qDebug("stmt2 all %d columns bind %d rows data as row format", boundInfo->numOfBound, rowNum);
×
1281

1282
_return:
×
1283

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

1287
  return code;
×
1288
}
1289

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

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

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

1311
  *fieldNum = numOfBound;
×
1312

1313
  return TSDB_CODE_SUCCESS;
×
1314
}
1315

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) {
1318
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
×
1319
  bool           hastag = (tags != NULL) && !(tbNameFlag & IS_FIXED_TAG);
×
1320
  bool           hasPreBindTbname =
×
1321
      (tbNameFlag & IS_FIXED_VALUE) == 0 && ((tbNameFlag & USING_CLAUSE) != 0 || pMeta->tableType == TSDB_NORMAL_TABLE);
×
1322
  int32_t numOfBound = boundColsInfo.numOfBound + (hasPreBindTbname ? 1 : 0);
×
1323
  if (hastag) {
×
1324
    numOfBound += tags->mixTagsCols ? 0 : tags->numOfBound;
×
1325
  }
1326

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

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

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

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

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

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

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

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

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

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

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

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

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

1430
  *fieldNum = numOfBound;
×
1431

1432
  return TSDB_CODE_SUCCESS;
×
1433
}
1434

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

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

1446
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
×
1447
  if (tags->numOfBound <= 0) {
×
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));
×
1455

1456
  return TSDB_CODE_SUCCESS;
×
1457
}
1458

1459
int32_t qBuildStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1460
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
1461
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
1462
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
×
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,
×
1472
                              fieldNum, fields, pDataBlock->pMeta->tableInfo.precision));
1473

1474
  return TSDB_CODE_SUCCESS;
×
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,
×
1522
                               int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
1523
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
1524
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1656
  return code;
10,147✔
1657
}
1658

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

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

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

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

1683
  return TSDB_CODE_SUCCESS;
×
1684
}
1685

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

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

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