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

taosdata / TDengine / #3904

24 Apr 2025 11:36AM UTC coverage: 55.345% (+0.04%) from 55.307%
#3904

push

travis-ci

happyguoxy
Sync branches at 2025-04-24 19:35

175143 of 316459 relevant lines covered (55.34%)

1209922.72 hits per line

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

75.29
/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 "tglobal.h"
23
#include "ttime.h"
24
#include "ttypes.h"
25

26
typedef struct SKvParam {
27
  int16_t  pos;
28
  SArray*  pTagVals;
29
  SSchema* schema;
30
  char     buf[TSDB_MAX_TAGS_LEN];
31
} SKvParam;
32

33
int32_t qCloneCurrentTbData(STableDataCxt* pDataBlock, SSubmitTbData** pData) {
238✔
34
  *pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
238✔
35
  if (NULL == *pData) {
238✔
36
    return terrno;
×
37
  }
38

39
  SSubmitTbData* pNew = *pData;
238✔
40

41
  *pNew = *pDataBlock->pData;
238✔
42

43
  int32_t code = cloneSVreateTbReq(pDataBlock->pData->pCreateTbReq, &pNew->pCreateTbReq);
238✔
44
  if (TSDB_CODE_SUCCESS != code) {
238✔
45
    taosMemoryFreeClear(*pData);
×
46
    return code;
×
47
  }
48
  pNew->aCol = taosArrayDup(pDataBlock->pData->aCol, NULL);
238✔
49
  if (!pNew->aCol) {
237✔
50
    code = terrno;
×
51
    taosMemoryFreeClear(*pData);
×
52
    return code;
×
53
  }
54

55
  int32_t colNum = taosArrayGetSize(pNew->aCol);
237✔
56
  for (int32_t i = 0; i < colNum; ++i) {
1,955✔
57
    if (pDataBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,718✔
58
      SColData* pCol = (SColData*)taosArrayGet(pNew->aCol, i);
1,716✔
59
      tColDataDeepClear(pCol);
1,716✔
60
    } else {
61
      pNew->aCol = taosArrayInit(20, POINTER_BYTES);
2✔
62
    }
63
  }
64

65
  return TSDB_CODE_SUCCESS;
237✔
66
}
67

68
int32_t qAppendStmtTableOutput(SQuery* pQuery, SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
46,174✔
69
                               SStbInterlaceInfo* pBuildInfo, SVCreateTbReq* ctbReq) {
70
  // merge according to vgId
71
  return insAppendStmtTableDataCxt(pAllVgHash, pTbData, pTbCtx, pBuildInfo, ctbReq);
46,174✔
72
}
73

74
int32_t qBuildStmtFinOutput(SQuery* pQuery, SHashObj* pAllVgHash, SArray* pVgDataBlocks) {
13,703✔
75
  int32_t             code = TSDB_CODE_SUCCESS;
13,703✔
76
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
13,703✔
77

78
  if (TSDB_CODE_SUCCESS == code) {
13,703✔
79
    code = insBuildVgDataBlocks(pAllVgHash, pVgDataBlocks, &pStmt->pDataBlocks, true);
13,704✔
80
  }
81

82
  if (pStmt->freeArrayFunc) {
13,704✔
83
    pStmt->freeArrayFunc(pVgDataBlocks);
13,705✔
84
  }
85
  return code;
13,705✔
86
}
87

88
/*
89
int32_t qBuildStmtOutputFromTbList(SQuery* pQuery, SHashObj* pVgHash, SArray* pBlockList, STableDataCxt* pTbCtx, int32_t
90
tbNum) { int32_t             code = TSDB_CODE_SUCCESS; SArray*             pVgDataBlocks = NULL; SVnodeModifyOpStmt*
91
pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
92

93
  // merge according to vgId
94
  if (tbNum > 0) {
95
    code = insMergeStmtTableDataCxt(pTbCtx, pBlockList, &pVgDataBlocks, true, tbNum);
96
  }
97

98
  if (TSDB_CODE_SUCCESS == code) {
99
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks);
100
  }
101

102
  if (pStmt->freeArrayFunc) {
103
    pStmt->freeArrayFunc(pVgDataBlocks);
104
  }
105
  return code;
106
}
107
*/
108

109
int32_t qBuildStmtOutput(SQuery* pQuery, SHashObj* pVgHash, SHashObj* pBlockHash) {
237✔
110
  int32_t             code = TSDB_CODE_SUCCESS;
237✔
111
  SArray*             pVgDataBlocks = NULL;
237✔
112
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
237✔
113

114
  // merge according to vgId
115
  if (taosHashGetSize(pBlockHash) > 0) {
237✔
116
    code = insMergeTableDataCxt(pBlockHash, &pVgDataBlocks, true);
238✔
117
  }
118

119
  if (TSDB_CODE_SUCCESS == code) {
237✔
120
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks, false);
237✔
121
  }
122

123
  if (pStmt->freeArrayFunc) {
238✔
124
    pStmt->freeArrayFunc(pVgDataBlocks);
238✔
125
  }
126
  return code;
238✔
127
}
128

129
int32_t qBindStmtTagsValue(void* pBlock, void* boundTags, int64_t suid, const char* sTableName, char* tName,
89✔
130
                           TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen, void* charsetCxt) {
131
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
89✔
132
  SMsgBuf        pBuf = {.buf = msgBuf, .len = msgBufLen};
89✔
133
  int32_t        code = TSDB_CODE_SUCCESS;
89✔
134
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
89✔
135
  if (NULL == tags) {
89✔
136
    return TSDB_CODE_APP_ERROR;
×
137
  }
138

139
  SArray* pTagArray = taosArrayInit(tags->numOfBound, sizeof(STagVal));
89✔
140
  if (!pTagArray) {
89✔
141
    return buildInvalidOperationMsg(&pBuf, "out of memory");
×
142
  }
143

144
  SArray* tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
89✔
145
  if (!tagName) {
89✔
146
    code = buildInvalidOperationMsg(&pBuf, "out of memory");
×
147
    goto end;
×
148
  }
149

150
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
89✔
151

152
  bool  isJson = false;
89✔
153
  STag* pTag = NULL;
89✔
154

155
  for (int c = 0; c < tags->numOfBound; ++c) {
316✔
156
    if (bind[c].is_null && bind[c].is_null[0]) {
227✔
157
      continue;
×
158
    }
159

160
    SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
227✔
161
    int32_t  colLen = pTagSchema->bytes;
227✔
162
    if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
227✔
163
      if (!bind[c].length) {
121✔
164
        code = buildInvalidOperationMsg(&pBuf, "var tag length is null");
×
165
        goto end;
×
166
      }
167
      colLen = bind[c].length[0];
121✔
168
      if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
121✔
169
        code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
×
170
        goto end;
×
171
      }
172
    }
173
    if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
454✔
174
      code = terrno;
×
175
      goto end;
×
176
    }
177
    if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
227✔
178
      if (colLen > (TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
10✔
179
        code = buildSyntaxErrMsg(&pBuf, "json string too long than 4095", bind[c].buffer);
×
180
        goto end;
×
181
      }
182

183
      isJson = true;
10✔
184
      char* tmp = taosMemoryCalloc(1, colLen + 1);
10✔
185
      if (!tmp) {
10✔
186
        code = terrno;
×
187
        goto end;
×
188
      }
189
      memcpy(tmp, bind[c].buffer, colLen);
10✔
190
      code = parseJsontoTagData(tmp, pTagArray, &pTag, &pBuf, charsetCxt);
10✔
191
      taosMemoryFree(tmp);
10✔
192
      if (code != TSDB_CODE_SUCCESS) {
10✔
193
        goto end;
×
194
      }
195
    } else {
196
      STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
217✔
197
      //      strcpy(val.colName, pTagSchema->name);
198
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
217✔
199
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
128✔
200
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
100✔
201
          code = initCtxAsText();
11✔
202
          if (code) {
11✔
203
            qError("geometry init failed:%s", tstrerror(code));
×
204
            goto end;
×
205
          }
206
          code = checkWKB(bind[c].buffer, colLen);
11✔
207
          if (code) {
11✔
208
            qError("stmt bind invalid geometry tag:%s, must be WKB format", (char*)bind[c].buffer);
×
209
            goto end;
×
210
          }
211
        }
212
        val.pData = (uint8_t*)bind[c].buffer;
100✔
213
        val.nData = colLen;
100✔
214
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
117✔
215
        int32_t output = 0;
11✔
216
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
11✔
217
        if (p == NULL) {
11✔
218
          code = terrno;
×
219
          goto end;
×
220
        }
221
        if (!taosMbsToUcs4(bind[c].buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
11✔
222
          if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
223
            taosMemoryFree(p);
×
224
            code = generateSyntaxErrMsg(&pBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pTagSchema->name);
×
225
            goto end;
×
226
          }
227
          char buf[512] = {0};
×
228
          snprintf(buf, tListLen(buf), " taosMbsToUcs4 error:%s", strerror(terrno));
×
229
          taosMemoryFree(p);
×
230
          code = buildSyntaxErrMsg(&pBuf, buf, bind[c].buffer);
×
231
          goto end;
×
232
        }
233
        val.pData = p;
11✔
234
        val.nData = output;
11✔
235
      } else {
236
        memcpy(&val.i64, bind[c].buffer, colLen);
106✔
237
      }
238
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
217✔
239
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
240
        goto end;
×
241
      }
242
      if (NULL == taosArrayPush(pTagArray, &val)) {
217✔
243
        code = terrno;
×
244
        goto end;
×
245
      }
246
    }
247
  }
248

249
  if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) {
89✔
250
    goto end;
×
251
  }
252

253
  if (NULL == pDataBlock->pData->pCreateTbReq) {
89✔
254
    pDataBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
12✔
255
    if (NULL == pDataBlock->pData->pCreateTbReq) {
12✔
256
      code = terrno;
×
257
      goto end;
×
258
    }
259
  } else {
260
    SVCreateTbReq* tmp = pDataBlock->pData->pCreateTbReq;
77✔
261
    taosMemoryFreeClear(tmp->name);
77✔
262
    taosMemoryFreeClear(tmp->ctb.pTag);
77✔
263
    taosMemoryFreeClear(tmp->ctb.stbName);
77✔
264
    taosArrayDestroy(tmp->ctb.tagName);
77✔
265
    tmp->ctb.tagName = NULL;
77✔
266
  }
267

268
  code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName,
89✔
269
                             pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
89✔
270
  pTag = NULL;
89✔
271

272
end:
89✔
273
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
316✔
274
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
227✔
275
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
227✔
276
      taosMemoryFreeClear(p->pData);
21✔
277
    }
278
  }
279
  taosArrayDestroy(pTagArray);
89✔
280
  taosArrayDestroy(tagName);
89✔
281
  taosMemoryFree(pTag);
89✔
282

283
  return code;
89✔
284
}
285

286
int32_t convertStmtNcharCol(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_MULTI_BIND* src, TAOS_MULTI_BIND* dst, void* charsetCxt) {
960,898✔
287
  int32_t output = 0;
960,898✔
288
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
960,898✔
289
  if (dst->buffer_length < newBuflen) {
960,898✔
290
    dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
977,611✔
291
    if (NULL == dst->buffer) {
991,220✔
292
      return terrno;
×
293
    }
294
  }
295

296
  if (NULL == dst->length) {
974,507✔
297
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
991,370✔
298
    if (NULL == dst->length) {
1,016,550✔
299
      taosMemoryFreeClear(dst->buffer);
×
300
      return terrno;
×
301
    }
302
  }
303

304
  dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
999,687✔
305

306
  for (int32_t i = 0; i < src->num; ++i) {
2,000,281✔
307
    if (src->is_null && src->is_null[i]) {
983,645✔
308
      continue;
50,551✔
309
    }
310

311
    if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
951,261✔
312
                       (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output, charsetCxt)) {
933,094✔
313
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
1,218✔
314
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
315
      }
316
      char buf[512] = {0};
×
317
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
318
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
319
    }
320

321
    dst->length[i] = output;
950,043✔
322
  }
323

324
  dst->buffer_type = src->buffer_type;
1,016,636✔
325
  dst->is_null = src->is_null;
1,016,636✔
326
  dst->num = src->num;
1,016,636✔
327

328
  return TSDB_CODE_SUCCESS;
1,016,636✔
329
}
330

331
int32_t qBindStmtStbColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
45,561✔
332
                              STSchema** pTSchema, SBindInfo* pBindInfos, void* charsetCxt) {
333
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
45,561✔
334
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
45,561✔
335
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
45,566✔
336
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
45,566✔
337
  int32_t          rowNum = bind->num;
45,566✔
338
  TAOS_MULTI_BIND  ncharBind = {0};
45,566✔
339
  TAOS_MULTI_BIND* pBind = NULL;
45,566✔
340
  int32_t          code = 0;
45,566✔
341
  int16_t          lastColId = -1;
45,566✔
342
  bool             colInOrder = true;
45,566✔
343

344
  if (NULL == pTSchema || NULL == *pTSchema) {
45,566✔
345
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
32✔
346
  }
347

348
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
225,971✔
349
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
180,399✔
350
    if (pColSchema->colId <= lastColId) {
180,399✔
351
      colInOrder = false;
×
352
    } else {
353
      lastColId = pColSchema->colId;
180,399✔
354
    }
355
    // SColData* pCol = taosArrayGet(pCols, c);
356

357
    if (bind[c].num != rowNum) {
180,399✔
358
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
359
      goto _return;
×
360
    }
361

362
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
180,399✔
363
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
180,392✔
364
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
365
      goto _return;
×
366
    }
367

368
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
180,399✔
369
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
×
370
      if (code) {
×
371
        goto _return;
×
372
      }
373
      pBind = &ncharBind;
×
374
    } else {
375
      pBind = bind + c;
180,399✔
376
    }
377

378
    pBindInfos[c].columnId = pColSchema->colId;
180,399✔
379
    pBindInfos[c].bind = pBind;
180,399✔
380
    pBindInfos[c].type = pColSchema->type;
180,399✔
381

382
    // code = tColDataAddValueByBind(pCol, pBind, IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes -
383
    // VARSTR_HEADER_SIZE: -1); if (code) {
384
    //   goto _return;
385
    // }
386
  }
387

388
  code = tRowBuildFromBind(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols, &pDataBlock->ordered, &pDataBlock->duplicateTs);
45,572✔
389

390
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
45,575✔
391

392
_return:
45,575✔
393

394
  taosMemoryFree(ncharBind.buffer);
45,575✔
395
  taosMemoryFree(ncharBind.length);
45,582✔
396

397
  return code;
45,582✔
398
}
399

400
int32_t qBindStmtColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen, void* charsetCxt) {
969,367✔
401
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
969,367✔
402
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
969,367✔
403
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
962,046✔
404
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
962,046✔
405
  int32_t          rowNum = bind->num;
962,046✔
406
  TAOS_MULTI_BIND  ncharBind = {0};
962,046✔
407
  TAOS_MULTI_BIND* pBind = NULL;
962,046✔
408
  int32_t          code = 0;
962,046✔
409

410
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
13,219,578✔
411
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
12,174,901✔
412
    SColData* pCol = taosArrayGet(pCols, c);
12,174,901✔
413

414
    if (bind[c].num != rowNum) {
12,013,114✔
415
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
416
      goto _return;
×
417
    }
418

419
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
12,495,767✔
420
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
11,897,535✔
421
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
422
      goto _return;
×
423
    }
424

425
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
12,495,767✔
426
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
975,879✔
427
      if (code) {
975,286✔
428
        goto _return;
×
429
      }
430
      pBind = &ncharBind;
975,286✔
431
    } else {
432
      pBind = bind + c;
11,519,888✔
433
    }
434

435
    code = tColDataAddValueByBind(pCol, pBind,
12,495,174✔
436
                                  IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1,
12,495,174✔
437
                                  initCtxAsText, checkWKB);
438
    if (code) {
12,257,533✔
439
      goto _return;
1✔
440
    }
441
  }
442

443
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
1,044,677✔
444

445
_return:
1,044,677✔
446

447
  taosMemoryFree(ncharBind.buffer);
1,044,678✔
448
  taosMemoryFree(ncharBind.length);
975,835✔
449

450
  return code;
979,876✔
451
}
452

453
int32_t qBindStmtSingleColValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
140✔
454
                                int32_t colIdx, int32_t rowNum, void* charsetCxt) {
455
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
140✔
456
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
140✔
457
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
140✔
458
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
140✔
459
  SSchema*         pColSchema = &pSchema[boundInfo->pColIndex[colIdx]];
140✔
460
  SColData*        pCol = taosArrayGet(pCols, colIdx);
140✔
461
  TAOS_MULTI_BIND  ncharBind = {0};
140✔
462
  TAOS_MULTI_BIND* pBind = NULL;
140✔
463
  int32_t          code = 0;
140✔
464

465
  if (bind->num != rowNum) {
140✔
466
    return buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
467
  }
468

469
  // Column index exceeds the number of columns
470
  if (colIdx >= pCols->size && pCol == NULL) {
140✔
471
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
472
  }
473

474
  if (bind->buffer_type != pColSchema->type) {
140✔
475
    return buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
476
  }
477

478
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
140✔
479
    code = convertStmtNcharCol(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
10✔
480
    if (code) {
10✔
481
      goto _return;
×
482
    }
483
    pBind = &ncharBind;
10✔
484
  } else {
485
    pBind = bind;
130✔
486
  }
487

488
  code = tColDataAddValueByBind(pCol, pBind,
140✔
489
                                IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1,
140✔
490
                                initCtxAsText, checkWKB);
491

492
  parserDebug("stmt col %d bind %d rows data", colIdx, rowNum);
140✔
493

494
_return:
140✔
495

496
  taosMemoryFree(ncharBind.buffer);
140✔
497
  taosMemoryFree(ncharBind.length);
140✔
498

499
  return code;
140✔
500
}
501

502
int32_t qBindStmtTagsValue2(void* pBlock, void* boundTags, int64_t suid, const char* sTableName, char* tName,
140✔
503
                            TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen, void* charsetCxt,
504
                            SVCreateTbReq* pCreateTbReq) {
505
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
140✔
506
  SMsgBuf        pBuf = {.buf = msgBuf, .len = msgBufLen};
140✔
507
  int32_t        code = TSDB_CODE_SUCCESS;
140✔
508
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
140✔
509
  if (NULL == tags) {
140✔
510
    return TSDB_CODE_APP_ERROR;
×
511
  }
512

513
  SArray* pTagArray = taosArrayInit(tags->numOfBound, sizeof(STagVal));
140✔
514
  if (!pTagArray) {
140✔
515
    return buildInvalidOperationMsg(&pBuf, "out of memory");
×
516
  }
517

518
  SArray* tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
140✔
519
  if (!tagName) {
140✔
520
    code = buildInvalidOperationMsg(&pBuf, "out of memory");
×
521
    goto end;
×
522
  }
523

524
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
140✔
525

526
  bool  isJson = false;
140✔
527
  STag* pTag = NULL;
140✔
528

529
  for (int c = 0; c < tags->numOfBound; ++c) {
431✔
530
    if (bind == NULL) {
292✔
531
      break;
×
532
    }
533
    if (bind[c].is_null && bind[c].is_null[0]) {
292✔
534
      continue;
×
535
    }
536

537
    SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
292✔
538
    int32_t  colLen = pTagSchema->bytes;
292✔
539
    if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
292✔
540
      if (!bind[c].length) {
122✔
541
        code = buildInvalidOperationMsg(&pBuf, "var tag length is null");
×
542
        goto end;
×
543
      }
544
      colLen = bind[c].length[0];
122✔
545
      if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
122✔
546
        code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
×
547
        goto end;
×
548
      }
549
    }
550
    if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
584✔
551
      code = terrno;
×
552
      goto end;
×
553
    }
554
    if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
292✔
555
      if (colLen > (TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
×
556
        code = buildSyntaxErrMsg(&pBuf, "json string too long than 4095", bind[c].buffer);
×
557
        goto end;
×
558
      }
559

560
      isJson = true;
×
561
      char* tmp = taosMemoryCalloc(1, colLen + 1);
×
562
      if (!tmp) {
×
563
        code = terrno;
×
564
        goto end;
×
565
      }
566
      memcpy(tmp, bind[c].buffer, colLen);
×
567
      code = parseJsontoTagData(tmp, pTagArray, &pTag, &pBuf, charsetCxt);
×
568
      taosMemoryFree(tmp);
×
569
      if (code != TSDB_CODE_SUCCESS) {
×
570
        goto end;
×
571
      }
572
    } else {
573
      STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
292✔
574
      //      strcpy(val.colName, pTagSchema->name);
575
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
292✔
576
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
181✔
577
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
118✔
578
          code = initCtxAsText();
7✔
579
          if (code) {
7✔
580
            qError("geometry init failed:%s", tstrerror(code));
×
581
            goto end;
1✔
582
          }
583
          code = checkWKB(bind[c].buffer, colLen);
7✔
584
          if (code) {
7✔
585
            qError("stmt2 bind invalid geometry tag:%s, must be WKB format", (char*)bind[c].buffer);
1✔
586
            goto end;
1✔
587
          }
588
        }
589
        val.pData = (uint8_t*)bind[c].buffer;
117✔
590
        val.nData = colLen;
117✔
591
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
174✔
592
        int32_t output = 0;
4✔
593
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
4✔
594
        if (p == NULL) {
4✔
595
          code = terrno;
×
596
          goto end;
×
597
        }
598
        if (!taosMbsToUcs4(bind[c].buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
4✔
599
          if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
600
            taosMemoryFree(p);
×
601
            code = generateSyntaxErrMsg(&pBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pTagSchema->name);
×
602
            goto end;
×
603
          }
604
          char buf[512] = {0};
×
605
          snprintf(buf, tListLen(buf), " taosMbsToUcs4 error:%s", strerror(terrno));
×
606
          taosMemoryFree(p);
×
607
          code = buildSyntaxErrMsg(&pBuf, buf, bind[c].buffer);
×
608
          goto end;
×
609
        }
610
        val.pData = p;
4✔
611
        val.nData = output;
4✔
612
      } else {
613
        memcpy(&val.i64, bind[c].buffer, colLen);
170✔
614
      }
615
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
291✔
616
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
617
        goto end;
×
618
      }
619
      if (NULL == taosArrayPush(pTagArray, &val)) {
291✔
620
        code = terrno;
×
621
        goto end;
×
622
      }
623
    }
624
  }
625

626
  if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) {
139✔
627
    goto end;
×
628
  }
629

630
  if (pCreateTbReq){
139✔
631
    code = insBuildCreateTbReq(pCreateTbReq, tName, pTag, suid, sTableName, tagName,
38✔
632
                               pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
38✔
633
    pTag = NULL;
38✔
634
    goto end;
38✔
635
  }
636

637
  if (NULL == pDataBlock->pData->pCreateTbReq) {
101✔
638
    pDataBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
10✔
639
    if (NULL == pDataBlock->pData->pCreateTbReq) {
10✔
640
      code = terrno;
×
641
      goto end;
×
642
    }
643
  } else {
644
    SVCreateTbReq* tmp = pDataBlock->pData->pCreateTbReq;
91✔
645
    taosMemoryFreeClear(tmp->name);
91✔
646
    taosMemoryFreeClear(tmp->ctb.pTag);
91✔
647
    taosMemoryFreeClear(tmp->ctb.stbName);
91✔
648
    taosArrayDestroy(tmp->ctb.tagName);
91✔
649
    tmp->ctb.tagName = NULL;
91✔
650
  }
651

652
  code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName,
101✔
653
                             pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
101✔
654
  pTag = NULL;
101✔
655

656
end:
140✔
657
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
431✔
658
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
291✔
659
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
291✔
660
      taosMemoryFreeClear(p->pData);
4✔
661
    }
662
  }
663
  taosArrayDestroy(pTagArray);
140✔
664
  taosArrayDestroy(tagName);
140✔
665
  taosMemoryFree(pTag);
140✔
666

667
  return code;
140✔
668
}
669

670
static int32_t convertStmtStbNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst, void *charsetCxt) {
5✔
671
  int32_t       output = 0;
5✔
672
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
5✔
673

674
  dst->buffer = taosMemoryCalloc(src->num, max_buf_len);
5✔
675
  if (NULL == dst->buffer) {
5✔
676
    return terrno;
×
677
  }
678

679
  dst->length = taosMemoryCalloc(src->num, sizeof(int32_t));
5✔
680
  if (NULL == dst->length) {
5✔
681
    taosMemoryFreeClear(dst->buffer);
×
682
    return terrno;
×
683
  }
684

685
  char* src_buf = src->buffer;
5✔
686
  char* dst_buf = dst->buffer;
5✔
687
  for (int32_t i = 0; i < src->num; ++i) {
55✔
688
    if (src->is_null && src->is_null[i]) {
50✔
689
      continue;
×
690
    }
691

692
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
50✔
693
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
694
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
695
      }
696
      char buf[512] = {0};
×
697
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
698
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
699
    }
700

701
    dst->length[i] = output;
50✔
702
    src_buf += src->length[i];
50✔
703
    dst_buf += output;
50✔
704
  }
705

706
  dst->buffer_type = src->buffer_type;
5✔
707
  dst->is_null = src->is_null;
5✔
708
  dst->num = src->num;
5✔
709

710
  return TSDB_CODE_SUCCESS;
5✔
711
}
712

713
int32_t qBindStmtStbColsValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen,
593✔
714
                               STSchema** pTSchema, SBindInfo2* pBindInfos, void *charsetCxt) {
715
  STableDataCxt*  pDataBlock = (STableDataCxt*)pBlock;
593✔
716
  SSchema*        pSchema = getTableColumnSchema(pDataBlock->pMeta);
593✔
717
  SBoundColInfo*  boundInfo = &pDataBlock->boundColsInfo;
593✔
718
  SMsgBuf         pBuf = {.buf = msgBuf, .len = msgBufLen};
593✔
719
  int32_t         rowNum = bind->num;
593✔
720
  SArray*         ncharBinds = NULL;
593✔
721
  TAOS_STMT2_BIND ncharBind = {0};
593✔
722
  int32_t         code = 0;
593✔
723
  int16_t         lastColId = -1;
593✔
724
  bool            colInOrder = true;
593✔
725
  int             ncharColNums = 0;
593✔
726

727
  if (NULL == pTSchema || NULL == *pTSchema) {
593✔
728
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
33✔
729
  }
730

731
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
11,157✔
732
    if (TSDB_DATA_TYPE_NCHAR == pSchema[boundInfo->pColIndex[c]].type) {
10,564✔
733
      ncharColNums++;
5✔
734
    }
735
  }
736
  if (ncharColNums > 0) {
593✔
737
    ncharBinds = taosArrayInit(ncharColNums, sizeof(ncharBind));
1✔
738
    if (!ncharBinds) {
1✔
739
      code = terrno;
×
740
      goto _return;
×
741
    }
742
  }
743

744
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
11,157✔
745
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
10,564✔
746
    if (pColSchema->colId <= lastColId) {
10,564✔
747
      colInOrder = false;
1✔
748
    } else {
749
      lastColId = pColSchema->colId;
10,563✔
750
    }
751

752
    if (bind[c].num != rowNum) {
10,564✔
753
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
754
      goto _return;
×
755
    }
756

757
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
10,564✔
758
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
10,564✔
759
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
760
      goto _return;
×
761
    }
762

763
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
10,564✔
764
      code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
5✔
765
      if (code) {
5✔
766
        goto _return;
×
767
      }
768
      if (!taosArrayPush(ncharBinds, &ncharBind)) {
5✔
769
        code = terrno;
×
770
        goto _return;
×
771
      }
772
      pBindInfos[c].bind = taosArrayGetLast(ncharBinds);
5✔
773
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
10,559✔
774
      code = initCtxAsText();
1✔
775
      if (code) {
1✔
776
        qError("geometry init failed:%s", tstrerror(code));
×
777
        goto _return;
×
778
      }
779
      uint8_t* buf = bind[c].buffer;
1✔
780
      for (int j = 0; j < bind[c].num; j++) {
5✔
781
        if (bind[c].is_null && bind[c].is_null[j]) {
4✔
782
          continue;
1✔
783
        }
784
        code = checkWKB(buf, bind[c].length[j]);
3✔
785
        if (code) {
3✔
786
          qError("stmt2 interlace mode geometry data[%d]:{%s},length:%d must be in WKB format", c, buf,
×
787
                 bind[c].length[j]);
788
          goto _return;
×
789
        }
790
        buf += bind[c].length[j];
3✔
791
      }
792
      pBindInfos[c].bind = bind + c;
1✔
793
    } else {
794
      pBindInfos[c].bind = bind + c;
10,558✔
795
    }
796

797
    pBindInfos[c].columnId = pColSchema->colId;
10,564✔
798
    pBindInfos[c].type = pColSchema->type;
10,564✔
799
    pBindInfos[c].bytes = pColSchema->bytes;
10,564✔
800
  }
801

802
  code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols, &pDataBlock->ordered, &pDataBlock->duplicateTs);
593✔
803

804
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
593✔
805

806
_return:
593✔
807
  if (ncharBinds) {
593✔
808
    for (int i = 0; i < TARRAY_SIZE(ncharBinds); ++i) {
6✔
809
      TAOS_STMT2_BIND* ncBind = TARRAY_DATA(ncharBinds);
5✔
810
      taosMemoryFree(ncBind[i].buffer);
5✔
811
      taosMemoryFree(ncBind[i].length);
5✔
812
    }
813
    taosArrayDestroy(ncharBinds);
1✔
814
  }
815

816
  return code;
593✔
817
}
818

819
static int32_t convertStmtNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst, void *charsetCxt) {
3✔
820
  int32_t       output = 0;
3✔
821
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
3✔
822

823
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
3✔
824
  // if (dst->buffer_length < newBuflen) {
825
  dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
3✔
826
  if (NULL == dst->buffer) {
3✔
827
    return terrno;
×
828
  }
829
  //}
830

831
  if (NULL == dst->length) {
3✔
832
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
3✔
833
    if (NULL == dst->length) {
3✔
834
      taosMemoryFreeClear(dst->buffer);
×
835
      return terrno;
×
836
    }
837
  }
838

839
  // dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
840
  char* src_buf = src->buffer;
3✔
841
  char* dst_buf = dst->buffer;
3✔
842
  for (int32_t i = 0; i < src->num; ++i) {
6✔
843
    if (src->is_null && src->is_null[i]) {
3✔
844
      continue;
×
845
    }
846

847
    /*if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
848
      (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output)) {*/
849
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
3✔
850
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
851
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
852
      }
853
      char buf[512] = {0};
×
854
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
855
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
856
    }
857

858
    dst->length[i] = output;
3✔
859
    src_buf += src->length[i];
3✔
860
    dst_buf += output;
3✔
861
  }
862

863
  dst->buffer_type = src->buffer_type;
3✔
864
  dst->is_null = src->is_null;
3✔
865
  dst->num = src->num;
3✔
866

867
  return TSDB_CODE_SUCCESS;
3✔
868
}
869

870
int32_t qBindStmtColsValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen,
128✔
871
                            void* charsetCxt) {
872
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
128✔
873
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
128✔
874
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
128✔
875
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
128✔
876
  int32_t          rowNum = bind->num;
128✔
877
  TAOS_STMT2_BIND  ncharBind = {0};
128✔
878
  TAOS_STMT2_BIND* pBind = NULL;
128✔
879
  int32_t          code = 0;
128✔
880

881
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
427✔
882
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
299✔
883
    SColData* pCol = taosArrayGet(pCols, c);
299✔
884
    if (pCol == NULL || pColSchema == NULL) {
299✔
885
      code = buildInvalidOperationMsg(&pBuf, "get column schema or column data failed");
×
886
      goto _return;
×
887
    }
888

889
    if (boundInfo->pColIndex[c] == 0) {
299✔
890
      pCol->cflag |= COL_IS_KEY;
128✔
891
    }
892

893
    if (bind[c].num != rowNum) {
299✔
894
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
895
      goto _return;
×
896
    }
897

898
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
299✔
899
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
299✔
900
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
901
      goto _return;
×
902
    }
903

904
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
299✔
905
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
1✔
906
      if (code) {
1✔
907
        goto _return;
×
908
      }
909
      pBind = &ncharBind;
1✔
910
    } else {
911
      pBind = bind + c;
298✔
912
    }
913

914
    code = tColDataAddValueByBind2(pCol, pBind,
299✔
915
                                   IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1,
299✔
916
                                   initCtxAsText, checkWKB);
917
    if (code) {
299✔
918
      goto _return;
×
919
    }
920
  }
921

922
  parserDebug("stmt2 all %d columns bind %d rows data as col format", boundInfo->numOfBound, rowNum);
128✔
923

924
_return:
128✔
925

926
  taosMemoryFree(ncharBind.buffer);
128✔
927
  taosMemoryFree(ncharBind.length);
128✔
928

929
  return code;
128✔
930
}
931

932
int32_t qBindStmtSingleColValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen,
6✔
933
                                 int32_t colIdx, int32_t rowNum, void *charsetCxt) {
934
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
6✔
935
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
6✔
936
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
6✔
937
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
6✔
938
  SSchema*         pColSchema = &pSchema[boundInfo->pColIndex[colIdx]];
6✔
939
  SColData*        pCol = taosArrayGet(pCols, colIdx);
6✔
940
  TAOS_STMT2_BIND  ncharBind = {0};
6✔
941
  TAOS_STMT2_BIND* pBind = NULL;
6✔
942
  int32_t          code = 0;
6✔
943

944
  if (bind->num != rowNum) {
6✔
945
    return buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
946
  }
947

948
  // Column index exceeds the number of columns
949
  if (colIdx >= pCols->size && pCol == NULL) {
6✔
950
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
951
  }
952

953
  if (bind->buffer_type != pColSchema->type) {
6✔
954
    return buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
955
  }
956

957
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
6✔
958
    code = convertStmtNcharCol2(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
×
959
    if (code) {
×
960
      goto _return;
×
961
    }
962
    pBind = &ncharBind;
×
963
  } else {
964
    pBind = bind;
6✔
965
  }
966

967
  code = tColDataAddValueByBind2(pCol, pBind,
6✔
968
                                 IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1,
6✔
969
                                 initCtxAsText, checkWKB);
970

971
  parserDebug("stmt col %d bind %d rows data", colIdx, rowNum);
6✔
972

973
_return:
6✔
974

975
  taosMemoryFree(ncharBind.buffer);
6✔
976
  taosMemoryFree(ncharBind.length);
6✔
977

978
  return code;
6✔
979
}
980

981
int32_t qBindStmt2RowValue(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen,
2✔
982
                            STSchema** pTSchema, SBindInfo2* pBindInfos, void* charsetCxt) {
983
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
2✔
984
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
2✔
985
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
2✔
986
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
2✔
987
  int32_t          rowNum = bind->num;
2✔
988
  TAOS_STMT2_BIND  ncharBind = {0};
2✔
989
  TAOS_STMT2_BIND* pBind = NULL;
2✔
990
  int32_t          code = 0;
2✔
991
  int16_t          lastColId = -1;
2✔
992
  bool             colInOrder = true;
2✔
993

994
  if (NULL == pTSchema || NULL == *pTSchema) {
2✔
995
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
1✔
996
  }
997

998
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
24✔
999
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
22✔
1000
    if (pColSchema->colId <= lastColId) {
22✔
1001
      colInOrder = false;
×
1002
    } else {
1003
      lastColId = pColSchema->colId;
22✔
1004
    }
1005

1006
    if (bind[c].num != rowNum) {
22✔
1007
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1008
      goto _return;
×
1009
    }
1010

1011
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
22✔
1012
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
22✔
1013
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
1014
      goto _return;
×
1015
    }
1016

1017
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
22✔
1018
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
2✔
1019
      if (code) {
2✔
1020
        goto _return;
×
1021
      }
1022
      pBindInfos[c].bind = &ncharBind;
2✔
1023
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
20✔
1024
      code = initCtxAsText();
2✔
1025
      if (code) {
2✔
1026
        qError("geometry init failed:%s", tstrerror(code));
×
1027
        goto _return;
×
1028
      }
1029
      uint8_t *buf = bind[c].buffer;
2✔
1030
      for (int j = 0; j < bind[c].num; j++) {
4✔
1031
        if (bind[c].is_null && bind[c].is_null[j]) {
2✔
1032
          continue;
×
1033
        }
1034
        code = checkWKB(buf, bind[c].length[j]);
2✔
1035
        if (code) {
2✔
1036
          qError("stmt2 row bind geometry data[%d]:{%s},length:%d must be in WKB format", c, buf, bind[c].length[j]);
×
1037
          goto _return;
×
1038
        }
1039
        buf += bind[c].length[j];
2✔
1040
      }
1041
      pBindInfos[c].bind = bind + c;
2✔
1042
    } else {
1043
      pBindInfos[c].bind = bind + c;
18✔
1044
    }
1045

1046
    pBindInfos[c].columnId = pColSchema->colId;
22✔
1047
    pBindInfos[c].type = pColSchema->type;
22✔
1048
    pBindInfos[c].bytes = pColSchema->bytes;
22✔
1049

1050
    if (code) {
22✔
1051
      goto _return;
×
1052
    }
1053
  }
1054

1055
  pDataBlock->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
2✔
1056
  if (pDataBlock->pData->pCreateTbReq != NULL) {
2✔
1057
    pDataBlock->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
2✔
1058
  }
1059

1060
  code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols, &pDataBlock->ordered,
2✔
1061
                            &pDataBlock->duplicateTs);
1062
  qDebug("stmt2 all %d columns bind %d rows data as row format", boundInfo->numOfBound, rowNum);
2✔
1063

1064
_return:
2✔
1065

1066
  taosMemoryFree(ncharBind.buffer);
2✔
1067
  taosMemoryFree(ncharBind.length);
2✔
1068

1069
  return code;
2✔
1070
}
1071

1072
int32_t buildBoundFields(int32_t numOfBound, int16_t* boundColumns, SSchema* pSchema, int32_t* fieldNum,
117✔
1073
                         TAOS_FIELD_E** fields, uint8_t timePrec) {
1074
  if (fields != NULL) {
117✔
1075
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
116✔
1076
    if (NULL == *fields) {
116✔
1077
      return terrno;
×
1078
    }
1079

1080
    SSchema* schema = &pSchema[boundColumns[0]];
116✔
1081
    if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
116✔
1082
      (*fields)[0].precision = timePrec;
105✔
1083
    }
1084

1085
    for (int32_t i = 0; i < numOfBound; ++i) {
558✔
1086
      schema = &pSchema[boundColumns[i]];
442✔
1087
      tstrncpy((*fields)[i].name, schema->name, 65);
442✔
1088
      (*fields)[i].type = schema->type;
442✔
1089
      (*fields)[i].bytes = calcTypeBytesFromSchemaBytes(schema->type, schema->bytes, true);
442✔
1090
    }
1091
  }
1092

1093
  *fieldNum = numOfBound;
117✔
1094

1095
  return TSDB_CODE_SUCCESS;
117✔
1096
}
1097

1098
int32_t buildStbBoundFields(SBoundColInfo boundColsInfo, SSchema* pSchema, int32_t* fieldNum, TAOS_FIELD_ALL** fields,
43✔
1099
                            STableMeta* pMeta, void* boundTags, uint8_t tbNameFlag) {
1100
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
43✔
1101
  bool           hastag = (tags != NULL) && !(tbNameFlag & IS_FIXED_TAG);
43✔
1102
  bool           hasPreBindTbname =
43✔
1103
      (tbNameFlag & IS_FIXED_VALUE) == 0 && ((tbNameFlag & USING_CLAUSE) != 0 || pMeta->tableType == TSDB_NORMAL_TABLE);
43✔
1104
  int32_t numOfBound = boundColsInfo.numOfBound + (hasPreBindTbname ? 1 : 0);
43✔
1105
  if (hastag) {
43✔
1106
    numOfBound += tags->mixTagsCols ? 0 : tags->numOfBound;
24✔
1107
  }
1108
  int32_t idx = 0;
43✔
1109
  if (fields != NULL) {
43✔
1110
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_ALL));
43✔
1111
    if (NULL == *fields) {
43✔
1112
      return terrno;
×
1113
    }
1114

1115
    if (hasPreBindTbname) {
43✔
1116
      (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
28✔
1117
      tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
28✔
1118
      (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
28✔
1119
      (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
28✔
1120
      idx++;
28✔
1121
    }
1122

1123
    if (hastag && tags->numOfBound > 0 && !tags->mixTagsCols) {
43✔
1124
      SSchema* tagSchema = getTableTagSchema(pMeta);
10✔
1125

1126
      for (int32_t i = 0; i < tags->numOfBound; ++i) {
43✔
1127
        (*fields)[idx].field_type = TAOS_FIELD_TAG;
33✔
1128

1129
        SSchema* schema = &tagSchema[tags->pColIndex[i]];
33✔
1130
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[i].name));
33✔
1131
        (*fields)[idx].type = schema->type;
33✔
1132
        (*fields)[idx].bytes = schema->bytes;
33✔
1133
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
33✔
1134
          (*fields)[idx].precision = pMeta->tableInfo.precision;
1✔
1135
        }
1136
        idx++;
33✔
1137
      }
1138
    }
1139

1140
    if (boundColsInfo.numOfBound > 0) {
43✔
1141
      SSchema* schema = &pSchema[boundColsInfo.pColIndex[0]];
43✔
1142

1143
      for (int32_t i = 0; i < boundColsInfo.numOfBound; ++i) {
199✔
1144
        int16_t idxCol = boundColsInfo.pColIndex[i];
156✔
1145

1146
        if (idxCol == pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags) {
156✔
1147
          (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
8✔
1148
          tstrncpy((*fields)[i].name, "tbname", sizeof((*fields)[idx].name));
8✔
1149
          (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
8✔
1150
          (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
8✔
1151

1152
          idx++;
8✔
1153
          continue;
8✔
1154
        } else if (idxCol < pMeta->tableInfo.numOfColumns) {
148✔
1155
          (*fields)[idx].field_type = TAOS_FIELD_COL;
122✔
1156
        } else {
1157
          (*fields)[idx].field_type = TAOS_FIELD_TAG;
26✔
1158
        }
1159

1160
        schema = &pSchema[idxCol];
148✔
1161
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[idx].name));
148✔
1162
        (*fields)[idx].type = schema->type;
148✔
1163
        (*fields)[idx].bytes = schema->bytes;
148✔
1164
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
148✔
1165
          (*fields)[idx].precision = pMeta->tableInfo.precision;
44✔
1166
        }
1167
        idx++;
148✔
1168
      }
1169
    }
1170
  }
1171

1172
  *fieldNum = numOfBound;
43✔
1173

1174
  return TSDB_CODE_SUCCESS;
43✔
1175
}
1176

1177
int32_t qBuildStmtTagFields(void* pBlock, void* boundTags, int32_t* fieldNum, TAOS_FIELD_E** fields) {
11✔
1178
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
11✔
1179
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
11✔
1180
  if (NULL == tags) {
11✔
1181
    return TSDB_CODE_APP_ERROR;
×
1182
  }
1183

1184
  if (pDataBlock->pMeta->tableType != TSDB_SUPER_TABLE && pDataBlock->pMeta->tableType != TSDB_CHILD_TABLE) {
11✔
1185
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1186
  }
1187

1188
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
11✔
1189
  if (tags->numOfBound <= 0) {
11✔
1190
    *fieldNum = 0;
×
1191
    *fields = NULL;
×
1192

1193
    return TSDB_CODE_SUCCESS;
×
1194
  }
1195

1196
  CHECK_CODE(buildBoundFields(tags->numOfBound, tags->pColIndex, pSchema, fieldNum, fields, 0));
11✔
1197

1198
  return TSDB_CODE_SUCCESS;
11✔
1199
}
1200

1201
int32_t qBuildStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields) {
106✔
1202
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
106✔
1203
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
106✔
1204
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
106✔
1205
    *fieldNum = 0;
×
1206
    if (fields != NULL) {
×
1207
      *fields = NULL;
×
1208
    }
1209

1210
    return TSDB_CODE_SUCCESS;
×
1211
  }
1212

1213
  CHECK_CODE(buildBoundFields(pDataBlock->boundColsInfo.numOfBound, pDataBlock->boundColsInfo.pColIndex, pSchema,
106✔
1214
                              fieldNum, fields, pDataBlock->pMeta->tableInfo.precision));
1215

1216
  return TSDB_CODE_SUCCESS;
106✔
1217
}
1218

1219
int32_t qBuildStmtStbColFields(void* pBlock, void* boundTags, uint8_t tbNameFlag, int32_t* fieldNum,
43✔
1220
                               TAOS_FIELD_ALL** fields) {
1221
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
43✔
1222
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
43✔
1223
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
43✔
1224
    *fieldNum = 0;
×
1225
    if (fields != NULL) {
×
1226
      *fields = NULL;
×
1227
    }
1228

1229
    return TSDB_CODE_SUCCESS;
×
1230
  }
1231

1232
  CHECK_CODE(buildStbBoundFields(pDataBlock->boundColsInfo, pSchema, fieldNum, fields, pDataBlock->pMeta, boundTags,
43✔
1233
                                 tbNameFlag));
1234

1235
  return TSDB_CODE_SUCCESS;
43✔
1236
}
1237

1238
int32_t qResetStmtColumns(SArray* pCols, bool deepClear) {
×
1239
  int32_t colNum = taosArrayGetSize(pCols);
×
1240

1241
  for (int32_t i = 0; i < colNum; ++i) {
×
1242
    SColData* pCol = (SColData*)taosArrayGet(pCols, i);
×
1243
    if (pCol == NULL) {
×
1244
      parserError("qResetStmtColumns column:%d is NULL", i);
×
1245
      return terrno;
×
1246
    }
1247
    if (deepClear) {
×
1248
      tColDataDeepClear(pCol);
×
1249
    } else {
1250
      tColDataClear(pCol);
×
1251
    }
1252
  }
1253

1254
  return TSDB_CODE_SUCCESS;
×
1255
}
1256

1257
int32_t qResetStmtDataBlock(STableDataCxt* block, bool deepClear) {
342✔
1258
  STableDataCxt* pBlock = (STableDataCxt*)block;
342✔
1259
  int32_t        colNum = taosArrayGetSize(pBlock->pData->aCol);
342✔
1260

1261
  for (int32_t i = 0; i < colNum; ++i) {
5,168✔
1262
    if (pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
4,827✔
1263
      SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i);
4,826✔
1264
      if (pCol == NULL) {
4,826✔
1265
        parserError("qResetStmtDataBlock column:%d is NULL", i);
×
1266
        return terrno;
×
1267
      }
1268
      if (deepClear) {
4,826✔
1269
        tColDataDeepClear(pCol);
3,123✔
1270
      } else {
1271
        tColDataClear(pCol);
1,703✔
1272
      }
1273
    } else {
1274
      pBlock->pData->aRowP = taosArrayInit(20, POINTER_BYTES);
1✔
1275
    }
1276
  }
1277

1278
  return TSDB_CODE_SUCCESS;
341✔
1279
}
1280

1281
int32_t qCloneStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, bool reset) {
273✔
1282
  int32_t code = 0;
273✔
1283

1284
  *pDst = taosMemoryCalloc(1, sizeof(STableDataCxt));
273✔
1285
  if (NULL == *pDst) {
274✔
1286
    return terrno;
×
1287
  }
1288

1289
  STableDataCxt* pNewCxt = (STableDataCxt*)*pDst;
274✔
1290
  STableDataCxt* pCxt = (STableDataCxt*)pSrc;
274✔
1291
  pNewCxt->pSchema = NULL;
274✔
1292
  pNewCxt->pValues = NULL;
274✔
1293

1294
  if (pCxt->pMeta) {
274✔
1295
    void* pNewMeta = taosMemoryMalloc(TABLE_META_SIZE(pCxt->pMeta));
274✔
1296
    if (NULL == pNewMeta) {
274✔
1297
      insDestroyTableDataCxt(*pDst);
×
1298
      return terrno;
×
1299
    }
1300
    memcpy(pNewMeta, pCxt->pMeta, TABLE_META_SIZE(pCxt->pMeta));
274✔
1301
    pNewCxt->pMeta = pNewMeta;
274✔
1302
  }
1303

1304
  memcpy(&pNewCxt->boundColsInfo, &pCxt->boundColsInfo, sizeof(pCxt->boundColsInfo));
274✔
1305
  pNewCxt->boundColsInfo.pColIndex = NULL;
274✔
1306

1307
  if (pCxt->boundColsInfo.pColIndex) {
274✔
1308
    void* pNewColIdx = taosMemoryMalloc(pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
274✔
1309
    if (NULL == pNewColIdx) {
274✔
1310
      insDestroyTableDataCxt(*pDst);
×
1311
      return terrno;
×
1312
    }
1313
    memcpy(pNewColIdx, pCxt->boundColsInfo.pColIndex,
274✔
1314
           pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
274✔
1315
    pNewCxt->boundColsInfo.pColIndex = pNewColIdx;
274✔
1316
  }
1317

1318
  if (pCxt->pData) {
274✔
1319
    SSubmitTbData* pNewTb = (SSubmitTbData*)taosMemoryMalloc(sizeof(SSubmitTbData));
274✔
1320
    if (NULL == pNewTb) {
274✔
1321
      insDestroyTableDataCxt(*pDst);
×
1322
      return terrno;
×
1323
    }
1324

1325
    memcpy(pNewTb, pCxt->pData, sizeof(*pCxt->pData));
274✔
1326
    pNewTb->pCreateTbReq = NULL;
274✔
1327

1328
    pNewTb->aCol = taosArrayDup(pCxt->pData->aCol, NULL);
274✔
1329
    if (NULL == pNewTb->aCol) {
274✔
1330
      insDestroyTableDataCxt(*pDst);
×
1331
      return terrno;
×
1332
    }
1333

1334
    pNewCxt->pData = pNewTb;
274✔
1335

1336
    if (reset) {
274✔
1337
      code = qResetStmtDataBlock(*pDst, true);
105✔
1338
    }
1339
  }
1340

1341
  return code;
274✔
1342
}
1343

1344
int32_t qRebuildStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, uint64_t uid, uint64_t suid, int32_t vgId,
169✔
1345
                              bool rebuildCreateTb) {
1346
  int32_t code = qCloneStmtDataBlock(pDst, pSrc, false);
169✔
1347
  if (code) {
169✔
1348
    return code;
×
1349
  }
1350

1351
  STableDataCxt* pBlock = (STableDataCxt*)*pDst;
169✔
1352
  if (pBlock->pMeta) {
169✔
1353
    pBlock->pMeta->uid = uid;
169✔
1354
    pBlock->pMeta->vgId = vgId;
169✔
1355
    pBlock->pMeta->suid = suid;
169✔
1356
  }
1357

1358
  pBlock->pData->suid = suid;
169✔
1359
  pBlock->pData->uid = uid;
169✔
1360

1361
  if (rebuildCreateTb && NULL == pBlock->pData->pCreateTbReq) {
169✔
1362
    pBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
143✔
1363
    if (NULL == pBlock->pData->pCreateTbReq) {
143✔
1364
      return terrno;
×
1365
    }
1366
  }
1367

1368
  return TSDB_CODE_SUCCESS;
169✔
1369
}
1370

1371
STableMeta* qGetTableMetaInDataBlock(STableDataCxt* pDataBlock) { return ((STableDataCxt*)pDataBlock)->pMeta; }
526✔
1372

1373
void qDestroyStmtDataBlock(STableDataCxt* pBlock) {
657✔
1374
  if (pBlock == NULL) {
657✔
1375
    return;
164✔
1376
  }
1377

1378
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
493✔
1379
  insDestroyTableDataCxt(pDataBlock);
493✔
1380
}
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