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

taosdata / TDengine / #3558

17 Dec 2024 06:05AM UTC coverage: 59.778% (+1.6%) from 58.204%
#3558

push

travis-ci

web-flow
Merge pull request #29179 from taosdata/merge/mainto3.0

merge: form main to 3.0 branch

132787 of 287595 branches covered (46.17%)

Branch coverage included in aggregate %.

104 of 191 new or added lines in 5 files covered. (54.45%)

6085 existing lines in 168 files now uncovered.

209348 of 284746 relevant lines covered (73.52%)

8164844.48 hits per line

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

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

39
  SSubmitTbData* pNew = *pData;
34✔
40

41
  *pNew = *pDataBlock->pData;
34✔
42

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

55
  int32_t colNum = taosArrayGetSize(pNew->aCol);
34✔
56
  for (int32_t i = 0; i < colNum; ++i) {
475✔
57
    SColData* pCol = (SColData*)taosArrayGet(pNew->aCol, i);
441✔
58
    tColDataDeepClear(pCol);
441✔
59
  }
60

61
  return TSDB_CODE_SUCCESS;
34✔
62
}
63

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

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

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

UNCOV
78
  if (pStmt->freeArrayFunc) {
×
UNCOV
79
    pStmt->freeArrayFunc(pVgDataBlocks);
×
80
  }
UNCOV
81
  return code;
×
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) {
34✔
106
  int32_t             code = TSDB_CODE_SUCCESS;
34✔
107
  SArray*             pVgDataBlocks = NULL;
34✔
108
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
34✔
109

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

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

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

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

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

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

UNCOV
146
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
×
147

UNCOV
148
  bool  isJson = false;
×
UNCOV
149
  STag* pTag = NULL;
×
150

UNCOV
151
  for (int c = 0; c < tags->numOfBound; ++c) {
×
UNCOV
152
    if (bind[c].is_null && bind[c].is_null[0]) {
×
153
      continue;
×
154
    }
155

UNCOV
156
    SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
×
UNCOV
157
    int32_t  colLen = pTagSchema->bytes;
×
UNCOV
158
    if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
×
159
      if (!bind[c].length) {
×
160
        code = buildInvalidOperationMsg(&pBuf, "var tag length is null");
×
161
        goto end;
×
162
      }
163
      colLen = bind[c].length[0];
×
164
      if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
×
165
        code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
×
166
        goto end;
×
167
      }
168
    }
UNCOV
169
    if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
×
170
      code = terrno;
×
171
      goto end;
×
172
    }
UNCOV
173
    if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
×
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 {
UNCOV
192
      STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
×
193
      //      strcpy(val.colName, pTagSchema->name);
UNCOV
194
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
×
UNCOV
195
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
×
196
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
×
197
          if (initCtxAsText() || checkWKB(bind[c].buffer, colLen)) {
×
198
            code = buildSyntaxErrMsg(&pBuf, "invalid geometry tag", bind[c].buffer);
×
199
            goto end;
×
200
          }
201
        }
202
        val.pData = (uint8_t*)bind[c].buffer;
×
203
        val.nData = colLen;
×
UNCOV
204
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
×
205
        int32_t output = 0;
×
206
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
×
207
        if (p == NULL) {
×
208
          code = terrno;
×
209
          goto end;
×
210
        }
211
        if (!taosMbsToUcs4(bind[c].buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
×
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;
×
224
        val.nData = output;
×
225
      } else {
UNCOV
226
        memcpy(&val.i64, bind[c].buffer, colLen);
×
227
      }
UNCOV
228
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
×
229
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
230
        goto end;
×
231
      }
UNCOV
232
      if (NULL == taosArrayPush(pTagArray, &val)) {
×
233
        code = terrno;
×
234
        goto end;
×
235
      }
236
    }
237
  }
238

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

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

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

UNCOV
255
end:
×
UNCOV
256
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
×
UNCOV
257
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
×
UNCOV
258
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
×
259
      taosMemoryFreeClear(p->pData);
×
260
    }
261
  }
UNCOV
262
  taosArrayDestroy(pTagArray);
×
UNCOV
263
  taosArrayDestroy(tagName);
×
UNCOV
264
  taosMemoryFree(pTag);
×
265

UNCOV
266
  return code;
×
267
}
268

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

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

287
  dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
32✔
288

289
  for (int32_t i = 0; i < src->num; ++i) {
352✔
290
    if (src->is_null && src->is_null[i]) {
320!
291
      continue;
×
292
    }
293

294
    if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
320!
295
                       (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output, charsetCxt)) {
320✔
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;
320✔
305
  }
306

307
  dst->buffer_type = src->buffer_type;
32✔
308
  dst->is_null = src->is_null;
32✔
309
  dst->num = src->num;
32✔
310

311
  return TSDB_CODE_SUCCESS;
32✔
312
}
313

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

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

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

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

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

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

UNCOV
361
    pBindInfos[c].columnId = pColSchema->colId;
×
UNCOV
362
    pBindInfos[c].bind = pBind;
×
UNCOV
363
    pBindInfos[c].type = pColSchema->type;
×
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

UNCOV
371
  code = tRowBuildFromBind(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols);
×
372

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

UNCOV
375
_return:
×
376

UNCOV
377
  taosMemoryFree(ncharBind.buffer);
×
UNCOV
378
  taosMemoryFree(ncharBind.length);
×
379

UNCOV
380
  return code;
×
381
}
382

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

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

397
    if (bind[c].num != rowNum) {
452!
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)) &&
452!
403
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
452!
404
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
405
      goto _return;
×
406
    }
407

408
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
452✔
409
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
32✔
410
      if (code) {
32!
411
        goto _return;
×
412
      }
413
      pBind = &ncharBind;
32✔
414
    } else {
415
      pBind = bind + c;
420✔
416
    }
417

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

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

428
_return:
32✔
429

430
  taosMemoryFree(ncharBind.buffer);
34!
431
  taosMemoryFree(ncharBind.length);
34!
432

433
  return code;
34✔
434
}
435

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

448
  if (bind->num != rowNum) {
×
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) {
×
454
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
455
  }
456

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

461
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
×
462
    code = convertStmtNcharCol(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
×
463
    if (code) {
×
464
      goto _return;
×
465
    }
466
    pBind = &ncharBind;
×
467
  } else {
468
    pBind = bind;
×
469
  }
470

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

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

477
_return:
×
478

479
  taosMemoryFree(ncharBind.buffer);
×
480
  taosMemoryFree(ncharBind.length);
×
481

482
  return code;
×
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[c].is_null && bind[c].is_null[0]) {
×
513
      continue;
×
514
    }
515

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

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

599
  if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) {
×
600
    goto end;
×
601
  }
602

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

618
  code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName,
×
619
                             pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
×
UNCOV
620
  pTag = NULL;
×
621

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

633
  return code;
×
634
}
635

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

640
  dst->buffer = taosMemoryCalloc(src->num, max_buf_len);
×
641
  if (NULL == dst->buffer) {
×
UNCOV
642
    return terrno;
×
643
  }
644

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

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

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

667
    dst->length[i] = output;
×
UNCOV
668
    src_buf += src->length[i];
×
669
    dst_buf += output;
×
670
  }
671

672
  dst->buffer_type = src->buffer_type;
×
UNCOV
673
  dst->is_null = src->is_null;
×
674
  dst->num = src->num;
×
675

676
  return TSDB_CODE_SUCCESS;
×
677
}
678

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

UNCOV
693
  if (NULL == *pTSchema) {
×
UNCOV
694
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
×
695
  }
696

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

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

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

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

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

743
    pBindInfos[c].columnId = pColSchema->colId;
×
UNCOV
744
    pBindInfos[c].type = pColSchema->type;
×
745
    pBindInfos[c].bytes = pColSchema->bytes;
×
746
  }
747

748
  code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols);
×
749

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

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

762
  return code;
×
763
}
764

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

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

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

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

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

804
    dst->length[i] = output;
×
UNCOV
805
    src_buf += src->length[i];
×
806
    dst_buf += output;
×
807
  }
808

809
  dst->buffer_type = src->buffer_type;
×
810
  dst->is_null = src->is_null;
×
811
  dst->num = src->num;
×
812

813
  return TSDB_CODE_SUCCESS;
×
814
}
815

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

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

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

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

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

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

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

UNCOV
865
_return:
×
866

UNCOV
867
  taosMemoryFree(ncharBind.buffer);
×
868
  taosMemoryFree(ncharBind.length);
×
869

870
  return code;
×
871
}
872

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

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

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

894
  if (bind->buffer_type != pColSchema->type) {
×
UNCOV
895
    return buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
896
  }
897

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

UNCOV
908
  code = tColDataAddValueByBind2(pCol, pBind,
×
909
                                 IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1,
×
910
                                 initCtxAsText, checkWKB);
911

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

UNCOV
914
_return:
×
915

UNCOV
916
  taosMemoryFree(ncharBind.buffer);
×
917
  taosMemoryFree(ncharBind.length);
×
918

919
  return code;
×
920
}
921

UNCOV
922
int32_t buildBoundFields(int32_t numOfBound, int16_t* boundColumns, SSchema* pSchema, int32_t* fieldNum,
×
923
                         TAOS_FIELD_E** fields, uint8_t timePrec) {
924
  if (fields != NULL) {
×
925
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
×
UNCOV
926
    if (NULL == *fields) {
×
UNCOV
927
      return terrno;
×
928
    }
929

930
    SSchema* schema = &pSchema[boundColumns[0]];
×
931
    if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
×
932
      (*fields)[0].precision = timePrec;
×
933
    }
934

UNCOV
935
    for (int32_t i = 0; i < numOfBound; ++i) {
×
936
      schema = &pSchema[boundColumns[i]];
×
UNCOV
937
      tstrncpy((*fields)[i].name, schema->name, 65);
×
938
      (*fields)[i].type = schema->type;
×
UNCOV
939
      (*fields)[i].bytes = schema->bytes;
×
940
    }
941
  }
942

943
  *fieldNum = numOfBound;
×
944

945
  return TSDB_CODE_SUCCESS;
×
946
}
947

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

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

971
    if (hastag && tags->numOfBound > 0 && !tags->mixTagsCols) {
×
UNCOV
972
      SSchema* tagSchema = getTableTagSchema(pMeta);
×
973

UNCOV
974
      for (int32_t i = 0; i < tags->numOfBound; ++i) {
×
UNCOV
975
        (*fields)[idx].field_type = TAOS_FIELD_TAG;
×
976

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

UNCOV
988
    if (boundColsInfo.numOfBound > 0) {
×
989
      SSchema* schema = &pSchema[boundColsInfo.pColIndex[0]];
×
990

991
      for (int32_t i = 0; i < boundColsInfo.numOfBound; ++i) {
×
992
        int16_t idxCol = boundColsInfo.pColIndex[i];
×
993

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

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

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

UNCOV
1020
  *fieldNum = numOfBound;
×
1021

1022
  return TSDB_CODE_SUCCESS;
×
1023
}
1024

1025
int32_t qBuildStmtTagFields(void* pBlock, void* boundTags, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1026
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
1027
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
×
1028
  if (NULL == tags) {
×
UNCOV
1029
    return TSDB_CODE_APP_ERROR;
×
1030
  }
1031

UNCOV
1032
  if (pDataBlock->pMeta->tableType != TSDB_SUPER_TABLE && pDataBlock->pMeta->tableType != TSDB_CHILD_TABLE) {
×
1033
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1034
  }
1035

UNCOV
1036
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
×
UNCOV
1037
  if (tags->numOfBound <= 0) {
×
1038
    *fieldNum = 0;
×
1039
    *fields = NULL;
×
1040

1041
    return TSDB_CODE_SUCCESS;
×
1042
  }
1043

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

UNCOV
1046
  return TSDB_CODE_SUCCESS;
×
1047
}
1048

UNCOV
1049
int32_t qBuildStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1050
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
UNCOV
1051
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
UNCOV
1052
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
×
1053
    *fieldNum = 0;
×
UNCOV
1054
    if (fields != NULL) {
×
UNCOV
1055
      *fields = NULL;
×
1056
    }
1057

1058
    return TSDB_CODE_SUCCESS;
×
1059
  }
1060

1061
  CHECK_CODE(buildBoundFields(pDataBlock->boundColsInfo.numOfBound, pDataBlock->boundColsInfo.pColIndex, pSchema,
×
1062
                              fieldNum, fields, pDataBlock->pMeta->tableInfo.precision));
1063

UNCOV
1064
  return TSDB_CODE_SUCCESS;
×
1065
}
1066

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

UNCOV
1077
    return TSDB_CODE_SUCCESS;
×
1078
  }
1079

1080
  CHECK_CODE(buildStbBoundFields(pDataBlock->boundColsInfo, pSchema, fieldNum, fields, pDataBlock->pMeta, boundTags,
×
1081
                                 preCtbname));
1082

UNCOV
1083
  return TSDB_CODE_SUCCESS;
×
1084
}
1085

UNCOV
1086
int32_t qResetStmtColumns(SArray* pCols, bool deepClear) {
×
1087
  int32_t colNum = taosArrayGetSize(pCols);
×
1088

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

1102
  return TSDB_CODE_SUCCESS;
×
1103
}
1104

1105
int32_t qResetStmtDataBlock(STableDataCxt* block, bool deepClear) {
50✔
1106
  STableDataCxt* pBlock = (STableDataCxt*)block;
50✔
1107
  int32_t        colNum = taosArrayGetSize(pBlock->pData->aCol);
50✔
1108

1109
  for (int32_t i = 0; i < colNum; ++i) {
720✔
1110
    SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i);
670✔
1111
    if (pCol == NULL) {
670!
UNCOV
1112
      qError("qResetStmtDataBlock column is NULL");
×
UNCOV
1113
      return terrno;
×
1114
    }
1115
    if (deepClear) {
676✔
1116
      tColDataDeepClear(pCol);
224✔
1117
    } else {
1118
      tColDataClear(pCol);
452✔
1119
    }
1120
  }
1121

1122
  return TSDB_CODE_SUCCESS;
50✔
1123
}
1124

1125
int32_t qCloneStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, bool reset) {
16✔
1126
  int32_t code = 0;
16✔
1127

1128
  *pDst = taosMemoryCalloc(1, sizeof(STableDataCxt));
16!
1129
  if (NULL == *pDst) {
16!
1130
    return terrno;
×
1131
  }
1132

1133
  STableDataCxt* pNewCxt = (STableDataCxt*)*pDst;
16✔
1134
  STableDataCxt* pCxt = (STableDataCxt*)pSrc;
16✔
1135
  pNewCxt->pSchema = NULL;
16✔
1136
  pNewCxt->pValues = NULL;
16✔
1137

1138
  if (pCxt->pMeta) {
16!
1139
    void* pNewMeta = taosMemoryMalloc(TABLE_META_SIZE(pCxt->pMeta));
16!
1140
    if (NULL == pNewMeta) {
16!
UNCOV
1141
      insDestroyTableDataCxt(*pDst);
×
UNCOV
1142
      return terrno;
×
1143
    }
1144
    memcpy(pNewMeta, pCxt->pMeta, TABLE_META_SIZE(pCxt->pMeta));
16!
1145
    pNewCxt->pMeta = pNewMeta;
16✔
1146
  }
1147

1148
  memcpy(&pNewCxt->boundColsInfo, &pCxt->boundColsInfo, sizeof(pCxt->boundColsInfo));
16✔
1149
  pNewCxt->boundColsInfo.pColIndex = NULL;
16✔
1150

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

1162
  if (pCxt->pData) {
16!
1163
    SSubmitTbData* pNewTb = (SSubmitTbData*)taosMemoryMalloc(sizeof(SSubmitTbData));
16!
1164
    if (NULL == pNewTb) {
16!
UNCOV
1165
      insDestroyTableDataCxt(*pDst);
×
UNCOV
1166
      return terrno;
×
1167
    }
1168

1169
    memcpy(pNewTb, pCxt->pData, sizeof(*pCxt->pData));
16✔
1170
    pNewTb->pCreateTbReq = NULL;
16✔
1171

1172
    pNewTb->aCol = taosArrayDup(pCxt->pData->aCol, NULL);
16✔
1173
    if (NULL == pNewTb->aCol) {
16!
UNCOV
1174
      insDestroyTableDataCxt(*pDst);
×
UNCOV
1175
      return terrno;
×
1176
    }
1177

1178
    pNewCxt->pData = pNewTb;
16✔
1179

1180
    if (reset) {
16!
1181
      code = qResetStmtDataBlock(*pDst, true);
16✔
1182
    }
1183
  }
1184

1185
  return code;
16✔
1186
}
1187

1188
int32_t qRebuildStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, uint64_t uid, uint64_t suid, int32_t vgId,
×
1189
                              bool rebuildCreateTb) {
UNCOV
1190
  int32_t code = qCloneStmtDataBlock(pDst, pSrc, false);
×
1191
  if (code) {
×
1192
    return code;
×
1193
  }
1194

1195
  STableDataCxt* pBlock = (STableDataCxt*)*pDst;
×
1196
  if (pBlock->pMeta) {
×
1197
    pBlock->pMeta->uid = uid;
×
UNCOV
1198
    pBlock->pMeta->vgId = vgId;
×
UNCOV
1199
    pBlock->pMeta->suid = suid;
×
1200
  }
1201

UNCOV
1202
  pBlock->pData->suid = suid;
×
UNCOV
1203
  pBlock->pData->uid = uid;
×
1204

UNCOV
1205
  if (rebuildCreateTb && NULL == pBlock->pData->pCreateTbReq) {
×
UNCOV
1206
    pBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
UNCOV
1207
    if (NULL == pBlock->pData->pCreateTbReq) {
×
UNCOV
1208
      return terrno;
×
1209
    }
1210
  }
1211

UNCOV
1212
  return TSDB_CODE_SUCCESS;
×
1213
}
1214

1215
STableMeta* qGetTableMetaInDataBlock(STableDataCxt* pDataBlock) { return ((STableDataCxt*)pDataBlock)->pMeta; }
51✔
1216

1217
void qDestroyStmtDataBlock(STableDataCxt* pBlock) {
51✔
1218
  if (pBlock == NULL) {
51✔
1219
    return;
18✔
1220
  }
1221

1222
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
33✔
1223
  insDestroyTableDataCxt(pDataBlock);
33✔
1224
}
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