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

taosdata / TDengine / #4827

28 Oct 2025 01:21AM UTC coverage: 59.754% (+0.02%) from 59.732%
#4827

push

travis-ci

web-flow
test: tidy up cases on main branch(10-27) (#33381)

121341 of 258518 branches covered (46.94%)

Branch coverage included in aggregate %.

193621 of 268583 relevant lines covered (72.09%)

4339204.91 hits per line

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

63.26
/source/libs/parser/src/parInsertStmt.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "geosWrapper.h"
17
#include "os.h"
18
#include "parInsertUtil.h"
19
#include "parInt.h"
20
#include "parToken.h"
21
#include "query.h"
22
#include "tdataformat.h"
23
#include "tglobal.h"
24
#include "ttime.h"
25
#include "ttypes.h"
26

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

34
int32_t qCloneCurrentTbData(STableDataCxt* pDataBlock, SSubmitTbData** pData) {
10,913✔
35
  if (pDataBlock == NULL || pDataBlock->pData == NULL) {
10,913!
36
    qError("stmt qCloneCurrentTbData input is null, maybe not bind data before execute");
×
37
    return TSDB_CODE_TSC_STMT_API_ERROR;
1✔
38
  }
39
  *pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
10,935!
40
  if (NULL == *pData) {
10,926!
41
    return terrno;
×
42
  }
43

44
  int8_t         flag = 1;
10,926✔
45
  SSubmitTbData* pNew = *pData;
10,926✔
46

47
  *pNew = *pDataBlock->pData;
10,926✔
48
  pNew->pBlobSet = NULL;
10,926✔
49

50
  int32_t code = cloneSVreateTbReq(pDataBlock->pData->pCreateTbReq, &pNew->pCreateTbReq);
10,926✔
51
  if (TSDB_CODE_SUCCESS != code) {
10,930!
52
    taosMemoryFreeClear(*pData);
×
53
    return code;
×
54
  }
55
  pNew->aCol = taosArrayDup(pDataBlock->pData->aCol, NULL);
10,930✔
56
  if (!pNew->aCol) {
10,930!
57
    code = terrno;
×
58
    taosMemoryFreeClear(*pData);
×
59
    return code;
×
60
  }
61

62
  int32_t colNum = taosArrayGetSize(pNew->aCol);
10,930✔
63
  for (int32_t i = 0; i < colNum; ++i) {
47,353✔
64
    if (pDataBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
36,427✔
65
      SColData* pCol = (SColData*)taosArrayGet(pNew->aCol, i);
36,425✔
66
      tColDataDeepClear(pCol);
36,426✔
67
    } else {
68
      pNew->aCol = taosArrayInit(20, POINTER_BYTES);
2✔
69
      if (pNew->aCol == NULL) {
2!
70
        code = terrno;
×
71
        taosMemoryFreeClear(*pData);
×
72
        return code;
×
73
      }
74
    }
75
  }
76

77
  return code;
10,926✔
78
}
79

80
int32_t qAppendStmtTableOutput(SQuery* pQuery, SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
5,626✔
81
                               SStbInterlaceInfo* pBuildInfo) {
82
  // merge according to vgId
83
  return insAppendStmtTableDataCxt(pAllVgHash, pTbData, pTbCtx, pBuildInfo);
5,626✔
84
}
85

86
int32_t qAppendStmt2TableOutput(SQuery* pQuery, SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
7,517✔
87
                                SStbInterlaceInfo* pBuildInfo, SVCreateTbReq* ctbReq) {
88
  // merge according to vgId
89
  return insAppendStmt2TableDataCxt(pAllVgHash, pTbData, pTbCtx, pBuildInfo, ctbReq);
7,517✔
90
}
91

92
int32_t qBuildStmtFinOutput(SQuery* pQuery, SHashObj* pAllVgHash, SArray* pVgDataBlocks) {
9,067✔
93
  int32_t             code = TSDB_CODE_SUCCESS;
9,067✔
94
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
9,067✔
95

96
  if (TSDB_CODE_SUCCESS == code) {
9,067!
97
    code = insBuildVgDataBlocks(pAllVgHash, pVgDataBlocks, &pStmt->pDataBlocks, true);
9,074✔
98
  }
99

100
  if (pStmt->freeArrayFunc) {
9,064!
101
    pStmt->freeArrayFunc(pVgDataBlocks);
9,071✔
102
  }
103
  return code;
9,073✔
104
}
105

106
/*
107
int32_t qBuildStmtOutputFromTbList(SQuery* pQuery, SHashObj* pVgHash, SArray* pBlockList, STableDataCxt* pTbCtx, int32_t
108
tbNum) { int32_t             code = TSDB_CODE_SUCCESS; SArray*             pVgDataBlocks = NULL; SVnodeModifyOpStmt*
109
pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
110

111
  // merge according to vgId
112
  if (tbNum > 0) {
113
    code = insMergeStmtTableDataCxt(pTbCtx, pBlockList, &pVgDataBlocks, true, tbNum);
114
  }
115

116
  if (TSDB_CODE_SUCCESS == code) {
117
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks);
118
  }
119

120
  if (pStmt->freeArrayFunc) {
121
    pStmt->freeArrayFunc(pVgDataBlocks);
122
  }
123
  return code;
124
}
125
*/
126

127
int32_t qBuildStmtOutput(SQuery* pQuery, SHashObj* pVgHash, SHashObj* pBlockHash) {
10,914✔
128
  int32_t             code = TSDB_CODE_SUCCESS;
10,914✔
129
  SArray*             pVgDataBlocks = NULL;
10,914✔
130
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
10,914✔
131

132
  // merge according to vgId
133
  if (taosHashGetSize(pBlockHash) > 0) {
10,914!
134
    code = insMergeTableDataCxt(pBlockHash, &pVgDataBlocks, true);
10,935✔
135
  }
136

137
  if (TSDB_CODE_SUCCESS == code) {
10,926✔
138
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks, false);
10,923✔
139
  }
140

141
  if (pStmt->freeArrayFunc) {
10,936✔
142
    pStmt->freeArrayFunc(pVgDataBlocks);
10,935✔
143
  }
144
  return code;
10,931✔
145
}
146

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

157
  SArray* pTagArray = taosArrayInit(tags->numOfBound, sizeof(STagVal));
11✔
158
  if (!pTagArray) {
11!
159
    return buildInvalidOperationMsg(&pBuf, "out of memory");
×
160
  }
161

162
  SArray* tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
11✔
163
  if (!tagName) {
11!
164
    code = buildInvalidOperationMsg(&pBuf, "out of memory");
×
165
    goto end;
×
166
  }
167

168
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
11✔
169

170
  bool  isJson = false;
11✔
171
  STag* pTag = NULL;
11✔
172

173
  for (int c = 0; c < tags->numOfBound; ++c) {
42✔
174
    if (bind[c].is_null && bind[c].is_null[0]) {
31!
175
      continue;
×
176
    }
177

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

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

267
  if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) {
11!
268
    goto end;
×
269
  }
270

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

286
  code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName,
11✔
287
                             pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
11✔
288
  pTag = NULL;
11✔
289

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

301
  return code;
11✔
302
}
303

304
int32_t convertStmtNcharCol(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_MULTI_BIND* src, TAOS_MULTI_BIND* dst,
24,722✔
305
                            void* charsetCxt) {
306
  int32_t output = 0;
24,722✔
307
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
24,722✔
308
  if (dst->buffer_length < newBuflen) {
24,722!
309
    dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
24,732!
310
    if (NULL == dst->buffer) {
24,800!
311
      return terrno;
×
312
    }
313
  }
314

315
  if (NULL == dst->length) {
24,790!
316
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
24,800!
317
    if (NULL == dst->length) {
24,756!
318
      taosMemoryFreeClear(dst->buffer);
×
319
      return terrno;
×
320
    }
321
  }
322

323
  dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
24,746✔
324

325
  for (int32_t i = 0; i < src->num; ++i) {
64,928✔
326
    if (src->is_null && src->is_null[i]) {
40,053✔
327
      continue;
462✔
328
    }
329

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

341
    dst->length[i] = output;
39,720✔
342
  }
343

344
  dst->buffer_type = src->buffer_type;
24,875✔
345
  dst->is_null = src->is_null;
24,875✔
346
  dst->num = src->num;
24,875✔
347

348
  return TSDB_CODE_SUCCESS;
24,875✔
349
}
350

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

364
  if (NULL == pTSchema || NULL == *pTSchema) {
5,620!
365
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
74✔
366
  }
367

368
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
39,356✔
369
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
33,733✔
370
    if (pColSchema->colId <= lastColId) {
33,733!
371
      colInOrder = false;
×
372
    } else {
373
      lastColId = pColSchema->colId;
33,733✔
374
    }
375
    // SColData* pCol = taosArrayGet(pCols, c);
376

377
    if (bind[c].num != rowNum) {
33,733!
378
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
379
      goto _return;
×
380
    }
381

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

388
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
33,733✔
389
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
168✔
390
      if (code) {
168!
391
        goto _return;
×
392
      }
393
      pBind = &ncharBind;
168✔
394
    } else {
395
      pBind = bind + c;
33,565✔
396
    }
397

398
    pBindInfos[c].columnId = pColSchema->colId;
33,733✔
399
    pBindInfos[c].bind = pBind;
33,733✔
400
    pBindInfos[c].type = pColSchema->type;
33,733✔
401
  }
402

403
  code = tRowBuildFromBind(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols, &pDataBlock->ordered,
5,623✔
404
                           &pDataBlock->duplicateTs);
405

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

408
_return:
5,623✔
409

410
  taosMemoryFree(ncharBind.buffer);
5,623!
411
  taosMemoryFree(ncharBind.length);
5,624!
412

413
  return code;
5,626✔
414
}
415

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

427
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
583,145✔
428
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
500,234✔
429
    SColData* pCol = taosArrayGet(pCols, c);
500,234✔
430

431
    if (bind[c].num != rowNum) {
498,906!
432
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
433
      goto _return;
×
434
    }
435

436
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
500,466!
437
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
499,936!
438
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
439
      goto _return;
×
440
    }
441

442
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
500,466✔
443
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
24,583✔
444
      if (code) {
24,678!
445
        goto _return;
×
446
      }
447
      pBind = &ncharBind;
24,678✔
448
    } else {
449
      pBind = bind + c;
475,883✔
450
    }
451

452
    int32_t bytes = 0;
500,561✔
453
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
500,561!
454
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
48,819!
455
        bytes = pColSchema->bytes - BLOBSTR_HEADER_SIZE;
781✔
456
      } else {
457
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
48,038✔
458
      }
459
    } else {
460
      bytes = -1;
451,742✔
461
    }
462
    code = tColDataAddValueByBind(pCol, pBind, bytes, initCtxAsText, checkWKB);
500,561✔
463
    if (code) {
503,779✔
464
      goto _return;
1✔
465
    }
466
  }
467

468
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
82,911!
469

470
_return:
82,911✔
471

472
  taosMemoryFree(ncharBind.buffer);
82,912!
473
  taosMemoryFree(ncharBind.length);
80,043!
474

475
  return code;
80,559✔
476
}
477

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

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

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

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

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

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

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

538
_return:
5✔
539

540
  taosMemoryFree(ncharBind.buffer);
5!
541
  taosMemoryFree(ncharBind.length);
5!
542

543
  return code;
5✔
544
}
545

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

558
  if (bind->num != rowNum) {
×
559
    return buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
560
  }
561

562
  // Column index exceeds the number of columns
563
  if (colIdx >= pCols->size && pCol == NULL) {
×
564
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
565
  }
566

567
  if (bind->buffer_type != pColSchema->type) {
×
568
    return buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
569
  }
570

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

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

587
_return:
×
588

589
  taosMemoryFree(ncharBind.buffer);
×
590
  taosMemoryFree(ncharBind.length);
×
591

592
  return code;
×
593
}
594

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

606
  SArray* pTagArray;
607
  if (tags->parseredTags) {
203✔
608
    pTagArray = taosArrayDup(tags->parseredTags->pTagVals, NULL);
24✔
609
  } else {
610
    pTagArray = taosArrayInit(tags->numOfBound, sizeof(STagVal));
179✔
611
  }
612
  if (!pTagArray) {
203!
613
    return buildInvalidOperationMsg(&pBuf, "out of memory");
×
614
  }
615

616
  SArray* tagName;
617
  if (tags->parseredTags) {
203✔
618
    tagName = taosArrayDup(tags->parseredTags->STagNames, NULL);
24✔
619
  } else {
620
    tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
179✔
621
  }
622

623
  if (!tagName) {
203!
624
    code = buildInvalidOperationMsg(&pBuf, "out of memory");
×
625
    goto end;
×
626
  }
627

628
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
203✔
629

630
  bool  isJson = false;
203✔
631
  STag* pTag = NULL;
203✔
632

633
  int bindIdx = 0;
203✔
634
  for (int c = 0; c < tags->numOfBound; ++c) {
778✔
635
    if (bind == NULL) {
597✔
636
      break;
21✔
637
    }
638
    if (tags->parseredTags) {
576✔
639
      bool found = false;
264✔
640
      for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
720✔
641
        if (tags->parseredTags->pTagIndex[k] == tags->pColIndex[c]) {
504✔
642
          found = true;
48✔
643
          break;
48✔
644
        }
645
      }
646
      if (found) {
264✔
647
        continue;
48✔
648
      }
649
    }
650

651
    TAOS_STMT2_BIND bindData = bind[bindIdx++];
528✔
652

653
    if (bindData.is_null && bindData.is_null[0]) {
528!
654
      continue;
×
655
    }
656

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

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

738
      } else {
739
        memcpy(&val.i64, bindData.buffer, colLen);
318✔
740
      }
741
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
527!
742
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
743
        goto end;
×
744
      }
745
      if (NULL == taosArrayPush(pTagArray, &val)) {
527!
746
        code = terrno;
×
747
        goto end;
×
748
      }
749
    }
750
  }
751

752
  if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) {
202!
753
    goto end;
×
754
  }
755

756
  if (pCreateTbReq) {
202✔
757
    code = insBuildCreateTbReq(pCreateTbReq, tName, pTag, suid, sTableName, tagName,
91✔
758
                               pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
91✔
759
    pTag = NULL;
91✔
760
    goto end;
91✔
761
  }
762

763
  if (NULL == pDataBlock->pData->pCreateTbReq) {
111✔
764
    pDataBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
10!
765
    if (NULL == pDataBlock->pData->pCreateTbReq) {
10!
766
      code = terrno;
×
767
      goto end;
×
768
    }
769
  } else {
770
    SVCreateTbReq* tmp = pDataBlock->pData->pCreateTbReq;
101✔
771
    taosMemoryFreeClear(tmp->name);
101!
772
    taosMemoryFreeClear(tmp->ctb.pTag);
101!
773
    taosMemoryFreeClear(tmp->ctb.stbName);
101!
774
    taosArrayDestroy(tmp->ctb.tagName);
101✔
775
    tmp->ctb.tagName = NULL;
101✔
776
  }
777

778
  code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName,
111✔
779
                             pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
111✔
780
  pTag = NULL;
111✔
781

782
end:
203✔
783
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
778✔
784
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
575✔
785
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
575✔
786
      taosMemoryFreeClear(p->pData);
42!
787
    }
788
  }
789
  taosArrayDestroy(pTagArray);
203✔
790
  taosArrayDestroy(tagName);
203✔
791
  taosMemoryFree(pTag);
203!
792

793
  return code;
203✔
794
}
795

796
static int32_t convertStmtStbNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst,
28✔
797
                                       void* charsetCxt) {
798
  int32_t       output = 0;
28✔
799
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
28✔
800

801
  dst->buffer = taosMemoryCalloc(src->num, max_buf_len);
28!
802
  if (NULL == dst->buffer) {
28!
803
    return terrno;
×
804
  }
805

806
  dst->length = taosMemoryCalloc(src->num, sizeof(int32_t));
28!
807
  if (NULL == dst->length) {
28!
808
    taosMemoryFreeClear(dst->buffer);
×
809
    return terrno;
×
810
  }
811

812
  char* src_buf = src->buffer;
28✔
813
  char* dst_buf = dst->buffer;
28✔
814
  for (int32_t i = 0; i < src->num; ++i) {
3,066✔
815
    if (src->is_null && src->is_null[i]) {
3,038✔
816
      continue;
1✔
817
    }
818

819
    if (src->length[i] == 0) {
3,037✔
820
      dst->length[i] = 0;
1✔
821
      continue;
1✔
822
    }
823

824
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
3,036!
825
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
826
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
827
      }
828
      char buf[512] = {0};
×
829
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
830
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
831
    }
832

833
    dst->length[i] = output;
3,036✔
834
    src_buf += src->length[i];
3,036✔
835
    dst_buf += output;
3,036✔
836
  }
837

838
  dst->buffer_type = src->buffer_type;
28✔
839
  dst->is_null = src->is_null;
28✔
840
  dst->num = src->num;
28✔
841

842
  return TSDB_CODE_SUCCESS;
28✔
843
}
844

845
int32_t qBindStmtStbColsValue2(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
7,506✔
846
                               int32_t msgBufLen, STSchema** pTSchema, SBindInfo2* pBindInfos, void* charsetCxt,
847
                               SBlobSet** ppBlob) {
848
  STableDataCxt*  pDataBlock = (STableDataCxt*)pBlock;
7,506✔
849
  SSchema*        pSchema = getTableColumnSchema(pDataBlock->pMeta);
7,506✔
850
  SSchemaExt*     pSchemaExt = getTableColumnExtSchema(pDataBlock->pMeta);
7,505✔
851
  SBoundColInfo*  boundInfo = &pDataBlock->boundColsInfo;
7,520✔
852
  SMsgBuf         pBuf = {.buf = msgBuf, .len = msgBufLen};
7,520✔
853
  int32_t         rowNum = bind->num;
7,520✔
854
  SArray*         ncharBinds = NULL;
7,520✔
855
  TAOS_STMT2_BIND ncharBind = {0};
7,520✔
856
  int32_t         code = 0;
7,520✔
857
  int16_t         lastColId = -1;
7,520✔
858
  bool            colInOrder = true;
7,520✔
859
  int             ncharColNums = 0;
7,520✔
860
  int32_t         bindIdx = 0;
7,520✔
861
  int8_t          hasBlob = 0;
7,520✔
862
  int32_t         lino = 0;
7,520✔
863
  if (NULL == pTSchema || NULL == *pTSchema) {
7,520✔
864
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
116✔
865
  }
866

867
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
118,601✔
868
    if (TSDB_DATA_TYPE_NCHAR == pSchema[boundInfo->pColIndex[c]].type) {
111,082✔
869
      ncharColNums++;
28✔
870
    }
871
  }
872
  if (ncharColNums > 0) {
7,519✔
873
    ncharBinds = taosArrayInit(ncharColNums, sizeof(ncharBind));
28✔
874
    if (!ncharBinds) {
28!
875
      code = terrno;
×
876
      TAOS_CHECK_GOTO(code, &lino, _return);
×
877
    }
878
  }
879

880
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
118,135✔
881
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
110,616✔
882
    if (pColSchema->colId <= lastColId) {
110,616✔
883
      colInOrder = false;
7✔
884
    } else {
885
      lastColId = pColSchema->colId;
110,609✔
886
    }
887

888
    if (parsedCols) {
110,616✔
889
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
144✔
890
      if (pParsedVal) {
144✔
891
        pBindInfos[c].columnId = pColSchema->colId;
30✔
892
        pBindInfos[c].type = pColSchema->type;
30✔
893
        pBindInfos[c].bytes = pColSchema->bytes;
30✔
894
        continue;
30✔
895
      }
896
    }
897

898
    TAOS_STMT2_BIND bindData = bind[bindIdx];
110,586✔
899

900
    if (bindData.num != rowNum) {
110,586!
901
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
902
      goto _return;
×
903
    }
904

905
    if ((!(rowNum == 1 && bindData.is_null && *bindData.is_null)) &&
110,586!
906
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
110,590!
907
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
908
      goto _return;
×
909
    }
910

911
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
110,586✔
912
      code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
28✔
913
      TAOS_CHECK_GOTO(code, &lino, _return);
28!
914

915
      if (!taosArrayPush(ncharBinds, &ncharBind)) {
28!
916
        code = terrno;
×
917
        goto _return;
×
918
      }
919
      pBindInfos[c].bind = taosArrayGetLast(ncharBinds);
28✔
920
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
110,558✔
921
      code = initCtxAsText();
13✔
922
      if (code) {
13!
923
        parserError("stmt2 bind geometry init failed, ErrCode: 0x%x", code);
×
924
        goto _return;
×
925
      }
926
      uint8_t* buf = bindData.buffer;
13✔
927
      for (int j = 0; j < bindData.num; j++) {
29✔
928
        if (bindData.is_null && bindData.is_null[j]) {
16✔
929
          continue;
1✔
930
        }
931
        code = checkWKB(buf, bindData.length[j]);
15✔
932
        if (code) {
15!
933
          parserError("stmt2 interlace mode geometry data col:%d, row:%d must be in WKB format", c, j);
×
934
          goto _return;
×
935
        }
936
        buf += bindData.length[j];
15✔
937
      }
938
      pBindInfos[c].bind = bind + bindIdx;
13✔
939
    } else {
940
      if (IS_STR_DATA_BLOB(pColSchema->type)) hasBlob = 1;
110,545!
941
      pBindInfos[c].bind = bind + bindIdx;
110,545✔
942
    }
943

944
    pBindInfos[c].columnId = pColSchema->colId;
110,586✔
945
    pBindInfos[c].type = pColSchema->type;
110,586✔
946
    pBindInfos[c].bytes = pColSchema->bytes;
110,586✔
947

948
    bindIdx++;
110,586✔
949
  }
950

951
  if (hasBlob == 0) {
7,519✔
952
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pSchemaExt, pCols,
7,516✔
953
                              &pDataBlock->ordered, &pDataBlock->duplicateTs);
954
    TAOS_CHECK_GOTO(code, &lino, _return);
7,518✔
955
  } else {
956
    code = tBlobSetCreate(1024, 1, ppBlob);
3✔
957
    TAOS_CHECK_GOTO(code, &lino, _return);
×
958

959
    code = tRowBuildFromBind2WithBlob(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols,
×
960
                                      &pDataBlock->ordered, &pDataBlock->duplicateTs, *ppBlob);
961
    TAOS_CHECK_GOTO(code, &lino, _return);
×
962
  }
963

964
  parserDebug("stmt2 all %d columns bind %d rows data as row format", boundInfo->numOfBound, rowNum);
7,516!
965

966
_return:
7,516✔
967
  if (ncharBinds) {
7,518✔
968
    for (int i = 0; i < TARRAY_SIZE(ncharBinds); ++i) {
56✔
969
      TAOS_STMT2_BIND* ncBind = TARRAY_DATA(ncharBinds);
28✔
970
      taosMemoryFree(ncBind[i].buffer);
28!
971
      taosMemoryFree(ncBind[i].length);
28!
972
    }
973
    taosArrayDestroy(ncharBinds);
28✔
974
  }
975
  if (code != 0) {
7,518✔
976
    parserError("stmt2 failed to bind stb col at lino %d since %s", lino, tstrerror(code));
2!
977
  }
978

979
  return code;
7,511✔
980
}
981

982
static int32_t convertStmtNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst,
22✔
983
                                    void* charsetCxt) {
984
  int32_t       output = 0;
22✔
985
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
22✔
986

987
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
22✔
988
  // if (dst->buffer_length < newBuflen) {
989
  dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
22!
990
  if (NULL == dst->buffer) {
22!
991
    return terrno;
×
992
  }
993
  //}
994

995
  if (NULL == dst->length) {
22✔
996
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
17!
997
    if (NULL == dst->length) {
17!
998
      taosMemoryFreeClear(dst->buffer);
×
999
      return terrno;
×
1000
    }
1001
  }
1002

1003
  // dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
1004
  char* src_buf = src->buffer;
22✔
1005
  char* dst_buf = dst->buffer;
22✔
1006
  for (int32_t i = 0; i < src->num; ++i) {
99✔
1007
    if (src->is_null && src->is_null[i]) {
77✔
1008
      continue;
1✔
1009
    }
1010

1011
    if (src->length[i] == 0) {
76✔
1012
      dst->length[i] = 0;
11✔
1013
      continue;
11✔
1014
    }
1015

1016
    /*if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
1017
      (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output)) {*/
1018
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
65!
1019
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
1020
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
1021
      }
1022
      char buf[512] = {0};
×
1023
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
1024
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
1025
    }
1026

1027
    dst->length[i] = output;
65✔
1028
    src_buf += src->length[i];
65✔
1029
    dst_buf += output;
65✔
1030
  }
1031

1032
  dst->buffer_type = src->buffer_type;
22✔
1033
  dst->is_null = src->is_null;
22✔
1034
  dst->num = src->num;
22✔
1035

1036
  return TSDB_CODE_SUCCESS;
22✔
1037
}
1038

1039
int32_t qBindStmtColsValue2(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
132✔
1040
                            int32_t msgBufLen, void* charsetCxt) {
1041
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
132✔
1042
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
132✔
1043
  SSchemaExt*      pExtSchema = getTableColumnExtSchema(pDataBlock->pMeta);
132✔
1044
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
132✔
1045
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
132✔
1046
  int32_t          rowNum = bind->num;
132✔
1047
  TAOS_STMT2_BIND  ncharBind = {0};
132✔
1048
  TAOS_STMT2_BIND* pBind = NULL;
132✔
1049
  int32_t          code = 0;
132✔
1050
  int32_t          lino = 0;
132✔
1051
  int32_t          bindIdx = 0;
132✔
1052

1053
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
516✔
1054
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
384✔
1055
    SColData* pCol = taosArrayGet(pCols, c);
384✔
1056
    if (pCol == NULL || pColSchema == NULL) {
384!
1057
      code = buildInvalidOperationMsg(&pBuf, "get column schema or column data failed");
×
1058
      goto _return;
×
1059
    }
1060

1061
    if (boundInfo->pColIndex[c] == 0) {
384✔
1062
      pCol->cflag |= COL_IS_KEY;
132✔
1063
    }
1064

1065
    if (parsedCols) {
384✔
1066
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
132✔
1067
      if (pParsedVal) {
132✔
1068
        for (int row = 0; row < rowNum; row++) {
48✔
1069
          code = tColDataAppendValue(pCol, pParsedVal);
24✔
1070
          if (code) {
24!
1071
            parserError("stmt2 failed to add fixed value col:%d ,type:%d, ErrCode: 0x%x", c, pColSchema->type, code);
×
1072
            goto _return;
×
1073
          }
1074
        }
1075
        continue;
24✔
1076
      }
1077
    }
1078
    TAOS_STMT2_BIND bindData = bind[bindIdx];
360✔
1079
    if (bindData.num != rowNum) {
360!
1080
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1081
      goto _return;
×
1082
    }
1083

1084
    if ((!(rowNum == 1 && bindData.is_null && *bindData.is_null)) &&
360!
1085
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
358!
1086
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
1087
      goto _return;
×
1088
    }
1089

1090
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
360✔
1091
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
20✔
1092
      if (code) {
20!
1093
        goto _return;
×
1094
      }
1095
      pBind = &ncharBind;
20✔
1096
    } else {
1097
      pBind = bind + bindIdx;
340✔
1098
    }
1099
    int8_t  isBlob = 0;
360✔
1100
    int32_t bytes = -1;
360✔
1101
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
360!
1102
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
141!
1103
        isBlob = 1;
×
1104
        bytes = TSDB_MAX_BLOB_LEN;
×
1105
      } else {
1106
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
141✔
1107
      }
1108
    }
1109

1110
    if (pColSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
360✔
1111
      code = tColDataAddValueByBind2WithGeos(pCol, pBind, bytes, initCtxAsText, checkWKB);
15✔
1112
    } else if (isBlob == 1) {
345!
1113
      if (pDataBlock->pData->pBlobSet == NULL) {
×
1114
        code = tBlobSetCreate(1024, 1, &pDataBlock->pData->pBlobSet);
×
1115
        if (code) {
×
1116
          parserError("stmt2 failed to create blob set, ErrCode: 0x%x", code);
×
1117
          goto _return;
×
1118
        }
1119
      }
1120
      code = tColDataAddValueByBind2WithBlob(pCol, pBind, bytes, pDataBlock->pData->pBlobSet);
×
1121
    } else if (IS_DECIMAL_TYPE(pColSchema->type)) {
353✔
1122
      if (pExtSchema == NULL) {
8!
1123
        code = buildInvalidOperationMsg(&pBuf, "decimal column ext schema is null");
×
1124
        goto _return;
×
1125
      }
1126
      uint8_t precision = 0, scale = 0;
8✔
1127
      decimalFromTypeMod(pExtSchema[c].typeMod, &precision, &scale);
8✔
1128
      code = tColDataAddValueByBind2WithDecimal(pCol, pBind, bytes, precision, scale);
8✔
1129
    } else {
1130
      code = tColDataAddValueByBind2(pCol, pBind, bytes);
337✔
1131
    }
1132

1133
    if (code) {
360!
1134
      parserError("stmt2 failed to add col:%d ,type:%d, ErrCode: 0x%x", c, pColSchema->type, code);
×
1135
      goto _return;
×
1136
    }
1137
    bindIdx++;
360✔
1138
  }
1139

1140
  parserDebug("stmt2 all %d columns bind %d rows data as col format", boundInfo->numOfBound, rowNum);
132!
1141

1142
_return:
132✔
1143

1144
  taosMemoryFree(ncharBind.buffer);
132!
1145
  taosMemoryFree(ncharBind.length);
132!
1146

1147
  return code;
132✔
1148
}
1149

1150
int32_t qBindStmtSingleColValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen,
6✔
1151
                                 int32_t colIdx, int32_t rowNum, void* charsetCxt) {
1152
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
6✔
1153
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
6✔
1154
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
6✔
1155
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
6✔
1156
  SSchema*         pColSchema = &pSchema[boundInfo->pColIndex[colIdx]];
6✔
1157
  SColData*        pCol = taosArrayGet(pCols, colIdx);
6✔
1158
  TAOS_STMT2_BIND  ncharBind = {0};
6✔
1159
  TAOS_STMT2_BIND* pBind = NULL;
6✔
1160
  int32_t          code = 0;
6✔
1161
  int32_t          lino = 0;
6✔
1162

1163
  if (bind->num != rowNum) {
6!
1164
    return buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1165
  }
1166

1167
  // Column index exceeds the number of columns
1168
  if (colIdx >= pCols->size && pCol == NULL) {
6!
1169
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
1170
  }
1171

1172
  if (bind->buffer_type != pColSchema->type) {
6!
1173
    return buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
1174
  }
1175

1176
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
6!
1177
    code = convertStmtNcharCol2(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
×
1178
    if (code) {
×
1179
      goto _return;
×
1180
    }
1181
    pBind = &ncharBind;
×
1182
  } else {
1183
    pBind = bind;
6✔
1184
  }
1185

1186
  int32_t bytes = -1;
6✔
1187
  int8_t  hasBlob = 0;
6✔
1188
  if (IS_VAR_DATA_TYPE(pColSchema->type)) {
6!
1189
    if (IS_STR_DATA_BLOB(pColSchema->type)) {
3!
1190
      bytes = TSDB_MAX_BLOB_LEN;
×
1191
      hasBlob = 1;
×
1192
    } else {
1193
      bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
3✔
1194
    }
1195
  } else {
1196
    bytes = -1;
3✔
1197
  }
1198

1199
  if (hasBlob) {
6!
1200
    if (pDataBlock->pData->pBlobSet == NULL) {
×
1201
      code = tBlobSetCreate(1024, 1, &pDataBlock->pData->pBlobSet);
×
1202
      if (code) {
×
1203
        parserError("stmt2 failed to create blob set, ErrCode: 0x%x", code);
×
1204
        goto _return;
×
1205
      }
1206
    }
1207
    code = tColDataAddValueByBind2WithBlob(pCol, pBind, bytes, pDataBlock->pData->pBlobSet);
×
1208
  } else if (pColSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
6!
1209
    code = tColDataAddValueByBind2WithGeos(pCol, pBind, bytes, initCtxAsText, checkWKB);
×
1210
  } else if (IS_DECIMAL_TYPE(pColSchema->type)) {
6!
1211
    SSchemaExt* pExtSchema = getTableColumnExtSchema(pDataBlock->pMeta);
×
1212
    if (pExtSchema == NULL) {
×
1213
      code = buildInvalidOperationMsg(&pBuf, "decimal column ext schema is null");
×
1214
      goto _return;
×
1215
    }
1216
    uint8_t precision = 0, scale = 0;
×
1217
    decimalFromTypeMod(pExtSchema->typeMod, &precision, &scale);
×
1218
    code = tColDataAddValueByBind2WithDecimal(pCol, pBind, bytes, precision, scale);
×
1219
  } else {
1220
    code = tColDataAddValueByBind2(pCol, pBind, bytes);
6✔
1221
  }
1222

1223
  if (code) {
6!
1224
    parserError("stmt2 failed to add single col:%d ,type:%d, ErrCode: 0x%x", colIdx, pColSchema->type, code);
×
1225
    goto _return;
×
1226
  }
1227

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

1230
_return:
6✔
1231

1232
  taosMemoryFree(ncharBind.buffer);
6!
1233
  taosMemoryFree(ncharBind.length);
6!
1234

1235
  return code;
6✔
1236
}
1237

1238
int32_t qBindStmt2RowValue(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
2✔
1239
                           int32_t msgBufLen, STSchema** pTSchema, SBindInfo2* pBindInfos, void* charsetCxt) {
1240
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
2✔
1241
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
2✔
1242
  SSchemaExt*      pSchemaExt = getTableColumnExtSchema(pDataBlock->pMeta);
2✔
1243
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
2✔
1244
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
2✔
1245
  int32_t          rowNum = bind->num;
2✔
1246
  TAOS_STMT2_BIND  ncharBind = {0};
2✔
1247
  TAOS_STMT2_BIND* pBind = NULL;
2✔
1248
  int32_t          code = 0;
2✔
1249
  int16_t          lastColId = -1;
2✔
1250
  bool             colInOrder = true;
2✔
1251
  int8_t           hasBlob = 0;
2✔
1252

1253
  if (NULL == pTSchema || NULL == *pTSchema) {
2!
1254
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
1✔
1255
  }
1256

1257
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
24✔
1258
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
22✔
1259
    if (pColSchema->colId <= lastColId) {
22!
1260
      colInOrder = false;
×
1261
    } else {
1262
      lastColId = pColSchema->colId;
22✔
1263
    }
1264

1265
    if (bind[c].num != rowNum) {
22!
1266
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1267
      goto _return;
×
1268
    }
1269

1270
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
22!
1271
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
22!
1272
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
1273
      goto _return;
×
1274
    }
1275

1276
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
22✔
1277
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
2✔
1278
      if (code) {
2!
1279
        goto _return;
×
1280
      }
1281
      pBindInfos[c].bind = &ncharBind;
2✔
1282
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
20✔
1283
      code = initCtxAsText();
2✔
1284
      if (code) {
2!
1285
        qError("geometry init failed:%s", tstrerror(code));
×
1286
        goto _return;
×
1287
      }
1288
      uint8_t* buf = bind[c].buffer;
2✔
1289
      for (int j = 0; j < bind[c].num; j++) {
4✔
1290
        if (bind[c].is_null && bind[c].is_null[j]) {
2!
1291
          continue;
×
1292
        }
1293
        code = checkWKB(buf, bind[c].length[j]);
2✔
1294
        if (code) {
2!
1295
          qError("stmt2 row bind geometry data[%d]:{%s},length:%d must be in WKB format", c, buf, bind[c].length[j]);
×
1296
          goto _return;
×
1297
        }
1298
        buf += bind[c].length[j];
2✔
1299
      }
1300
      pBindInfos[c].bind = bind + c;
2✔
1301
    } else {
1302
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
18!
1303
        hasBlob = 1;
×
1304
      }
1305
      pBindInfos[c].bind = bind + c;
18✔
1306
    }
1307

1308
    pBindInfos[c].columnId = pColSchema->colId;
22✔
1309
    pBindInfos[c].type = pColSchema->type;
22✔
1310
    pBindInfos[c].bytes = pColSchema->bytes;
22✔
1311

1312
    if (code) {
22!
1313
      goto _return;
×
1314
    }
1315
  }
1316

1317
  pDataBlock->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
2✔
1318
  if (pDataBlock->pData->pCreateTbReq != NULL) {
2!
1319
    pDataBlock->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
2✔
1320
  }
1321

1322
  if (hasBlob == 0) {
2!
1323
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pSchemaExt, pCols,
2✔
1324
                              &pDataBlock->ordered, &pDataBlock->duplicateTs);
1325
  } else {
1326
    code = TSDB_CODE_BLOB_NOT_SUPPORT;
×
1327
  }
1328
  qDebug("stmt2 all %d columns bind %d rows data as row format", boundInfo->numOfBound, rowNum);
2!
1329

1330
_return:
2✔
1331

1332
  taosMemoryFree(ncharBind.buffer);
2!
1333
  taosMemoryFree(ncharBind.length);
2!
1334

1335
  return code;
2✔
1336
}
1337

1338
int32_t buildBoundFields(int32_t numOfBound, int16_t* boundColumns, SSchema* pSchema, int32_t* fieldNum,
7✔
1339
                         TAOS_FIELD_E** fields, uint8_t timePrec) {
1340
  if (fields != NULL) {
7✔
1341
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
6!
1342
    if (NULL == *fields) {
6!
1343
      return terrno;
×
1344
    }
1345

1346
    SSchema* schema = &pSchema[boundColumns[0]];
6✔
1347
    if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
6✔
1348
      (*fields)[0].precision = timePrec;
5✔
1349
    }
1350

1351
    for (int32_t i = 0; i < numOfBound; ++i) {
28✔
1352
      schema = &pSchema[boundColumns[i]];
22✔
1353
      tstrncpy((*fields)[i].name, schema->name, 65);
22✔
1354
      (*fields)[i].type = schema->type;
22✔
1355
      (*fields)[i].bytes = calcTypeBytesFromSchemaBytes(schema->type, schema->bytes, true);
22✔
1356
    }
1357
  }
1358

1359
  *fieldNum = numOfBound;
7✔
1360

1361
  return TSDB_CODE_SUCCESS;
7✔
1362
}
1363

1364
int32_t buildStbBoundFields(SBoundColInfo boundColsInfo, SSchema* pSchema, int32_t* fieldNum, TAOS_FIELD_ALL** fields,
52✔
1365
                            STableMeta* pMeta, void* boundTags, SSHashObj* parsedCols, uint8_t tbNameFlag) {
1366
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
52✔
1367
  bool           hastag = (tags != NULL) && !(tbNameFlag & IS_FIXED_TAG);
52!
1368
  bool           hasPreBindTbname =
52✔
1369
      (tbNameFlag & IS_FIXED_VALUE) == 0 && ((tbNameFlag & USING_CLAUSE) != 0 || pMeta->tableType == TSDB_NORMAL_TABLE);
52!
1370
  int32_t numOfBound = boundColsInfo.numOfBound + (hasPreBindTbname ? 1 : 0);
52✔
1371
  if (hastag) {
52✔
1372
    numOfBound += tags->mixTagsCols ? 0 : tags->numOfBound;
34✔
1373
  }
1374

1375
  // Adjust the number of bound fields if there are parsed tags or parsed columns
1376
  if (tags->parseredTags) {
52✔
1377
    numOfBound -= tags->parseredTags->numOfTags;
2✔
1378
  }
1379
  if (parsedCols) {
52✔
1380
    numOfBound -= tSimpleHashGetSize(parsedCols);
4✔
1381
  }
1382

1383
  int32_t idx = 0;
52✔
1384
  if (fields != NULL) {
52!
1385
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_ALL));
52!
1386
    if (NULL == *fields) {
52!
1387
      return terrno;
×
1388
    }
1389

1390
    if (hasPreBindTbname) {
52✔
1391
      (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
32✔
1392
      tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
32✔
1393
      (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
32✔
1394
      (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
32✔
1395
      idx++;
32✔
1396
    }
1397

1398
    if (hastag && tags->numOfBound > 0 && !tags->mixTagsCols) {
52✔
1399
      SSchema* tagSchema = getTableTagSchema(pMeta);
15✔
1400

1401
      for (int32_t i = 0; i < tags->numOfBound; ++i) {
56✔
1402
        SSchema* schema = &tagSchema[tags->pColIndex[i]];
41✔
1403

1404
        if (tags->parseredTags && tags->parseredTags->numOfTags > 0) {
41!
1405
          int32_t tag_idx = schema->colId - 1 - pMeta->tableInfo.numOfColumns;
2✔
1406
          bool    found = false;
2✔
1407
          for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
3✔
1408
            if (tags->parseredTags->pTagIndex[k] == tag_idx) {
2✔
1409
              found = true;
1✔
1410
              break;
1✔
1411
            }
1412
          }
1413
          if (found) {
2✔
1414
            continue;
1✔
1415
          }
1416
        }
1417

1418
        (*fields)[idx].field_type = TAOS_FIELD_TAG;
40✔
1419

1420
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[i].name));
40✔
1421
        (*fields)[idx].type = schema->type;
40✔
1422
        (*fields)[idx].bytes = schema->bytes;
40✔
1423
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
40✔
1424
          (*fields)[idx].precision = pMeta->tableInfo.precision;
1✔
1425
        }
1426
        idx++;
40✔
1427
      }
1428
    }
1429

1430
    if (boundColsInfo.numOfBound > 0) {
52!
1431
      SSchema* schema = &pSchema[boundColsInfo.pColIndex[0]];
52✔
1432

1433
      for (int32_t i = 0; i < boundColsInfo.numOfBound; ++i) {
239✔
1434
        int16_t idxCol = boundColsInfo.pColIndex[i];
187✔
1435

1436
        if (idxCol == pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags) {
187✔
1437
          (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
11✔
1438
          tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
11✔
1439
          (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
11✔
1440
          (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
11✔
1441

1442
          idx++;
11✔
1443
          continue;
11✔
1444
        } else if (idxCol < pMeta->tableInfo.numOfColumns) {
176✔
1445
          if (parsedCols && tSimpleHashGet(parsedCols, &pSchema[idxCol].colId, sizeof(int16_t))) {
144✔
1446
            continue;
5✔
1447
          }
1448
          (*fields)[idx].field_type = TAOS_FIELD_COL;
139✔
1449
        } else {
1450
          if (tags->parseredTags && tags->parseredTags->numOfTags > 0) {
32!
1451
            int32_t tag_idx = idxCol - pMeta->tableInfo.numOfColumns;
2✔
1452
            bool    found = false;
2✔
1453
            for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
3✔
1454
              if (tags->parseredTags->pTagIndex[k] == tag_idx) {
2✔
1455
                found = true;
1✔
1456
                break;
1✔
1457
              }
1458
            }
1459
            if (found) {
2✔
1460
              continue;
1✔
1461
            }
1462
          }
1463
          (*fields)[idx].field_type = TAOS_FIELD_TAG;
31✔
1464
        }
1465

1466
        schema = &pSchema[idxCol];
170✔
1467
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[idx].name));
170✔
1468
        (*fields)[idx].type = schema->type;
170✔
1469
        (*fields)[idx].bytes = schema->bytes;
170✔
1470
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
170✔
1471
          (*fields)[idx].precision = pMeta->tableInfo.precision;
51✔
1472
        }
1473
        if (TSDB_DATA_TYPE_DECIMAL64 == schema->type || TSDB_DATA_TYPE_DECIMAL == schema->type) {
170✔
1474
          decimalFromTypeMod(pMeta->schemaExt[idxCol].typeMod, &(*fields)[idx].precision, &(*fields)[idx].scale);
6✔
1475
        }
1476
        idx++;
170✔
1477
      }
1478
    }
1479
  }
1480

1481
  *fieldNum = numOfBound;
52✔
1482

1483
  return TSDB_CODE_SUCCESS;
52✔
1484
}
1485

1486
int32_t qBuildStmtTagFields(void* pBlock, void* boundTags, int32_t* fieldNum, TAOS_FIELD_E** fields) {
1✔
1487
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
1✔
1488
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
1✔
1489
  if (NULL == tags) {
1!
1490
    return TSDB_CODE_APP_ERROR;
×
1491
  }
1492

1493
  if (pDataBlock->pMeta->tableType != TSDB_SUPER_TABLE && pDataBlock->pMeta->tableType != TSDB_CHILD_TABLE) {
1!
1494
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1495
  }
1496

1497
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
1✔
1498
  if (tags->numOfBound <= 0) {
1!
1499
    *fieldNum = 0;
×
1500
    *fields = NULL;
×
1501

1502
    return TSDB_CODE_SUCCESS;
×
1503
  }
1504

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

1507
  return TSDB_CODE_SUCCESS;
1✔
1508
}
1509

1510
int32_t qBuildStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields) {
6✔
1511
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
6✔
1512
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
6✔
1513
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
6!
1514
    *fieldNum = 0;
×
1515
    if (fields != NULL) {
×
1516
      *fields = NULL;
×
1517
    }
1518

1519
    return TSDB_CODE_SUCCESS;
×
1520
  }
1521

1522
  CHECK_CODE(buildBoundFields(pDataBlock->boundColsInfo.numOfBound, pDataBlock->boundColsInfo.pColIndex, pSchema,
6!
1523
                              fieldNum, fields, pDataBlock->pMeta->tableInfo.precision));
1524

1525
  return TSDB_CODE_SUCCESS;
6✔
1526
}
1527

1528
int32_t qBuildUpdateStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields, SSHashObj* parsedCols) {
5✔
1529
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
5✔
1530
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
5✔
1531
  int32_t        numOfBound = pDataBlock->boundColsInfo.numOfBound + tSimpleHashGetSize(parsedCols);
5✔
1532
  if (numOfBound <= 0) {
5!
1533
    *fieldNum = 0;
×
1534
    if (fields != NULL) {
×
1535
      *fields = NULL;
×
1536
    }
1537

1538
    return TSDB_CODE_SUCCESS;
×
1539
  }
1540

1541
  if (fields != NULL) {
5!
1542
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
5!
1543
    if (NULL == *fields) {
5!
1544
      return terrno;
×
1545
    }
1546

1547
    int32_t actualIdx = 0;
5✔
1548
    for (int32_t i = 0; i < numOfBound; ++i) {
29✔
1549
      SSchema* schema;
1550
      int32_t* idx = (int32_t*)tSimpleHashGet(parsedCols, &i, sizeof(int32_t));
24✔
1551
      if (idx) {
24✔
1552
        schema = &pSchema[*idx];
4✔
1553
      } else {
1554
        schema = &pSchema[pDataBlock->boundColsInfo.pColIndex[actualIdx++]];
20✔
1555
      }
1556
      tstrncpy((*fields)[i].name, schema->name, 65);
24✔
1557
      (*fields)[i].type = schema->type;
24✔
1558
      (*fields)[i].bytes = calcTypeBytesFromSchemaBytes(schema->type, schema->bytes, true);
24✔
1559
      if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
24✔
1560
        (*fields)[i].precision = pDataBlock->pMeta->tableInfo.precision;
5✔
1561
      }
1562
    }
1563
  }
1564

1565
  *fieldNum = numOfBound;
5✔
1566

1567
  return TSDB_CODE_SUCCESS;
5✔
1568
}
1569

1570
int32_t qBuildStmtStbColFields(void* pBlock, void* boundTags, SSHashObj* parsedCols, uint8_t tbNameFlag,
52✔
1571
                               int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
1572
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
52✔
1573
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
52✔
1574
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
52!
1575
    *fieldNum = 0;
×
1576
    if (fields != NULL) {
×
1577
      *fields = NULL;
×
1578
    }
1579

1580
    return TSDB_CODE_SUCCESS;
×
1581
  }
1582

1583
  CHECK_CODE(buildStbBoundFields(pDataBlock->boundColsInfo, pSchema, fieldNum, fields, pDataBlock->pMeta, boundTags,
52!
1584
                                 parsedCols, tbNameFlag));
1585

1586
  return TSDB_CODE_SUCCESS;
52✔
1587
}
1588

1589
int32_t qResetStmtColumns(SArray* pCols, bool deepClear) {
×
1590
  int32_t colNum = taosArrayGetSize(pCols);
×
1591

1592
  for (int32_t i = 0; i < colNum; ++i) {
×
1593
    SColData* pCol = (SColData*)taosArrayGet(pCols, i);
×
1594
    if (pCol == NULL) {
×
1595
      parserError("qResetStmtColumns column:%d is NULL", i);
×
1596
      return terrno;
×
1597
    }
1598
    if (deepClear) {
×
1599
      tColDataDeepClear(pCol);
×
1600
    } else {
1601
      tColDataClear(pCol);
×
1602
    }
1603
  }
1604

1605
  return TSDB_CODE_SUCCESS;
×
1606
}
1607

1608
int32_t qResetStmtDataBlock(STableDataCxt* block, bool deepClear) {
21,158✔
1609
  int32_t        code = 0;
21,158✔
1610
  STableDataCxt* pBlock = (STableDataCxt*)block;
21,158✔
1611
  int32_t        colNum = taosArrayGetSize(pBlock->pData->aCol);
21,158✔
1612

1613
  int8_t flag = 0;
21,156✔
1614
  for (int32_t i = 0; i < colNum; ++i) {
89,664✔
1615
    flag = pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
68,512✔
1616
    if (pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
68,512✔
1617
      SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i);
68,511✔
1618
      if (pCol == NULL) {
68,504!
1619
        parserError("qResetStmtDataBlock column:%d is NULL", i);
×
1620
        return terrno;
×
1621
      }
1622
      if (deepClear) {
68,520✔
1623
        tColDataDeepClear(pCol);
32,123✔
1624

1625
      } else {
1626
        tColDataClear(pCol);
36,397✔
1627
      }
1628

1629
    } else {
1630
      pBlock->pData->aRowP = taosArrayInit(20, POINTER_BYTES);
1✔
1631
    }
1632
  }
1633

1634
  tBlobSetDestroy(pBlock->pData->pBlobSet);
21,152✔
1635
  pBlock->pData->pBlobSet = NULL;
21,145✔
1636
  return code;
21,145✔
1637
}
1638

1639
int32_t qCloneStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, bool reset) {
10,298✔
1640
  int32_t code = 0;
10,298✔
1641

1642
  *pDst = taosMemoryCalloc(1, sizeof(STableDataCxt));
10,298!
1643
  if (NULL == *pDst) {
10,309!
1644
    return terrno;
×
1645
  }
1646

1647
  STableDataCxt* pNewCxt = (STableDataCxt*)*pDst;
10,309✔
1648
  STableDataCxt* pCxt = (STableDataCxt*)pSrc;
10,309✔
1649
  pNewCxt->hasBlob = pSrc->hasBlob;
10,309✔
1650
  pNewCxt->pSchema = NULL;
10,309✔
1651
  pNewCxt->pValues = NULL;
10,309✔
1652

1653
  if (pCxt->pMeta) {
10,309!
1654
    pNewCxt->pMeta = tableMetaDup(pCxt->pMeta);
10,311✔
1655
    // void* pNewMeta = taosMemoryMalloc(TABLE_META_SIZE(pCxt->pMeta));
1656
    // if (NULL == pNewMeta) {
1657
    //   insDestroyTableDataCxt(*pDst);
1658
    //   return terrno;
1659
    // }
1660
    // memcpy(pNewMeta, pCxt->pMeta, TABLE_META_SIZE(pCxt->pMeta));
1661
    // pNewCxt->pMeta = pNewMeta;
1662
  }
1663

1664
  memcpy(&pNewCxt->boundColsInfo, &pCxt->boundColsInfo, sizeof(pCxt->boundColsInfo));
10,318✔
1665
  pNewCxt->boundColsInfo.pColIndex = NULL;
10,318✔
1666

1667
  if (pCxt->boundColsInfo.pColIndex) {
10,318!
1668
    void* pNewColIdx = taosMemoryMalloc(pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
10,320!
1669
    if (NULL == pNewColIdx) {
10,320!
1670
      insDestroyTableDataCxt(*pDst);
×
1671
      return terrno;
×
1672
    }
1673
    memcpy(pNewColIdx, pCxt->boundColsInfo.pColIndex,
10,320✔
1674
           pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
10,320✔
1675
    pNewCxt->boundColsInfo.pColIndex = pNewColIdx;
10,320✔
1676
  }
1677

1678
  if (pCxt->pData) {
10,318✔
1679
    int8_t         flag = 1;
10,316✔
1680
    SSubmitTbData* pNewTb = (SSubmitTbData*)taosMemoryMalloc(sizeof(SSubmitTbData));
10,316!
1681
    if (NULL == pNewTb) {
10,311!
1682
      insDestroyTableDataCxt(*pDst);
×
1683
      return terrno;
×
1684
    }
1685

1686
    memcpy(pNewTb, pCxt->pData, sizeof(*pCxt->pData));
10,311✔
1687
    pNewTb->pCreateTbReq = NULL;
10,311✔
1688
    if (pNewTb->pBlobSet != NULL) {
10,311!
1689
      flag = pNewTb->pBlobSet->type;
×
1690
    }
1691
    pNewTb->pBlobSet = NULL;
10,311✔
1692

1693
    pNewTb->aCol = taosArrayDup(pCxt->pData->aCol, NULL);
10,311✔
1694
    if (NULL == pNewTb->aCol) {
10,320!
1695
      insDestroyTableDataCxt(*pDst);
×
1696
      return terrno;
×
1697
    }
1698

1699
    pNewCxt->pData = pNewTb;
10,320✔
1700

1701
    if (reset) {
10,320✔
1702
      code = qResetStmtDataBlock(*pDst, true);
10,230✔
1703
    }
1704
  }
1705

1706
  return code;
10,312✔
1707
}
1708

1709
int32_t qRebuildStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, uint64_t uid, uint64_t suid, int32_t vgId,
87✔
1710
                              bool rebuildCreateTb) {
1711
  int32_t code = qCloneStmtDataBlock(pDst, pSrc, false);
87✔
1712
  if (code) {
87!
1713
    return code;
×
1714
  }
1715

1716
  STableDataCxt* pBlock = (STableDataCxt*)*pDst;
87✔
1717
  if (pBlock->pMeta) {
87!
1718
    pBlock->pMeta->uid = uid;
87✔
1719
    pBlock->pMeta->vgId = vgId;
87✔
1720
    pBlock->pMeta->suid = suid;
87✔
1721
  }
1722

1723
  pBlock->pData->suid = suid;
87✔
1724
  pBlock->pData->uid = uid;
87✔
1725

1726
  if (rebuildCreateTb && NULL == pBlock->pData->pCreateTbReq) {
87!
1727
    pBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
67!
1728
    if (NULL == pBlock->pData->pCreateTbReq) {
67!
1729
      return terrno;
×
1730
    }
1731
  }
1732

1733
  return TSDB_CODE_SUCCESS;
87✔
1734
}
1735

1736
STableMeta* qGetTableMetaInDataBlock(STableDataCxt* pDataBlock) { return ((STableDataCxt*)pDataBlock)->pMeta; }
21,901✔
1737

1738
void qDestroyStmtDataBlock(STableDataCxt* pBlock) {
32,452✔
1739
  if (pBlock == NULL) {
32,452✔
1740
    return;
11,011✔
1741
  }
1742

1743
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
21,441✔
1744
  insDestroyTableDataCxt(pDataBlock);
21,441✔
1745
}
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