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

taosdata / TDengine / #3531

19 Nov 2024 10:42AM UTC coverage: 60.213% (-0.006%) from 60.219%
#3531

push

travis-ci

web-flow
Merge pull request #28777 from taosdata/fix/3.0/TD-32366

fix:TD-32366/stmt add geometry datatype check

118529 of 252344 branches covered (46.97%)

Branch coverage included in aggregate %.

7 of 48 new or added lines in 3 files covered. (14.58%)

2282 existing lines in 115 files now uncovered.

199096 of 275161 relevant lines covered (72.36%)

6067577.83 hits per line

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

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

39
  SSubmitTbData* pNew = *pData;
57✔
40

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

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

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

61
  return TSDB_CODE_SUCCESS;
57✔
62
}
63

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

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

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

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

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

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

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

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

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

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

146
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
8✔
147

148
  bool  isJson = false;
8✔
149
  STag* pTag = NULL;
8✔
150

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

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

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

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

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

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

266
  return code;
8✔
267
}
268

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

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

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

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

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

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

311
  return TSDB_CODE_SUCCESS;
51✔
312
}
313

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

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

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

340
    if (bind[c].num != rowNum) {
240!
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)) &&
240!
346
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
240!
347
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
348
      goto _return;
×
349
    }
350

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

361
    pBindInfos[c].columnId = pColSchema->colId;
240✔
362
    pBindInfos[c].bind = pBind;
240✔
363
    pBindInfos[c].type = pColSchema->type;
240✔
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);
16✔
372

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

375
_return:
16✔
376

377
  taosMemoryFree(ncharBind.buffer);
16✔
378
  taosMemoryFree(ncharBind.length);
16✔
379

380
  return code;
16✔
381
}
382

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

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

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

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

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

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

428
_return:
43✔
429

430
  taosMemoryFree(ncharBind.buffer);
73✔
431
  taosMemoryFree(ncharBind.length);
72✔
432

433
  return code;
72✔
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) {
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);
×
463
    if (code) {
×
464
      goto _return;
×
465
    }
466
    pBind = &ncharBind;
×
467
  } else {
468
    pBind = bind;
×
469
  }
470

471
  code = tColDataAddValueByBind(pCol, pBind,
×
NEW
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) {
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);
×
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) {
×
NEW
556
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
×
NEW
557
          if (initCtxAsText() || checkWKB(bind[c].buffer, colLen)) {
×
NEW
558
            code = buildSyntaxErrMsg(&pBuf, "invalid geometry tag", bind[c].buffer);
×
NEW
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)) {
×
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
  }
610

611
  code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName,
×
612
                             pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
×
613
  pTag = NULL;
×
614

615
end:
×
616
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
×
617
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
×
618
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
×
619
      taosMemoryFreeClear(p->pData);
×
620
    }
621
  }
622
  taosArrayDestroy(pTagArray);
×
623
  taosArrayDestroy(tagName);
×
624
  taosMemoryFree(pTag);
×
625

626
  return code;
×
627
}
628

629
static int32_t convertStmtStbNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst) {
×
630
  int32_t       output = 0;
×
631
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
×
632

633
  dst->buffer = taosMemoryCalloc(src->num, max_buf_len);
×
634
  if (NULL == dst->buffer) {
×
635
    return terrno;
×
636
  }
637

638
  dst->length = taosMemoryCalloc(src->num, sizeof(int32_t));
×
639
  if (NULL == dst->length) {
×
640
    taosMemoryFreeClear(dst->buffer);
×
641
    return terrno;
×
642
  }
643

644
  char* src_buf = src->buffer;
×
645
  char* dst_buf = dst->buffer;
×
646
  for (int32_t i = 0; i < src->num; ++i) {
×
647
    if (src->is_null && src->is_null[i]) {
×
648
      continue;
×
649
    }
650

651
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output)) {
×
652
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
653
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
654
      }
655
      char buf[512] = {0};
×
656
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
657
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
658
    }
659

660
    dst->length[i] = output;
×
661
    src_buf += src->length[i];
×
662
    dst_buf += output;
×
663
  }
664

665
  dst->buffer_type = src->buffer_type;
×
666
  dst->is_null = src->is_null;
×
667
  dst->num = src->num;
×
668

669
  return TSDB_CODE_SUCCESS;
×
670
}
671

672
int32_t qBindStmtStbColsValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen,
×
673
                               STSchema** pTSchema, SBindInfo2* pBindInfos) {
674
  STableDataCxt*  pDataBlock = (STableDataCxt*)pBlock;
×
675
  SSchema*        pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
676
  SBoundColInfo*  boundInfo = &pDataBlock->boundColsInfo;
×
677
  SMsgBuf         pBuf = {.buf = msgBuf, .len = msgBufLen};
×
678
  int32_t         rowNum = bind->num;
×
679
  SArray*         ncharBinds = NULL;
×
680
  TAOS_STMT2_BIND ncharBind = {0};
×
681
  int32_t         code = 0;
×
682
  int16_t         lastColId = -1;
×
683
  bool            colInOrder = true;
×
684

685
  if (NULL == *pTSchema) {
×
686
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
×
687
  }
688

689
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
×
690
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
×
691
    if (pColSchema->colId <= lastColId) {
×
692
      colInOrder = false;
×
693
    } else {
694
      lastColId = pColSchema->colId;
×
695
    }
696

697
    if (bind[c].num != rowNum) {
×
698
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
699
      goto _return;
×
700
    }
701

702
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
×
703
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
×
704
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
705
      goto _return;
×
706
    }
707

708
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
×
709
      code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind);
×
710
      if (code) {
×
711
        goto _return;
×
712
      }
713
      if (!ncharBinds) {
×
714
        ncharBinds = taosArrayInit(1, sizeof(ncharBind));
×
715
        if (!ncharBinds) {
×
716
          code = terrno;
×
717
          goto _return;
×
718
        }
719
      }
720
      if (!taosArrayPush(ncharBinds, &ncharBind)) {
×
721
        code = terrno;
×
722
        goto _return;
×
723
      }
724
      pBindInfos[c].bind = taosArrayGetLast(ncharBinds);
×
725
    } else {
726
      pBindInfos[c].bind = bind + c;
×
727
    }
728

729
    pBindInfos[c].columnId = pColSchema->colId;
×
730
    pBindInfos[c].type = pColSchema->type;
×
731
    pBindInfos[c].bytes = pColSchema->bytes;
×
732
  }
733

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

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

738
_return:
×
739
  if (ncharBinds) {
×
740
    for (int i = 0; i < TARRAY_SIZE(ncharBinds); ++i) {
×
741
      TAOS_STMT2_BIND* ncBind = TARRAY_DATA(ncharBinds);
×
742
      taosMemoryFree(ncBind[i].buffer);
×
743
      taosMemoryFree(ncBind[i].length);
×
744
    }
745
    taosArrayDestroy(ncharBinds);
×
746
  }
747

748
  return code;
×
749
}
750

751
static int32_t convertStmtNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst) {
×
752
  int32_t       output = 0;
×
753
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
×
754

755
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
×
756
  // if (dst->buffer_length < newBuflen) {
757
  dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
×
758
  if (NULL == dst->buffer) {
×
759
    return terrno;
×
760
  }
761
  //}
762

763
  if (NULL == dst->length) {
×
764
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
×
765
    if (NULL == dst->length) {
×
766
      taosMemoryFreeClear(dst->buffer);
×
767
      return terrno;
×
768
    }
769
  }
770

771
  // dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
772
  char* src_buf = src->buffer;
×
773
  char* dst_buf = dst->buffer;
×
774
  for (int32_t i = 0; i < src->num; ++i) {
×
775
    if (src->is_null && src->is_null[i]) {
×
776
      continue;
×
777
    }
778

779
    /*if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
780
      (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output)) {*/
781
    if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output)) {
×
782
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
783
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
784
      }
785
      char buf[512] = {0};
×
786
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
787
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
788
    }
789

790
    dst->length[i] = output;
×
791
    src_buf += src->length[i];
×
792
    dst_buf += output;
×
793
  }
794

795
  dst->buffer_type = src->buffer_type;
×
796
  dst->is_null = src->is_null;
×
797
  dst->num = src->num;
×
798

799
  return TSDB_CODE_SUCCESS;
×
800
}
801

802
int32_t qBindStmtColsValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen) {
×
803
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
×
804
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
805
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
×
806
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
×
807
  int32_t          rowNum = bind->num;
×
808
  TAOS_STMT2_BIND  ncharBind = {0};
×
809
  TAOS_STMT2_BIND* pBind = NULL;
×
810
  int32_t          code = 0;
×
811

812
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
×
813
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
×
814
    SColData* pCol = taosArrayGet(pCols, c);
×
815
    if (pCol == NULL || pColSchema == NULL) {
×
816
      code = buildInvalidOperationMsg(&pBuf, "get column schema or column data failed");
×
817
      goto _return;
×
818
    }
819

820
    if (bind[c].num != rowNum) {
×
821
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
822
      goto _return;
×
823
    }
824

825
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
×
826
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
×
827
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
828
      goto _return;
×
829
    }
830

831
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
×
832
      code = convertStmtNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind);
×
833
      if (code) {
×
834
        goto _return;
×
835
      }
836
      pBind = &ncharBind;
×
837
    } else {
838
      pBind = bind + c;
×
839
    }
840

841
    code = tColDataAddValueByBind2(pCol, pBind,
×
NEW
842
                                   IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1,
×
843
                                   initCtxAsText, checkWKB);
844
    if (code) {
×
845
      goto _return;
×
846
    }
847
  }
848

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

851
_return:
×
852

853
  taosMemoryFree(ncharBind.buffer);
×
854
  taosMemoryFree(ncharBind.length);
×
855

856
  return code;
×
857
}
858

859
int32_t qBindStmtSingleColValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen,
×
860
                                 int32_t colIdx, int32_t rowNum) {
861
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
×
862
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
863
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
×
864
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
×
865
  SSchema*         pColSchema = &pSchema[boundInfo->pColIndex[colIdx]];
×
866
  SColData*        pCol = taosArrayGet(pCols, colIdx);
×
867
  TAOS_STMT2_BIND  ncharBind = {0};
×
868
  TAOS_STMT2_BIND* pBind = NULL;
×
869
  int32_t          code = 0;
×
870

871
  if (bind->num != rowNum) {
×
872
    return buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
873
  }
874

875
  // Column index exceeds the number of columns
876
  if (colIdx >= pCols->size && pCol == NULL) {
×
877
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
878
  }
879

880
  if (bind->buffer_type != pColSchema->type) {
×
881
    return buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
882
  }
883

884
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
×
885
    code = convertStmtNcharCol2(&pBuf, pColSchema, bind, &ncharBind);
×
886
    if (code) {
×
887
      goto _return;
×
888
    }
889
    pBind = &ncharBind;
×
890
  } else {
891
    pBind = bind;
×
892
  }
893

894
  code = tColDataAddValueByBind2(pCol, pBind,
×
NEW
895
                                 IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1,
×
896
                                 initCtxAsText, checkWKB);
897

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

900
_return:
×
901

902
  taosMemoryFree(ncharBind.buffer);
×
903
  taosMemoryFree(ncharBind.length);
×
904

905
  return code;
×
906
}
907

908
int32_t buildBoundFields(int32_t numOfBound, int16_t* boundColumns, SSchema* pSchema, int32_t* fieldNum,
×
909
                         TAOS_FIELD_E** fields, uint8_t timePrec) {
910
  if (fields != NULL) {
×
911
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
×
912
    if (NULL == *fields) {
×
913
      return terrno;
×
914
    }
915

916
    SSchema* schema = &pSchema[boundColumns[0]];
×
917
    if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
×
918
      (*fields)[0].precision = timePrec;
×
919
    }
920

921
    for (int32_t i = 0; i < numOfBound; ++i) {
×
922
      schema = &pSchema[boundColumns[i]];
×
923
      strcpy((*fields)[i].name, schema->name);
×
924
      (*fields)[i].type = schema->type;
×
925
      (*fields)[i].bytes = schema->bytes;
×
926
    }
927
  }
928

929
  *fieldNum = numOfBound;
×
930

931
  return TSDB_CODE_SUCCESS;
×
932
}
933

934
int32_t buildStbBoundFields(SBoundColInfo boundColsInfo, SSchema* pSchema, int32_t* fieldNum, TAOS_FIELD_STB** fields,
×
935
                            STableMeta* pMeta) {
936
  if (fields != NULL) {
×
937
    *fields = taosMemoryCalloc(boundColsInfo.numOfBound, sizeof(TAOS_FIELD_STB));
×
938
    if (NULL == *fields) {
×
939
      return terrno;
×
940
    }
941

942
    SSchema* schema = &pSchema[boundColsInfo.pColIndex[0]];
×
943
    if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
×
944
      (*fields)[0].precision = pMeta->tableInfo.precision;
×
945
    }
946

947
    for (int32_t i = 0; i < boundColsInfo.numOfBound; ++i) {
×
948
      int16_t idx = boundColsInfo.pColIndex[i];
×
949

950
      if (idx == pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags) {
×
951
        (*fields)[i].field_type = TAOS_FIELD_TBNAME;
×
952
        tstrncpy((*fields)[i].name, "tbname", sizeof((*fields)[i].name));
×
953
        continue;
×
954
      } else if (idx < pMeta->tableInfo.numOfColumns) {
×
955
        (*fields)[i].field_type = TAOS_FIELD_COL;
×
956
      } else {
957
        (*fields)[i].field_type = TAOS_FIELD_TAG;
×
958
      }
959

960
      schema = &pSchema[idx];
×
961
      tstrncpy((*fields)[i].name, schema->name, sizeof((*fields)[i].name));
×
962
      (*fields)[i].type = schema->type;
×
963
      (*fields)[i].bytes = schema->bytes;
×
964
    }
965
  }
966

967
  *fieldNum = boundColsInfo.numOfBound;
×
968

969
  return TSDB_CODE_SUCCESS;
×
970
}
971

972
int32_t qBuildStmtTagFields(void* pBlock, void* boundTags, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
973
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
974
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
×
975
  if (NULL == tags) {
×
976
    return TSDB_CODE_APP_ERROR;
×
977
  }
978
  /*
979
  if (pDataBlock->pMeta->tableType != TSDB_SUPER_TABLE && pDataBlock->pMeta->tableType != TSDB_CHILD_TABLE) {
980
    return TSDB_CODE_TSC_STMT_API_ERROR;
981
  }
982
  */
983
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
×
984
  if (tags->numOfBound <= 0) {
×
985
    *fieldNum = 0;
×
986
    *fields = NULL;
×
987

988
    return TSDB_CODE_SUCCESS;
×
989
  }
990

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

993
  return TSDB_CODE_SUCCESS;
×
994
}
995

996
int32_t qBuildStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
997
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
998
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
999
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
×
1000
    *fieldNum = 0;
×
1001
    if (fields != NULL) {
×
1002
      *fields = NULL;
×
1003
    }
1004

1005
    return TSDB_CODE_SUCCESS;
×
1006
  }
1007

1008
  CHECK_CODE(buildBoundFields(pDataBlock->boundColsInfo.numOfBound, pDataBlock->boundColsInfo.pColIndex, pSchema,
×
1009
                              fieldNum, fields, pDataBlock->pMeta->tableInfo.precision));
1010

1011
  return TSDB_CODE_SUCCESS;
×
1012
}
1013

1014
int32_t qBuildStmtStbColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_STB** fields) {
×
1015
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
1016
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
1017
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
×
1018
    *fieldNum = 0;
×
1019
    if (fields != NULL) {
×
1020
      *fields = NULL;
×
1021
    }
1022

1023
    return TSDB_CODE_SUCCESS;
×
1024
  }
1025

1026
  CHECK_CODE(buildStbBoundFields(pDataBlock->boundColsInfo, pSchema, fieldNum, fields, pDataBlock->pMeta));
×
1027

1028
  return TSDB_CODE_SUCCESS;
×
1029
}
1030

1031
int32_t qResetStmtColumns(SArray* pCols, bool deepClear) {
×
1032
  int32_t colNum = taosArrayGetSize(pCols);
×
1033

1034
  for (int32_t i = 0; i < colNum; ++i) {
×
1035
    SColData* pCol = (SColData*)taosArrayGet(pCols, i);
×
1036
    if (pCol == NULL) {
×
1037
      qError("qResetStmtColumns column is NULL");
×
1038
      return terrno;
×
1039
    }
1040
    if (deepClear) {
×
1041
      tColDataDeepClear(pCol);
×
1042
    } else {
1043
      tColDataClear(pCol);
×
1044
    }
1045
  }
1046

1047
  return TSDB_CODE_SUCCESS;
×
1048
}
1049

1050
int32_t qResetStmtDataBlock(STableDataCxt* block, bool deepClear) {
84✔
1051
  STableDataCxt* pBlock = (STableDataCxt*)block;
84✔
1052
  int32_t        colNum = taosArrayGetSize(pBlock->pData->aCol);
84✔
1053

1054
  for (int32_t i = 0; i < colNum; ++i) {
987✔
1055
    SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i);
903✔
1056
    if (pCol == NULL) {
903!
UNCOV
1057
      qError("qResetStmtDataBlock column is NULL");
×
UNCOV
1058
      return terrno;
×
1059
    }
1060
    if (deepClear) {
903✔
1061
      tColDataDeepClear(pCol);
327✔
1062
    } else {
1063
      tColDataClear(pCol);
576✔
1064
    }
1065
  }
1066

1067
  return TSDB_CODE_SUCCESS;
84✔
1068
}
1069

1070
int32_t qCloneStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, bool reset) {
28✔
1071
  int32_t code = 0;
28✔
1072

1073
  *pDst = taosMemoryCalloc(1, sizeof(STableDataCxt));
28✔
1074
  if (NULL == *pDst) {
28!
1075
    return terrno;
×
1076
  }
1077

1078
  STableDataCxt* pNewCxt = (STableDataCxt*)*pDst;
28✔
1079
  STableDataCxt* pCxt = (STableDataCxt*)pSrc;
28✔
1080
  pNewCxt->pSchema = NULL;
28✔
1081
  pNewCxt->pValues = NULL;
28✔
1082

1083
  if (pCxt->pMeta) {
28!
1084
    void* pNewMeta = taosMemoryMalloc(TABLE_META_SIZE(pCxt->pMeta));
28!
1085
    if (NULL == pNewMeta) {
28!
1086
      insDestroyTableDataCxt(*pDst);
×
1087
      return terrno;
×
1088
    }
1089
    memcpy(pNewMeta, pCxt->pMeta, TABLE_META_SIZE(pCxt->pMeta));
28!
1090
    pNewCxt->pMeta = pNewMeta;
28✔
1091
  }
1092

1093
  memcpy(&pNewCxt->boundColsInfo, &pCxt->boundColsInfo, sizeof(pCxt->boundColsInfo));
28✔
1094
  pNewCxt->boundColsInfo.pColIndex = NULL;
28✔
1095

1096
  if (pCxt->boundColsInfo.pColIndex) {
28!
1097
    void* pNewColIdx = taosMemoryMalloc(pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
28✔
1098
    if (NULL == pNewColIdx) {
28!
1099
      insDestroyTableDataCxt(*pDst);
×
1100
      return terrno;
×
1101
    }
1102
    memcpy(pNewColIdx, pCxt->boundColsInfo.pColIndex,
28✔
1103
           pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
28✔
1104
    pNewCxt->boundColsInfo.pColIndex = pNewColIdx;
28✔
1105
  }
1106

1107
  if (pCxt->pData) {
28!
1108
    SSubmitTbData* pNewTb = (SSubmitTbData*)taosMemoryMalloc(sizeof(SSubmitTbData));
28✔
1109
    if (NULL == pNewTb) {
28!
1110
      insDestroyTableDataCxt(*pDst);
×
1111
      return terrno;
×
1112
    }
1113

1114
    memcpy(pNewTb, pCxt->pData, sizeof(*pCxt->pData));
28✔
1115
    pNewTb->pCreateTbReq = NULL;
28✔
1116

1117
    pNewTb->aCol = taosArrayDup(pCxt->pData->aCol, NULL);
28✔
1118
    if (NULL == pNewTb->aCol) {
28!
1119
      insDestroyTableDataCxt(*pDst);
×
1120
      return terrno;
×
1121
    }
1122

1123
    pNewCxt->pData = pNewTb;
28✔
1124

1125
    if (reset) {
28!
1126
      code = qResetStmtDataBlock(*pDst, true);
28✔
1127
    }
1128
  }
1129

1130
  return code;
28✔
1131
}
1132

1133
int32_t qRebuildStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, uint64_t uid, uint64_t suid, int32_t vgId,
×
1134
                              bool rebuildCreateTb) {
1135
  int32_t code = qCloneStmtDataBlock(pDst, pSrc, false);
×
1136
  if (code) {
×
1137
    return code;
×
1138
  }
1139

1140
  STableDataCxt* pBlock = (STableDataCxt*)*pDst;
×
1141
  if (pBlock->pMeta) {
×
1142
    pBlock->pMeta->uid = uid;
×
1143
    pBlock->pMeta->vgId = vgId;
×
1144
    pBlock->pMeta->suid = suid;
×
1145
  }
1146

1147
  pBlock->pData->suid = suid;
×
1148
  pBlock->pData->uid = uid;
×
1149

1150
  if (rebuildCreateTb && NULL == pBlock->pData->pCreateTbReq) {
×
1151
    pBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1152
    if (NULL == pBlock->pData->pCreateTbReq) {
×
1153
      return terrno;
×
1154
    }
1155
  }
1156

1157
  return TSDB_CODE_SUCCESS;
×
1158
}
1159

1160
STableMeta* qGetTableMetaInDataBlock(STableDataCxt* pDataBlock) { return ((STableDataCxt*)pDataBlock)->pMeta; }
84✔
1161

1162
void qDestroyStmtDataBlock(STableDataCxt* pBlock) {
108✔
1163
  if (pBlock == NULL) {
108✔
1164
    return;
48✔
1165
  }
1166

1167
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
60✔
1168
  insDestroyTableDataCxt(pDataBlock);
60✔
1169
}
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