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

taosdata / TDengine / #4983

13 Mar 2026 03:38AM UTC coverage: 68.653% (+0.07%) from 68.587%
#4983

push

travis-ci

web-flow
feat/6641435300-save-audit-in-self (#34738)

434 of 584 new or added lines in 10 files covered. (74.32%)

434 existing lines in 121 files now uncovered.

212745 of 309883 relevant lines covered (68.65%)

134272959.11 hits per line

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

54.45
/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) {
6,453,868✔
35
  if (pDataBlock == NULL || pDataBlock->pData == NULL) {
6,453,868✔
36
    qError("stmt qCloneCurrentTbData input is null, maybe not bind data before execute");
2,847✔
37
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
38
  }
39
  *pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
6,454,373✔
40
  if (NULL == *pData) {
6,447,711✔
41
    return terrno;
×
42
  }
43

44
  int8_t         flag = 1;
6,452,137✔
45
  SSubmitTbData* pNew = *pData;
6,452,137✔
46

47
  *pNew = *pDataBlock->pData;
6,453,405✔
48
  pNew->pBlobSet = NULL;
6,455,283✔
49

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

62
  int32_t colNum = taosArrayGetSize(pNew->aCol);
6,458,788✔
63
  for (int32_t i = 0; i < colNum; ++i) {
26,353,957✔
64
    if (pDataBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
19,890,492✔
65
      SColData* pCol = (SColData*)taosArrayGet(pNew->aCol, i);
19,892,047✔
66
      tColDataDeepClear(pCol);
19,895,600✔
67
    } else {
68
      pNew->aCol = taosArrayInit(20, POINTER_BYTES);
×
69
      if (pNew->aCol == NULL) {
×
70
        code = terrno;
×
71
        taosMemoryFreeClear(*pData);
×
72
        return code;
×
73
      }
74
    }
75
  }
76

77
  return code;
6,463,465✔
78
}
79

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

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

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

96
  if (TSDB_CODE_SUCCESS == code) {
721,021✔
97
    code = insBuildVgDataBlocks(pAllVgHash, pVgDataBlocks, &pStmt->pDataBlocks, true);
721,109✔
98
  }
99

100
  if (pStmt->freeArrayFunc) {
721,043✔
101
    pStmt->freeArrayFunc(pVgDataBlocks);
721,120✔
102
  }
103
  return code;
721,015✔
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) {
6,454,922✔
128
  int32_t             code = TSDB_CODE_SUCCESS;
6,454,922✔
129
  SArray*             pVgDataBlocks = NULL;
6,454,922✔
130
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
6,458,080✔
131

132
  // merge according to vgId
133
  if (taosHashGetSize(pBlockHash) > 0) {
6,459,825✔
134
    code = insMergeTableDataCxt(pBlockHash, &pVgDataBlocks, true);
6,458,440✔
135
  }
136

137
  if (TSDB_CODE_SUCCESS == code) {
6,460,355✔
138
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks, false);
6,462,221✔
139
  }
140

141
  if (pStmt->freeArrayFunc) {
6,458,465✔
142
    pStmt->freeArrayFunc(pVgDataBlocks);
6,462,269✔
143
  }
144
  return code;
6,443,859✔
145
}
146

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

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

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

168
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
5,674✔
169

170
  bool  isJson = false;
5,674✔
171
  STag* pTag = NULL;
5,674✔
172

173
  for (int c = 0; c < tags->numOfBound; ++c) {
21,275✔
174
    if (bind[c].is_null && bind[c].is_null[0]) {
15,601✔
175
      continue;
×
176
    }
177

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

201
      isJson = true;
68✔
202
      char* tmp = taosMemoryCalloc(1, colLen + 1);
68✔
203
      if (!tmp) {
68✔
204
        code = terrno;
×
205
        goto end;
×
206
      }
207
      memcpy(tmp, bind[c].buffer, colLen);
68✔
208
      code = parseJsontoTagData(tmp, pTagArray, &pTag, &pBuf, charsetCxt);
68✔
209
      taosMemoryFree(tmp);
68✔
210
      if (code != TSDB_CODE_SUCCESS) {
68✔
211
        goto end;
×
212
      }
213
    } else {
214
      STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
15,533✔
215
      //      strcpy(val.colName, pTagSchema->name);
216
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
15,533✔
217
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
14,352✔
218
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
1,241✔
219
          code = initCtxAsText();
60✔
220
          if (code) {
60✔
221
            qError("geometry init failed:%s", tstrerror(code));
×
222
            goto end;
×
223
          }
224
          code = checkWKB(bind[c].buffer, colLen);
60✔
225
          if (code) {
60✔
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;
1,241✔
231
        val.nData = colLen;
1,241✔
232
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
14,292✔
233
        int32_t output = 0;
731✔
234
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
731✔
235
        if (p == NULL) {
731✔
236
          code = terrno;
×
237
          goto end;
×
238
        }
239
        if (!taosMbsToUcs4(bind[c].buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
731✔
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;
731✔
252
        val.nData = output;
731✔
253
      } else {
254
        memcpy(&val.i64, bind[c].buffer, colLen);
13,561✔
255
      }
256
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
15,533✔
257
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
258
        goto end;
×
259
      }
260
      if (NULL == taosArrayPush(pTagArray, &val)) {
15,533✔
261
        code = terrno;
×
262
        goto end;
×
263
      }
264
    }
265
  }
266

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

271
  if (NULL == pDataBlock->pData->pCreateTbReq) {
5,674✔
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;
5,674✔
279
    taosMemoryFreeClear(tmp->name);
5,674✔
280
    taosMemoryFreeClear(tmp->ctb.pTag);
5,674✔
281
    taosMemoryFreeClear(tmp->ctb.stbName);
5,674✔
282
    taosArrayDestroy(tmp->ctb.tagName);
5,674✔
283
    tmp->ctb.tagName = NULL;
5,674✔
284
  }
285

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

290
end:
5,674✔
291
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
21,267✔
292
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
15,593✔
293
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
15,593✔
294
      taosMemoryFreeClear(p->pData);
791✔
295
    }
296
  }
297
  taosArrayDestroy(pTagArray);
5,674✔
298
  taosArrayDestroy(tagName);
5,674✔
299
  taosMemoryFree(pTag);
5,674✔
300

301
  return code;
5,674✔
302
}
303

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

315
  if (NULL == dst->length) {
5,588✔
316
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
5,614✔
317
    if (NULL == dst->length) {
5,627✔
318
      taosMemoryFreeClear(dst->buffer);
×
319
      return terrno;
×
320
    }
321
  }
322

323
  dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
5,601✔
324

325
  for (int32_t i = 0; i < src->num; ++i) {
786,746✔
326
    if (src->is_null && src->is_null[i]) {
781,158✔
327
      continue;
303,183✔
328
    }
329

330
    if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
477,975✔
331
                       (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output,
477,975✔
332
                       charsetCxt)) {
333
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
13✔
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;
477,962✔
342
  }
343

344
  dst->buffer_type = src->buffer_type;
5,588✔
345
  dst->is_null = src->is_null;
5,588✔
346
  dst->num = src->num;
5,588✔
347

348
  return TSDB_CODE_SUCCESS;
5,588✔
349
}
350

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

364
  if (NULL == pTSchema || NULL == *pTSchema) {
1,769,855✔
365
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
6,290✔
366
  }
367

368
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
9,069,043✔
369
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
7,297,771✔
370
    if (pColSchema->colId <= lastColId) {
7,299,865✔
371
      colInOrder = false;
×
372
    } else {
373
      lastColId = pColSchema->colId;
7,301,432✔
374
    }
375
    // SColData* pCol = taosArrayGet(pCols, c);
376

377
    if (bind[c].num != rowNum) {
7,300,386✔
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)) &&
7,300,228✔
383
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
7,299,860✔
384
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
385
      goto _return;
×
386
    }
387

388
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
7,298,508✔
389
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
1,958✔
390
      if (code) {
1,958✔
391
        goto _return;
×
392
      }
393
      pBind = &ncharBind;
1,958✔
394
    } else {
395
      pBind = bind + c;
7,298,437✔
396
    }
397

398
    pBindInfos[c].columnId = pColSchema->colId;
7,299,102✔
399
    pBindInfos[c].bind = pBind;
7,300,118✔
400
    pBindInfos[c].type = pColSchema->type;
7,299,672✔
401
  }
402

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

406
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
1,769,748✔
407

408
_return:
1,769,748✔
409

410
  taosMemoryFree(ncharBind.buffer);
1,769,334✔
411
  taosMemoryFree(ncharBind.length);
1,769,997✔
412

413
  return code;
1,769,923✔
414
}
415

416
int32_t qBindStmtColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
6,349,493✔
417
                           void* charsetCxt) {
418
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
6,349,493✔
419
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
6,349,493✔
420
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
6,354,255✔
421
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
6,355,523✔
422
  int32_t          rowNum = bind->num;
6,358,023✔
423
  TAOS_MULTI_BIND  ncharBind = {0};
6,349,159✔
424
  TAOS_MULTI_BIND* pBind = NULL;
6,348,226✔
425
  int32_t          code = 0;
6,348,226✔
426

427
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
25,478,845✔
428
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
19,122,851✔
429
    SColData* pCol = taosArrayGet(pCols, c);
19,129,119✔
430

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

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

442
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
19,125,613✔
443
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
3,596✔
444
      if (code) {
3,596✔
445
        goto _return;
×
446
      }
447
      pBind = &ncharBind;
3,596✔
448
    } else {
449
      pBind = bind + c;
19,125,558✔
450
    }
451

452
    int32_t bytes = 0;
19,123,711✔
453
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
19,123,711✔
454
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
15,562✔
455
        bytes = pColSchema->bytes - BLOBSTR_HEADER_SIZE;
×
456
      } else {
457
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
18,469✔
458
      }
459
    } else {
460
      bytes = -1;
19,101,522✔
461
    }
462
    code = tColDataAddValueByBind(pCol, pBind, bytes, initCtxAsText, checkWKB);
19,119,991✔
463
    if (code) {
19,134,513✔
464
      goto _return;
3,894✔
465
    }
466
  }
467

468
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
6,358,231✔
469

470
_return:
6,362,125✔
471

472
  taosMemoryFree(ncharBind.buffer);
6,360,869✔
473
  taosMemoryFree(ncharBind.length);
6,359,948✔
474

475
  return code;
6,360,260✔
476
}
477

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

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

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

509
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
×
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;
×
517
    }
518

519
    int32_t bytes = 0;
×
520
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
×
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;
×
528
    }
529
    code = tColDataAddValueByBind(pCol, pBind, bytes, initCtxAsText, checkWKB);
×
530
    if (code) {
×
531
      goto _return;
×
532
    }
533
    actualIndex++;
×
534
  }
535

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

538
_return:
×
539

540
  taosMemoryFree(ncharBind.buffer);
×
541
  taosMemoryFree(ncharBind.length);
×
542

543
  return code;
×
544
}
545

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

558
  if (bind->num != rowNum) {
840✔
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) {
840✔
564
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
565
  }
566

567
  if (bind->buffer_type != pColSchema->type) {
840✔
568
    return buildInvalidOperationMsgExt(&pBuf, "column type:%d mis-match with buffer type:%d", pColSchema->type,
×
569
                                       bind->buffer_type);
570
  }
571

572
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
840✔
573
    code = convertStmtNcharCol(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
60✔
574
    if (code) {
60✔
575
      goto _return;
×
576
    }
577
    pBind = &ncharBind;
60✔
578
  } else {
579
    pBind = bind;
780✔
580
  }
581

582
  code = tColDataAddValueByBind(pCol, pBind,
840✔
583
                                IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1,
840✔
584
                                initCtxAsText, checkWKB);
585

586
  parserDebug("stmt col %d bind %d rows data", colIdx, rowNum);
840✔
587

588
_return:
840✔
589

590
  taosMemoryFree(ncharBind.buffer);
840✔
591
  taosMemoryFree(ncharBind.length);
840✔
592

593
  return code;
840✔
594
}
595

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

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

617
  SArray* tagName;
618
  if (tags->parseredTags) {
484,920✔
619
    tagName = taosArrayDup(tags->parseredTags->STagNames, NULL);
×
620
  } else {
621
    tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
484,920✔
622
  }
623

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

629
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
484,920✔
630

631
  bool  isJson = false;
484,872✔
632
  STag* pTag = NULL;
484,872✔
633

634
  int bindIdx = 0;
484,872✔
635
  for (int c = 0; c < tags->numOfBound; ++c) {
1,454,520✔
636
    if (bind == NULL) {
969,612✔
637
      break;
×
638
    }
639
    if (tags->parseredTags) {
969,612✔
640
      bool found = false;
×
641
      for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
×
642
        if (tags->parseredTags->pTagIndex[k] == tags->pColIndex[c]) {
×
643
          found = true;
×
644
          break;
×
645
        }
646
      }
647
      if (found) {
×
648
        continue;
×
649
      }
650
    }
651

652
    TAOS_STMT2_BIND bindData = bind[bindIdx++];
969,612✔
653

654
    if (bindData.is_null && bindData.is_null[0]) {
969,612✔
655
      continue;
×
656
    }
657

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

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

739
      } else {
740
        uint8_t* buf = (uint8_t*)bindData.buffer;
484,860✔
741
        if (TSDB_DATA_TYPE_BOOL == pTagSchema->type && *buf > 1) {
484,860✔
742
          *buf = 1;
×
743
        }
744
        memcpy(&val.i64, buf, colLen);
484,860✔
745
      }
746
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
969,780✔
747
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
748
        goto end;
×
749
      }
750
      if (NULL == taosArrayPush(pTagArray, &val)) {
969,660✔
751
        code = terrno;
×
752
        goto end;
×
753
      }
754
    }
755
  }
756

757
  if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) {
484,908✔
758
    goto end;
×
759
  }
760

761
  if (pCreateTbReq) {
485,028✔
762
    code = insBuildCreateTbReq(pCreateTbReq, tName, pTag, suid, sTableName, tagName,
484,844✔
763
                               pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
484,844✔
764
    pTag = NULL;
484,820✔
765
    goto end;
484,820✔
766
  }
767

768
  if (NULL == pDataBlock->pData->pCreateTbReq) {
184✔
769
    pDataBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
16✔
770
    if (NULL == pDataBlock->pData->pCreateTbReq) {
16✔
771
      code = terrno;
×
772
      goto end;
×
773
    }
774
  } else {
775
    SVCreateTbReq* tmp = pDataBlock->pData->pCreateTbReq;
168✔
776
    taosMemoryFreeClear(tmp->name);
168✔
777
    taosMemoryFreeClear(tmp->ctb.pTag);
168✔
778
    taosMemoryFreeClear(tmp->ctb.stbName);
168✔
779
    taosArrayDestroy(tmp->ctb.tagName);
168✔
780
    tmp->ctb.tagName = NULL;
168✔
781
  }
782

783
  code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName,
184✔
784
                             pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
184✔
785
  pTag = NULL;
184✔
786

787
end:
485,004✔
788
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
1,454,688✔
789
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
969,720✔
790
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
969,684✔
791
      taosMemoryFreeClear(p->pData);
×
792
    }
793
  }
794
  taosArrayDestroy(pTagArray);
484,668✔
795
  taosArrayDestroy(tagName);
485,004✔
796
  taosMemoryFree(pTag);
484,932✔
797

798
  return code;
484,944✔
799
}
800

801
static int32_t convertStmtStbNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst,
33✔
802
                                       void* charsetCxt) {
803
  int32_t       output = 0;
33✔
804
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
33✔
805

806
  dst->buffer = taosMemoryCalloc(src->num, max_buf_len);
33✔
807
  if (NULL == dst->buffer) {
33✔
808
    return terrno;
×
809
  }
810

811
  dst->length = taosMemoryCalloc(src->num, sizeof(int32_t));
33✔
812
  if (NULL == dst->length) {
33✔
813
    taosMemoryFreeClear(dst->buffer);
×
814
    return terrno;
×
815
  }
816

817
  char* src_buf = src->buffer;
33✔
818
  char* dst_buf = dst->buffer;
33✔
819
  for (int32_t i = 0; i < src->num; ++i) {
33,033✔
820
    if (src->is_null && src->is_null[i]) {
33,000✔
821
      continue;
×
822
    }
823

824
    if (src->length[i] == 0) {
33,000✔
825
      dst->length[i] = 0;
×
826
      continue;
×
827
    }
828

829
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
33,000✔
UNCOV
830
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
831
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
832
      }
833
      char buf[512] = {0};
×
834
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
835
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
836
    }
837

838
    dst->length[i] = output;
33,000✔
839
    src_buf += src->length[i];
33,000✔
840
    dst_buf += output;
33,000✔
841
  }
842

843
  dst->buffer_type = src->buffer_type;
33✔
844
  dst->is_null = src->is_null;
33✔
845
  dst->num = src->num;
33✔
846

847
  return TSDB_CODE_SUCCESS;
33✔
848
}
849

850
int32_t qBindStmtStbColsValue2(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
960,149✔
851
                               int32_t msgBufLen, STSchema** pTSchema, SBindInfo2* pBindInfos, void* charsetCxt,
852
                               SBlobSet** ppBlob) {
853
  STableDataCxt*  pDataBlock = (STableDataCxt*)pBlock;
960,149✔
854
  SSchema*        pSchema = getTableColumnSchema(pDataBlock->pMeta);
960,149✔
855
  SSchemaExt*     pSchemaExt = getTableColumnExtSchema(pDataBlock->pMeta);
960,308✔
856
  SBoundColInfo*  boundInfo = &pDataBlock->boundColsInfo;
960,483✔
857
  SMsgBuf         pBuf = {.buf = msgBuf, .len = msgBufLen};
960,521✔
858
  int32_t         rowNum = bind->num;
960,521✔
859
  SArray*         ncharBinds = NULL;
960,483✔
860
  TAOS_STMT2_BIND ncharBind = {0};
960,483✔
861
  int32_t         code = 0;
960,483✔
862
  int16_t         lastColId = -1;
960,483✔
863
  bool            colInOrder = true;
960,483✔
864
  int             ncharColNums = 0;
960,483✔
865
  int32_t         bindIdx = 0;
960,483✔
866
  int8_t          hasBlob = 0;
960,483✔
867
  int32_t         lino = 0;
960,483✔
868
  if (NULL == pTSchema || NULL == *pTSchema) {
960,434✔
869
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
2,786✔
870
  }
871

872
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
8,115,405✔
873
    if (TSDB_DATA_TYPE_NCHAR == pSchema[boundInfo->pColIndex[c]].type) {
7,160,154✔
874
      ncharColNums++;
33✔
875
    }
876
  }
877
  if (ncharColNums > 0) {
960,510✔
878
    ncharBinds = taosArrayInit(ncharColNums, sizeof(ncharBind));
33✔
879
    if (!ncharBinds) {
33✔
880
      code = terrno;
×
881
      TAOS_CHECK_GOTO(code, &lino, _return);
×
882
    }
883
  }
884

885
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
8,152,581✔
886
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
7,187,735✔
887
    if (pColSchema->colId <= lastColId) {
7,189,941✔
888
      colInOrder = false;
×
889
    } else {
890
      lastColId = pColSchema->colId;
7,193,548✔
891
    }
892

893
    if (parsedCols) {
7,192,131✔
894
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
×
895
      if (pParsedVal) {
×
896
        pBindInfos[c].columnId = pColSchema->colId;
×
897
        pBindInfos[c].type = pColSchema->type;
×
898
        pBindInfos[c].bytes = pColSchema->bytes;
×
899
        continue;
×
900
      }
901
    }
902

903
    TAOS_STMT2_BIND bindData = bind[bindIdx];
7,192,131✔
904

905
    if (bindData.num != rowNum) {
7,187,768✔
906
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
907
      goto _return;
×
908
    }
909

910
    if ((!(rowNum == 1 && bindData.is_null && *bindData.is_null)) &&
7,187,768✔
911
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
7,186,034✔
912
      code = buildInvalidOperationMsgExt(&pBuf, "bind[%d] column type:%d mis-match with buffer type:%d", bindIdx,
×
913
                                         pColSchema->type, bindData.buffer_type);
×
914
      goto _return;
×
915
    }
916

917
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
7,186,446✔
918
      code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
33✔
919
      TAOS_CHECK_GOTO(code, &lino, _return);
33✔
920

921
      if (!taosArrayPush(ncharBinds, &ncharBind)) {
33✔
922
        code = terrno;
×
923
        goto _return;
×
924
      }
925
      pBindInfos[c].bind = taosArrayGetLast(ncharBinds);
33✔
926
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
7,184,032✔
927
      code = initCtxAsText();
×
928
      if (code) {
×
929
        parserError("stmt2 bind geometry init failed, ErrCode: 0x%x", code);
×
930
        goto _return;
×
931
      }
932
      uint8_t* buf = bindData.buffer;
×
933
      for (int j = 0; j < bindData.num; j++) {
×
934
        if (bindData.is_null && bindData.is_null[j]) {
×
935
          continue;
×
936
        }
937
        code = checkWKB(buf, bindData.length[j]);
×
938
        if (code) {
×
939
          parserError("stmt2 interlace mode geometry data col:%d, row:%d must be in WKB format", c, j);
×
940
          goto _return;
×
941
        }
942
        buf += bindData.length[j];
×
943
      }
944
      pBindInfos[c].bind = bind + bindIdx;
×
945
    } else {
946
      if (IS_STR_DATA_BLOB(pColSchema->type)) hasBlob = 1;
7,183,196✔
947
      pBindInfos[c].bind = bind + bindIdx;
7,182,952✔
948
    }
949

950
    pBindInfos[c].columnId = pColSchema->colId;
7,180,112✔
951
    pBindInfos[c].type = pColSchema->type;
7,187,395✔
952
    pBindInfos[c].bytes = pColSchema->bytes;
7,189,330✔
953

954
    bindIdx++;
7,192,122✔
955
  }
956

957
  if (hasBlob == 0) {
960,735✔
958
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pSchemaExt, pCols,
960,326✔
959
                              &pDataBlock->ordered, &pDataBlock->duplicateTs);
960
    TAOS_CHECK_GOTO(code, &lino, _return);
960,636✔
961
  } else {
962
    code = tBlobSetCreate(1024, 1, ppBlob);
409✔
963
    TAOS_CHECK_GOTO(code, &lino, _return);
×
964

965
    code = tRowBuildFromBind2WithBlob(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols,
×
966
                                      &pDataBlock->ordered, &pDataBlock->duplicateTs, *ppBlob);
967
    TAOS_CHECK_GOTO(code, &lino, _return);
1✔
968
  }
969

970
  parserDebug("stmt2 all %d columns bind %d rows data as row format", boundInfo->numOfBound, rowNum);
960,637✔
971

972
_return:
960,637✔
973
  if (ncharBinds) {
960,709✔
974
    for (int i = 0; i < TARRAY_SIZE(ncharBinds); ++i) {
66✔
975
      TAOS_STMT2_BIND* ncBind = TARRAY_DATA(ncharBinds);
33✔
976
      taosMemoryFree(ncBind[i].buffer);
33✔
977
      taosMemoryFree(ncBind[i].length);
33✔
978
    }
979
    taosArrayDestroy(ncharBinds);
33✔
980
  }
981
  if (code != 0) {
960,709✔
982
    parserError("stmt2 failed to bind stb col at lino %d since %s", lino, tstrerror(code));
×
983
  }
984

985
  return code;
960,683✔
986
}
987

988
static int32_t convertStmtNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst,
21,699,292✔
989
                                    void* charsetCxt) {
990
  int32_t       output = 0;
21,699,292✔
991
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
21,814,424✔
992

993
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
21,909,383✔
994
  // if (dst->buffer_length < newBuflen) {
995
  dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
22,041,632✔
996
  if (NULL == dst->buffer) {
21,570,638✔
997
    return terrno;
×
998
  }
999
  //}
1000

1001
  if (NULL == dst->length) {
21,639,206✔
1002
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
21,767,094✔
1003
    if (NULL == dst->length) {
21,782,410✔
1004
      taosMemoryFreeClear(dst->buffer);
×
1005
      return terrno;
×
1006
    }
1007
  }
1008

1009
  // dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
1010
  char* src_buf = src->buffer;
21,759,257✔
1011
  char* dst_buf = dst->buffer;
21,833,868✔
1012
  for (int32_t i = 0; i < src->num; ++i) {
43,693,685✔
1013
    if (src->is_null && src->is_null[i]) {
21,704,934✔
1014
      continue;
1,072,413✔
1015
    }
1016

1017
    if (src->length[i] == 0) {
20,775,075✔
1018
      dst->length[i] = 0;
16,005✔
1019
      continue;
16,005✔
1020
    }
1021

1022
    /*if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
1023
      (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output)) {*/
1024
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
20,860,888✔
1025
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
1026
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
1027
      }
1028
      char buf[512] = {0};
×
1029
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
1030
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
1031
    }
1032

1033
    dst->length[i] = output;
20,652,938✔
1034
    src_buf += src->length[i];
20,774,716✔
1035
    dst_buf += output;
20,927,428✔
1036
  }
1037

1038
  dst->buffer_type = src->buffer_type;
21,903,516✔
1039
  dst->is_null = src->is_null;
21,972,954✔
1040
  dst->num = src->num;
21,990,977✔
1041

1042
  return TSDB_CODE_SUCCESS;
21,804,251✔
1043
}
1044

1045
int32_t qBindStmtColsValue2(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
45,802,100✔
1046
                            int32_t msgBufLen, void* charsetCxt) {
1047
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
45,802,100✔
1048
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
45,802,100✔
1049
  SSchemaExt*      pExtSchema = getTableColumnExtSchema(pDataBlock->pMeta);
46,577,796✔
1050
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
46,587,164✔
1051
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
46,716,246✔
1052
  int32_t          rowNum = bind->num;
46,761,749✔
1053
  TAOS_STMT2_BIND  ncharBind = {0};
46,437,493✔
1054
  TAOS_STMT2_BIND* pBind = NULL;
45,614,660✔
1055
  int32_t          code = 0;
45,614,660✔
1056
  int32_t          lino = 0;
45,614,660✔
1057
  int32_t          bindIdx = 0;
45,614,660✔
1058

1059
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
481,230,426✔
1060
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
433,084,073✔
1061
    SColData* pCol = taosArrayGet(pCols, c);
433,402,577✔
1062
    if (pCol == NULL || pColSchema == NULL) {
428,716,446✔
UNCOV
1063
      code = buildInvalidOperationMsg(&pBuf, "get column schema or column data failed");
×
1064
      goto _return;
×
1065
    }
1066

1067
    if (boundInfo->pColIndex[c] == 0) {
430,245,741✔
1068
      pCol->cflag |= COL_IS_KEY;
46,621,698✔
1069
    }
1070

1071
    if (parsedCols) {
431,738,809✔
1072
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
×
1073
      if (pParsedVal) {
×
1074
        for (int row = 0; row < rowNum; row++) {
×
1075
          code = tColDataAppendValue(pCol, pParsedVal);
×
1076
          if (code) {
×
1077
            parserError("stmt2 failed to add fixed value col:%d ,type:%d, ErrCode: 0x%x", c, pColSchema->type, code);
×
1078
            goto _return;
×
1079
          }
1080
        }
1081
        continue;
×
1082
      }
1083
    }
1084
    TAOS_STMT2_BIND bindData = bind[bindIdx];
431,738,809✔
1085
    if (bindData.num != rowNum) {
432,184,549✔
1086
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1087
      goto _return;
×
1088
    }
1089

1090
    if ((!(rowNum == 1 && bindData.is_null && *bindData.is_null)) &&
432,184,549✔
1091
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
419,387,512✔
1092
      code = buildInvalidOperationMsgExt(&pBuf, "bind[%d] column type:%d mis-match with buffer type:%d", bindIdx,
×
1093
                                         pColSchema->type, bindData.buffer_type);
×
1094
      goto _return;
×
1095
    }
1096

1097
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
429,592,631✔
1098
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
21,958,828✔
1099
      if (code) {
21,573,088✔
1100
        goto _return;
×
1101
      }
1102
      pBind = &ncharBind;
21,573,088✔
1103
    } else {
1104
      pBind = bind + bindIdx;
411,665,045✔
1105
    }
1106
    int8_t  isBlob = 0;
431,411,370✔
1107
    int32_t bytes = -1;
431,411,370✔
1108
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
431,411,370✔
1109
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
37,532,146✔
1110
        isBlob = 1;
174✔
1111
        bytes = TSDB_MAX_BLOB_LEN;
174✔
1112
      } else {
1113
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
44,590,161✔
1114
      }
1115
    }
1116

1117
    if (pColSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
439,053,951✔
1118
      code = tColDataAddValueByBind2WithGeos(pCol, pBind, bytes, initCtxAsText, checkWKB);
14,894✔
1119
    } else if (isBlob == 1) {
432,415,921✔
1120
      if (pDataBlock->pData->pBlobSet == NULL) {
174✔
1121
        code = tBlobSetCreate(1024, 1, &pDataBlock->pData->pBlobSet);
150✔
1122
        if (code) {
150✔
1123
          parserError("stmt2 failed to create blob set, ErrCode: 0x%x", code);
×
1124
          goto _return;
×
1125
        }
1126
      }
1127
      code = tColDataAddValueByBind2WithBlob(pCol, pBind, bytes, pDataBlock->pData->pBlobSet);
174✔
1128
    } else if (IS_DECIMAL_TYPE(pColSchema->type)) {
432,415,961✔
1129
      if (pExtSchema == NULL) {
214✔
1130
        code = buildInvalidOperationMsg(&pBuf, "decimal column ext schema is null");
×
1131
        goto _return;
×
1132
      }
1133
      uint8_t precision = 0, scale = 0;
214✔
1134
      decimalFromTypeMod(pExtSchema[c].typeMod, &precision, &scale);
214✔
1135
      code = tColDataAddValueByBind2WithDecimal(pCol, pBind, bytes, precision, scale);
214✔
1136
    } else {
1137
      code = tColDataAddValueByBind2(pCol, pBind, bytes);
433,362,935✔
1138
    }
1139

1140
    if (code) {
435,615,766✔
1141
      parserError("stmt2 failed to add col:%d ,type:%d, ErrCode: 0x%x", c, pColSchema->type, code);
×
1142
      goto _return;
×
1143
    }
1144
    bindIdx++;
435,615,766✔
1145
  }
1146

1147
  parserDebug("stmt2 all %d columns bind %d rows data as col format", boundInfo->numOfBound, rowNum);
46,843,374✔
1148

1149
_return:
46,843,374✔
1150

1151
  taosMemoryFree(ncharBind.buffer);
46,948,246✔
1152
  taosMemoryFree(ncharBind.length);
46,514,372✔
1153

1154
  return code;
46,808,710✔
1155
}
1156

1157
int32_t qBindStmtSingleColValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen,
×
1158
                                 int32_t colIdx, int32_t rowNum, void* charsetCxt) {
1159
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
×
1160
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
1161
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
×
1162
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
×
1163
  SSchema*         pColSchema = &pSchema[boundInfo->pColIndex[colIdx]];
×
1164
  SColData*        pCol = taosArrayGet(pCols, colIdx);
×
1165
  TAOS_STMT2_BIND  ncharBind = {0};
×
1166
  TAOS_STMT2_BIND* pBind = NULL;
×
1167
  int32_t          code = 0;
×
1168
  int32_t          lino = 0;
×
1169

1170
  if (bind->num != rowNum) {
×
1171
    return buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1172
  }
1173

1174
  // Column index exceeds the number of columns
1175
  if (colIdx >= pCols->size && pCol == NULL) {
×
1176
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
1177
  }
1178

1179
  if (bind->buffer_type != pColSchema->type) {
×
1180
    return buildInvalidOperationMsgExt(&pBuf, "column type:%d mis-match with buffer type:%d", pColSchema->type,
×
1181
                                       bind->buffer_type);
1182
  }
1183

1184
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
×
1185
    code = convertStmtNcharCol2(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
×
1186
    if (code) {
×
1187
      goto _return;
×
1188
    }
1189
    pBind = &ncharBind;
×
1190
  } else {
1191
    pBind = bind;
×
1192
  }
1193

1194
  int32_t bytes = -1;
×
1195
  int8_t  hasBlob = 0;
×
1196
  if (IS_VAR_DATA_TYPE(pColSchema->type)) {
×
1197
    if (IS_STR_DATA_BLOB(pColSchema->type)) {
×
1198
      bytes = TSDB_MAX_BLOB_LEN;
×
1199
      hasBlob = 1;
×
1200
    } else {
1201
      bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
×
1202
    }
1203
  } else {
1204
    bytes = -1;
×
1205
  }
1206

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

1231
  if (code) {
×
1232
    parserError("stmt2 failed to add single col:%d ,type:%d, ErrCode: 0x%x", colIdx, pColSchema->type, code);
×
1233
    goto _return;
×
1234
  }
1235

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

1238
_return:
×
1239

1240
  taosMemoryFree(ncharBind.buffer);
×
1241
  taosMemoryFree(ncharBind.length);
×
1242

1243
  return code;
×
1244
}
1245

1246
int32_t qBindStmt2RowValue(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
×
1247
                           int32_t msgBufLen, STSchema** pTSchema, SBindInfo2* pBindInfos, void* charsetCxt) {
1248
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
×
1249
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
1250
  SSchemaExt*      pSchemaExt = getTableColumnExtSchema(pDataBlock->pMeta);
×
1251
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
×
1252
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
×
1253
  int32_t          rowNum = bind->num;
×
1254
  TAOS_STMT2_BIND  ncharBind = {0};
×
1255
  TAOS_STMT2_BIND* pBind = NULL;
×
1256
  int32_t          code = 0;
×
1257
  int16_t          lastColId = -1;
×
1258
  bool             colInOrder = true;
×
1259
  int8_t           hasBlob = 0;
×
1260

1261
  if (NULL == pTSchema || NULL == *pTSchema) {
×
1262
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
×
1263
  }
1264

1265
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
×
1266
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
×
1267
    if (pColSchema->colId <= lastColId) {
×
1268
      colInOrder = false;
×
1269
    } else {
1270
      lastColId = pColSchema->colId;
×
1271
    }
1272

1273
    if (bind[c].num != rowNum) {
×
1274
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1275
      goto _return;
×
1276
    }
1277

1278
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
×
1279
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
×
1280
      code = buildInvalidOperationMsgExt(&pBuf, "bind[%d] column type:%d mis-match with buffer type:%d", c,
×
1281
                                         pColSchema->type, bind[c].buffer_type);
×
1282
      goto _return;
×
1283
    }
1284

1285
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
×
1286
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
×
1287
      if (code) {
×
1288
        goto _return;
×
1289
      }
1290
      pBindInfos[c].bind = &ncharBind;
×
1291
    } else if (TSDB_DATA_TYPE_GEOMETRY == pColSchema->type) {
×
1292
      code = initCtxAsText();
×
1293
      if (code) {
×
1294
        qError("geometry init failed:%s", tstrerror(code));
×
1295
        goto _return;
×
1296
      }
1297
      uint8_t* buf = bind[c].buffer;
×
1298
      for (int j = 0; j < bind[c].num; j++) {
×
1299
        if (bind[c].is_null && bind[c].is_null[j]) {
×
1300
          continue;
×
1301
        }
1302
        code = checkWKB(buf, bind[c].length[j]);
×
1303
        if (code) {
×
1304
          qError("stmt2 row bind geometry data[%d]:{%s},length:%d must be in WKB format", c, buf, bind[c].length[j]);
×
1305
          goto _return;
×
1306
        }
1307
        buf += bind[c].length[j];
×
1308
      }
1309
      pBindInfos[c].bind = bind + c;
×
1310
    } else {
1311
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
×
1312
        hasBlob = 1;
×
1313
      }
1314
      pBindInfos[c].bind = bind + c;
×
1315
    }
1316

1317
    pBindInfos[c].columnId = pColSchema->colId;
×
1318
    pBindInfos[c].type = pColSchema->type;
×
1319
    pBindInfos[c].bytes = pColSchema->bytes;
×
1320

1321
    if (code) {
×
1322
      goto _return;
×
1323
    }
1324
  }
1325

1326
  pDataBlock->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
×
1327
  if (pDataBlock->pData->pCreateTbReq != NULL) {
×
1328
    pDataBlock->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
×
1329
  }
1330

1331
  if (hasBlob == 0) {
×
1332
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pSchemaExt, pCols,
×
1333
                              &pDataBlock->ordered, &pDataBlock->duplicateTs);
1334
  } else {
1335
    code = TSDB_CODE_BLOB_NOT_SUPPORT;
×
1336
  }
1337
  qDebug("stmt2 all %d columns bind %d rows data as row format", boundInfo->numOfBound, rowNum);
×
1338

1339
_return:
×
1340

1341
  taosMemoryFree(ncharBind.buffer);
×
1342
  taosMemoryFree(ncharBind.length);
×
1343

1344
  return code;
×
1345
}
1346

1347
int32_t buildBoundFields(int32_t numOfBound, int16_t* boundColumns, SSchema* pSchema, int32_t* fieldNum,
×
1348
                         TAOS_FIELD_E** fields, uint8_t timePrec) {
1349
  if (fields != NULL) {
×
1350
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
×
1351
    if (NULL == *fields) {
×
1352
      return terrno;
×
1353
    }
1354

1355
    SSchema* schema = &pSchema[boundColumns[0]];
×
1356
    if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
×
1357
      (*fields)[0].precision = timePrec;
×
1358
    }
1359

1360
    for (int32_t i = 0; i < numOfBound; ++i) {
×
1361
      schema = &pSchema[boundColumns[i]];
×
1362
      tstrncpy((*fields)[i].name, schema->name, 65);
×
1363
      (*fields)[i].type = schema->type;
×
1364
      (*fields)[i].bytes = calcTypeBytesFromSchemaBytes(schema->type, schema->bytes, true);
×
1365
    }
1366
  }
1367

1368
  *fieldNum = numOfBound;
×
1369

1370
  return TSDB_CODE_SUCCESS;
×
1371
}
1372

1373
int32_t buildStbBoundFields(SBoundColInfo boundColsInfo, SSchema* pSchema, int32_t* fieldNum, TAOS_FIELD_ALL** fields,
22✔
1374
                            STableMeta* pMeta, void* boundTags, SSHashObj* parsedCols, uint8_t tbNameFlag,
1375
                            int32_t* placeholderOfTags, int32_t* placeholderOfCols) {
1376
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
22✔
1377
  bool           hastag = (tags != NULL) && !(tbNameFlag & IS_FIXED_TAG);
22✔
1378
  bool           hasPreBindTbname =
22✔
1379
      (tbNameFlag & IS_FIXED_VALUE) == 0 && ((tbNameFlag & USING_CLAUSE) != 0 || pMeta->tableType == TSDB_NORMAL_TABLE);
22✔
1380
  int32_t numOfBound = boundColsInfo.numOfBound + (hasPreBindTbname ? 1 : 0);
22✔
1381
  if (hastag) {
22✔
1382
    numOfBound += tags->mixTagsCols ? 0 : tags->numOfBound;
22✔
1383
  }
1384

1385
  // Adjust the number of bound fields if there are parsed tags or parsed columns
1386
  if (tags && tags->parseredTags) {
22✔
1387
    numOfBound -= tags->parseredTags->numOfTags;
×
1388
  }
1389
  if (parsedCols) {
22✔
1390
    numOfBound -= tSimpleHashGetSize(parsedCols);
×
1391
  }
1392

1393
  int32_t idx = 0;
22✔
1394
  if (fields != NULL) {
22✔
1395
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_ALL));
22✔
1396
    if (NULL == *fields) {
22✔
1397
      return terrno;
×
1398
    }
1399
  }
1400

1401
  if (hasPreBindTbname && fields != NULL) {
22✔
1402
    (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
14✔
1403
    tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
14✔
1404
    (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
14✔
1405
    (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
14✔
1406
    idx++;
14✔
1407
  }
1408

1409
  if (hastag && tags->numOfBound > 0 && !tags->mixTagsCols) {
22✔
1410
    SSchema* tagSchema = getTableTagSchema(pMeta);
14✔
1411

1412
    for (int32_t i = 0; i < tags->numOfBound; ++i) {
42✔
1413
      SSchema* schema = &tagSchema[tags->pColIndex[i]];
28✔
1414

1415
      if (tags->parseredTags && tags->parseredTags->numOfTags > 0) {
28✔
1416
        int32_t tag_idx = schema->colId - 1 - pMeta->tableInfo.numOfColumns;
×
1417
        bool    found = false;
×
1418
        for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
×
1419
          if (tags->parseredTags->pTagIndex[k] == tag_idx) {
×
1420
            found = true;
×
1421
            break;
×
1422
          }
1423
        }
1424
        if (found) {
×
1425
          continue;
×
1426
        }
1427
      }
1428

1429
      if (placeholderOfTags != NULL) {
28✔
1430
        (*placeholderOfTags)++;
28✔
1431
      }
1432

1433
      if (fields != NULL) {
28✔
1434
        (*fields)[idx].field_type = TAOS_FIELD_TAG;
28✔
1435
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[i].name));
28✔
1436
        (*fields)[idx].type = schema->type;
28✔
1437
        (*fields)[idx].bytes = schema->bytes;
28✔
1438
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
28✔
1439
          (*fields)[idx].precision = pMeta->tableInfo.precision;
×
1440
        }
1441
      }
1442
      idx++;
28✔
1443
    }
1444
  }
1445

1446
  if (boundColsInfo.numOfBound > 0) {
22✔
1447
    SSchema* schema = &pSchema[boundColsInfo.pColIndex[0]];
22✔
1448

1449
    for (int32_t i = 0; i < boundColsInfo.numOfBound; ++i) {
134✔
1450
      int16_t idxCol = boundColsInfo.pColIndex[i];
112✔
1451

1452
      if (idxCol == pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags) {
112✔
1453
        if (fields != NULL) {
8✔
1454
          (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
8✔
1455
          tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
8✔
1456
          (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
8✔
1457
          (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
8✔
1458
        }
1459
        idx++;
8✔
1460
        continue;
8✔
1461
      } else if (idxCol < pMeta->tableInfo.numOfColumns) {
104✔
1462
        if (parsedCols && tSimpleHashGet(parsedCols, &pSchema[idxCol].colId, sizeof(int16_t))) {
88✔
1463
          continue;
×
1464
        }
1465
        if (fields != NULL) {
88✔
1466
          (*fields)[idx].field_type = TAOS_FIELD_COL;
88✔
1467
        }
1468
        if (placeholderOfCols != NULL) {
88✔
1469
          (*placeholderOfCols)++;
88✔
1470
        }
1471
      } else {
1472
        if (tags->parseredTags && tags->parseredTags->numOfTags > 0) {
16✔
1473
          int32_t tag_idx = idxCol - pMeta->tableInfo.numOfColumns;
×
1474
          bool    found = false;
×
1475
          for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
×
1476
            if (tags->parseredTags->pTagIndex[k] == tag_idx) {
×
1477
              found = true;
×
1478
              break;
×
1479
            }
1480
          }
1481
          if (found) {
×
1482
            continue;
×
1483
          }
1484
        }
1485
        if (fields != NULL) {
16✔
1486
          (*fields)[idx].field_type = TAOS_FIELD_TAG;
16✔
1487
        }
1488
        if (placeholderOfTags != NULL) {
16✔
1489
          (*placeholderOfTags)++;
16✔
1490
        }
1491
      }
1492

1493
      if (fields != NULL) {
104✔
1494
        schema = &pSchema[idxCol];
104✔
1495
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[idx].name));
104✔
1496
        (*fields)[idx].type = schema->type;
104✔
1497
        (*fields)[idx].bytes = schema->bytes;
104✔
1498
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
104✔
1499
          (*fields)[idx].precision = pMeta->tableInfo.precision;
22✔
1500
        }
1501
        if (TSDB_DATA_TYPE_DECIMAL64 == schema->type || TSDB_DATA_TYPE_DECIMAL == schema->type) {
104✔
1502
          decimalFromTypeMod(pMeta->schemaExt[idxCol].typeMod, &(*fields)[idx].precision, &(*fields)[idx].scale);
×
1503
        }
1504
      }
1505
      idx++;
104✔
1506
    }
1507
  }
1508

1509
  if (fieldNum != NULL) {
22✔
1510
    *fieldNum = numOfBound;
22✔
1511
  }
1512

1513
  return TSDB_CODE_SUCCESS;
22✔
1514
}
1515

1516
int32_t qBuildStmtTagFields(void* pBlock, void* boundTags, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1517
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
1518
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
×
1519
  if (NULL == tags) {
×
1520
    return TSDB_CODE_APP_ERROR;
×
1521
  }
1522

1523
  if (pDataBlock->pMeta->tableType != TSDB_SUPER_TABLE && pDataBlock->pMeta->tableType != TSDB_CHILD_TABLE) {
×
1524
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1525
  }
1526

1527
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
×
1528
  if (tags->numOfBound <= 0) {
×
1529
    *fieldNum = 0;
×
1530
    *fields = NULL;
×
1531

1532
    return TSDB_CODE_SUCCESS;
×
1533
  }
1534

1535
  CHECK_CODE(buildBoundFields(tags->numOfBound, tags->pColIndex, pSchema, fieldNum, fields, 0));
×
1536

1537
  return TSDB_CODE_SUCCESS;
×
1538
}
1539

1540
int32_t qBuildStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1541
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
1542
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
1543
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
×
1544
    *fieldNum = 0;
×
1545
    if (fields != NULL) {
×
1546
      *fields = NULL;
×
1547
    }
1548

1549
    return TSDB_CODE_SUCCESS;
×
1550
  }
1551

1552
  CHECK_CODE(buildBoundFields(pDataBlock->boundColsInfo.numOfBound, pDataBlock->boundColsInfo.pColIndex, pSchema,
×
1553
                              fieldNum, fields, pDataBlock->pMeta->tableInfo.precision));
1554

1555
  return TSDB_CODE_SUCCESS;
×
1556
}
1557

1558
int32_t qBuildUpdateStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields, SSHashObj* parsedCols) {
×
1559
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
1560
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
1561
  int32_t        numOfBound = pDataBlock->boundColsInfo.numOfBound + tSimpleHashGetSize(parsedCols);
×
1562
  if (numOfBound <= 0) {
×
1563
    *fieldNum = 0;
×
1564
    if (fields != NULL) {
×
1565
      *fields = NULL;
×
1566
    }
1567

1568
    return TSDB_CODE_SUCCESS;
×
1569
  }
1570

1571
  if (fields != NULL) {
×
1572
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
×
1573
    if (NULL == *fields) {
×
1574
      return terrno;
×
1575
    }
1576

1577
    int32_t actualIdx = 0;
×
1578
    for (int32_t i = 0; i < numOfBound; ++i) {
×
1579
      SSchema* schema;
1580
      int32_t* idx = (int32_t*)tSimpleHashGet(parsedCols, &i, sizeof(int32_t));
×
1581
      if (idx) {
×
1582
        schema = &pSchema[*idx];
×
1583
      } else {
1584
        schema = &pSchema[pDataBlock->boundColsInfo.pColIndex[actualIdx++]];
×
1585
      }
1586
      tstrncpy((*fields)[i].name, schema->name, 65);
×
1587
      (*fields)[i].type = schema->type;
×
1588
      (*fields)[i].bytes = calcTypeBytesFromSchemaBytes(schema->type, schema->bytes, true);
×
1589
      if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
×
1590
        (*fields)[i].precision = pDataBlock->pMeta->tableInfo.precision;
×
1591
      }
1592
    }
1593
  }
1594

1595
  *fieldNum = numOfBound;
×
1596

1597
  return TSDB_CODE_SUCCESS;
×
1598
}
1599

1600
int32_t qBuildStmtStbColFields(void* pBlock, void* boundTags, SSHashObj* parsedCols, uint8_t tbNameFlag,
22✔
1601
                               int32_t* fieldNum, TAOS_FIELD_ALL** fields, int32_t* placeholderOfTags,
1602
                               int32_t* placeholderOfCols) {
1603
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
22✔
1604
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
22✔
1605
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
22✔
1606
    *fieldNum = 0;
×
1607
    if (fields != NULL) {
×
1608
      *fields = NULL;
×
1609
    }
1610

1611
    return TSDB_CODE_SUCCESS;
×
1612
  }
1613

1614
  CHECK_CODE(buildStbBoundFields(pDataBlock->boundColsInfo, pSchema, fieldNum, fields, pDataBlock->pMeta, boundTags,
22✔
1615
                                 parsedCols, tbNameFlag, placeholderOfTags, placeholderOfCols));
1616

1617
  return TSDB_CODE_SUCCESS;
22✔
1618
}
1619

1620
int32_t qResetStmtColumns(SArray* pCols, bool deepClear) {
×
1621
  int32_t colNum = taosArrayGetSize(pCols);
×
1622

1623
  for (int32_t i = 0; i < colNum; ++i) {
×
1624
    SColData* pCol = (SColData*)taosArrayGet(pCols, i);
×
1625
    if (pCol == NULL) {
×
1626
      parserError("qResetStmtColumns column:%d is NULL", i);
×
1627
      return terrno;
×
1628
    }
1629
    if (deepClear) {
×
1630
      tColDataDeepClear(pCol);
×
1631
    } else {
1632
      tColDataClear(pCol);
×
1633
    }
1634
  }
1635

1636
  return TSDB_CODE_SUCCESS;
×
1637
}
1638

1639
int32_t qResetStmtDataBlock(STableDataCxt* block, bool deepClear) {
12,806,713✔
1640
  int32_t        code = 0;
12,806,713✔
1641
  STableDataCxt* pBlock = (STableDataCxt*)block;
12,806,713✔
1642
  int32_t        colNum = taosArrayGetSize(pBlock->pData->aCol);
12,806,713✔
1643

1644
  int8_t flag = 0;
12,816,501✔
1645
  for (int32_t i = 0; i < colNum; ++i) {
51,903,304✔
1646
    flag = pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
39,087,481✔
1647
    if (pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
39,092,366✔
1648
      SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i);
39,092,951✔
1649
      if (pCol == NULL) {
39,096,502✔
UNCOV
1650
        parserError("qResetStmtDataBlock column:%d is NULL", i);
×
UNCOV
1651
        return terrno;
×
1652
      }
1653
      if (deepClear) {
39,096,541✔
1654
        tColDataDeepClear(pCol);
19,198,960✔
1655

1656
      } else {
1657
        tColDataClear(pCol);
19,897,581✔
1658
      }
1659

1660
    } else {
1661
      pBlock->pData->aRowP = taosArrayInit(20, POINTER_BYTES);
×
1662
    }
1663
  }
1664

1665
  tBlobSetDestroy(pBlock->pData->pBlobSet);
12,815,823✔
1666
  pBlock->pData->pBlobSet = NULL;
12,808,310✔
1667
  return code;
12,813,086✔
1668
}
1669

1670
int32_t qCloneStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, bool reset) {
6,346,948✔
1671
  int32_t code = 0;
6,346,948✔
1672

1673
  *pDst = taosMemoryCalloc(1, sizeof(STableDataCxt));
6,346,948✔
1674
  if (NULL == *pDst) {
6,344,623✔
1675
    return terrno;
×
1676
  }
1677

1678
  STableDataCxt* pNewCxt = (STableDataCxt*)*pDst;
6,345,245✔
1679
  STableDataCxt* pCxt = (STableDataCxt*)pSrc;
6,345,556✔
1680
  pNewCxt->hasBlob = pSrc->hasBlob;
6,345,556✔
1681
  pNewCxt->pSchema = NULL;
6,351,827✔
1682
  pNewCxt->pValues = NULL;
6,349,860✔
1683

1684
  if (pCxt->pMeta) {
6,350,186✔
1685
    pNewCxt->pMeta = tableMetaDup(pCxt->pMeta);
6,352,345✔
1686
    // void* pNewMeta = taosMemoryMalloc(TABLE_META_SIZE(pCxt->pMeta));
1687
    // if (NULL == pNewMeta) {
1688
    //   insDestroyTableDataCxt(*pDst);
1689
    //   return terrno;
1690
    // }
1691
    // memcpy(pNewMeta, pCxt->pMeta, TABLE_META_SIZE(pCxt->pMeta));
1692
    // pNewCxt->pMeta = pNewMeta;
1693
  }
1694

1695
  memcpy(&pNewCxt->boundColsInfo, &pCxt->boundColsInfo, sizeof(pCxt->boundColsInfo));
6,348,077✔
1696
  pNewCxt->boundColsInfo.pColIndex = NULL;
6,349,537✔
1697

1698
  if (pCxt->boundColsInfo.pColIndex) {
6,352,036✔
1699
    void* pNewColIdx = taosMemoryMalloc(pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
6,349,154✔
1700
    if (NULL == pNewColIdx) {
6,347,339✔
1701
      insDestroyTableDataCxt(*pDst);
×
1702
      return terrno;
×
1703
    }
1704
    memcpy(pNewColIdx, pCxt->boundColsInfo.pColIndex,
6,348,853✔
1705
           pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
6,347,339✔
1706
    pNewCxt->boundColsInfo.pColIndex = pNewColIdx;
6,350,174✔
1707
  }
1708

1709
  if (pCxt->pData) {
6,353,927✔
1710
    int8_t         flag = 1;
6,346,025✔
1711
    SSubmitTbData* pNewTb = (SSubmitTbData*)taosMemoryMalloc(sizeof(SSubmitTbData));
6,346,025✔
1712
    if (NULL == pNewTb) {
6,343,306✔
1713
      insDestroyTableDataCxt(*pDst);
×
1714
      return terrno;
×
1715
    }
1716

1717
    memcpy(pNewTb, pCxt->pData, sizeof(*pCxt->pData));
6,343,306✔
1718
    pNewTb->pCreateTbReq = NULL;
6,345,842✔
1719
    if (pNewTb->pBlobSet != NULL) {
6,347,421✔
1720
      flag = pNewTb->pBlobSet->type;
×
1721
    }
1722
    pNewTb->pBlobSet = NULL;
6,348,366✔
1723

1724
    pNewTb->aCol = taosArrayDup(pCxt->pData->aCol, NULL);
6,345,481✔
1725
    if (NULL == pNewTb->aCol) {
6,350,212✔
1726
      insDestroyTableDataCxt(*pDst);
×
1727
      return terrno;
×
1728
    }
1729

1730
    pNewCxt->pData = pNewTb;
6,347,675✔
1731

1732
    if (reset) {
6,345,819✔
1733
      code = qResetStmtDataBlock(*pDst, true);
6,350,483✔
1734
    }
1735
  }
1736

1737
  return code;
6,351,172✔
1738
}
1739

1740
int32_t qRebuildStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, uint64_t uid, uint64_t suid, int32_t vgId,
673✔
1741
                              bool rebuildCreateTb) {
1742
  int32_t code = qCloneStmtDataBlock(pDst, pSrc, false);
673✔
1743
  if (code) {
673✔
1744
    return code;
×
1745
  }
1746

1747
  STableDataCxt* pBlock = (STableDataCxt*)*pDst;
673✔
1748
  if (pBlock->pMeta) {
673✔
1749
    pBlock->pMeta->uid = uid;
673✔
1750
    pBlock->pMeta->vgId = vgId;
673✔
1751
    pBlock->pMeta->suid = suid;
673✔
1752
  }
1753

1754
  pBlock->pData->suid = suid;
673✔
1755
  pBlock->pData->uid = uid;
673✔
1756

1757
  if (rebuildCreateTb && NULL == pBlock->pData->pCreateTbReq) {
673✔
1758
    pBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
673✔
1759
    if (NULL == pBlock->pData->pCreateTbReq) {
673✔
1760
      return terrno;
×
1761
    }
1762
  }
1763

1764
  return TSDB_CODE_SUCCESS;
673✔
1765
}
1766

1767
STableMeta* qGetTableMetaInDataBlock(STableDataCxt* pDataBlock) { return ((STableDataCxt*)pDataBlock)->pMeta; }
12,917,977✔
1768

1769
void qDestroyStmtDataBlock(STableDataCxt* pBlock) {
19,281,090✔
1770
  if (pBlock == NULL) {
19,281,090✔
1771
    return;
6,467,532✔
1772
  }
1773

1774
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
12,813,558✔
1775
  insDestroyTableDataCxt(pDataBlock);
12,813,558✔
1776
}
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