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

taosdata / TDengine / #3593

24 Jan 2025 08:57AM UTC coverage: 63.239% (-0.3%) from 63.546%
#3593

push

travis-ci

web-flow
Merge pull request #29638 from taosdata/docs/TS-5846-3.0

enh: TDengine modify taosBenchmark new query rule cases and add doc

140619 of 285630 branches covered (49.23%)

Branch coverage included in aggregate %.

218877 of 282844 relevant lines covered (77.38%)

19647377.39 hits per line

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

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

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

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

39
  SSubmitTbData* pNew = *pData;
91,206✔
40

41
  *pNew = *pDataBlock->pData;
91,206✔
42

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

55
  int32_t colNum = taosArrayGetSize(pNew->aCol);
91,206✔
56
  for (int32_t i = 0; i < colNum; ++i) {
884,931✔
57
    SColData* pCol = (SColData*)taosArrayGet(pNew->aCol, i);
793,726✔
58
    tColDataDeepClear(pCol);
793,726✔
59
  }
60

61
  return TSDB_CODE_SUCCESS;
91,205✔
62
}
63

64
int32_t qAppendStmtTableOutput(SQuery* pQuery, SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
5,493✔
65
                               SStbInterlaceInfo* pBuildInfo) {
66
  // merge according to vgId
67
  return insAppendStmtTableDataCxt(pAllVgHash, pTbData, pTbCtx, pBuildInfo);
5,493✔
68
}
69

70
int32_t qBuildStmtFinOutput(SQuery* pQuery, SHashObj* pAllVgHash, SArray* pVgDataBlocks) {
115✔
71
  int32_t             code = TSDB_CODE_SUCCESS;
115✔
72
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
115✔
73

74
  if (TSDB_CODE_SUCCESS == code) {
115!
75
    code = insBuildVgDataBlocks(pAllVgHash, pVgDataBlocks, &pStmt->pDataBlocks, true);
115✔
76
  }
77

78
  if (pStmt->freeArrayFunc) {
115!
79
    pStmt->freeArrayFunc(pVgDataBlocks);
115✔
80
  }
81
  return code;
115✔
82
}
83

84
/*
85
int32_t qBuildStmtOutputFromTbList(SQuery* pQuery, SHashObj* pVgHash, SArray* pBlockList, STableDataCxt* pTbCtx, int32_t
86
tbNum) { int32_t             code = TSDB_CODE_SUCCESS; SArray*             pVgDataBlocks = NULL; SVnodeModifyOpStmt*
87
pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
88

89
  // merge according to vgId
90
  if (tbNum > 0) {
91
    code = insMergeStmtTableDataCxt(pTbCtx, pBlockList, &pVgDataBlocks, true, tbNum);
92
  }
93

94
  if (TSDB_CODE_SUCCESS == code) {
95
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks);
96
  }
97

98
  if (pStmt->freeArrayFunc) {
99
    pStmt->freeArrayFunc(pVgDataBlocks);
100
  }
101
  return code;
102
}
103
*/
104

105
int32_t qBuildStmtOutput(SQuery* pQuery, SHashObj* pVgHash, SHashObj* pBlockHash) {
91,206✔
106
  int32_t             code = TSDB_CODE_SUCCESS;
91,206✔
107
  SArray*             pVgDataBlocks = NULL;
91,206✔
108
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
91,206✔
109

110
  // merge according to vgId
111
  if (taosHashGetSize(pBlockHash) > 0) {
91,206!
112
    code = insMergeTableDataCxt(pBlockHash, &pVgDataBlocks, true);
91,206✔
113
  }
114

115
  if (TSDB_CODE_SUCCESS == code) {
91,206!
116
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks, false);
91,206✔
117
  }
118

119
  if (pStmt->freeArrayFunc) {
91,206!
120
    pStmt->freeArrayFunc(pVgDataBlocks);
91,206✔
121
  }
122
  return code;
91,206✔
123
}
124

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

135
  SArray* pTagArray = taosArrayInit(tags->numOfBound, sizeof(STagVal));
39,864✔
136
  if (!pTagArray) {
39,864!
137
    return buildInvalidOperationMsg(&pBuf, "out of memory");
×
138
  }
139

140
  SArray* tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
39,864✔
141
  if (!tagName) {
39,864!
142
    code = buildInvalidOperationMsg(&pBuf, "out of memory");
×
143
    goto end;
×
144
  }
145

146
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
39,864✔
147

148
  bool  isJson = false;
39,864✔
149
  STag* pTag = NULL;
39,864✔
150

151
  for (int c = 0; c < tags->numOfBound; ++c) {
362,118✔
152
    if (bind[c].is_null && bind[c].is_null[0]) {
322,254✔
153
      continue;
1,565✔
154
    }
155

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

179
      isJson = true;
×
180
      char* tmp = taosMemoryCalloc(1, colLen + 1);
×
181
      if (!tmp) {
×
182
        code = terrno;
×
183
        goto end;
×
184
      }
185
      memcpy(tmp, bind[c].buffer, colLen);
×
186
      code = parseJsontoTagData(tmp, pTagArray, &pTag, &pBuf, charsetCxt);
×
187
      taosMemoryFree(tmp);
×
188
      if (code != TSDB_CODE_SUCCESS) {
×
189
        goto end;
×
190
      }
191
    } else {
192
      STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
320,689✔
193
      //      strcpy(val.colName, pTagSchema->name);
194
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
320,689!
195
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
297,733✔
196
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
22,957✔
197
          if (initCtxAsText() || checkWKB(bind[c].buffer, colLen)) {
1!
198
            code = buildSyntaxErrMsg(&pBuf, "invalid geometry tag", bind[c].buffer);
×
199
            goto end;
×
200
          }
201
        }
202
        val.pData = (uint8_t*)bind[c].buffer;
22,957✔
203
        val.nData = colLen;
22,957✔
204
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
297,732✔
205
        int32_t output = 0;
22,726✔
206
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
22,726!
207
        if (p == NULL) {
22,726!
208
          code = terrno;
×
209
          goto end;
×
210
        }
211
        if (!taosMbsToUcs4(bind[c].buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
22,726!
212
          if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
213
            taosMemoryFree(p);
×
214
            code = generateSyntaxErrMsg(&pBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pTagSchema->name);
×
215
            goto end;
×
216
          }
217
          char buf[512] = {0};
×
218
          snprintf(buf, tListLen(buf), " taosMbsToUcs4 error:%s", strerror(terrno));
×
219
          taosMemoryFree(p);
×
220
          code = buildSyntaxErrMsg(&pBuf, buf, bind[c].buffer);
×
221
          goto end;
×
222
        }
223
        val.pData = p;
22,726✔
224
        val.nData = output;
22,726✔
225
      } else {
226
        memcpy(&val.i64, bind[c].buffer, colLen);
275,006✔
227
      }
228
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
320,689!
229
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
230
        goto end;
×
231
      }
232
      if (NULL == taosArrayPush(pTagArray, &val)) {
320,689!
233
        code = terrno;
×
234
        goto end;
×
235
      }
236
    }
237
  }
238

239
  if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) {
39,864!
240
    goto end;
×
241
  }
242

243
  if (NULL == pDataBlock->pData->pCreateTbReq) {
39,864✔
244
    pDataBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
186!
245
    if (NULL == pDataBlock->pData->pCreateTbReq) {
186!
246
      code = terrno;
×
247
      goto end;
×
248
    }
249
  }
250

251
  code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName,
39,864✔
252
                             pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
39,864✔
253
  pTag = NULL;
39,864✔
254

255
end:
39,864✔
256
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
360,553✔
257
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
320,689✔
258
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
320,689✔
259
      taosMemoryFreeClear(p->pData);
22,726!
260
    }
261
  }
262
  taosArrayDestroy(pTagArray);
39,864✔
263
  taosArrayDestroy(tagName);
39,864✔
264
  taosMemoryFree(pTag);
39,864!
265

266
  return code;
39,864✔
267
}
268

269
int32_t convertStmtNcharCol(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_MULTI_BIND* src, TAOS_MULTI_BIND* dst, void* charsetCxt) {
150,002✔
270
  int32_t output = 0;
150,002✔
271
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
150,002✔
272
  if (dst->buffer_length < newBuflen) {
150,002!
273
    dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
150,002!
274
    if (NULL == dst->buffer) {
150,002!
275
      return terrno;
×
276
    }
277
  }
278

279
  if (NULL == dst->length) {
150,002!
280
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
150,002!
281
    if (NULL == dst->length) {
150,002!
282
      taosMemoryFreeClear(dst->buffer);
×
283
      return terrno;
×
284
    }
285
  }
286

287
  dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
150,002✔
288

289
  for (int32_t i = 0; i < src->num; ++i) {
447,582✔
290
    if (src->is_null && src->is_null[i]) {
297,579✔
291
      continue;
1,600✔
292
    }
293

294
    if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
295,980!
295
                       (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output, charsetCxt)) {
295,979✔
296
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
297
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
298
      }
299
      char buf[512] = {0};
×
300
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
301
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
302
    }
303

304
    dst->length[i] = output;
295,980✔
305
  }
306

307
  dst->buffer_type = src->buffer_type;
150,003✔
308
  dst->is_null = src->is_null;
150,003✔
309
  dst->num = src->num;
150,003✔
310

311
  return TSDB_CODE_SUCCESS;
150,003✔
312
}
313

314
int32_t qBindStmtStbColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
5,492✔
315
                              STSchema** pTSchema, SBindInfo* pBindInfos, void* charsetCxt) {
316
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
5,492✔
317
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
5,492✔
318
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
5,493✔
319
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
5,493✔
320
  int32_t          rowNum = bind->num;
5,493✔
321
  TAOS_MULTI_BIND  ncharBind = {0};
5,493✔
322
  TAOS_MULTI_BIND* pBind = NULL;
5,493✔
323
  int32_t          code = 0;
5,493✔
324
  int16_t          lastColId = -1;
5,493✔
325
  bool             colInOrder = true;
5,493✔
326

327
  if (NULL == *pTSchema) {
5,493✔
328
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
14✔
329
  }
330

331
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
82,140✔
332
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
76,647✔
333
    if (pColSchema->colId <= lastColId) {
76,647!
334
      colInOrder = false;
×
335
    } else {
336
      lastColId = pColSchema->colId;
76,647✔
337
    }
338
    // SColData* pCol = taosArrayGet(pCols, c);
339

340
    if (bind[c].num != rowNum) {
76,647!
341
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
342
      goto _return;
×
343
    }
344

345
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
76,647!
346
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
76,647!
347
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
348
      goto _return;
×
349
    }
350

351
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
76,647✔
352
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
5,466✔
353
      if (code) {
5,466!
354
        goto _return;
×
355
      }
356
      pBind = &ncharBind;
5,466✔
357
    } else {
358
      pBind = bind + c;
71,181✔
359
    }
360

361
    pBindInfos[c].columnId = pColSchema->colId;
76,647✔
362
    pBindInfos[c].bind = pBind;
76,647✔
363
    pBindInfos[c].type = pColSchema->type;
76,647✔
364

365
    // code = tColDataAddValueByBind(pCol, pBind, IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes -
366
    // VARSTR_HEADER_SIZE: -1); if (code) {
367
    //   goto _return;
368
    // }
369
  }
370

371
  code = tRowBuildFromBind(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols, &pDataBlock->ordered, &pDataBlock->duplicateTs);
5,493✔
372

373
  qDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
5,493!
374

375
_return:
5,493✔
376

377
  taosMemoryFree(ncharBind.buffer);
5,493!
378
  taosMemoryFree(ncharBind.length);
5,493!
379

380
  return code;
5,493✔
381
}
382

383
int32_t qBindStmtColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen, void* charsetCxt) {
134,581✔
384
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
134,581✔
385
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
134,581✔
386
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
134,582✔
387
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
134,582✔
388
  int32_t          rowNum = bind->num;
134,582✔
389
  TAOS_MULTI_BIND  ncharBind = {0};
134,582✔
390
  TAOS_MULTI_BIND* pBind = NULL;
134,582✔
391
  int32_t          code = 0;
134,582✔
392

393
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
1,281,382✔
394
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
1,146,810✔
395
    SColData* pCol = taosArrayGet(pCols, c);
1,146,810✔
396

397
    if (bind[c].num != rowNum) {
1,146,809!
398
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
399
      goto _return;
×
400
    }
401

402
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
1,146,809✔
403
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
1,146,806!
404
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
405
      goto _return;
×
406
    }
407

408
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
1,146,809✔
409
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
73,661✔
410
      if (code) {
73,661!
411
        goto _return;
×
412
      }
413
      pBind = &ncharBind;
73,661✔
414
    } else {
415
      pBind = bind + c;
1,073,148✔
416
    }
417

418
    code = tColDataAddValueByBind(pCol, pBind,
1,146,809✔
419
                                  IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1,
1,146,809!
420
                                  initCtxAsText, checkWKB);
421
    if (code) {
1,146,810✔
422
      goto _return;
10✔
423
    }
424
  }
425

426
  qDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
134,572✔
427

428
_return:
134,568✔
429

430
  taosMemoryFree(ncharBind.buffer);
134,582!
431
  taosMemoryFree(ncharBind.length);
134,582!
432

433
  return code;
134,582✔
434
}
435

436
int32_t qBindStmtSingleColValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
1,102,302✔
437
                                int32_t colIdx, int32_t rowNum, void* charsetCxt) {
438
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
1,102,302✔
439
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
1,102,302✔
440
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
1,102,302✔
441
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
1,102,302✔
442
  SSchema*         pColSchema = &pSchema[boundInfo->pColIndex[colIdx]];
1,102,302✔
443
  SColData*        pCol = taosArrayGet(pCols, colIdx);
1,102,302✔
444
  TAOS_MULTI_BIND  ncharBind = {0};
1,102,302✔
445
  TAOS_MULTI_BIND* pBind = NULL;
1,102,302✔
446
  int32_t          code = 0;
1,102,302✔
447

448
  if (bind->num != rowNum) {
1,102,302!
449
    return buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
450
  }
451

452
  // Column index exceeds the number of columns
453
  if (colIdx >= pCols->size && pCol == NULL) {
1,102,302!
454
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
455
  }
456

457
  if (bind->buffer_type != pColSchema->type) {
1,102,302!
458
    return buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
459
  }
460

461
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
1,102,302✔
462
    code = convertStmtNcharCol(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
70,875✔
463
    if (code) {
70,875!
464
      goto _return;
×
465
    }
466
    pBind = &ncharBind;
70,875✔
467
  } else {
468
    pBind = bind;
1,031,427✔
469
  }
470

471
  code = tColDataAddValueByBind(pCol, pBind,
1,102,302✔
472
                                IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1,
1,102,302!
473
                                initCtxAsText, checkWKB);
474

475
  qDebug("stmt col %d bind %d rows data", colIdx, rowNum);
1,102,302!
476

477
_return:
1,102,302✔
478

479
  taosMemoryFree(ncharBind.buffer);
1,102,302!
480
  taosMemoryFree(ncharBind.length);
1,102,302!
481

482
  return code;
1,102,302✔
483
}
484

485
int32_t qBindStmtTagsValue2(void* pBlock, void* boundTags, int64_t suid, const char* sTableName, char* tName,
×
486
                            TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen, void* charsetCxt) {
487
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
488
  SMsgBuf        pBuf = {.buf = msgBuf, .len = msgBufLen};
×
489
  int32_t        code = TSDB_CODE_SUCCESS;
×
490
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
×
491
  if (NULL == tags) {
×
492
    return TSDB_CODE_APP_ERROR;
×
493
  }
494

495
  SArray* pTagArray = taosArrayInit(tags->numOfBound, sizeof(STagVal));
×
496
  if (!pTagArray) {
×
497
    return buildInvalidOperationMsg(&pBuf, "out of memory");
×
498
  }
499

500
  SArray* tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
×
501
  if (!tagName) {
×
502
    code = buildInvalidOperationMsg(&pBuf, "out of memory");
×
503
    goto end;
×
504
  }
505

506
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
×
507

508
  bool  isJson = false;
×
509
  STag* pTag = NULL;
×
510

511
  for (int c = 0; c < tags->numOfBound; ++c) {
×
512
    if (bind == NULL) {
×
513
      break;
×
514
    }
515
    if (bind[c].is_null && bind[c].is_null[0]) {
×
516
      continue;
×
517
    }
518

519
    SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
×
520
    int32_t  colLen = pTagSchema->bytes;
×
521
    if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
×
522
      if (!bind[c].length) {
×
523
        code = buildInvalidOperationMsg(&pBuf, "var tag length is null");
×
524
        goto end;
×
525
      }
526
      colLen = bind[c].length[0];
×
527
      if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
×
528
        code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
×
529
        goto end;
×
530
      }
531
    }
532
    if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
×
533
      code = terrno;
×
534
      goto end;
×
535
    }
536
    if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
×
537
      if (colLen > (TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
×
538
        code = buildSyntaxErrMsg(&pBuf, "json string too long than 4095", bind[c].buffer);
×
539
        goto end;
×
540
      }
541

542
      isJson = true;
×
543
      char* tmp = taosMemoryCalloc(1, colLen + 1);
×
544
      if (!tmp) {
×
545
        code = terrno;
×
546
        goto end;
×
547
      }
548
      memcpy(tmp, bind[c].buffer, colLen);
×
549
      code = parseJsontoTagData(tmp, pTagArray, &pTag, &pBuf, charsetCxt);
×
550
      taosMemoryFree(tmp);
×
551
      if (code != TSDB_CODE_SUCCESS) {
×
552
        goto end;
×
553
      }
554
    } else {
555
      STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
×
556
      //      strcpy(val.colName, pTagSchema->name);
557
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
×
558
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
×
559
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
×
560
          if (initCtxAsText() || checkWKB(bind[c].buffer, colLen)) {
×
561
            code = buildSyntaxErrMsg(&pBuf, "invalid geometry tag", bind[c].buffer);
×
562
            goto end;
×
563
          }
564
        }
565
        val.pData = (uint8_t*)bind[c].buffer;
×
566
        val.nData = colLen;
×
567
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
×
568
        int32_t output = 0;
×
569
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
×
570
        if (p == NULL) {
×
571
          code = terrno;
×
572
          goto end;
×
573
        }
574
        if (!taosMbsToUcs4(bind[c].buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
×
575
          if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
576
            taosMemoryFree(p);
×
577
            code = generateSyntaxErrMsg(&pBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pTagSchema->name);
×
578
            goto end;
×
579
          }
580
          char buf[512] = {0};
×
581
          snprintf(buf, tListLen(buf), " taosMbsToUcs4 error:%s", strerror(terrno));
×
582
          taosMemoryFree(p);
×
583
          code = buildSyntaxErrMsg(&pBuf, buf, bind[c].buffer);
×
584
          goto end;
×
585
        }
586
        val.pData = p;
×
587
        val.nData = output;
×
588
      } else {
589
        memcpy(&val.i64, bind[c].buffer, colLen);
×
590
      }
591
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
×
592
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
593
        goto end;
×
594
      }
595
      if (NULL == taosArrayPush(pTagArray, &val)) {
×
596
        code = terrno;
×
597
        goto end;
×
598
      }
599
    }
600
  }
601

602
  if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) {
×
603
    goto end;
×
604
  }
605

606
  if (NULL == pDataBlock->pData->pCreateTbReq) {
×
607
    pDataBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
608
    if (NULL == pDataBlock->pData->pCreateTbReq) {
×
609
      code = terrno;
×
610
      goto end;
×
611
    }
612
  } else {
613
    SVCreateTbReq* tmp = pDataBlock->pData->pCreateTbReq;
×
614
    taosMemoryFreeClear(tmp->name);
×
615
    taosMemoryFreeClear(tmp->ctb.pTag);
×
616
    taosMemoryFreeClear(tmp->ctb.stbName);
×
617
    taosArrayDestroy(tmp->ctb.tagName);
×
618
    tmp->ctb.tagName = NULL;
×
619
  }
620

621
  code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName,
×
622
                             pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
×
623
  pTag = NULL;
×
624

625
end:
×
626
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
×
627
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
×
628
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
×
629
      taosMemoryFreeClear(p->pData);
×
630
    }
631
  }
632
  taosArrayDestroy(pTagArray);
×
633
  taosArrayDestroy(tagName);
×
634
  taosMemoryFree(pTag);
×
635

636
  return code;
×
637
}
638

639
static int32_t convertStmtStbNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst, void *charsetCxt) {
×
640
  int32_t       output = 0;
×
641
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
×
642

643
  dst->buffer = taosMemoryCalloc(src->num, max_buf_len);
×
644
  if (NULL == dst->buffer) {
×
645
    return terrno;
×
646
  }
647

648
  dst->length = taosMemoryCalloc(src->num, sizeof(int32_t));
×
649
  if (NULL == dst->length) {
×
650
    taosMemoryFreeClear(dst->buffer);
×
651
    return terrno;
×
652
  }
653

654
  char* src_buf = src->buffer;
×
655
  char* dst_buf = dst->buffer;
×
656
  for (int32_t i = 0; i < src->num; ++i) {
×
657
    if (src->is_null && src->is_null[i]) {
×
658
      continue;
×
659
    }
660

661
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
×
662
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
663
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
664
      }
665
      char buf[512] = {0};
×
666
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
667
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
668
    }
669

670
    dst->length[i] = output;
×
671
    src_buf += src->length[i];
×
672
    dst_buf += output;
×
673
  }
674

675
  dst->buffer_type = src->buffer_type;
×
676
  dst->is_null = src->is_null;
×
677
  dst->num = src->num;
×
678

679
  return TSDB_CODE_SUCCESS;
×
680
}
681

682
int32_t qBindStmtStbColsValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen,
×
683
                               STSchema** pTSchema, SBindInfo2* pBindInfos, void *charsetCxt) {
684
  STableDataCxt*  pDataBlock = (STableDataCxt*)pBlock;
×
685
  SSchema*        pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
686
  SBoundColInfo*  boundInfo = &pDataBlock->boundColsInfo;
×
687
  SMsgBuf         pBuf = {.buf = msgBuf, .len = msgBufLen};
×
688
  int32_t         rowNum = bind->num;
×
689
  SArray*         ncharBinds = NULL;
×
690
  TAOS_STMT2_BIND ncharBind = {0};
×
691
  int32_t         code = 0;
×
692
  int16_t         lastColId = -1;
×
693
  bool            colInOrder = true;
×
694
  int             ncharColNums = 0;
×
695

696
  if (NULL == *pTSchema) {
×
697
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
×
698
  }
699

700
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
×
701
    if (TSDB_DATA_TYPE_NCHAR == pSchema[boundInfo->pColIndex[c]].type) {
×
702
      ncharColNums++;
×
703
    }
704
  }
705
  if (ncharColNums > 0) {
×
706
    ncharBinds = taosArrayInit(ncharColNums, sizeof(ncharBind));
×
707
    if (!ncharBinds) {
×
708
      code = terrno;
×
709
      goto _return;
×
710
    }
711
  }
712

713
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
×
714
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
×
715
    if (pColSchema->colId <= lastColId) {
×
716
      colInOrder = false;
×
717
    } else {
718
      lastColId = pColSchema->colId;
×
719
    }
720

721
    if (bind[c].num != rowNum) {
×
722
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
723
      goto _return;
×
724
    }
725

726
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
×
727
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
×
728
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
729
      goto _return;
×
730
    }
731

732
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
×
733
      code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
×
734
      if (code) {
×
735
        goto _return;
×
736
      }
737
      if (!taosArrayPush(ncharBinds, &ncharBind)) {
×
738
        code = terrno;
×
739
        goto _return;
×
740
      }
741
      pBindInfos[c].bind = taosArrayGetLast(ncharBinds);
×
742
    } else {
743
      pBindInfos[c].bind = bind + c;
×
744
    }
745

746
    pBindInfos[c].columnId = pColSchema->colId;
×
747
    pBindInfos[c].type = pColSchema->type;
×
748
    pBindInfos[c].bytes = pColSchema->bytes;
×
749
  }
750

751
  code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols, &pDataBlock->ordered, &pDataBlock->duplicateTs);
×
752

753
  qDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
×
754

755
_return:
×
756
  if (ncharBinds) {
×
757
    for (int i = 0; i < TARRAY_SIZE(ncharBinds); ++i) {
×
758
      TAOS_STMT2_BIND* ncBind = TARRAY_DATA(ncharBinds);
×
759
      taosMemoryFree(ncBind[i].buffer);
×
760
      taosMemoryFree(ncBind[i].length);
×
761
    }
762
    taosArrayDestroy(ncharBinds);
×
763
  }
764

765
  return code;
×
766
}
767

768
static int32_t convertStmtNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst, void *charsetCxt) {
×
769
  int32_t       output = 0;
×
770
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
×
771

772
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
×
773
  // if (dst->buffer_length < newBuflen) {
774
  dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
×
775
  if (NULL == dst->buffer) {
×
776
    return terrno;
×
777
  }
778
  //}
779

780
  if (NULL == dst->length) {
×
781
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
×
782
    if (NULL == dst->length) {
×
783
      taosMemoryFreeClear(dst->buffer);
×
784
      return terrno;
×
785
    }
786
  }
787

788
  // dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
789
  char* src_buf = src->buffer;
×
790
  char* dst_buf = dst->buffer;
×
791
  for (int32_t i = 0; i < src->num; ++i) {
×
792
    if (src->is_null && src->is_null[i]) {
×
793
      continue;
×
794
    }
795

796
    /*if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
797
      (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output)) {*/
798
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) {
×
799
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
800
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
801
      }
802
      char buf[512] = {0};
×
803
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
804
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
805
    }
806

807
    dst->length[i] = output;
×
808
    src_buf += src->length[i];
×
809
    dst_buf += output;
×
810
  }
811

812
  dst->buffer_type = src->buffer_type;
×
813
  dst->is_null = src->is_null;
×
814
  dst->num = src->num;
×
815

816
  return TSDB_CODE_SUCCESS;
×
817
}
818

819
int32_t qBindStmtColsValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen, void *charsetCxt) {
×
820
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
×
821
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
822
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
×
823
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
×
824
  int32_t          rowNum = bind->num;
×
825
  TAOS_STMT2_BIND  ncharBind = {0};
×
826
  TAOS_STMT2_BIND* pBind = NULL;
×
827
  int32_t          code = 0;
×
828

829
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
×
830
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
×
831
    SColData* pCol = taosArrayGet(pCols, c);
×
832
    if (pCol == NULL || pColSchema == NULL) {
×
833
      code = buildInvalidOperationMsg(&pBuf, "get column schema or column data failed");
×
834
      goto _return;
×
835
    }
836

837
    if (bind[c].num != rowNum) {
×
838
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
839
      goto _return;
×
840
    }
841

842
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
×
843
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
×
844
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
845
      goto _return;
×
846
    }
847

848
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
×
849
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
×
850
      if (code) {
×
851
        goto _return;
×
852
      }
853
      pBind = &ncharBind;
×
854
    } else {
855
      pBind = bind + c;
×
856
    }
857

858
    code = tColDataAddValueByBind2(pCol, pBind,
×
859
                                   IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1,
×
860
                                   initCtxAsText, checkWKB);
861
    if (code) {
×
862
      goto _return;
×
863
    }
864
  }
865

866
  qDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
×
867

868
_return:
×
869

870
  taosMemoryFree(ncharBind.buffer);
×
871
  taosMemoryFree(ncharBind.length);
×
872

873
  return code;
×
874
}
875

876
int32_t qBindStmtSingleColValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen,
×
877
                                 int32_t colIdx, int32_t rowNum, void *charsetCxt) {
878
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
×
879
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
880
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
×
881
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
×
882
  SSchema*         pColSchema = &pSchema[boundInfo->pColIndex[colIdx]];
×
883
  SColData*        pCol = taosArrayGet(pCols, colIdx);
×
884
  TAOS_STMT2_BIND  ncharBind = {0};
×
885
  TAOS_STMT2_BIND* pBind = NULL;
×
886
  int32_t          code = 0;
×
887

888
  if (bind->num != rowNum) {
×
889
    return buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
890
  }
891

892
  // Column index exceeds the number of columns
893
  if (colIdx >= pCols->size && pCol == NULL) {
×
894
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
895
  }
896

897
  if (bind->buffer_type != pColSchema->type) {
×
898
    return buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
899
  }
900

901
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
×
902
    code = convertStmtNcharCol2(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
×
903
    if (code) {
×
904
      goto _return;
×
905
    }
906
    pBind = &ncharBind;
×
907
  } else {
908
    pBind = bind;
×
909
  }
910

911
  code = tColDataAddValueByBind2(pCol, pBind,
×
912
                                 IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1,
×
913
                                 initCtxAsText, checkWKB);
914

915
  qDebug("stmt col %d bind %d rows data", colIdx, rowNum);
×
916

917
_return:
×
918

919
  taosMemoryFree(ncharBind.buffer);
×
920
  taosMemoryFree(ncharBind.length);
×
921

922
  return code;
×
923
}
924

925
int32_t buildBoundFields(int32_t numOfBound, int16_t* boundColumns, SSchema* pSchema, int32_t* fieldNum,
2,614,074✔
926
                         TAOS_FIELD_E** fields, uint8_t timePrec) {
927
  if (fields != NULL) {
2,614,074✔
928
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
2,612,942!
929
    if (NULL == *fields) {
2,612,942!
930
      return terrno;
×
931
    }
932

933
    SSchema* schema = &pSchema[boundColumns[0]];
2,612,942✔
934
    if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
2,612,942✔
935
      (*fields)[0].precision = timePrec;
2,598,407✔
936
    }
937

938
    for (int32_t i = 0; i < numOfBound; ++i) {
30,987,640✔
939
      schema = &pSchema[boundColumns[i]];
28,374,698✔
940
      tstrncpy((*fields)[i].name, schema->name, 65);
28,374,698✔
941
      (*fields)[i].type = schema->type;
28,374,698✔
942
      (*fields)[i].bytes = schema->bytes;
28,374,698✔
943
    }
944
  }
945

946
  *fieldNum = numOfBound;
2,614,074✔
947

948
  return TSDB_CODE_SUCCESS;
2,614,074✔
949
}
950

951
int32_t buildStbBoundFields(SBoundColInfo boundColsInfo, SSchema* pSchema, int32_t* fieldNum, TAOS_FIELD_ALL** fields,
×
952
                            STableMeta* pMeta, void* boundTags, bool preCtbname) {
953
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
×
954
  bool           hastag = tags != NULL;
×
955
  int32_t        numOfBound = boundColsInfo.numOfBound + (preCtbname ? 1 : 0);
×
956
  if (hastag) {
×
957
    numOfBound += tags->mixTagsCols ? 0 : tags->numOfBound;
×
958
  }
959
  int32_t idx = 0;
×
960
  if (fields != NULL) {
×
961
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_ALL));
×
962
    if (NULL == *fields) {
×
963
      return terrno;
×
964
    }
965

966
    if (preCtbname && numOfBound != boundColsInfo.numOfBound) {
×
967
      (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
×
968
      tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
×
969
      (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
×
970
      (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
×
971
      idx++;
×
972
    }
973

974
    if (hastag && tags->numOfBound > 0 && !tags->mixTagsCols) {
×
975
      SSchema* tagSchema = getTableTagSchema(pMeta);
×
976

977
      for (int32_t i = 0; i < tags->numOfBound; ++i) {
×
978
        (*fields)[idx].field_type = TAOS_FIELD_TAG;
×
979

980
        SSchema* schema = &tagSchema[tags->pColIndex[i]];
×
981
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[i].name));
×
982
        (*fields)[idx].type = schema->type;
×
983
        (*fields)[idx].bytes = schema->bytes;
×
984
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
×
985
          (*fields)[idx].precision = pMeta->tableInfo.precision;
×
986
        }
987
        idx++;
×
988
      }
989
    }
990

991
    if (boundColsInfo.numOfBound > 0) {
×
992
      SSchema* schema = &pSchema[boundColsInfo.pColIndex[0]];
×
993

994
      for (int32_t i = 0; i < boundColsInfo.numOfBound; ++i) {
×
995
        int16_t idxCol = boundColsInfo.pColIndex[i];
×
996

997
        if (idxCol == pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags) {
×
998
          (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
×
999
          tstrncpy((*fields)[i].name, "tbname", sizeof((*fields)[idx].name));
×
1000
          (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
×
1001
          (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
×
1002

1003
          idx++;
×
1004
          continue;
×
1005
        } else if (idxCol < pMeta->tableInfo.numOfColumns) {
×
1006
          (*fields)[idx].field_type = TAOS_FIELD_COL;
×
1007
        } else {
1008
          (*fields)[idx].field_type = TAOS_FIELD_TAG;
×
1009
        }
1010

1011
        schema = &pSchema[idxCol];
×
1012
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[idx].name));
×
1013
        (*fields)[idx].type = schema->type;
×
1014
        (*fields)[idx].bytes = schema->bytes;
×
1015
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
×
1016
          (*fields)[idx].precision = pMeta->tableInfo.precision;
×
1017
        }
1018
        idx++;
×
1019
      }
1020
    }
1021
  }
1022

1023
  *fieldNum = numOfBound;
×
1024

1025
  return TSDB_CODE_SUCCESS;
×
1026
}
1027

1028
int32_t qBuildStmtTagFields(void* pBlock, void* boundTags, int32_t* fieldNum, TAOS_FIELD_E** fields) {
19,836✔
1029
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
19,836✔
1030
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
19,836✔
1031
  if (NULL == tags) {
19,836!
1032
    return TSDB_CODE_APP_ERROR;
×
1033
  }
1034

1035
  if (pDataBlock->pMeta->tableType != TSDB_SUPER_TABLE && pDataBlock->pMeta->tableType != TSDB_CHILD_TABLE) {
19,836!
1036
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1037
  }
1038

1039
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
19,836✔
1040
  if (tags->numOfBound <= 0) {
19,836!
1041
    *fieldNum = 0;
×
1042
    *fields = NULL;
×
1043

1044
    return TSDB_CODE_SUCCESS;
×
1045
  }
1046

1047
  CHECK_CODE(buildBoundFields(tags->numOfBound, tags->pColIndex, pSchema, fieldNum, fields, 0));
19,836!
1048

1049
  return TSDB_CODE_SUCCESS;
19,836✔
1050
}
1051

1052
int32_t qBuildStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields) {
2,594,238✔
1053
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
2,594,238✔
1054
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
2,594,238✔
1055
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
2,594,238!
1056
    *fieldNum = 0;
×
1057
    if (fields != NULL) {
×
1058
      *fields = NULL;
×
1059
    }
1060

1061
    return TSDB_CODE_SUCCESS;
×
1062
  }
1063

1064
  CHECK_CODE(buildBoundFields(pDataBlock->boundColsInfo.numOfBound, pDataBlock->boundColsInfo.pColIndex, pSchema,
2,594,238!
1065
                              fieldNum, fields, pDataBlock->pMeta->tableInfo.precision));
1066

1067
  return TSDB_CODE_SUCCESS;
2,594,238✔
1068
}
1069

1070
int32_t qBuildStmtStbColFields(void* pBlock, void* boundTags, bool preCtbname, int32_t* fieldNum,
×
1071
                               TAOS_FIELD_ALL** fields) {
1072
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
1073
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
1074
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
×
1075
    *fieldNum = 0;
×
1076
    if (fields != NULL) {
×
1077
      *fields = NULL;
×
1078
    }
1079

1080
    return TSDB_CODE_SUCCESS;
×
1081
  }
1082

1083
  CHECK_CODE(buildStbBoundFields(pDataBlock->boundColsInfo, pSchema, fieldNum, fields, pDataBlock->pMeta, boundTags,
×
1084
                                 preCtbname));
1085

1086
  return TSDB_CODE_SUCCESS;
×
1087
}
1088

1089
int32_t qResetStmtColumns(SArray* pCols, bool deepClear) {
×
1090
  int32_t colNum = taosArrayGetSize(pCols);
×
1091

1092
  for (int32_t i = 0; i < colNum; ++i) {
×
1093
    SColData* pCol = (SColData*)taosArrayGet(pCols, i);
×
1094
    if (pCol == NULL) {
×
1095
      qError("qResetStmtColumns column is NULL");
×
1096
      return terrno;
×
1097
    }
1098
    if (deepClear) {
×
1099
      tColDataDeepClear(pCol);
×
1100
    } else {
1101
      tColDataClear(pCol);
×
1102
    }
1103
  }
1104

1105
  return TSDB_CODE_SUCCESS;
×
1106
}
1107

1108
int32_t qResetStmtDataBlock(STableDataCxt* block, bool deepClear) {
94,751✔
1109
  STableDataCxt* pBlock = (STableDataCxt*)block;
94,751✔
1110
  int32_t        colNum = taosArrayGetSize(pBlock->pData->aCol);
94,751✔
1111

1112
  for (int32_t i = 0; i < colNum; ++i) {
918,478✔
1113
    SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i);
823,729✔
1114
    if (pCol == NULL) {
823,728!
1115
      qError("qResetStmtDataBlock column is NULL");
×
1116
      return terrno;
×
1117
    }
1118
    if (deepClear) {
823,728✔
1119
      tColDataDeepClear(pCol);
29,991✔
1120
    } else {
1121
      tColDataClear(pCol);
793,737✔
1122
    }
1123
  }
1124

1125
  return TSDB_CODE_SUCCESS;
94,749✔
1126
}
1127

1128
int32_t qCloneStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, bool reset) {
59,429✔
1129
  int32_t code = 0;
59,429✔
1130

1131
  *pDst = taosMemoryCalloc(1, sizeof(STableDataCxt));
59,429!
1132
  if (NULL == *pDst) {
59,433!
1133
    return terrno;
×
1134
  }
1135

1136
  STableDataCxt* pNewCxt = (STableDataCxt*)*pDst;
59,433✔
1137
  STableDataCxt* pCxt = (STableDataCxt*)pSrc;
59,433✔
1138
  pNewCxt->pSchema = NULL;
59,433✔
1139
  pNewCxt->pValues = NULL;
59,433✔
1140

1141
  if (pCxt->pMeta) {
59,433!
1142
    void* pNewMeta = taosMemoryMalloc(TABLE_META_SIZE(pCxt->pMeta));
59,433!
1143
    if (NULL == pNewMeta) {
59,433!
1144
      insDestroyTableDataCxt(*pDst);
×
1145
      return terrno;
×
1146
    }
1147
    memcpy(pNewMeta, pCxt->pMeta, TABLE_META_SIZE(pCxt->pMeta));
59,433!
1148
    pNewCxt->pMeta = pNewMeta;
59,433✔
1149
  }
1150

1151
  memcpy(&pNewCxt->boundColsInfo, &pCxt->boundColsInfo, sizeof(pCxt->boundColsInfo));
59,433✔
1152
  pNewCxt->boundColsInfo.pColIndex = NULL;
59,433✔
1153

1154
  if (pCxt->boundColsInfo.pColIndex) {
59,433!
1155
    void* pNewColIdx = taosMemoryMalloc(pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
59,433!
1156
    if (NULL == pNewColIdx) {
59,433!
1157
      insDestroyTableDataCxt(*pDst);
×
1158
      return terrno;
×
1159
    }
1160
    memcpy(pNewColIdx, pCxt->boundColsInfo.pColIndex,
59,433✔
1161
           pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
59,433✔
1162
    pNewCxt->boundColsInfo.pColIndex = pNewColIdx;
59,433✔
1163
  }
1164

1165
  if (pCxt->pData) {
59,433!
1166
    SSubmitTbData* pNewTb = (SSubmitTbData*)taosMemoryMalloc(sizeof(SSubmitTbData));
59,433!
1167
    if (NULL == pNewTb) {
59,433!
1168
      insDestroyTableDataCxt(*pDst);
×
1169
      return terrno;
×
1170
    }
1171

1172
    memcpy(pNewTb, pCxt->pData, sizeof(*pCxt->pData));
59,433✔
1173
    pNewTb->pCreateTbReq = NULL;
59,433✔
1174

1175
    pNewTb->aCol = taosArrayDup(pCxt->pData->aCol, NULL);
59,433✔
1176
    if (NULL == pNewTb->aCol) {
59,433!
1177
      insDestroyTableDataCxt(*pDst);
×
1178
      return terrno;
×
1179
    }
1180

1181
    pNewCxt->pData = pNewTb;
59,433✔
1182

1183
    if (reset) {
59,433✔
1184
      code = qResetStmtDataBlock(*pDst, true);
3,547✔
1185
    }
1186
  }
1187

1188
  return code;
59,431✔
1189
}
1190

1191
int32_t qRebuildStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, uint64_t uid, uint64_t suid, int32_t vgId,
55,886✔
1192
                              bool rebuildCreateTb) {
1193
  int32_t code = qCloneStmtDataBlock(pDst, pSrc, false);
55,886✔
1194
  if (code) {
55,886!
1195
    return code;
×
1196
  }
1197

1198
  STableDataCxt* pBlock = (STableDataCxt*)*pDst;
55,886✔
1199
  if (pBlock->pMeta) {
55,886!
1200
    pBlock->pMeta->uid = uid;
55,886✔
1201
    pBlock->pMeta->vgId = vgId;
55,886✔
1202
    pBlock->pMeta->suid = suid;
55,886✔
1203
  }
1204

1205
  pBlock->pData->suid = suid;
55,886✔
1206
  pBlock->pData->uid = uid;
55,886✔
1207

1208
  if (rebuildCreateTb && NULL == pBlock->pData->pCreateTbReq) {
55,886!
1209
    pBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
39,678!
1210
    if (NULL == pBlock->pData->pCreateTbReq) {
39,678!
1211
      return terrno;
×
1212
    }
1213
  }
1214

1215
  return TSDB_CODE_SUCCESS;
55,886✔
1216
}
1217

1218
STableMeta* qGetTableMetaInDataBlock(STableDataCxt* pDataBlock) { return ((STableDataCxt*)pDataBlock)->pMeta; }
150,649✔
1219

1220
void qDestroyStmtDataBlock(STableDataCxt* pBlock) {
63,785✔
1221
  if (pBlock == NULL) {
63,785✔
1222
    return;
780✔
1223
  }
1224

1225
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
63,005✔
1226
  insDestroyTableDataCxt(pDataBlock);
63,005✔
1227
}
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