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

taosdata / TDengine / #3873

21 Apr 2025 07:22AM UTC coverage: 63.063% (+0.1%) from 62.968%
#3873

push

travis-ci

GitHub
docs(opc): add perssit data support (#30783)

156631 of 316378 branches covered (49.51%)

Branch coverage included in aggregate %.

242184 of 316027 relevant lines covered (76.63%)

20271838.47 hits per line

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

64.48
/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) {
40,700✔
34
  *pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
40,700!
35
  if (NULL == *pData) {
40,727!
36
    return terrno;
×
37
  }
38

39
  SSubmitTbData* pNew = *pData;
40,727✔
40

41
  *pNew = *pDataBlock->pData;
40,727✔
42

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

55
  int32_t colNum = taosArrayGetSize(pNew->aCol);
40,725✔
56
  for (int32_t i = 0; i < colNum; ++i) {
165,815✔
57
    if (pDataBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
125,139✔
58
      SColData* pCol = (SColData*)taosArrayGet(pNew->aCol, i);
125,137✔
59
      tColDataDeepClear(pCol);
125,133✔
60
    } else {
61
      pNew->aCol = taosArrayInit(20, POINTER_BYTES);
2✔
62
    }
63
  }
64

65
  return TSDB_CODE_SUCCESS;
40,676✔
66
}
67

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

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

78
  if (TSDB_CODE_SUCCESS == code) {
22,300!
79
    code = insBuildVgDataBlocks(pAllVgHash, pVgDataBlocks, &pStmt->pDataBlocks, true);
22,314✔
80
  }
81

82
  if (pStmt->freeArrayFunc) {
22,296!
83
    pStmt->freeArrayFunc(pVgDataBlocks);
22,311✔
84
  }
85
  return code;
22,312✔
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) {
40,678✔
110
  int32_t             code = TSDB_CODE_SUCCESS;
40,678✔
111
  SArray*             pVgDataBlocks = NULL;
40,678✔
112
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
40,678✔
113

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

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

123
  if (pStmt->freeArrayFunc) {
40,690!
124
    pStmt->freeArrayFunc(pVgDataBlocks);
40,713✔
125
  }
126
  return code;
40,730✔
127
}
128

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

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

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

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

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

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

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

183
      isJson = true;
×
184
      char* tmp = taosMemoryCalloc(1, colLen + 1);
×
185
      if (!tmp) {
×
186
        code = terrno;
×
187
        goto end;
×
188
      }
189
      memcpy(tmp, bind[c].buffer, colLen);
×
190
      code = parseJsontoTagData(tmp, pTagArray, &pTag, &pBuf, charsetCxt);
×
191
      taosMemoryFree(tmp);
×
192
      if (code != TSDB_CODE_SUCCESS) {
×
193
        goto end;
×
194
      }
195
    } else {
196
      STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
50✔
197
      //      strcpy(val.colName, pTagSchema->name);
198
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
50!
199
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
38✔
200
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
13✔
201
          code = initCtxAsText();
1✔
202
          if (code) {
1!
203
            qError("geometry init failed:%s", tstrerror(code));
×
204
            goto end;
×
205
          }
206
          code = checkWKB(bind[c].buffer, colLen);
1✔
207
          if (code) {
1!
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;
13✔
213
        val.nData = colLen;
13✔
214
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
37✔
215
        int32_t output = 0;
2✔
216
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
2!
217
        if (p == NULL) {
2!
218
          code = terrno;
×
219
          goto end;
×
220
        }
221
        if (!taosMbsToUcs4(bind[c].buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
2!
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;
2✔
234
        val.nData = output;
2✔
235
      } else {
236
        memcpy(&val.i64, bind[c].buffer, colLen);
35✔
237
      }
238
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
50!
239
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
240
        goto end;
×
241
      }
242
      if (NULL == taosArrayPush(pTagArray, &val)) {
50!
243
        code = terrno;
×
244
        goto end;
×
245
      }
246
    }
247
  }
248

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

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

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

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

283
  return code;
15✔
284
}
285

286
int32_t convertStmtNcharCol(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_MULTI_BIND* src, TAOS_MULTI_BIND* dst, void* charsetCxt) {
943,594✔
287
  int32_t output = 0;
943,594✔
288
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
943,594✔
289
  if (dst->buffer_length < newBuflen) {
943,594!
290
    dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
944,015!
291
    if (NULL == dst->buffer) {
972,483!
292
      return terrno;
×
293
    }
294
  }
295

296
  if (NULL == dst->length) {
972,062!
297
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
977,384!
298
    if (NULL == dst->length) {
1,008,283!
299
      taosMemoryFreeClear(dst->buffer);
×
300
      return terrno;
×
301
    }
302
  }
303

304
  dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
1,002,961✔
305

306
  for (int32_t i = 0; i < src->num; ++i) {
2,040,057✔
307
    if (src->is_null && src->is_null[i]) {
1,018,394✔
308
      continue;
51,114✔
309
    }
310

311
    if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
987,013✔
312
                       (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output, charsetCxt)) {
967,280✔
313
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
1,031!
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;
985,982✔
322
  }
323

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

328
  return TSDB_CODE_SUCCESS;
1,021,663✔
329
}
330

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

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

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

357
    if (bind[c].num != rowNum) {
202,950!
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)) &&
202,950!
363
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
203,012!
364
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
365
      goto _return;
×
366
    }
367

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

378
    pBindInfos[c].columnId = pColSchema->colId;
202,950✔
379
    pBindInfos[c].bind = pBind;
202,950✔
380
    pBindInfos[c].type = pColSchema->type;
202,950✔
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);
50,378✔
389

390
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
50,999!
391

392
_return:
50,999✔
393

394
  taosMemoryFree(ncharBind.buffer);
50,999!
395
  taosMemoryFree(ncharBind.length);
50,953!
396

397
  return code;
50,918✔
398
}
399

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

410
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
9,007,084✔
411
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
7,463,151✔
412
    SColData* pCol = taosArrayGet(pCols, c);
7,463,151✔
413

414
    if (bind[c].num != rowNum) {
7,106,567!
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)) &&
7,760,617✔
420
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
7,186,705!
421
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
422
      goto _return;
×
423
    }
424

425
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
7,760,617✔
426
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
944,699✔
427
      if (code) {
1,002,293!
428
        goto _return;
×
429
      }
430
      pBind = &ncharBind;
1,002,293✔
431
    } else {
432
      pBind = bind + c;
6,815,918✔
433
    }
434

435
    code = tColDataAddValueByBind(pCol, pBind,
7,818,211✔
436
                                  IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1,
7,818,211!
437
                                  initCtxAsText, checkWKB);
438
    if (code) {
7,987,825✔
439
      goto _return;
1✔
440
    }
441
  }
442

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

445
_return:
1,543,927✔
446

447
  taosMemoryFree(ncharBind.buffer);
1,543,934!
448
  taosMemoryFree(ncharBind.length);
978,647!
449

450
  return code;
1,017,851✔
451
}
452

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

465
  if (bind->num != rowNum) {
450!
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) {
450!
471
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
472
  }
473

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

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

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

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

494
_return:
450✔
495

496
  taosMemoryFree(ncharBind.buffer);
450!
497
  taosMemoryFree(ncharBind.length);
450!
498

499
  return code;
450✔
500
}
501

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

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

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

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

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

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

537
    SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
252✔
538
    int32_t  colLen = pTagSchema->bytes;
252✔
539
    if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
252!
540
      if (!bind[c].length) {
102!
541
        code = buildInvalidOperationMsg(&pBuf, "var tag length is null");
×
542
        goto end;
×
543
      }
544
      colLen = bind[c].length[0];
102✔
545
      if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
102!
546
        code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
×
547
        goto end;
×
548
      }
549
    }
550
    if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
504!
551
      code = terrno;
×
552
      goto end;
×
553
    }
554
    if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
252!
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};
252✔
574
      //      strcpy(val.colName, pTagSchema->name);
575
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
252!
576
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
161✔
577
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
98✔
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;
97✔
590
        val.nData = colLen;
97✔
591
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
154✔
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);
150✔
614
      }
615
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
251!
616
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
617
        goto end;
×
618
      }
619
      if (NULL == taosArrayPush(pTagArray, &val)) {
251!
620
        code = terrno;
×
621
        goto end;
×
622
      }
623
    }
624
  }
625

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

630
  if (pCreateTbReq){
119✔
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) {
81✔
638
    pDataBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
8!
639
    if (NULL == pDataBlock->pData->pCreateTbReq) {
8!
640
      code = terrno;
×
641
      goto end;
×
642
    }
643
  } else {
644
    SVCreateTbReq* tmp = pDataBlock->pData->pCreateTbReq;
73✔
645
    taosMemoryFreeClear(tmp->name);
73!
646
    taosMemoryFreeClear(tmp->ctb.pTag);
73!
647
    taosMemoryFreeClear(tmp->ctb.stbName);
73!
648
    taosArrayDestroy(tmp->ctb.tagName);
73✔
649
    tmp->ctb.tagName = NULL;
73✔
650
  }
651

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

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

667
  return code;
120✔
668
}
669

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

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

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

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

692
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
3,036!
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;
3,039✔
702
    src_buf += src->length[i];
3,039✔
703
    dst_buf += output;
3,039✔
704
  }
705

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

710
  return TSDB_CODE_SUCCESS;
11✔
711
}
712

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

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

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

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

752
    if (bind[c].num != rowNum) {
30,765!
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)) &&
30,765!
758
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
30,768!
759
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
760
      goto _return;
×
761
    }
762

763
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
30,765✔
764
      code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
8✔
765
      if (code) {
8!
766
        goto _return;
×
767
      }
768
      if (!taosArrayPush(ncharBinds, &ncharBind)) {
8!
769
        code = terrno;
×
770
        goto _return;
×
771
      }
772
      pBindInfos[c].bind = taosArrayGetLast(ncharBinds);
8✔
773
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
30,757✔
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;
30,756✔
795
    }
796

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

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

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

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

816
  return code;
5,737✔
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,
108✔
871
                            void* charsetCxt) {
872
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
108✔
873
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
108✔
874
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
108✔
875
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
108✔
876
  int32_t          rowNum = bind->num;
108✔
877
  TAOS_STMT2_BIND  ncharBind = {0};
108✔
878
  TAOS_STMT2_BIND* pBind = NULL;
108✔
879
  int32_t          code = 0;
108✔
880

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

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

893
    if (bind[c].num != rowNum) {
219!
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)) &&
219!
899
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
219!
900
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
901
      goto _return;
×
902
    }
903

904
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
219✔
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;
218✔
912
    }
913

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

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

924
_return:
108✔
925

926
  taosMemoryFree(ncharBind.buffer);
108!
927
  taosMemoryFree(ncharBind.length);
108!
928

929
  return code;
108✔
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,
1,060✔
1073
                         TAOS_FIELD_E** fields, uint8_t timePrec) {
1074
  if (fields != NULL) {
1,060✔
1075
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
1,059!
1076
    if (NULL == *fields) {
1,059!
1077
      return terrno;
×
1078
    }
1079

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

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

1093
  *fieldNum = numOfBound;
1,060✔
1094

1095
  return TSDB_CODE_SUCCESS;
1,060✔
1096
}
1097

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

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

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

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

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

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

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

1146
        if (idxCol == pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags) {
132✔
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) {
124✔
1155
          (*fields)[idx].field_type = TAOS_FIELD_COL;
98✔
1156
        } else {
1157
          (*fields)[idx].field_type = TAOS_FIELD_TAG;
26✔
1158
        }
1159

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

1172
  *fieldNum = numOfBound;
32✔
1173

1174
  return TSDB_CODE_SUCCESS;
32✔
1175
}
1176

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

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

1188
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
1✔
1189
  if (tags->numOfBound <= 0) {
1!
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));
1!
1197

1198
  return TSDB_CODE_SUCCESS;
1✔
1199
}
1200

1201
int32_t qBuildStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields) {
1,059✔
1202
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
1,059✔
1203
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
1,059✔
1204
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
1,059!
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,
1,059!
1214
                              fieldNum, fields, pDataBlock->pMeta->tableInfo.precision));
1215

1216
  return TSDB_CODE_SUCCESS;
1,059✔
1217
}
1218

1219
int32_t qBuildStmtStbColFields(void* pBlock, void* boundTags, uint8_t tbNameFlag, int32_t* fieldNum,
32✔
1220
                               TAOS_FIELD_ALL** fields) {
1221
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
32✔
1222
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
32✔
1223
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
32!
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,
32!
1233
                                 tbNameFlag));
1234

1235
  return TSDB_CODE_SUCCESS;
32✔
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) {
60,933✔
1258
  STableDataCxt* pBlock = (STableDataCxt*)block;
60,933✔
1259
  int32_t        colNum = taosArrayGetSize(pBlock->pData->aCol);
60,933✔
1260

1261
  for (int32_t i = 0; i < colNum; ++i) {
249,430✔
1262
    if (pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
188,533✔
1263
      SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i);
188,532✔
1264
      if (pCol == NULL) {
188,528!
1265
        parserError("qResetStmtDataBlock column:%d is NULL", i);
×
1266
        return terrno;
×
1267
      }
1268
      if (deepClear) {
188,578✔
1269
        tColDataDeepClear(pCol);
63,357✔
1270
      } else {
1271
        tColDataClear(pCol);
125,221✔
1272
      }
1273
    } else {
1274
      pBlock->pData->aRowP = taosArrayInit(20, POINTER_BYTES);
1✔
1275
    }
1276
  }
1277

1278
  return TSDB_CODE_SUCCESS;
60,897✔
1279
}
1280

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

1284
  *pDst = taosMemoryCalloc(1, sizeof(STableDataCxt));
20,256!
1285
  if (NULL == *pDst) {
20,322!
1286
    return terrno;
×
1287
  }
1288

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

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

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

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

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

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

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

1334
    pNewCxt->pData = pNewTb;
20,320✔
1335

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

1341
  return code;
20,277✔
1342
}
1343

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

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

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

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

1368
  return TSDB_CODE_SUCCESS;
83✔
1369
}
1370

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

1373
void qDestroyStmtDataBlock(STableDataCxt* pBlock) {
61,897✔
1374
  if (pBlock == NULL) {
61,897✔
1375
    return;
20,742✔
1376
  }
1377

1378
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
41,155✔
1379
  insDestroyTableDataCxt(pDataBlock);
41,155✔
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