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

taosdata / TDengine / #4908

30 Dec 2025 10:52AM UTC coverage: 65.386% (-0.2%) from 65.541%
#4908

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

1330 existing lines in 113 files now uncovered.

193461 of 295877 relevant lines covered (65.39%)

115765274.47 hits per line

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

47.86
/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) {
3,140,888✔
35
  if (pDataBlock == NULL || pDataBlock->pData == NULL) {
3,140,888✔
36
    qError("stmt qCloneCurrentTbData input is null, maybe not bind data before execute");
840✔
37
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
38
  }
39
  *pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
3,141,494✔
40
  if (NULL == *pData) {
3,140,994✔
41
    return terrno;
×
42
  }
43

44
  int8_t         flag = 1;
3,140,691✔
45
  SSubmitTbData* pNew = *pData;
3,140,691✔
46

47
  *pNew = *pDataBlock->pData;
3,141,600✔
48
  pNew->pBlobSet = NULL;
3,143,009✔
49

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

62
  int32_t colNum = taosArrayGetSize(pNew->aCol);
3,140,585✔
63
  for (int32_t i = 0; i < colNum; ++i) {
13,039,752✔
64
    if (pDataBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
9,894,925✔
65
      SColData* pCol = (SColData*)taosArrayGet(pNew->aCol, i);
9,897,509✔
66
      tColDataDeepClear(pCol);
9,895,797✔
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;
3,144,827✔
78
}
79

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

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

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

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

100
  if (pStmt->freeArrayFunc) {
708,422✔
101
    pStmt->freeArrayFunc(pVgDataBlocks);
708,677✔
102
  }
103
  return code;
708,745✔
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) {
3,139,671✔
128
  int32_t             code = TSDB_CODE_SUCCESS;
3,139,671✔
129
  SArray*             pVgDataBlocks = NULL;
3,139,671✔
130
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
3,140,851✔
131

132
  // merge according to vgId
133
  if (taosHashGetSize(pBlockHash) > 0) {
3,141,457✔
134
    code = insMergeTableDataCxt(pBlockHash, &pVgDataBlocks, true);
3,143,615✔
135
  }
136

137
  if (TSDB_CODE_SUCCESS == code) {
3,143,312✔
138
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks, false);
3,143,918✔
139
  }
140

141
  if (pStmt->freeArrayFunc) {
3,140,085✔
142
    pStmt->freeArrayFunc(pVgDataBlocks);
3,142,812✔
143
  }
144
  return code;
3,127,662✔
145
}
146

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

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

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

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

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

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

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

201
      isJson = true;
67✔
202
      char* tmp = taosMemoryCalloc(1, colLen + 1);
67✔
203
      if (!tmp) {
67✔
204
        code = terrno;
×
205
        goto end;
×
206
      }
207
      memcpy(tmp, bind[c].buffer, colLen);
67✔
208
      code = parseJsontoTagData(tmp, pTagArray, &pTag, &pBuf, charsetCxt);
67✔
209
      taosMemoryFree(tmp);
67✔
210
      if (code != TSDB_CODE_SUCCESS) {
67✔
211
        goto end;
×
212
      }
213
    } else {
214
      STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
14,308✔
215
      //      strcpy(val.colName, pTagSchema->name);
216
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
14,308✔
217
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
13,258✔
218
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
1,110✔
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,110✔
231
        val.nData = colLen;
1,110✔
232
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
13,198✔
233
        int32_t output = 0;
678✔
234
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
678✔
235
        if (p == NULL) {
678✔
236
          code = terrno;
×
237
          goto end;
×
238
        }
239
        if (!taosMbsToUcs4(bind[c].buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
678✔
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;
678✔
252
        val.nData = output;
678✔
253
      } else {
254
        memcpy(&val.i64, bind[c].buffer, colLen);
12,520✔
255
      }
256
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
14,308✔
257
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
258
        goto end;
×
259
      }
260
      if (NULL == taosArrayPush(pTagArray, &val)) {
14,308✔
261
        code = terrno;
×
262
        goto end;
×
263
      }
264
    }
265
  }
266

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

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

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

290
end:
5,211✔
291
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
19,579✔
292
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
14,368✔
293
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
14,368✔
294
      taosMemoryFreeClear(p->pData);
738✔
295
    }
296
  }
297
  taosArrayDestroy(pTagArray);
5,211✔
298
  taosArrayDestroy(tagName);
5,211✔
299
  taosMemoryFree(pTag);
5,211✔
300

301
  return code;
5,211✔
302
}
303

304
int32_t convertStmtNcharCol(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_MULTI_BIND* src, TAOS_MULTI_BIND* dst,
18,913,785✔
305
                            void* charsetCxt) {
306
  int32_t output = 0;
18,913,785✔
307
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
19,006,111✔
308
  if (dst->buffer_length < newBuflen) {
19,115,754✔
309
    dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
19,151,738✔
310
    if (NULL == dst->buffer) {
18,858,620✔
311
      return terrno;
×
312
    }
313
  }
314

315
  if (NULL == dst->length) {
18,878,998✔
316
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
19,051,331✔
317
    if (NULL == dst->length) {
19,170,002✔
318
      taosMemoryFreeClear(dst->buffer);
×
319
      return terrno;
×
320
    }
321
  }
322

323
  dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
19,037,814✔
324

325
  for (int32_t i = 0; i < src->num; ++i) {
38,759,731✔
326
    if (src->is_null && src->is_null[i]) {
19,686,433✔
327
      continue;
1,196,663✔
328
    }
329

330
    if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
18,502,142✔
331
                       (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output,
18,514,391✔
332
                       charsetCxt)) {
333
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
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;
18,362,009✔
342
  }
343

344
  dst->buffer_type = src->buffer_type;
19,126,561✔
345
  dst->is_null = src->is_null;
19,149,161✔
346
  dst->num = src->num;
19,054,317✔
347

348
  return TSDB_CODE_SUCCESS;
19,061,148✔
349
}
350

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

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

368
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
8,754,358✔
369
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
7,052,876✔
370
    if (pColSchema->colId <= lastColId) {
7,054,084✔
371
      colInOrder = false;
×
372
    } else {
373
      lastColId = pColSchema->colId;
7,053,910✔
374
    }
375
    // SColData* pCol = taosArrayGet(pCols, c);
376

377
    if (bind[c].num != rowNum) {
7,054,337✔
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,053,694✔
383
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
7,053,479✔
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,050,982✔
389
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
296✔
390
      if (code) {
296✔
391
        goto _return;
×
392
      }
393
      pBind = &ncharBind;
296✔
394
    } else {
395
      pBind = bind + c;
7,052,239✔
396
    }
397

398
    pBindInfos[c].columnId = pColSchema->colId;
7,052,163✔
399
    pBindInfos[c].bind = pBind;
7,053,578✔
400
    pBindInfos[c].type = pColSchema->type;
7,054,316✔
401
  }
402

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

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

408
_return:
1,700,244✔
409

410
  taosMemoryFree(ncharBind.buffer);
1,700,414✔
411
  taosMemoryFree(ncharBind.length);
1,700,601✔
412

413
  return code;
1,700,729✔
414
}
415

416
int32_t qBindStmtColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
45,445,878✔
417
                           void* charsetCxt) {
418
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
45,445,878✔
419
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
45,445,878✔
420
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
46,058,058✔
421
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
45,944,937✔
422
  int32_t          rowNum = bind->num;
45,990,740✔
423
  TAOS_MULTI_BIND  ncharBind = {0};
46,055,841✔
424
  TAOS_MULTI_BIND* pBind = NULL;
46,048,384✔
425
  int32_t          code = 0;
46,048,384✔
426

427
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
438,139,181✔
428
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
393,450,367✔
429
    SColData* pCol = taosArrayGet(pCols, c);
391,549,461✔
430

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

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

442
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
395,535,817✔
443
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
19,117,148✔
444
      if (code) {
18,787,183✔
445
        goto _return;
×
446
      }
447
      pBind = &ncharBind;
18,787,183✔
448
    } else {
449
      pBind = bind + c;
376,975,184✔
450
    }
451

452
    int32_t bytes = 0;
396,075,240✔
453
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
396,075,240✔
454
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
36,588,889✔
UNCOV
455
        bytes = pColSchema->bytes - BLOBSTR_HEADER_SIZE;
×
456
      } else {
457
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
39,776,172✔
458
      }
459
    } else {
460
      bytes = -1;
356,142,845✔
461
    }
462
    code = tColDataAddValueByBind(pCol, pBind, bytes, initCtxAsText, checkWKB);
395,683,790✔
463
    if (code) {
392,094,403✔
464
      goto _return;
3,606✔
465
    }
466
  }
467

468
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
46,695,238✔
469

470
_return:
46,698,844✔
471

472
  taosMemoryFree(ncharBind.buffer);
46,666,759✔
473
  taosMemoryFree(ncharBind.length);
45,860,146✔
474

475
  return code;
46,183,624✔
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 buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
569
  }
570

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

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

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

587
_return:
840✔
588

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

592
  return code;
840✔
593
}
594

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

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

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

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

628
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
403,994✔
629

630
  bool  isJson = false;
404,114✔
631
  STag* pTag = NULL;
404,114✔
632

633
  int bindIdx = 0;
404,114✔
634
  for (int c = 0; c < tags->numOfBound; ++c) {
1,212,132✔
635
    if (bind == NULL) {
807,988✔
636
      break;
×
637
    }
638
    if (tags->parseredTags) {
807,988✔
639
      bool found = false;
×
640
      for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
×
641
        if (tags->parseredTags->pTagIndex[k] == tags->pColIndex[c]) {
×
642
          found = true;
×
643
          break;
×
644
        }
645
      }
646
      if (found) {
×
647
        continue;
×
648
      }
649
    }
650

651
    TAOS_STMT2_BIND bindData = bind[bindIdx++];
807,988✔
652

653
    if (bindData.is_null && bindData.is_null[0]) {
807,988✔
654
      continue;
×
655
    }
656

657
    SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
807,988✔
658
    int32_t  colLen = pTagSchema->bytes;
807,988✔
659
    if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
807,988✔
660
      if (IS_STR_DATA_BLOB(pTagSchema->type)) {
404,064✔
661
        return TSDB_CODE_BLOB_NOT_SUPPORT_TAG;
×
662
      }
663
      if (!bindData.length) {
404,064✔
664
        code = buildInvalidOperationMsg(&pBuf, "var tag length is null");
×
665
        goto end;
×
666
      }
667
      colLen = bindData.length[0];
404,064✔
668
      if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
404,064✔
669
        code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
×
670
        goto end;
×
671
      }
672
    }
673
    if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
1,616,166✔
674
      code = terrno;
×
675
      goto end;
×
676
    }
677
    if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
808,178✔
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};
808,178✔
697
      //      strcpy(val.colName, pTagSchema->name);
698
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
808,178✔
699
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
404,124✔
700
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
404,204✔
701
          code = initCtxAsText();
×
702
          if (code) {
×
703
            qError("geometry init failed:%s", tstrerror(code));
×
704
            goto end;
×
705
          }
706
          code = checkWKB(bindData.buffer, colLen);
×
707
          if (code) {
×
708
            qError("stmt2 bind invalid geometry tag:%s, must be WKB format", (char*)bindData.buffer);
50✔
709
            goto end;
×
710
          }
711
        }
712
        val.pData = (uint8_t*)bindData.buffer;
404,154✔
713
        val.nData = colLen;
404,154✔
714
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
403,974✔
715
        int32_t output = 0;
×
716
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
×
717
        if (p == NULL) {
×
718
          code = terrno;
×
719
          goto end;
×
720
        }
721
        if (colLen != 0) {
×
722
          if (!taosMbsToUcs4(bindData.buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
×
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;
×
736
        val.nData = output;
×
737

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

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

760
  if (pCreateTbReq) {
404,134✔
761
    code = insBuildCreateTbReq(pCreateTbReq, tName, pTag, suid, sTableName, tagName,
403,990✔
762
                               pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
403,990✔
763
    pTag = NULL;
404,050✔
764
    goto end;
404,050✔
765
  }
766

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

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

786
end:
404,184✔
787
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
1,212,292✔
788
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
808,138✔
789
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
808,108✔
790
      taosMemoryFreeClear(p->pData);
×
791
    }
792
  }
793
  taosArrayDestroy(pTagArray);
404,024✔
794
  taosArrayDestroy(tagName);
404,204✔
795
  taosMemoryFree(pTag);
404,184✔
796

797
  return code;
404,144✔
798
}
799

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

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

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

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

823
    if (src->length[i] == 0) {
×
824
      dst->length[i] = 0;
×
825
      continue;
×
826
    }
827

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

837
    dst->length[i] = output;
×
838
    src_buf += src->length[i];
×
839
    dst_buf += output;
×
840
  }
841

842
  dst->buffer_type = src->buffer_type;
×
843
  dst->is_null = src->is_null;
×
844
  dst->num = src->num;
×
845

846
  return TSDB_CODE_SUCCESS;
×
847
}
848

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

871
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
7,996,932✔
872
    if (TSDB_DATA_TYPE_NCHAR == pSchema[boundInfo->pColIndex[c]].type) {
7,139,237✔
873
      ncharColNums++;
×
874
    }
875
  }
876
  if (ncharColNums > 0) {
862,574✔
877
    ncharBinds = taosArrayInit(ncharColNums, sizeof(ncharBind));
×
878
    if (!ncharBinds) {
×
879
      code = terrno;
×
880
      TAOS_CHECK_GOTO(code, &lino, _return);
×
881
    }
882
  }
883

884
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
8,031,113✔
885
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
7,168,237✔
886
    if (pColSchema->colId <= lastColId) {
7,167,746✔
887
      colInOrder = false;
×
888
    } else {
889
      lastColId = pColSchema->colId;
7,170,233✔
890
    }
891

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

902
    TAOS_STMT2_BIND bindData = bind[bindIdx];
7,172,713✔
903

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

909
    if ((!(rowNum == 1 && bindData.is_null && *bindData.is_null)) &&
7,170,603✔
910
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
7,168,196✔
911
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
912
      goto _return;
×
913
    }
914

915
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
7,170,782✔
916
      code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
×
917
      TAOS_CHECK_GOTO(code, &lino, _return);
×
918

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

948
    pBindInfos[c].columnId = pColSchema->colId;
7,157,422✔
949
    pBindInfos[c].type = pColSchema->type;
7,147,551✔
950
    pBindInfos[c].bytes = pColSchema->bytes;
7,168,750✔
951

952
    bindIdx++;
7,168,445✔
953
  }
954

955
  if (hasBlob == 0) {
862,773✔
956
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pSchemaExt, pCols,
862,449✔
957
                              &pDataBlock->ordered, &pDataBlock->duplicateTs);
958
    TAOS_CHECK_GOTO(code, &lino, _return);
862,644✔
959
  } else {
960
    code = tBlobSetCreate(1024, 1, ppBlob);
324✔
961
    TAOS_CHECK_GOTO(code, &lino, _return);
×
962

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

968
  parserDebug("stmt2 all %d columns bind %d rows data as row format", boundInfo->numOfBound, rowNum);
862,574✔
969

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

983
  return code;
862,489✔
984
}
985

986
static int32_t convertStmtNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst,
×
987
                                    void* charsetCxt) {
988
  int32_t       output = 0;
×
989
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
×
990

991
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
×
992
  // if (dst->buffer_length < newBuflen) {
993
  dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
×
994
  if (NULL == dst->buffer) {
×
995
    return terrno;
×
996
  }
997
  //}
998

999
  if (NULL == dst->length) {
×
1000
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
×
1001
    if (NULL == dst->length) {
×
1002
      taosMemoryFreeClear(dst->buffer);
×
1003
      return terrno;
×
1004
    }
1005
  }
1006

1007
  // dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
1008
  char* src_buf = src->buffer;
×
1009
  char* dst_buf = dst->buffer;
×
1010
  for (int32_t i = 0; i < src->num; ++i) {
×
1011
    if (src->is_null && src->is_null[i]) {
×
1012
      continue;
×
1013
    }
1014

1015
    if (src->length[i] == 0) {
×
1016
      dst->length[i] = 0;
×
1017
      continue;
×
1018
    }
1019

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

1031
    dst->length[i] = output;
×
1032
    src_buf += src->length[i];
×
1033
    dst_buf += output;
×
1034
  }
1035

1036
  dst->buffer_type = src->buffer_type;
×
1037
  dst->is_null = src->is_null;
×
1038
  dst->num = src->num;
×
1039

1040
  return TSDB_CODE_SUCCESS;
×
1041
}
1042

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

1057
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
670✔
1058
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
536✔
1059
    SColData* pCol = taosArrayGet(pCols, c);
536✔
1060
    if (pCol == NULL || pColSchema == NULL) {
536✔
1061
      code = buildInvalidOperationMsg(&pBuf, "get column schema or column data failed");
×
1062
      goto _return;
×
1063
    }
1064

1065
    if (boundInfo->pColIndex[c] == 0) {
536✔
1066
      pCol->cflag |= COL_IS_KEY;
134✔
1067
    }
1068

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

1088
    if ((!(rowNum == 1 && bindData.is_null && *bindData.is_null)) &&
536✔
1089
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
536✔
1090
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
1091
      goto _return;
×
1092
    }
1093

1094
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
536✔
1095
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
×
1096
      if (code) {
×
1097
        goto _return;
×
1098
      }
1099
      pBind = &ncharBind;
×
1100
    } else {
1101
      pBind = bind + bindIdx;
536✔
1102
    }
1103
    int8_t  isBlob = 0;
536✔
1104
    int32_t bytes = -1;
536✔
1105
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
536✔
1106
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
×
1107
        isBlob = 1;
×
1108
        bytes = TSDB_MAX_BLOB_LEN;
×
1109
      } else {
1110
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
×
1111
      }
1112
    }
1113

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

1137
    if (code) {
536✔
1138
      parserError("stmt2 failed to add col:%d ,type:%d, ErrCode: 0x%x", c, pColSchema->type, code);
×
1139
      goto _return;
×
1140
    }
1141
    bindIdx++;
536✔
1142
  }
1143

1144
  parserDebug("stmt2 all %d columns bind %d rows data as col format", boundInfo->numOfBound, rowNum);
134✔
1145

1146
_return:
134✔
1147

1148
  taosMemoryFree(ncharBind.buffer);
134✔
1149
  taosMemoryFree(ncharBind.length);
134✔
1150

1151
  return code;
134✔
1152
}
1153

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

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

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

1176
  if (bind->buffer_type != pColSchema->type) {
×
1177
    return buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
1178
  }
1179

1180
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
×
1181
    code = convertStmtNcharCol2(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
×
1182
    if (code) {
×
1183
      goto _return;
×
1184
    }
1185
    pBind = &ncharBind;
×
1186
  } else {
1187
    pBind = bind;
×
1188
  }
1189

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

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

1227
  if (code) {
×
1228
    parserError("stmt2 failed to add single col:%d ,type:%d, ErrCode: 0x%x", colIdx, pColSchema->type, code);
×
1229
    goto _return;
×
1230
  }
1231

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

1234
_return:
×
1235

1236
  taosMemoryFree(ncharBind.buffer);
×
1237
  taosMemoryFree(ncharBind.length);
×
1238

1239
  return code;
×
1240
}
1241

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

1257
  if (NULL == pTSchema || NULL == *pTSchema) {
×
1258
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
×
1259
  }
1260

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

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

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

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

1312
    pBindInfos[c].columnId = pColSchema->colId;
×
1313
    pBindInfos[c].type = pColSchema->type;
×
1314
    pBindInfos[c].bytes = pColSchema->bytes;
×
1315

1316
    if (code) {
×
1317
      goto _return;
×
1318
    }
1319
  }
1320

1321
  pDataBlock->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
×
1322
  if (pDataBlock->pData->pCreateTbReq != NULL) {
×
1323
    pDataBlock->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
×
1324
  }
1325

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

1334
_return:
×
1335

1336
  taosMemoryFree(ncharBind.buffer);
×
1337
  taosMemoryFree(ncharBind.length);
×
1338

1339
  return code;
×
1340
}
1341

1342
int32_t buildBoundFields(int32_t numOfBound, int16_t* boundColumns, SSchema* pSchema, int32_t* fieldNum,
×
1343
                         TAOS_FIELD_E** fields, uint8_t timePrec) {
1344
  if (fields != NULL) {
×
1345
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
×
1346
    if (NULL == *fields) {
×
1347
      return terrno;
×
1348
    }
1349

1350
    SSchema* schema = &pSchema[boundColumns[0]];
×
1351
    if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
×
1352
      (*fields)[0].precision = timePrec;
×
1353
    }
1354

1355
    for (int32_t i = 0; i < numOfBound; ++i) {
×
1356
      schema = &pSchema[boundColumns[i]];
×
1357
      tstrncpy((*fields)[i].name, schema->name, 65);
×
1358
      (*fields)[i].type = schema->type;
×
1359
      (*fields)[i].bytes = calcTypeBytesFromSchemaBytes(schema->type, schema->bytes, true);
×
1360
    }
1361
  }
1362

1363
  *fieldNum = numOfBound;
×
1364

1365
  return TSDB_CODE_SUCCESS;
×
1366
}
1367

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

1380
  // Adjust the number of bound fields if there are parsed tags or parsed columns
1381
  if (tags && tags->parseredTags) {
19✔
1382
    numOfBound -= tags->parseredTags->numOfTags;
×
1383
  }
1384
  if (parsedCols) {
19✔
1385
    numOfBound -= tSimpleHashGetSize(parsedCols);
×
1386
  }
1387

1388
  int32_t idx = 0;
19✔
1389
  if (fields != NULL) {
19✔
1390
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_ALL));
19✔
1391
    if (NULL == *fields) {
19✔
1392
      return terrno;
×
1393
    }
1394
  }
1395

1396
  if (hasPreBindTbname && fields != NULL) {
19✔
1397
    (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
11✔
1398
    tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
11✔
1399
    (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
11✔
1400
    (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
11✔
1401
    idx++;
11✔
1402
  }
1403

1404
  if (hastag && tags->numOfBound > 0 && !tags->mixTagsCols) {
19✔
1405
    SSchema* tagSchema = getTableTagSchema(pMeta);
11✔
1406

1407
    for (int32_t i = 0; i < tags->numOfBound; ++i) {
33✔
1408
      SSchema* schema = &tagSchema[tags->pColIndex[i]];
22✔
1409

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

1424
      if (placeholderOfTags != NULL) {
22✔
1425
        (*placeholderOfTags)++;
22✔
1426
      }
1427

1428
      if (fields != NULL) {
22✔
1429
        (*fields)[idx].field_type = TAOS_FIELD_TAG;
22✔
1430
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[i].name));
22✔
1431
        (*fields)[idx].type = schema->type;
22✔
1432
        (*fields)[idx].bytes = schema->bytes;
22✔
1433
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
22✔
1434
          (*fields)[idx].precision = pMeta->tableInfo.precision;
×
1435
        }
1436
      }
1437
      idx++;
22✔
1438
    }
1439
  }
1440

1441
  if (boundColsInfo.numOfBound > 0) {
19✔
1442
    SSchema* schema = &pSchema[boundColsInfo.pColIndex[0]];
19✔
1443

1444
    for (int32_t i = 0; i < boundColsInfo.numOfBound; ++i) {
119✔
1445
      int16_t idxCol = boundColsInfo.pColIndex[i];
100✔
1446

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

1488
      if (fields != NULL) {
92✔
1489
        schema = &pSchema[idxCol];
92✔
1490
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[idx].name));
92✔
1491
        (*fields)[idx].type = schema->type;
92✔
1492
        (*fields)[idx].bytes = schema->bytes;
92✔
1493
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
92✔
1494
          (*fields)[idx].precision = pMeta->tableInfo.precision;
19✔
1495
        }
1496
        if (TSDB_DATA_TYPE_DECIMAL64 == schema->type || TSDB_DATA_TYPE_DECIMAL == schema->type) {
92✔
1497
          decimalFromTypeMod(pMeta->schemaExt[idxCol].typeMod, &(*fields)[idx].precision, &(*fields)[idx].scale);
×
1498
        }
1499
      }
1500
      idx++;
92✔
1501
    }
1502
  }
1503

1504
  if (fieldNum != NULL) {
19✔
1505
    *fieldNum = numOfBound;
19✔
1506
  }
1507

1508
  return TSDB_CODE_SUCCESS;
19✔
1509
}
1510

1511
int32_t qBuildStmtTagFields(void* pBlock, void* boundTags, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1512
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
1513
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
×
1514
  if (NULL == tags) {
×
1515
    return TSDB_CODE_APP_ERROR;
×
1516
  }
1517

1518
  if (pDataBlock->pMeta->tableType != TSDB_SUPER_TABLE && pDataBlock->pMeta->tableType != TSDB_CHILD_TABLE) {
×
1519
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1520
  }
1521

1522
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
×
1523
  if (tags->numOfBound <= 0) {
×
1524
    *fieldNum = 0;
×
1525
    *fields = NULL;
×
1526

1527
    return TSDB_CODE_SUCCESS;
×
1528
  }
1529

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

1532
  return TSDB_CODE_SUCCESS;
×
1533
}
1534

1535
int32_t qBuildStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1536
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
1537
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
1538
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
×
1539
    *fieldNum = 0;
×
1540
    if (fields != NULL) {
×
1541
      *fields = NULL;
×
1542
    }
1543

1544
    return TSDB_CODE_SUCCESS;
×
1545
  }
1546

1547
  CHECK_CODE(buildBoundFields(pDataBlock->boundColsInfo.numOfBound, pDataBlock->boundColsInfo.pColIndex, pSchema,
×
1548
                              fieldNum, fields, pDataBlock->pMeta->tableInfo.precision));
1549

1550
  return TSDB_CODE_SUCCESS;
×
1551
}
1552

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

1563
    return TSDB_CODE_SUCCESS;
×
1564
  }
1565

1566
  if (fields != NULL) {
×
1567
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
×
1568
    if (NULL == *fields) {
×
1569
      return terrno;
×
1570
    }
1571

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

1590
  *fieldNum = numOfBound;
×
1591

1592
  return TSDB_CODE_SUCCESS;
×
1593
}
1594

1595
int32_t qBuildStmtStbColFields(void* pBlock, void* boundTags, SSHashObj* parsedCols, uint8_t tbNameFlag,
19✔
1596
                               int32_t* fieldNum, TAOS_FIELD_ALL** fields, int32_t* placeholderOfTags,
1597
                               int32_t* placeholderOfCols) {
1598
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
19✔
1599
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
19✔
1600
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
19✔
1601
    *fieldNum = 0;
×
1602
    if (fields != NULL) {
×
1603
      *fields = NULL;
×
1604
    }
1605

1606
    return TSDB_CODE_SUCCESS;
×
1607
  }
1608

1609
  CHECK_CODE(buildStbBoundFields(pDataBlock->boundColsInfo, pSchema, fieldNum, fields, pDataBlock->pMeta, boundTags,
19✔
1610
                                 parsedCols, tbNameFlag, placeholderOfTags, placeholderOfCols));
1611

1612
  return TSDB_CODE_SUCCESS;
19✔
1613
}
1614

1615
int32_t qResetStmtColumns(SArray* pCols, bool deepClear) {
×
1616
  int32_t colNum = taosArrayGetSize(pCols);
×
1617

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

1631
  return TSDB_CODE_SUCCESS;
×
1632
}
1633

1634
int32_t qResetStmtDataBlock(STableDataCxt* block, bool deepClear) {
6,182,401✔
1635
  int32_t        code = 0;
6,182,401✔
1636
  STableDataCxt* pBlock = (STableDataCxt*)block;
6,182,401✔
1637
  int32_t        colNum = taosArrayGetSize(pBlock->pData->aCol);
6,182,401✔
1638

1639
  int8_t flag = 0;
6,185,602✔
1640
  for (int32_t i = 0; i < colNum; ++i) {
25,350,493✔
1641
    flag = pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
19,166,791✔
1642
    if (pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
19,170,274✔
1643
      SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i);
19,172,774✔
1644
      if (pCol == NULL) {
19,171,238✔
1645
        parserError("qResetStmtDataBlock column:%d is NULL", i);
×
1646
        return terrno;
×
1647
      }
1648
      if (deepClear) {
19,171,238✔
1649
        tColDataDeepClear(pCol);
9,276,726✔
1650

1651
      } else {
1652
        tColDataClear(pCol);
9,894,512✔
1653
      }
1654

1655
    } else {
1656
      pBlock->pData->aRowP = taosArrayInit(20, POINTER_BYTES);
×
1657
    }
1658
  }
1659

1660
  tBlobSetDestroy(pBlock->pData->pBlobSet);
6,183,702✔
1661
  pBlock->pData->pBlobSet = NULL;
6,180,310✔
1662
  return code;
6,182,733✔
1663
}
1664

1665
int32_t qCloneStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, bool reset) {
3,040,536✔
1666
  int32_t code = 0;
3,040,536✔
1667

1668
  *pDst = taosMemoryCalloc(1, sizeof(STableDataCxt));
3,040,536✔
1669
  if (NULL == *pDst) {
3,040,520✔
1670
    return terrno;
×
1671
  }
1672

1673
  STableDataCxt* pNewCxt = (STableDataCxt*)*pDst;
3,040,520✔
1674
  STableDataCxt* pCxt = (STableDataCxt*)pSrc;
3,040,520✔
1675
  pNewCxt->hasBlob = pSrc->hasBlob;
3,040,520✔
1676
  pNewCxt->pSchema = NULL;
3,042,338✔
1677
  pNewCxt->pValues = NULL;
3,041,488✔
1678

1679
  if (pCxt->pMeta) {
3,041,732✔
1680
    pNewCxt->pMeta = tableMetaDup(pCxt->pMeta);
3,042,338✔
1681
    // void* pNewMeta = taosMemoryMalloc(TABLE_META_SIZE(pCxt->pMeta));
1682
    // if (NULL == pNewMeta) {
1683
    //   insDestroyTableDataCxt(*pDst);
1684
    //   return terrno;
1685
    // }
1686
    // memcpy(pNewMeta, pCxt->pMeta, TABLE_META_SIZE(pCxt->pMeta));
1687
    // pNewCxt->pMeta = pNewMeta;
1688
  }
1689

1690
  memcpy(&pNewCxt->boundColsInfo, &pCxt->boundColsInfo, sizeof(pCxt->boundColsInfo));
3,041,185✔
1691
  pNewCxt->boundColsInfo.pColIndex = NULL;
3,042,641✔
1692

1693
  if (pCxt->boundColsInfo.pColIndex) {
3,042,338✔
1694
    void* pNewColIdx = taosMemoryMalloc(pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
3,042,338✔
1695
    if (NULL == pNewColIdx) {
3,041,126✔
1696
      insDestroyTableDataCxt(*pDst);
×
1697
      return terrno;
×
1698
    }
1699
    memcpy(pNewColIdx, pCxt->boundColsInfo.pColIndex,
3,041,429✔
1700
           pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
3,041,126✔
1701
    pNewCxt->boundColsInfo.pColIndex = pNewColIdx;
3,041,429✔
1702
  }
1703

1704
  if (pCxt->pData) {
3,042,338✔
1705
    int8_t         flag = 1;
3,042,641✔
1706
    SSubmitTbData* pNewTb = (SSubmitTbData*)taosMemoryMalloc(sizeof(SSubmitTbData));
3,042,641✔
1707
    if (NULL == pNewTb) {
3,039,914✔
1708
      insDestroyTableDataCxt(*pDst);
×
1709
      return terrno;
×
1710
    }
1711

1712
    memcpy(pNewTb, pCxt->pData, sizeof(*pCxt->pData));
3,039,914✔
1713
    pNewTb->pCreateTbReq = NULL;
3,039,914✔
1714
    if (pNewTb->pBlobSet != NULL) {
3,042,944✔
1715
      flag = pNewTb->pBlobSet->type;
×
1716
    }
1717
    pNewTb->pBlobSet = NULL;
3,041,992✔
1718

1719
    pNewTb->aCol = taosArrayDup(pCxt->pData->aCol, NULL);
3,041,689✔
1720
    if (NULL == pNewTb->aCol) {
3,043,247✔
1721
      insDestroyTableDataCxt(*pDst);
×
1722
      return terrno;
×
1723
    }
1724

1725
    pNewCxt->pData = pNewTb;
3,039,973✔
1726

1727
    if (reset) {
3,041,126✔
1728
      code = qResetStmtDataBlock(*pDst, true);
3,040,332✔
1729
    }
1730
  }
1731

1732
  return code;
3,040,882✔
1733
}
1734

1735
int32_t qRebuildStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, uint64_t uid, uint64_t suid, int32_t vgId,
550✔
1736
                              bool rebuildCreateTb) {
1737
  int32_t code = qCloneStmtDataBlock(pDst, pSrc, false);
550✔
1738
  if (code) {
550✔
1739
    return code;
×
1740
  }
1741

1742
  STableDataCxt* pBlock = (STableDataCxt*)*pDst;
550✔
1743
  if (pBlock->pMeta) {
550✔
1744
    pBlock->pMeta->uid = uid;
550✔
1745
    pBlock->pMeta->vgId = vgId;
550✔
1746
    pBlock->pMeta->suid = suid;
550✔
1747
  }
1748

1749
  pBlock->pData->suid = suid;
550✔
1750
  pBlock->pData->uid = uid;
550✔
1751

1752
  if (rebuildCreateTb && NULL == pBlock->pData->pCreateTbReq) {
550✔
1753
    pBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
550✔
1754
    if (NULL == pBlock->pData->pCreateTbReq) {
550✔
1755
      return terrno;
×
1756
    }
1757
  }
1758

1759
  return TSDB_CODE_SUCCESS;
550✔
1760
}
1761

1762
STableMeta* qGetTableMetaInDataBlock(STableDataCxt* pDataBlock) { return ((STableDataCxt*)pDataBlock)->pMeta; }
6,277,200✔
1763

1764
void qDestroyStmtDataBlock(STableDataCxt* pBlock) {
9,333,771✔
1765
  if (pBlock == NULL) {
9,333,771✔
1766
    return;
3,147,213✔
1767
  }
1768

1769
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
6,186,558✔
1770
  insDestroyTableDataCxt(pDataBlock);
6,186,558✔
1771
}
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