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

taosdata / TDengine / #4864

26 Nov 2025 05:46AM UTC coverage: 64.548% (+0.009%) from 64.539%
#4864

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

769 of 945 new or added lines in 33 files covered. (81.38%)

3006 existing lines in 116 files now uncovered.

158227 of 245129 relevant lines covered (64.55%)

111826500.07 hits per line

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

41.59
/source/libs/parser/src/parInsertStmt.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "geosWrapper.h"
17
#include "os.h"
18
#include "parInsertUtil.h"
19
#include "parInt.h"
20
#include "parToken.h"
21
#include "query.h"
22
#include "tdataformat.h"
23
#include "tglobal.h"
24
#include "ttime.h"
25
#include "ttypes.h"
26

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

34
int32_t qCloneCurrentTbData(STableDataCxt* pDataBlock, SSubmitTbData** pData) {
834,382✔
35
  if (pDataBlock == NULL || pDataBlock->pData == NULL) {
834,382✔
UNCOV
36
    qError("stmt qCloneCurrentTbData input is null, maybe not bind data before execute");
×
37
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
38
  }
39
  *pData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
834,596✔
40
  if (NULL == *pData) {
834,530✔
41
    return terrno;
×
42
  }
43

44
  int8_t         flag = 1;
834,530✔
45
  SSubmitTbData* pNew = *pData;
834,530✔
46

47
  *pNew = *pDataBlock->pData;
834,565✔
48
  pNew->pBlobSet = NULL;
834,565✔
49

50
  int32_t code = cloneSVreateTbReq(pDataBlock->pData->pCreateTbReq, &pNew->pCreateTbReq);
834,600✔
51
  if (TSDB_CODE_SUCCESS != code) {
834,530✔
52
    taosMemoryFreeClear(*pData);
×
53
    return code;
×
54
  }
55
  pNew->aCol = taosArrayDup(pDataBlock->pData->aCol, NULL);
834,530✔
56
  if (!pNew->aCol) {
834,740✔
57
    code = terrno;
×
58
    taosMemoryFreeClear(*pData);
×
59
    return code;
×
60
  }
61

62
  int32_t colNum = taosArrayGetSize(pNew->aCol);
834,244✔
63
  for (int32_t i = 0; i < colNum; ++i) {
5,468,366✔
64
    if (pDataBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
4,634,175✔
65
      SColData* pCol = (SColData*)taosArrayGet(pNew->aCol, i);
4,634,566✔
66
      tColDataDeepClear(pCol);
4,634,353✔
67
    } else {
68
      pNew->aCol = taosArrayInit(20, POINTER_BYTES);
×
69
      if (pNew->aCol == NULL) {
×
70
        code = terrno;
×
71
        taosMemoryFreeClear(*pData);
×
72
        return code;
×
73
      }
74
    }
75
  }
76

77
  return code;
834,191✔
78
}
79

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

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

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

96
  if (TSDB_CODE_SUCCESS == code) {
10,565,389✔
97
    code = insBuildVgDataBlocks(pAllVgHash, pVgDataBlocks, &pStmt->pDataBlocks, true);
10,566,553✔
98
  }
99

100
  if (pStmt->freeArrayFunc) {
10,565,573✔
101
    pStmt->freeArrayFunc(pVgDataBlocks);
10,566,690✔
102
  }
103
  return code;
10,566,339✔
104
}
105

106
/*
107
int32_t qBuildStmtOutputFromTbList(SQuery* pQuery, SHashObj* pVgHash, SArray* pBlockList, STableDataCxt* pTbCtx, int32_t
108
tbNum) { int32_t             code = TSDB_CODE_SUCCESS; SArray*             pVgDataBlocks = NULL; SVnodeModifyOpStmt*
109
pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
110

111
  // merge according to vgId
112
  if (tbNum > 0) {
113
    code = insMergeStmtTableDataCxt(pTbCtx, pBlockList, &pVgDataBlocks, true, tbNum);
114
  }
115

116
  if (TSDB_CODE_SUCCESS == code) {
117
    code = insBuildVgDataBlocks(pVgHash, pVgDataBlocks, &pStmt->pDataBlocks);
118
  }
119

120
  if (pStmt->freeArrayFunc) {
121
    pStmt->freeArrayFunc(pVgDataBlocks);
122
  }
123
  return code;
124
}
125
*/
126

127
int32_t qBuildStmtOutput(SQuery* pQuery, SHashObj* pVgHash, SHashObj* pBlockHash) {
833,627✔
128
  int32_t             code = TSDB_CODE_SUCCESS;
833,627✔
129
  SArray*             pVgDataBlocks = NULL;
833,627✔
130
  SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;
834,452✔
131

132
  // merge according to vgId
133
  if (taosHashGetSize(pBlockHash) > 0) {
834,166✔
134
    code = insMergeTableDataCxt(pBlockHash, &pVgDataBlocks, true);
834,561✔
135
  }
136

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

141
  if (pStmt->freeArrayFunc) {
834,635✔
142
    pStmt->freeArrayFunc(pVgDataBlocks);
834,670✔
143
  }
144
  return code;
834,034✔
145
}
146

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

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

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

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

170
  bool  isJson = false;
27,183✔
171
  STag* pTag = NULL;
27,183✔
172

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

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

201
      isJson = true;
3,985✔
202
      char* tmp = taosMemoryCalloc(1, colLen + 1);
3,985✔
203
      if (!tmp) {
3,985✔
204
        code = terrno;
×
205
        goto end;
×
206
      }
207
      memcpy(tmp, bind[c].buffer, colLen);
3,985✔
208
      code = parseJsontoTagData(tmp, pTagArray, &pTag, &pBuf, charsetCxt);
3,985✔
209
      taosMemoryFree(tmp);
3,985✔
210
      if (code != TSDB_CODE_SUCCESS) {
3,985✔
211
        goto end;
×
212
      }
213
    } else {
214
      STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
80,278✔
215
      //      strcpy(val.colName, pTagSchema->name);
216
      if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
80,278✔
217
          pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
58,016✔
218
        if (pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
25,842✔
219
          code = initCtxAsText();
3,580✔
220
          if (code) {
3,580✔
221
            qError("geometry init failed:%s", tstrerror(code));
×
222
            goto end;
×
223
          }
224
          code = checkWKB(bind[c].buffer, colLen);
3,580✔
225
          if (code) {
3,580✔
226
            qError("stmt bind invalid geometry tag:%s, must be WKB format", (char*)bind[c].buffer);
×
227
            goto end;
×
228
          }
229
        }
230
        val.pData = (uint8_t*)bind[c].buffer;
25,842✔
231
        val.nData = colLen;
25,842✔
232
      } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
54,436✔
233
        int32_t output = 0;
8,272✔
234
        void*   p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
8,272✔
235
        if (p == NULL) {
8,272✔
236
          code = terrno;
×
237
          goto end;
×
238
        }
239
        if (!taosMbsToUcs4(bind[c].buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output, charsetCxt)) {
8,272✔
240
          if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
241
            taosMemoryFree(p);
×
242
            code = generateSyntaxErrMsg(&pBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pTagSchema->name);
×
243
            goto end;
×
244
          }
245
          char buf[512] = {0};
×
246
          snprintf(buf, tListLen(buf), " taosMbsToUcs4 error:%s", strerror(terrno));
×
247
          taosMemoryFree(p);
×
248
          code = buildSyntaxErrMsg(&pBuf, buf, bind[c].buffer);
×
249
          goto end;
×
250
        }
251
        val.pData = p;
8,272✔
252
        val.nData = output;
8,272✔
253
      } else {
254
        memcpy(&val.i64, bind[c].buffer, colLen);
46,164✔
255
      }
256
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
80,278✔
257
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
258
        goto end;
×
259
      }
260
      if (NULL == taosArrayPush(pTagArray, &val)) {
80,278✔
261
        code = terrno;
×
262
        goto end;
×
263
      }
264
    }
265
  }
266

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

271
  if (NULL == pDataBlock->pData->pCreateTbReq) {
27,183✔
272
    pDataBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
273
    if (NULL == pDataBlock->pData->pCreateTbReq) {
×
274
      code = terrno;
×
275
      goto end;
×
276
    }
277
  } else {
278
    SVCreateTbReq* tmp = pDataBlock->pData->pCreateTbReq;
27,183✔
279
    taosMemoryFreeClear(tmp->name);
27,183✔
280
    taosMemoryFreeClear(tmp->ctb.pTag);
27,183✔
281
    taosMemoryFreeClear(tmp->ctb.stbName);
27,183✔
282
    taosArrayDestroy(tmp->ctb.tagName);
27,183✔
283
    tmp->ctb.tagName = NULL;
27,183✔
284
  }
285

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

290
end:
27,183✔
291
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
111,041✔
292
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
83,858✔
293
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
83,858✔
294
      taosMemoryFreeClear(p->pData);
11,852✔
295
    }
296
  }
297
  taosArrayDestroy(pTagArray);
27,183✔
298
  taosArrayDestroy(tagName);
27,183✔
299
  taosMemoryFree(pTag);
27,183✔
300

301
  return code;
27,183✔
302
}
303

304
int32_t convertStmtNcharCol(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_MULTI_BIND* src, TAOS_MULTI_BIND* dst,
237,280,132✔
305
                            void* charsetCxt) {
306
  int32_t output = 0;
237,280,132✔
307
  int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
238,237,492✔
308
  if (dst->buffer_length < newBuflen) {
239,738,553✔
309
    dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
239,688,941✔
310
    if (NULL == dst->buffer) {
236,389,511✔
311
      return terrno;
×
312
    }
313
  }
314

315
  if (NULL == dst->length) {
236,747,419✔
316
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
237,499,248✔
317
    if (NULL == dst->length) {
240,412,162✔
318
      taosMemoryFreeClear(dst->buffer);
×
319
      return terrno;
×
320
    }
321
  }
322

323
  dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
239,999,102✔
324

325
  for (int32_t i = 0; i < src->num; ++i) {
478,954,808✔
326
    if (src->is_null && src->is_null[i]) {
239,929,753✔
327
      continue;
12,048,444✔
328
    }
329

330
    if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
228,099,615✔
331
                       (TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output,
228,454,741✔
332
                       charsetCxt)) {
333
      if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) {
×
334
        return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
×
335
      }
336
      char buf[512] = {0};
×
337
      snprintf(buf, tListLen(buf), "%s", strerror(terrno));
×
338
      return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
×
339
    }
340

341
    dst->length[i] = output;
226,203,017✔
342
  }
343

344
  dst->buffer_type = src->buffer_type;
239,519,569✔
345
  dst->is_null = src->is_null;
240,035,188✔
346
  dst->num = src->num;
239,054,983✔
347

348
  return TSDB_CODE_SUCCESS;
239,869,173✔
349
}
350

351
int32_t qBindStmtStbColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
12,630,853✔
352
                              STSchema** pTSchema, SBindInfo* pBindInfos, void* charsetCxt) {
353
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
12,630,853✔
354
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
12,630,853✔
355
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
12,656,348✔
356
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
12,658,273✔
357
  int32_t          rowNum = bind->num;
12,662,123✔
358
  TAOS_MULTI_BIND  ncharBind = {0};
12,656,898✔
359
  TAOS_MULTI_BIND* pBind = NULL;
12,663,484✔
360
  int32_t          code = 0;
12,663,484✔
361
  int16_t          lastColId = -1;
12,663,484✔
362
  bool             colInOrder = true;
12,663,484✔
363

364
  if (NULL == pTSchema || NULL == *pTSchema) {
12,663,484✔
365
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
43,390✔
366
  }
367

368
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
65,253,314✔
369
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
52,583,663✔
370
    if (pColSchema->colId <= lastColId) {
52,594,644✔
371
      colInOrder = false;
×
372
    } else {
373
      lastColId = pColSchema->colId;
52,599,153✔
374
    }
375
    // SColData* pCol = taosArrayGet(pCols, c);
376

377
    if (bind[c].num != rowNum) {
52,598,252✔
378
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
379
      goto _return;
×
380
    }
381

382
    if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
52,600,457✔
383
        bind[c].buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
52,591,382✔
384
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
385
      goto _return;
×
386
    }
387

388
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
52,590,268✔
389
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
4,396✔
390
      if (code) {
4,396✔
391
        goto _return;
×
392
      }
393
      pBind = &ncharBind;
4,396✔
394
    } else {
395
      pBind = bind + c;
52,573,852✔
396
    }
397

398
    pBindInfos[c].columnId = pColSchema->colId;
52,591,107✔
399
    pBindInfos[c].bind = pBind;
52,593,288✔
400
    pBindInfos[c].type = pColSchema->type;
52,593,149✔
401
  }
402

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

406
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
12,650,615✔
407

408
_return:
12,650,615✔
409

410
  taosMemoryFree(ncharBind.buffer);
12,654,465✔
411
  taosMemoryFree(ncharBind.length);
12,662,440✔
412

413
  return code;
12,671,473✔
414
}
415

416
int32_t qBindStmtColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
309,094,806✔
417
                           void* charsetCxt) {
418
  STableDataCxt*   pDataBlock = (STableDataCxt*)pBlock;
309,094,806✔
419
  SSchema*         pSchema = getTableColumnSchema(pDataBlock->pMeta);
309,094,806✔
420
  SBoundColInfo*   boundInfo = &pDataBlock->boundColsInfo;
312,940,457✔
421
  SMsgBuf          pBuf = {.buf = msgBuf, .len = msgBufLen};
312,309,500✔
422
  int32_t          rowNum = bind->num;
313,283,140✔
423
  TAOS_MULTI_BIND  ncharBind = {0};
314,040,698✔
424
  TAOS_MULTI_BIND* pBind = NULL;
313,571,783✔
425
  int32_t          code = 0;
313,571,783✔
426

427
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
2,147,483,647✔
428
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
2,147,483,647✔
429
    SColData* pCol = taosArrayGet(pCols, c);
2,147,483,647✔
430

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

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

442
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
2,147,483,647✔
443
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
240,489,292✔
444
      if (code) {
236,846,556✔
445
        goto _return;
×
446
      }
447
      pBind = &ncharBind;
236,846,556✔
448
    } else {
449
      pBind = bind + c;
2,147,483,647✔
450
    }
451

452
    int32_t bytes = 0;
2,147,483,647✔
453
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
2,147,483,647✔
454
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
450,088,363✔
455
        bytes = pColSchema->bytes - BLOBSTR_HEADER_SIZE;
141,163✔
456
      } else {
457
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
495,785,149✔
458
      }
459
    } else {
460
      bytes = -1;
2,147,483,647✔
461
    }
462
    code = tColDataAddValueByBind(pCol, pBind, bytes, initCtxAsText, checkWKB);
2,147,483,647✔
463
    if (code) {
2,147,483,647✔
464
      goto _return;
3,894✔
465
    }
466
  }
467

468
  parserDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
316,831,592✔
469

470
_return:
316,835,486✔
471

472
  taosMemoryFree(ncharBind.buffer);
316,611,187✔
473
  taosMemoryFree(ncharBind.length);
310,261,700✔
474

475
  return code;
314,498,036✔
476
}
477

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

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

498
    if (bind[c].num != rowNum) {
×
499
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
500
      goto _return;
×
501
    }
502

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

509
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
×
510
      code = convertStmtNcharCol(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt);
×
511
      if (code) {
×
512
        goto _return;
×
513
      }
514
      pBind = &ncharBind;
×
515
    } else {
516
      pBind = bind + c;
×
517
    }
518

519
    int32_t bytes = 0;
×
520
    if (IS_VAR_DATA_TYPE(pColSchema->type)) {
×
521
      if (IS_STR_DATA_BLOB(pColSchema->type)) {
×
522
        bytes = pColSchema->bytes - BLOBSTR_HEADER_SIZE;
×
523
      } else {
524
        bytes = pColSchema->bytes - VARSTR_HEADER_SIZE;
×
525
      }
526
    } else {
527
      bytes = -1;
×
528
    }
529
    code = tColDataAddValueByBind(pCol, pBind, bytes, initCtxAsText, checkWKB);
×
530
    if (code) {
×
531
      goto _return;
×
532
    }
533
    actualIndex++;
×
534
  }
535

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

538
_return:
×
539

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

543
  return code;
×
544
}
545

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

558
  if (bind->num != rowNum) {
50,120✔
559
    return buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
560
  }
561

562
  // Column index exceeds the number of columns
563
  if (colIdx >= pCols->size && pCol == NULL) {
50,120✔
564
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
565
  }
566

567
  if (bind->buffer_type != pColSchema->type) {
50,120✔
568
    return buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
569
  }
570

571
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
50,120✔
572
    code = convertStmtNcharCol(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
3,580✔
573
    if (code) {
3,580✔
574
      goto _return;
×
575
    }
576
    pBind = &ncharBind;
3,580✔
577
  } else {
578
    pBind = bind;
46,540✔
579
  }
580
  
581
  code = tColDataAddValueByBind(pCol, pBind,
50,120✔
582
                                IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1,
50,120✔
583
                                initCtxAsText, checkWKB);
584

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

587
_return:
50,120✔
588

589
  taosMemoryFree(ncharBind.buffer);
50,120✔
590
  taosMemoryFree(ncharBind.length);
50,120✔
591

592
  return code;
50,120✔
593
}
594

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

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

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

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

628
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
9,371,296✔
629

630
  bool  isJson = false;
9,370,660✔
631
  STag* pTag = NULL;
9,370,660✔
632

633
  int bindIdx = 0;
9,370,660✔
634
  for (int c = 0; c < tags->numOfBound; ++c) {
28,112,760✔
635
    if (bind == NULL) {
18,741,272✔
636
      break;
×
637
    }
638
    if (tags->parseredTags) {
18,741,272✔
639
      bool found = false;
×
640
      for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
×
641
        if (tags->parseredTags->pTagIndex[k] == tags->pColIndex[c]) {
×
642
          found = true;
×
643
          break;
×
644
        }
645
      }
646
      if (found) {
×
647
        continue;
×
648
      }
649
    }
650

651
    TAOS_STMT2_BIND bindData = bind[bindIdx++];
18,741,272✔
652

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

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

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

738
      } else {
739
        memcpy(&val.i64, bindData.buffer, colLen);
9,371,056✔
740
      }
741
      if (IS_VAR_DATA_TYPE(pTagSchema->type) && val.nData > pTagSchema->bytes) {
18,742,424✔
742
        code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
743
        goto end;
×
744
      }
745
      if (NULL == taosArrayPush(pTagArray, &val)) {
18,742,024✔
746
        code = terrno;
×
747
        goto end;
×
748
      }
749
    }
750
  }
751

752
  if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) {
9,371,488✔
753
    goto end;
×
754
  }
755

756
  if (pCreateTbReq) {
9,372,420✔
757
    code = insBuildCreateTbReq(pCreateTbReq, tName, pTag, suid, sTableName, tagName,
9,371,324✔
758
                               pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
9,371,324✔
759
    pTag = NULL;
9,370,936✔
760
    goto end;
9,370,936✔
761
  }
762

763
  if (NULL == pDataBlock->pData->pCreateTbReq) {
1,264✔
764
    pDataBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
236✔
765
    if (NULL == pDataBlock->pData->pCreateTbReq) {
236✔
766
      code = terrno;
×
767
      goto end;
×
768
    }
769
  } else {
770
    SVCreateTbReq* tmp = pDataBlock->pData->pCreateTbReq;
1,100✔
771
    taosMemoryFreeClear(tmp->name);
1,100✔
772
    taosMemoryFreeClear(tmp->ctb.pTag);
1,100✔
773
    taosMemoryFreeClear(tmp->ctb.stbName);
1,100✔
774
    taosArrayDestroy(tmp->ctb.tagName);
1,100✔
775
    tmp->ctb.tagName = NULL;
2,124✔
776
  }
777

778
  code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName,
2,360✔
779
                             pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
2,360✔
780
  pTag = NULL;
2,360✔
781

782
end:
9,373,296✔
783
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
28,114,396✔
784
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
18,741,496✔
785
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
18,741,100✔
786
      taosMemoryFreeClear(p->pData);
×
787
    }
788
  }
789
  taosArrayDestroy(pTagArray);
9,368,908✔
790
  taosArrayDestroy(tagName);
9,373,752✔
791
  taosMemoryFree(pTag);
9,374,376✔
792

793
  return code;
9,373,140✔
794
}
795

796
static int32_t convertStmtStbNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst,
×
797
                                       void* charsetCxt) {
798
  int32_t       output = 0;
×
799
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
×
800

801
  dst->buffer = taosMemoryCalloc(src->num, max_buf_len);
×
802
  if (NULL == dst->buffer) {
×
803
    return terrno;
×
804
  }
805

806
  dst->length = taosMemoryCalloc(src->num, sizeof(int32_t));
×
807
  if (NULL == dst->length) {
×
808
    taosMemoryFreeClear(dst->buffer);
×
809
    return terrno;
×
810
  }
811

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

819
    if (src->length[i] == 0) {
×
820
      dst->length[i] = 0;
×
821
      continue;
×
822
    }
823

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

833
    dst->length[i] = output;
×
834
    src_buf += src->length[i];
×
835
    dst_buf += output;
×
836
  }
837

838
  dst->buffer_type = src->buffer_type;
×
839
  dst->is_null = src->is_null;
×
840
  dst->num = src->num;
×
841

842
  return TSDB_CODE_SUCCESS;
×
843
}
844

845
int32_t qBindStmtStbColsValue2(void* pBlock, SArray* pCols, SSHashObj* parsedCols, TAOS_STMT2_BIND* bind, char* msgBuf,
15,220,485✔
846
                               int32_t msgBufLen, STSchema** pTSchema, SBindInfo2* pBindInfos, void* charsetCxt,
847
                               SBlobSet** ppBlob) {
848
  STableDataCxt*  pDataBlock = (STableDataCxt*)pBlock;
15,220,485✔
849
  SSchema*        pSchema = getTableColumnSchema(pDataBlock->pMeta);
15,220,485✔
850
  SSchemaExt*     pSchemaExt = getTableColumnExtSchema(pDataBlock->pMeta);
15,218,165✔
851
  SBoundColInfo*  boundInfo = &pDataBlock->boundColsInfo;
15,223,415✔
852
  SMsgBuf         pBuf = {.buf = msgBuf, .len = msgBufLen};
15,223,415✔
853
  int32_t         rowNum = bind->num;
15,223,415✔
854
  SArray*         ncharBinds = NULL;
15,223,415✔
855
  TAOS_STMT2_BIND ncharBind = {0};
15,223,415✔
856
  int32_t         code = 0;
15,223,415✔
857
  int16_t         lastColId = -1;
15,223,415✔
858
  bool            colInOrder = true;
15,223,415✔
859
  int             ncharColNums = 0;
15,223,415✔
860
  int32_t         bindIdx = 0;
15,223,415✔
861
  int8_t          hasBlob = 0;
15,223,415✔
862
  int32_t         lino = 0;
15,223,415✔
863
  if (NULL == pTSchema || NULL == *pTSchema) {
15,223,415✔
864
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
21,092✔
865
  }
866

867
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
99,023,482✔
868
    if (TSDB_DATA_TYPE_NCHAR == pSchema[boundInfo->pColIndex[c]].type) {
83,803,726✔
869
      ncharColNums++;
×
870
    }
871
  }
872
  if (ncharColNums > 0) {
15,221,035✔
873
    ncharBinds = taosArrayInit(ncharColNums, sizeof(ncharBind));
×
874
    if (!ncharBinds) {
×
875
      code = terrno;
×
876
      TAOS_CHECK_GOTO(code, &lino, _return);
×
877
    }
878
  }
879

880
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
99,020,498✔
881
    SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
83,799,246✔
882
    if (pColSchema->colId <= lastColId) {
83,798,984✔
883
      colInOrder = false;
×
884
    } else {
885
      lastColId = pColSchema->colId;
83,789,513✔
886
    }
887

888
    if (parsedCols) {
83,800,265✔
889
      SColVal* pParsedVal = tSimpleHashGet(parsedCols, &pColSchema->colId, sizeof(int16_t));
×
890
      if (pParsedVal) {
×
891
        pBindInfos[c].columnId = pColSchema->colId;
×
892
        pBindInfos[c].type = pColSchema->type;
×
893
        pBindInfos[c].bytes = pColSchema->bytes;
×
894
        continue;
×
895
      }
896
    }
897

898
    TAOS_STMT2_BIND bindData = bind[bindIdx];
83,800,265✔
899

900
    if (bindData.num != rowNum) {
83,796,680✔
901
      code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
902
      goto _return;
×
903
    }
904

905
    if ((!(rowNum == 1 && bindData.is_null && *bindData.is_null)) &&
83,796,680✔
906
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
83,796,424✔
907
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
908
      goto _return;
×
909
    }
910

911
    if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
83,793,605✔
912
      code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + bindIdx, &ncharBind, charsetCxt);
×
913
      TAOS_CHECK_GOTO(code, &lino, _return);
×
914

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

944
    pBindInfos[c].columnId = pColSchema->colId;
83,776,300✔
945
    pBindInfos[c].type = pColSchema->type;
83,792,077✔
946
    pBindInfos[c].bytes = pColSchema->bytes;
83,792,592✔
947

948
    bindIdx++;
83,796,432✔
949
  }
950

951
  if (hasBlob == 0) {
15,224,577✔
952
    code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, parsedCols, colInOrder, *pTSchema, pSchemaExt, pCols,
15,217,997✔
953
                              &pDataBlock->ordered, &pDataBlock->duplicateTs);
954
    TAOS_CHECK_GOTO(code, &lino, _return);
15,228,248✔
955
  } else {
956
    code = tBlobSetCreate(1024, 1, ppBlob);
6,580✔
957
    TAOS_CHECK_GOTO(code, &lino, _return);
×
958

959
    code = tRowBuildFromBind2WithBlob(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols,
×
960
                                      &pDataBlock->ordered, &pDataBlock->duplicateTs, *ppBlob);
961
    TAOS_CHECK_GOTO(code, &lino, _return);
×
962
  }
963

964
  parserDebug("stmt2 all %d columns bind %d rows data as row format", boundInfo->numOfBound, rowNum);
15,227,477✔
965

966
_return:
15,227,477✔
967
  if (ncharBinds) {
15,227,734✔
968
    for (int i = 0; i < TARRAY_SIZE(ncharBinds); ++i) {
×
969
      TAOS_STMT2_BIND* ncBind = TARRAY_DATA(ncharBinds);
×
970
      taosMemoryFree(ncBind[i].buffer);
×
971
      taosMemoryFree(ncBind[i].length);
×
972
    }
973
    taosArrayDestroy(ncharBinds);
×
974
  }
975
  if (code != 0) {
15,227,734✔
976
    parserError("stmt2 failed to bind stb col at lino %d since %s", lino, tstrerror(code));
×
977
  }
978

979
  return code;
15,226,028✔
980
}
981

982
static int32_t convertStmtNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst,
×
983
                                    void* charsetCxt) {
984
  int32_t       output = 0;
×
985
  const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
×
986

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

995
  if (NULL == dst->length) {
×
996
    dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
×
997
    if (NULL == dst->length) {
×
998
      taosMemoryFreeClear(dst->buffer);
×
999
      return terrno;
×
1000
    }
1001
  }
1002

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

1011
    if (src->length[i] == 0) {
×
1012
      dst->length[i] = 0;
×
1013
      continue;
×
1014
    }
1015

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

1027
    dst->length[i] = output;
×
1028
    src_buf += src->length[i];
×
1029
    dst_buf += output;
×
1030
  }
1031

1032
  dst->buffer_type = src->buffer_type;
×
1033
  dst->is_null = src->is_null;
×
1034
  dst->num = src->num;
×
1035

1036
  return TSDB_CODE_SUCCESS;
×
1037
}
1038

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

1053
  for (int c = 0; c < boundInfo->numOfBound; ++c) {
11,800✔
1054
    SSchema*  pColSchema = &pSchema[boundInfo->pColIndex[c]];
9,440✔
1055
    SColData* pCol = taosArrayGet(pCols, c);
9,440✔
1056
    if (pCol == NULL || pColSchema == NULL) {
9,440✔
1057
      code = buildInvalidOperationMsg(&pBuf, "get column schema or column data failed");
×
1058
      goto _return;
×
1059
    }
1060

1061
    if (boundInfo->pColIndex[c] == 0) {
9,440✔
1062
      pCol->cflag |= COL_IS_KEY;
2,360✔
1063
    }
1064

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

1084
    if ((!(rowNum == 1 && bindData.is_null && *bindData.is_null)) &&
9,440✔
1085
        bindData.buffer_type != pColSchema->type) {  // for rowNum ==1 , connector may not set buffer_type
9,440✔
1086
      code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
1087
      goto _return;
×
1088
    }
1089

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

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

1133
    if (code) {
9,440✔
1134
      parserError("stmt2 failed to add col:%d ,type:%d, ErrCode: 0x%x", c, pColSchema->type, code);
×
1135
      goto _return;
×
1136
    }
1137
    bindIdx++;
9,440✔
1138
  }
1139

1140
  parserDebug("stmt2 all %d columns bind %d rows data as col format", boundInfo->numOfBound, rowNum);
2,360✔
1141

1142
_return:
2,360✔
1143

1144
  taosMemoryFree(ncharBind.buffer);
2,360✔
1145
  taosMemoryFree(ncharBind.length);
2,360✔
1146

1147
  return code;
2,360✔
1148
}
1149

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

1163
  if (bind->num != rowNum) {
×
1164
    return buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
×
1165
  }
1166

1167
  // Column index exceeds the number of columns
1168
  if (colIdx >= pCols->size && pCol == NULL) {
×
1169
    return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
×
1170
  }
1171

1172
  if (bind->buffer_type != pColSchema->type) {
×
1173
    return buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
×
1174
  }
1175

1176
  if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
×
1177
    code = convertStmtNcharCol2(&pBuf, pColSchema, bind, &ncharBind, charsetCxt);
×
1178
    if (code) {
×
1179
      goto _return;
×
1180
    }
1181
    pBind = &ncharBind;
×
1182
  } else {
1183
    pBind = bind;
×
1184
  }
1185

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

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

1223
  if (code) {
×
1224
    parserError("stmt2 failed to add single col:%d ,type:%d, ErrCode: 0x%x", colIdx, pColSchema->type, code);
×
1225
    goto _return;
×
1226
  }
1227

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

1230
_return:
×
1231

1232
  taosMemoryFree(ncharBind.buffer);
×
1233
  taosMemoryFree(ncharBind.length);
×
1234

1235
  return code;
×
1236
}
1237

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

1253
  if (NULL == pTSchema || NULL == *pTSchema) {
×
1254
    *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
×
1255
  }
1256

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

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

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

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

1308
    pBindInfos[c].columnId = pColSchema->colId;
×
1309
    pBindInfos[c].type = pColSchema->type;
×
1310
    pBindInfos[c].bytes = pColSchema->bytes;
×
1311

1312
    if (code) {
×
1313
      goto _return;
×
1314
    }
1315
  }
1316

1317
  pDataBlock->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
×
1318
  if (pDataBlock->pData->pCreateTbReq != NULL) {
×
1319
    pDataBlock->pData->flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
×
1320
  }
1321

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

1330
_return:
×
1331

1332
  taosMemoryFree(ncharBind.buffer);
×
1333
  taosMemoryFree(ncharBind.length);
×
1334

1335
  return code;
×
1336
}
1337

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

1346
    SSchema* schema = &pSchema[boundColumns[0]];
×
1347
    if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
×
1348
      (*fields)[0].precision = timePrec;
×
1349
    }
1350

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

1359
  *fieldNum = numOfBound;
×
1360

1361
  return TSDB_CODE_SUCCESS;
×
1362
}
1363

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

1375
  // Adjust the number of bound fields if there are parsed tags or parsed columns
1376
  if (tags->parseredTags) {
×
1377
    numOfBound -= tags->parseredTags->numOfTags;
×
1378
  }
1379
  if (parsedCols) {
×
1380
    numOfBound -= tSimpleHashGetSize(parsedCols);
×
1381
  }
1382

1383
  int32_t idx = 0;
×
1384
  if (fields != NULL) {
×
1385
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_ALL));
×
1386
    if (NULL == *fields) {
×
1387
      return terrno;
×
1388
    }
1389

1390
    if (hasPreBindTbname) {
×
1391
      (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
×
1392
      tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
×
1393
      (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
×
1394
      (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
×
1395
      idx++;
×
1396
    }
1397

1398
    if (hastag && tags->numOfBound > 0 && !tags->mixTagsCols) {
×
1399
      SSchema* tagSchema = getTableTagSchema(pMeta);
×
1400

1401
      for (int32_t i = 0; i < tags->numOfBound; ++i) {
×
1402
        SSchema* schema = &tagSchema[tags->pColIndex[i]];
×
1403

1404
        if (tags->parseredTags && tags->parseredTags->numOfTags > 0) {
×
1405
          int32_t tag_idx = schema->colId - 1 - pMeta->tableInfo.numOfColumns;
×
1406
          bool    found = false;
×
1407
          for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
×
1408
            if (tags->parseredTags->pTagIndex[k] == tag_idx) {
×
1409
              found = true;
×
1410
              break;
×
1411
            }
1412
          }
1413
          if (found) {
×
1414
            continue;
×
1415
          }
1416
        }
1417

1418
        (*fields)[idx].field_type = TAOS_FIELD_TAG;
×
1419

1420
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[i].name));
×
1421
        (*fields)[idx].type = schema->type;
×
1422
        (*fields)[idx].bytes = schema->bytes;
×
1423
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
×
1424
          (*fields)[idx].precision = pMeta->tableInfo.precision;
×
1425
        }
1426
        idx++;
×
1427
      }
1428
    }
1429

1430
    if (boundColsInfo.numOfBound > 0) {
×
1431
      SSchema* schema = &pSchema[boundColsInfo.pColIndex[0]];
×
1432

1433
      for (int32_t i = 0; i < boundColsInfo.numOfBound; ++i) {
×
1434
        int16_t idxCol = boundColsInfo.pColIndex[i];
×
1435

1436
        if (idxCol == pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags) {
×
1437
          (*fields)[idx].field_type = TAOS_FIELD_TBNAME;
×
1438
          tstrncpy((*fields)[idx].name, "tbname", sizeof((*fields)[idx].name));
×
1439
          (*fields)[idx].type = TSDB_DATA_TYPE_BINARY;
×
1440
          (*fields)[idx].bytes = TSDB_TABLE_FNAME_LEN;
×
1441

1442
          idx++;
×
1443
          continue;
×
1444
        } else if (idxCol < pMeta->tableInfo.numOfColumns) {
×
1445
          if (parsedCols && tSimpleHashGet(parsedCols, &pSchema[idxCol].colId, sizeof(int16_t))) {
×
1446
            continue;
×
1447
          }
1448
          (*fields)[idx].field_type = TAOS_FIELD_COL;
×
1449
        } else {
1450
          if (tags->parseredTags && tags->parseredTags->numOfTags > 0) {
×
1451
            int32_t tag_idx = idxCol - pMeta->tableInfo.numOfColumns;
×
1452
            bool    found = false;
×
1453
            for (int k = 0; k < tags->parseredTags->numOfTags; k++) {
×
1454
              if (tags->parseredTags->pTagIndex[k] == tag_idx) {
×
1455
                found = true;
×
1456
                break;
×
1457
              }
1458
            }
1459
            if (found) {
×
1460
              continue;
×
1461
            }
1462
          }
1463
          (*fields)[idx].field_type = TAOS_FIELD_TAG;
×
1464
        }
1465

1466
        schema = &pSchema[idxCol];
×
1467
        tstrncpy((*fields)[idx].name, schema->name, sizeof((*fields)[idx].name));
×
1468
        (*fields)[idx].type = schema->type;
×
1469
        (*fields)[idx].bytes = schema->bytes;
×
1470
        if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
×
1471
          (*fields)[idx].precision = pMeta->tableInfo.precision;
×
1472
        }
1473
        if (TSDB_DATA_TYPE_DECIMAL64 == schema->type || TSDB_DATA_TYPE_DECIMAL == schema->type) {
×
1474
          decimalFromTypeMod(pMeta->schemaExt[idxCol].typeMod, &(*fields)[idx].precision, &(*fields)[idx].scale);
×
1475
        }
1476
        idx++;
×
1477
      }
1478
    }
1479
  }
1480

1481
  *fieldNum = numOfBound;
×
1482

1483
  return TSDB_CODE_SUCCESS;
×
1484
}
1485

1486
int32_t qBuildStmtTagFields(void* pBlock, void* boundTags, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1487
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
1488
  SBoundColInfo* tags = (SBoundColInfo*)boundTags;
×
1489
  if (NULL == tags) {
×
1490
    return TSDB_CODE_APP_ERROR;
×
1491
  }
1492

1493
  if (pDataBlock->pMeta->tableType != TSDB_SUPER_TABLE && pDataBlock->pMeta->tableType != TSDB_CHILD_TABLE) {
×
1494
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1495
  }
1496

1497
  SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
×
1498
  if (tags->numOfBound <= 0) {
×
1499
    *fieldNum = 0;
×
1500
    *fields = NULL;
×
1501

1502
    return TSDB_CODE_SUCCESS;
×
1503
  }
1504

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

1507
  return TSDB_CODE_SUCCESS;
×
1508
}
1509

1510
int32_t qBuildStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1511
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
1512
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
1513
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
×
1514
    *fieldNum = 0;
×
1515
    if (fields != NULL) {
×
1516
      *fields = NULL;
×
1517
    }
1518

1519
    return TSDB_CODE_SUCCESS;
×
1520
  }
1521

1522
  CHECK_CODE(buildBoundFields(pDataBlock->boundColsInfo.numOfBound, pDataBlock->boundColsInfo.pColIndex, pSchema,
×
1523
                              fieldNum, fields, pDataBlock->pMeta->tableInfo.precision));
1524

1525
  return TSDB_CODE_SUCCESS;
×
1526
}
1527

1528
int32_t qBuildUpdateStmtColFields(void* pBlock, int32_t* fieldNum, TAOS_FIELD_E** fields, SSHashObj* parsedCols) {
×
1529
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
1530
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
1531
  int32_t        numOfBound = pDataBlock->boundColsInfo.numOfBound + tSimpleHashGetSize(parsedCols);
×
1532
  if (numOfBound <= 0) {
×
1533
    *fieldNum = 0;
×
1534
    if (fields != NULL) {
×
1535
      *fields = NULL;
×
1536
    }
1537

1538
    return TSDB_CODE_SUCCESS;
×
1539
  }
1540

1541
  if (fields != NULL) {
×
1542
    *fields = taosMemoryCalloc(numOfBound, sizeof(TAOS_FIELD_E));
×
1543
    if (NULL == *fields) {
×
1544
      return terrno;
×
1545
    }
1546

1547
    int32_t actualIdx = 0;
×
1548
    for (int32_t i = 0; i < numOfBound; ++i) {
×
1549
      SSchema* schema;
1550
      int32_t* idx = (int32_t*)tSimpleHashGet(parsedCols, &i, sizeof(int32_t));
×
1551
      if (idx) {
×
1552
        schema = &pSchema[*idx];
×
1553
      } else {
1554
        schema = &pSchema[pDataBlock->boundColsInfo.pColIndex[actualIdx++]];
×
1555
      }
1556
      tstrncpy((*fields)[i].name, schema->name, 65);
×
1557
      (*fields)[i].type = schema->type;
×
1558
      (*fields)[i].bytes = calcTypeBytesFromSchemaBytes(schema->type, schema->bytes, true);
×
1559
      if (TSDB_DATA_TYPE_TIMESTAMP == schema->type) {
×
1560
        (*fields)[i].precision = pDataBlock->pMeta->tableInfo.precision;
×
1561
      }
1562
    }
1563
  }
1564

1565
  *fieldNum = numOfBound;
×
1566

1567
  return TSDB_CODE_SUCCESS;
×
1568
}
1569

1570
int32_t qBuildStmtStbColFields(void* pBlock, void* boundTags, SSHashObj* parsedCols, uint8_t tbNameFlag,
×
1571
                               int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
1572
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
×
1573
  SSchema*       pSchema = getTableColumnSchema(pDataBlock->pMeta);
×
1574
  if (pDataBlock->boundColsInfo.numOfBound <= 0) {
×
1575
    *fieldNum = 0;
×
1576
    if (fields != NULL) {
×
1577
      *fields = NULL;
×
1578
    }
1579

1580
    return TSDB_CODE_SUCCESS;
×
1581
  }
1582

1583
  CHECK_CODE(buildStbBoundFields(pDataBlock->boundColsInfo, pSchema, fieldNum, fields, pDataBlock->pMeta, boundTags,
×
1584
                                 parsedCols, tbNameFlag));
1585

1586
  return TSDB_CODE_SUCCESS;
×
1587
}
1588

1589
int32_t qResetStmtColumns(SArray* pCols, bool deepClear) {
×
1590
  int32_t colNum = taosArrayGetSize(pCols);
×
1591

1592
  for (int32_t i = 0; i < colNum; ++i) {
×
1593
    SColData* pCol = (SColData*)taosArrayGet(pCols, i);
×
1594
    if (pCol == NULL) {
×
1595
      parserError("qResetStmtColumns column:%d is NULL", i);
×
1596
      return terrno;
×
1597
    }
1598
    if (deepClear) {
×
1599
      tColDataDeepClear(pCol);
×
1600
    } else {
1601
      tColDataClear(pCol);
×
1602
    }
1603
  }
1604

1605
  return TSDB_CODE_SUCCESS;
×
1606
}
1607

1608
int32_t qResetStmtDataBlock(STableDataCxt* block, bool deepClear) {
1,239,784✔
1609
  int32_t        code = 0;
1,239,784✔
1610
  STableDataCxt* pBlock = (STableDataCxt*)block;
1,239,784✔
1611
  int32_t        colNum = taosArrayGetSize(pBlock->pData->aCol);
1,239,784✔
1612

1613
  int8_t flag = 0;
1,239,568✔
1614
  for (int32_t i = 0; i < colNum; ++i) {
8,189,558✔
1615
    flag = pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT;
6,950,516✔
1616
    if (pBlock->pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
6,951,819✔
1617
      SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i);
6,952,898✔
1618
      if (pCol == NULL) {
6,951,758✔
1619
        parserError("qResetStmtDataBlock column:%d is NULL", i);
29✔
1620
        return terrno;
29✔
1621
      }
1622
      if (deepClear) {
6,951,729✔
1623
        tColDataDeepClear(pCol);
2,315,914✔
1624

1625
      } else {
1626
        tColDataClear(pCol);
4,635,815✔
1627
      }
1628

1629
    } else {
1630
      pBlock->pData->aRowP = taosArrayInit(20, POINTER_BYTES);
×
1631
    }
1632
  }
1633

1634
  tBlobSetDestroy(pBlock->pData->pBlobSet);
1,239,042✔
1635
  pBlock->pData->pBlobSet = NULL;
1,239,384✔
1636
  return code;
1,238,912✔
1637
}
1638

1639
int32_t qCloneStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, bool reset) {
426,493✔
1640
  int32_t code = 0;
426,493✔
1641

1642
  *pDst = taosMemoryCalloc(1, sizeof(STableDataCxt));
426,493✔
1643
  if (NULL == *pDst) {
427,042✔
1644
    return terrno;
×
1645
  }
1646

1647
  STableDataCxt* pNewCxt = (STableDataCxt*)*pDst;
427,301✔
1648
  STableDataCxt* pCxt = (STableDataCxt*)pSrc;
427,301✔
1649
  pNewCxt->hasBlob = pSrc->hasBlob;
427,301✔
1650
  pNewCxt->pSchema = NULL;
427,336✔
1651
  pNewCxt->pValues = NULL;
427,336✔
1652

1653
  if (pCxt->pMeta) {
427,077✔
1654
    pNewCxt->pMeta = tableMetaDup(pCxt->pMeta);
427,371✔
1655
    // void* pNewMeta = taosMemoryMalloc(TABLE_META_SIZE(pCxt->pMeta));
1656
    // if (NULL == pNewMeta) {
1657
    //   insDestroyTableDataCxt(*pDst);
1658
    //   return terrno;
1659
    // }
1660
    // memcpy(pNewMeta, pCxt->pMeta, TABLE_META_SIZE(pCxt->pMeta));
1661
    // pNewCxt->pMeta = pNewMeta;
1662
  }
1663

1664
  memcpy(&pNewCxt->boundColsInfo, &pCxt->boundColsInfo, sizeof(pCxt->boundColsInfo));
427,301✔
1665
  pNewCxt->boundColsInfo.pColIndex = NULL;
427,371✔
1666

1667
  if (pCxt->boundColsInfo.pColIndex) {
427,336✔
1668
    void* pNewColIdx = taosMemoryMalloc(pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
427,371✔
1669
    if (NULL == pNewColIdx) {
427,371✔
1670
      insDestroyTableDataCxt(*pDst);
×
1671
      return terrno;
×
1672
    }
1673
    memcpy(pNewColIdx, pCxt->boundColsInfo.pColIndex,
427,042✔
1674
           pCxt->boundColsInfo.numOfBound * sizeof(*pCxt->boundColsInfo.pColIndex));
427,371✔
1675
    pNewCxt->boundColsInfo.pColIndex = pNewColIdx;
426,902✔
1676
  }
1677

1678
  if (pCxt->pData) {
427,336✔
1679
    int8_t         flag = 1;
427,112✔
1680
    SSubmitTbData* pNewTb = (SSubmitTbData*)taosMemoryMalloc(sizeof(SSubmitTbData));
427,112✔
1681
    if (NULL == pNewTb) {
427,301✔
1682
      insDestroyTableDataCxt(*pDst);
×
1683
      return terrno;
×
1684
    }
1685

1686
    memcpy(pNewTb, pCxt->pData, sizeof(*pCxt->pData));
427,301✔
1687
    pNewTb->pCreateTbReq = NULL;
427,301✔
1688
    if (pNewTb->pBlobSet != NULL) {
427,077✔
1689
      flag = pNewTb->pBlobSet->type;
×
1690
    }
1691
    pNewTb->pBlobSet = NULL;
427,371✔
1692

1693
    pNewTb->aCol = taosArrayDup(pCxt->pData->aCol, NULL);
427,077✔
1694
    if (NULL == pNewTb->aCol) {
427,336✔
1695
      insDestroyTableDataCxt(*pDst);
×
1696
      return terrno;
×
1697
    }
1698

1699
    pNewCxt->pData = pNewTb;
426,937✔
1700

1701
    if (reset) {
427,042✔
1702
      code = qResetStmtDataBlock(*pDst, true);
406,114✔
1703
    }
1704
  }
1705

1706
  return code;
426,944✔
1707
}
1708

1709
int32_t qRebuildStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, uint64_t uid, uint64_t suid, int32_t vgId,
21,222✔
1710
                              bool rebuildCreateTb) {
1711
  int32_t code = qCloneStmtDataBlock(pDst, pSrc, false);
21,222✔
1712
  if (code) {
21,222✔
1713
    return code;
×
1714
  }
1715

1716
  STableDataCxt* pBlock = (STableDataCxt*)*pDst;
21,222✔
1717
  if (pBlock->pMeta) {
21,222✔
1718
    pBlock->pMeta->uid = uid;
21,222✔
1719
    pBlock->pMeta->vgId = vgId;
21,222✔
1720
    pBlock->pMeta->suid = suid;
21,222✔
1721
  }
1722

1723
  pBlock->pData->suid = suid;
21,222✔
1724
  pBlock->pData->uid = uid;
21,222✔
1725

1726
  if (rebuildCreateTb && NULL == pBlock->pData->pCreateTbReq) {
21,222✔
1727
    pBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
21,222✔
1728
    if (NULL == pBlock->pData->pCreateTbReq) {
21,222✔
1729
      return terrno;
×
1730
    }
1731
  }
1732

1733
  return TSDB_CODE_SUCCESS;
21,222✔
1734
}
1735

1736
STableMeta* qGetTableMetaInDataBlock(STableDataCxt* pDataBlock) { return ((STableDataCxt*)pDataBlock)->pMeta; }
1,661,015✔
1737

1738
void qDestroyStmtDataBlock(STableDataCxt* pBlock) {
2,110,358✔
1739
  if (pBlock == NULL) {
2,110,358✔
1740
    return;
831,996✔
1741
  }
1742

1743
  STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
1,278,362✔
1744
  insDestroyTableDataCxt(pDataBlock);
1,278,362✔
1745
}
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