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

taosdata / TDengine / #4922

09 Jan 2026 08:13AM UTC coverage: 65.161% (-0.4%) from 65.541%
#4922

push

travis-ci

web-flow
merge: from main to 3.0 branch #34232

33 of 56 new or added lines in 8 files covered. (58.93%)

2171 existing lines in 120 files now uncovered.

197632 of 303297 relevant lines covered (65.16%)

117870313.81 hits per line

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

67.84
/source/libs/executor/src/dataInserter.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 <stdint.h>
17
#include <stdlib.h>
18
#include <string.h>
19
#include "dataSinkInt.h"
20
#include "dataSinkMgt.h"
21
#include "executor.h"
22
#include "executorInt.h"
23
#include "functionMgt.h"
24
#include "libs/new-stream/stream.h"
25
#include "osAtomic.h"
26
#include "osMemPool.h"
27
#include "osMemory.h"
28
#include "osSemaphore.h"
29
#include "planner.h"
30
#include "query.h"
31
#include "querytask.h"
32
#include "storageapi.h"
33
#include "taoserror.h"
34
#include "tarray.h"
35
#include "tcompression.h"
36
#include "tdatablock.h"
37
#include "tdataformat.h"
38
#include "tglobal.h"
39
#include "thash.h"
40
#include "tmsg.h"
41
#include "tqueue.h"
42

43
extern SDataSinkStat gDataSinkStat;
44
SHashObj*            gStreamGrpTableHash = NULL;
45
typedef struct SSubmitRes {
46
  int64_t      affectedRows;
47
  int32_t      code;
48
  SSubmitRsp2* pRsp;
49
} SSubmitRes;
50

51
typedef struct SSubmitTbDataMsg {
52
  int32_t vgId;
53
  int32_t len;
54
  void*   pData;
55
} SSubmitTbDataMsg;
56

57
typedef struct SSubmitTbDataSendInfo {
58
  SSubmitTbDataMsg msg;
59
  SEpSet epSet;
60
} SSubmitTbDataSendInfo;
61

62
typedef struct SSubmitReqSendInfo {
63
  SSubmitReq2* msg;
64
  SEpSet epSet;
65
} SSubmitReqSendInfo;
66

67
static void destroySSubmitTbDataMsg(void* p) {
×
68
  if (p == NULL) return;
×
69
  SSubmitTbDataMsg* pVg = p;
×
70
  taosMemoryFree(pVg->pData);
×
71
  taosMemoryFree(pVg);
×
72
}
73

74
static void destroySSubmitTbDataSendInfo(void* p) {
×
75
  if (p == NULL) return;
×
76
  SSubmitTbDataSendInfo* pSendInfo = p;
×
77
  taosMemoryFree(pSendInfo->msg.pData);
×
78
  taosMemoryFree(pSendInfo);
×
79
}
80

81
typedef struct SDataInserterHandle {
82
  SDataSinkHandle     sink;
83
  SDataSinkManager*   pManager;
84
  STSchema*           pSchema;
85
  SQueryInserterNode* pNode;
86
  SSubmitRes          submitRes;
87
  SInserterParam*     pParam;
88
  SArray*             pDataBlocks;
89
  SHashObj*           pCols;
90
  int32_t             status;
91
  bool                queryEnd;
92
  bool                fullOrderColList;
93
  uint64_t            useconds;
94
  uint64_t            cachedSize;
95
  uint64_t            flags;
96
  TdThreadMutex       mutex;
97
  tsem_t              ready;
98
  bool                explain;
99
  bool                isStbInserter;
100
  SSchemaWrapper*     pTagSchema;
101
  const char*         dbFName;
102
  SHashObj*           dbVgInfoMap;
103
  SUseDbRsp*          pRsp;
104
} SDataInserterHandle;
105

106
typedef struct SSubmitRspParam {
107
  SDataInserterHandle* pInserter;
108
  void*                putParam;
109
} SSubmitRspParam;
110

111
typedef struct SBuildInsertDataInfo {
112
  SSubmitTbData  pTbData;
113
  bool           isFirstBlock;
114
  bool           isLastBlock;
115
  int64_t        lastTs;
116
  bool           needSortMerge;
117
} SBuildInsertDataInfo;
118

119
typedef struct SDropTbCtx {
120
  SSTriggerDropRequest* req;
121
  tsem_t                ready;
122
  int32_t               code;
123
} SDropTbCtx;
124
typedef struct SDropTbDataMsg {
125
  SMsgHead header;
126
  void*    pData;
127
} SDropTbDataMsg;
128

129
typedef struct SRunnerDropTableInfo {
130
  SSTriggerDropRequest* pReq;
131
  int32_t               code;
132
} SRunnerDropTableInfo;
133

134
static int32_t initInsertProcessInfo(SBuildInsertDataInfo* pBuildInsertDataInfo, int32_t rows) {
2,583,991✔
135
  pBuildInsertDataInfo->isLastBlock = false;
2,583,991✔
136
  pBuildInsertDataInfo->lastTs = TSKEY_MIN;
2,584,648✔
137
  pBuildInsertDataInfo->isFirstBlock = true;
2,584,648✔
138
  pBuildInsertDataInfo->needSortMerge = false;
2,584,648✔
139

140
  if (!(pBuildInsertDataInfo->pTbData.aRowP = taosArrayInit(rows, sizeof(SRow*)))) {
2,584,437✔
141
    return terrno;
×
142
  }
143

144
  return TSDB_CODE_SUCCESS;
2,584,648✔
145
}
146

147
static void freeCacheTbInfo(void* pp) {
224,385✔
148
  if (pp == NULL || *(SInsertTableInfo**)pp == NULL) {
224,385✔
149
    return;
×
150
  }
151
  SInsertTableInfo* pTbInfo = *(SInsertTableInfo**)pp;
224,385✔
152
  if (pTbInfo->tbname) {
224,385✔
153
    taosMemFree(pTbInfo->tbname);
224,385✔
154
    pTbInfo->tbname = NULL;
224,385✔
155
  }
156
  if (pTbInfo->pSchema) {
224,385✔
157
    tDestroyTSchema(pTbInfo->pSchema);
224,385✔
158
    pTbInfo->pSchema = NULL;
224,385✔
159
  }
160
  taosMemoryFree(pTbInfo);
224,385✔
161
}
162

163
int32_t initInserterGrpInfo() {
554,278✔
164
  gStreamGrpTableHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
554,278✔
165
  if (NULL == gStreamGrpTableHash) {
554,278✔
166
    qError("failed to create stream group table hash");
×
167
    return terrno;
×
168
  }
169
  taosHashSetFreeFp(gStreamGrpTableHash, freeCacheTbInfo);
554,278✔
170
  return TSDB_CODE_SUCCESS;
554,278✔
171
}
172

173
void destroyInserterGrpInfo() {
554,278✔
174
  static int8_t destoryGrpInfo = 0;
175
  int8_t        flag = atomic_val_compare_exchange_8(&destoryGrpInfo, 0, 1);
554,278✔
176
  if (flag != 0) {
554,278✔
177
    return;
×
178
  }
179
  if (NULL != gStreamGrpTableHash) {
554,278✔
180
    taosHashCleanup(gStreamGrpTableHash);
554,278✔
181
    gStreamGrpTableHash = NULL;
554,278✔
182
  }
183
}
184

185
static int32_t checkResAndResetTableInfo(const SSubmitRes* pSubmitRes, SInsertTableInfo* res,
304,660✔
186
                                         bool* pSchemaChaned) {
187
  int32_t code = TSDB_CODE_SUCCESS;
304,660✔
188
  if (!pSubmitRes->pRsp) {
304,660✔
189
    stError("create table response is NULL");
×
190
    return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
191
  }
192
  if (pSubmitRes->pRsp->aCreateTbRsp->size < 1) {
304,060✔
193
    stError("create table response size is less than 1");
×
194
    return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
195
  }
196
  SVCreateTbRsp* pCreateTbRsp = taosArrayGet(pSubmitRes->pRsp->aCreateTbRsp, 0);
304,060✔
197
  if (pCreateTbRsp->code != 0 && pCreateTbRsp->code != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
304,060✔
198
    stError("create table failed, code:%d", pCreateTbRsp->code);
×
199
    return pCreateTbRsp->code;
×
200
  }
201
  if (!pCreateTbRsp->pMeta || pCreateTbRsp->pMeta->tuid == 0) {
304,060✔
202
    stError("create table can not get tuid");
×
203
    return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
204
  }
205

206
  *pSchemaChaned = false;
304,660✔
207
  res->vgid = pCreateTbRsp->pMeta->vgId;
304,660✔
208
  res->uid = pCreateTbRsp->pMeta->tuid;
304,660✔
209

210
  if (pCreateTbRsp->pMeta->sversion != 0 && res->version != pCreateTbRsp->pMeta->sversion) {
304,660✔
211
    *pSchemaChaned = true;
422✔
212
  }
213

214
  stDebug("inserter callback, uid:%" PRId64 "  vgid: %" PRId64 ", version: %d", res->uid, res->vgid, res->version);
304,060✔
215

216
  return TSDB_CODE_SUCCESS;
304,660✔
217
}
218

219
static int32_t createNewInsertTbInfo(const SSubmitRes* pSubmitRes, SInsertTableInfo* pOldInsertTbInfo,
422✔
220
                                     SInsertTableInfo** ppNewInsertTbInfo) {
221
  SVCreateTbRsp* pCreateTbRsp = taosArrayGet(pSubmitRes->pRsp->aCreateTbRsp, 0);
422✔
222
  if (pCreateTbRsp->code != 0 && pCreateTbRsp->code != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
422✔
223
    stError("create table failed, code:%d", pCreateTbRsp->code);
×
224
    return pCreateTbRsp->code;
×
225
  }
226

227
  SInsertTableInfo* res = taosMemoryCalloc(1, sizeof(SInsertTableInfo));
422✔
228
  if (res == NULL) {
422✔
229
    return terrno;
×
230
  }
231
  res->tbname = taosStrdup(pOldInsertTbInfo->tbname);
422✔
232
  if (res->tbname == NULL) {
422✔
233
    taosMemoryFree(res);
×
234
    stError("failed to allocate memory for table name");
×
235
    return terrno;
×
236
  }
237

238
  res->vgid = pCreateTbRsp->pMeta->vgId;
422✔
239

240
  res->uid = pCreateTbRsp->pMeta->tuid;
422✔
241
  res->vgid = pCreateTbRsp->pMeta->vgId;
422✔
242

243
  res->version = pCreateTbRsp->pMeta->sversion;
422✔
244
  res->pSchema = tBuildTSchema(pCreateTbRsp->pMeta->pSchemas, pCreateTbRsp->pMeta->numOfColumns, res->version);
422✔
245
  if (res->pSchema == NULL) {
422✔
246
    stError("failed to build schema for table:%s, uid:%" PRId64 ", vgid:%" PRId64 ", version:%d", res->tbname, res->uid,
×
247
            res->vgid, res->version);
248
    return terrno;
×
249
  }
250
  *ppNewInsertTbInfo = res;
422✔
251
  return TSDB_CODE_SUCCESS;
422✔
252
}
253

254
static int32_t updateInsertGrpTableInfo(SStreamDataInserterInfo* pInserterInfo, const SSubmitRes* pSubmitRes) {
304,449✔
255
  int32_t            code = TSDB_CODE_SUCCESS;
304,449✔
256
  int32_t            lino = 0;
304,449✔
257
  int64_t            key[2] = {pInserterInfo->streamId, pInserterInfo->groupId};
304,449✔
258
  SInsertTableInfo** ppTbRes = taosHashAcquire(gStreamGrpTableHash, key, sizeof(key));
304,660✔
259
  if (NULL == ppTbRes || *ppTbRes == NULL) {
304,660✔
260
    return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
261
  }
262

263
  bool schemaChanged = false;
304,660✔
264
  code = checkResAndResetTableInfo(pSubmitRes, *ppTbRes, &schemaChanged);
304,660✔
265
  QUERY_CHECK_CODE(code, lino, _exit);
304,660✔
266

267
  if (schemaChanged) {
304,660✔
268
    SInsertTableInfo* pNewInfo = NULL;
422✔
269
    code = createNewInsertTbInfo(pSubmitRes, *ppTbRes, &pNewInfo);
422✔
270
    QUERY_CHECK_CODE(code, lino, _exit);
422✔
271

272
    TAOS_UNUSED(taosHashRemove(gStreamGrpTableHash, key, sizeof(key)));
422✔
273

274
    code = taosHashPut(gStreamGrpTableHash, key, sizeof(key), &pNewInfo, sizeof(SInsertTableInfo*));
422✔
275

276
    if (code == TSDB_CODE_DUP_KEY) {
422✔
277
      freeCacheTbInfo(&pNewInfo);
×
278
      code = TSDB_CODE_SUCCESS;
×
279
      goto _exit;
×
280
    } else if (code != TSDB_CODE_SUCCESS) {
422✔
281
      freeCacheTbInfo(&pNewInfo);
×
282
      stError("failed to put new insert tbInfo for streamId:%" PRIx64 ", groupId:%" PRIx64 ", code:%d",
×
283
              pInserterInfo->streamId, pInserterInfo->groupId, code);
284
      QUERY_CHECK_CODE(code, lino, _exit);
×
285
    }
286

287
    stInfo("update table info for streamId:%" PRIx64 ", groupId:%" PRIx64 ", uid:%" PRId64 ", vgid:%" PRId64
422✔
288
           ", version:%d",
289
           pInserterInfo->streamId, pInserterInfo->groupId, pNewInfo->uid, pNewInfo->vgid, pNewInfo->version);
290
  }
291
  return TSDB_CODE_SUCCESS;
304,660✔
292

293
_exit:
×
294
  if (code != TSDB_CODE_SUCCESS) {
×
295
    stError("failed to check and reset table info for streamId:%" PRIx64 ", groupId:%" PRIx64 ", code:%d",
×
296
            pInserterInfo->streamId, pInserterInfo->groupId, code);
297
  }
298
  taosHashRelease(gStreamGrpTableHash, ppTbRes);
×
299
  return code;
×
300
}
301

302
static int32_t buildTSchmaFromInserter(SStreamInserterParam* pInsertParam, STSchema** ppTSchema);
303
static int32_t initTableInfo(SDataInserterHandle* pInserter, SStreamDataInserterInfo* pInserterInfo) {
307,048✔
304
  int32_t           code = TSDB_CODE_SUCCESS;
307,048✔
305
  int32_t           lino = 0;
307,048✔
306
  int64_t           key[2] = {pInserterInfo->streamId, pInserterInfo->groupId};
307,048✔
307
  
308
  // Check if key already exists to avoid unnecessary allocation
309
  SInsertTableInfo** ppExisting = taosHashGet(gStreamGrpTableHash, key, sizeof(key));
307,048✔
310
  if (ppExisting != NULL && *ppExisting != NULL) {
307,048✔
311
    return TSDB_CODE_SUCCESS;
83,085✔
312
  }
313

314
  SInsertTableInfo* res = taosMemoryCalloc(1, sizeof(SInsertTableInfo));
223,963✔
315
  if (res == NULL) {
223,963✔
316
    return terrno;
×
317
  }
318

319
  SStreamInserterParam* pInsertParam = pInserter->pParam->streamInserterParam;
223,963✔
320
  res->uid = 0;
223,963✔
321
  if (pInsertParam->tbType == TSDB_NORMAL_TABLE) {
223,963✔
322
    res->version = 1;
72,309✔
323
  } else {
324
    res->version = pInsertParam->sver;
151,654✔
325
  }
326

327
  res->tbname = taosStrdup(pInserterInfo->tbName);
223,963✔
328
  if (res->tbname == NULL) {
223,963✔
329
    taosMemoryFree(res);
×
330
    stError("failed to allocate memory for table name");
×
331
    return terrno;
×
332
  }
333

334
  code = buildTSchmaFromInserter(pInserter->pParam->streamInserterParam, &res->pSchema);
223,963✔
335
  QUERY_CHECK_CODE(code, lino, _return);
223,722✔
336

337
  code = taosHashPut(gStreamGrpTableHash, key, sizeof(key), &res, sizeof(SInsertTableInfo*));
223,722✔
338
  if (code == TSDB_CODE_DUP_KEY) {
223,963✔
339
    freeCacheTbInfo(&res);
×
340
    return TSDB_CODE_SUCCESS;
×
341
  }
342

343
_return:
223,963✔
344
  if (code != TSDB_CODE_SUCCESS) {
223,963✔
345
    stError("failed to build table info for streamId:%" PRIx64 ", groupId:%" PRIx64 ", code:%d",
×
346
            pInserterInfo->streamId, pInserterInfo->groupId, code);
347
    freeCacheTbInfo(&res);
×
348
  }
349
  return code;
223,963✔
350
}
351

352
static bool colsIsSupported(const STableMetaRsp* pTableMetaRsp, const SStreamInserterParam* pInserterParam) {
21,345✔
353
  SArray* pCreatingFields = pInserterParam->pFields;
21,345✔
354

355
  for (int32_t i = 0; i < pCreatingFields->size; ++i) {
122,725✔
356
    SFieldWithOptions* pField = taosArrayGet(pCreatingFields, i);
100,964✔
357
    if (NULL == pField) {
100,964✔
358
      stError("isSupportedSTableSchema: failed to get field from array");
×
359
      return false;
×
360
    }
361

362
    for (int j = 0; j < pTableMetaRsp->numOfColumns; ++j) {
305,608✔
363
      if (strncmp(pTableMetaRsp->pSchemas[j].name, pField->name, TSDB_COL_NAME_LEN) == 0) {
303,296✔
364
        if (pTableMetaRsp->pSchemas[j].type == pField->type && pTableMetaRsp->pSchemas[j].bytes == pField->bytes) {
99,276✔
365
          break;
366
        } else {
367
          return false;
×
368
        }
369
      }
370
    }
371
  }
372
  return true;
21,553✔
373
}
374

375
static bool TagsIsSupported(const STableMetaRsp* pTableMetaRsp, const SStreamInserterParam* pInserterParam) {
844✔
376
  SArray* pCreatingTags = pInserterParam->pTagFields;
844✔
377

378
  int32_t            tagIndexOffset = -1;
844✔
379
  SFieldWithOptions* pField = taosArrayGet(pCreatingTags, 0);
844✔
380
  if (NULL == pField) {
844✔
381
    stError("isSupportedSTableSchema: failed to get field from array");
×
382
    return false;
×
383
  }
384
  for (int32_t i = 0; i < pTableMetaRsp->numOfColumns + pTableMetaRsp->numOfTags; ++i) {
844✔
385
    if (strncmp(pTableMetaRsp->pSchemas[i].name, pField->name, TSDB_COL_NAME_LEN) != 0) {
422✔
386
      tagIndexOffset = i;
422✔
387
      break;
422✔
388
    }
389
  }
390
  if (tagIndexOffset == -1) {
844✔
391
    stError("isSupportedSTableSchema: failed to get tag index");
422✔
392
    return false;
422✔
393
  }
394

395
  for (int32_t i = 0; i < pTableMetaRsp->numOfTags; ++i) {
844✔
396
    int32_t            index = i + tagIndexOffset;
422✔
397
    SFieldWithOptions* pField = taosArrayGet(pCreatingTags, i);
422✔
398
    if (NULL == pField) {
422✔
399
      stError("isSupportedSTableSchema: failed to get field from array");
×
400
      return false;
×
401
    }
402

403
    for(int32_t j = 0; j < pTableMetaRsp->numOfTags; ++j) {
844✔
404
      if (strncmp(pTableMetaRsp->pSchemas[index].name, pField->name, TSDB_COL_NAME_LEN) == 0) {
422✔
405
        if (pTableMetaRsp->pSchemas[index].type == pField->type &&
×
406
            pTableMetaRsp->pSchemas[index].bytes == pField->bytes) {
×
407
          break;
408
        } else {
409
          return false;
×
410
        }
411
      }
412
    }
413
  }
414
  return true;
422✔
415
}
416

417
static bool isSupportedSTableSchema(const STableMetaRsp* pTableMetaRsp, const SStreamInserterParam* pInserterParam) {
844✔
418
  if (!colsIsSupported(pTableMetaRsp, pInserterParam)) {
844✔
419
    return false;
×
420
  }
421
  if (!TagsIsSupported(pTableMetaRsp, pInserterParam)) {
844✔
422
    return false;
422✔
423
  }
424
  return true;
422✔
425
}
426

427
static bool isSupportedNTableSchema(const STableMetaRsp* pTableMetaRsp, const SStreamInserterParam* pInserterParam) {
20,709✔
428
  return colsIsSupported(pTableMetaRsp, pInserterParam);
20,709✔
429
}
430

431
static int32_t checkAndSaveCreateGrpTableInfo(SDataInserterHandle*     pInserthandle,
21,553✔
432
                                              SStreamDataInserterInfo* pInserterInfo) {
433
  int32_t     code = TSDB_CODE_SUCCESS;
21,553✔
434
  SSubmitRes* pSubmitRes = &pInserthandle->submitRes;
21,553✔
435
  int8_t      tbType = pInserthandle->pParam->streamInserterParam->tbType;
21,553✔
436

437
  SVCreateTbRsp*        pCreateTbRsp = taosArrayGet(pSubmitRes->pRsp->aCreateTbRsp, 0);
21,553✔
438
  SSchema*              pExistRow = pCreateTbRsp->pMeta->pSchemas;
21,553✔
439
  SStreamInserterParam* pInserterParam = pInserthandle->pParam->streamInserterParam;
21,553✔
440

441
  if (tbType == TSDB_CHILD_TABLE || tbType == TSDB_SUPER_TABLE) {
21,553✔
442
    if (!isSupportedSTableSchema(pCreateTbRsp->pMeta, pInserterParam)) {
844✔
443
      stError("create table failed, schema is not supported");
422✔
444
      return TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
422✔
445
    }
446
  } else if (tbType == TSDB_NORMAL_TABLE) {
20,709✔
447
    if (!isSupportedNTableSchema(pCreateTbRsp->pMeta, pInserterParam)) {
20,709✔
448
      stError("create table failed, schema is not supported");
×
449
      return TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
450
    }
451
  } else {
452
    stError("checkAndSaveCreateGrpTableInfo failed, tbType:%d is not supported", tbType);
×
453
    return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
454
  }
455

456
  return updateInsertGrpTableInfo(pInserterInfo, pSubmitRes);
21,131✔
457
}
458

459
int32_t inserterCallback(void* param, SDataBuf* pMsg, int32_t code) {
2,645,517✔
460
  SSubmitRspParam*     pParam = (SSubmitRspParam*)param;
2,645,517✔
461
  SDataInserterHandle* pInserter = pParam->pInserter;
2,645,517✔
462
  int32_t              code2 = 0;
2,645,517✔
463

464
  if (code) {
2,645,517✔
465
    pInserter->submitRes.code = code;
24,847✔
466
  } else {
467
    pInserter->submitRes.code = TSDB_CODE_SUCCESS;
2,620,670✔
468
  }
469
  SDecoder coder = {0};
2,645,517✔
470

471
  if (code == TSDB_CODE_SUCCESS) {
2,645,517✔
472
    pInserter->submitRes.pRsp = taosMemoryCalloc(1, sizeof(SSubmitRsp2));
2,620,670✔
473
    if (NULL == pInserter->submitRes.pRsp) {
2,620,670✔
474
      pInserter->submitRes.code = terrno;
×
475
      goto _return;
×
476
    }
477

478
    tDecoderInit(&coder, pMsg->pData, pMsg->len);
2,620,670✔
479
    code = tDecodeSSubmitRsp2(&coder, pInserter->submitRes.pRsp);
2,620,670✔
480
    if (code) {
2,620,670✔
481
      tDestroySSubmitRsp2(pInserter->submitRes.pRsp, TSDB_MSG_FLG_DECODE);
×
482
      taosMemoryFree(pInserter->submitRes.pRsp);
×
483
      pInserter->submitRes.code = code;
×
484
      goto _return;
×
485
    }
486

487
    if (pInserter->submitRes.pRsp->affectedRows > 0) {
2,620,670✔
488
      SArray* pCreateTbList = pInserter->submitRes.pRsp->aCreateTbRsp;
665,456✔
489
      int32_t numOfTables = taosArrayGetSize(pCreateTbList);
665,667✔
490

491
      for (int32_t i = 0; i < numOfTables; ++i) {
950,271✔
492
        SVCreateTbRsp* pRsp = taosArrayGet(pCreateTbList, i);
284,993✔
493
        if (NULL == pRsp) {
285,204✔
494
          pInserter->submitRes.code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
495
          goto _return;
×
496
        }
497
        if (TSDB_CODE_SUCCESS != pRsp->code) {
285,204✔
498
          code = pRsp->code;
389✔
499
          tDestroySSubmitRsp2(pInserter->submitRes.pRsp, TSDB_MSG_FLG_DECODE);
×
500
          taosMemoryFree(pInserter->submitRes.pRsp);
×
501
          pInserter->submitRes.code = code;
×
502
          goto _return;
×
503
        }
504
      }
505
    }
506

507
    if (pParam->putParam != NULL && ((SStreamDataInserterInfo*)pParam->putParam)->isAutoCreateTable) {
2,620,281✔
508
      code2 = updateInsertGrpTableInfo((SStreamDataInserterInfo*)pParam->putParam, &pInserter->submitRes);
283,529✔
509
    }
510

511
    pInserter->submitRes.affectedRows += pInserter->submitRes.pRsp->affectedRows;
2,619,892✔
512
    qDebug("submit rsp received, affectedRows:%d, total:%" PRId64, pInserter->submitRes.pRsp->affectedRows,
2,620,670✔
513
           pInserter->submitRes.affectedRows);
514
    tDestroySSubmitRsp2(pInserter->submitRes.pRsp, TSDB_MSG_FLG_DECODE);
2,620,670✔
515
    taosMemoryFree(pInserter->submitRes.pRsp);
2,620,459✔
516
  } else if ((TSDB_CODE_TDB_TABLE_ALREADY_EXIST == code && pParam->putParam != NULL &&
24,847✔
517
              ((SStreamDataInserterInfo*)pParam->putParam)->isAutoCreateTable) ||
24,847✔
518
             TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER == code) {
519
    pInserter->submitRes.code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
21,553✔
520
    pInserter->submitRes.pRsp = taosMemoryCalloc(1, sizeof(SSubmitRsp2));
21,553✔
521
    if (NULL == pInserter->submitRes.pRsp) {
21,553✔
522
      code2 = terrno;
×
523
      goto _return;
×
524
    }
525

526
    tDecoderInit(&coder, pMsg->pData, pMsg->len);
21,553✔
527
    code2 = tDecodeSSubmitRsp2(&coder, pInserter->submitRes.pRsp);
21,553✔
528
    if (code2 == TSDB_CODE_SUCCESS) {
21,553✔
529
      code2 = checkAndSaveCreateGrpTableInfo(pInserter, (SStreamDataInserterInfo*)pParam->putParam);
21,553✔
530
    }
531
    tDestroySSubmitRsp2(pInserter->submitRes.pRsp, TSDB_MSG_FLG_DECODE);
21,553✔
532
    taosMemoryFree(pInserter->submitRes.pRsp);
21,553✔
533
  }
534

535
_return:
2,643,665✔
536

537
  if (code2) {
2,645,306✔
538
    qError("update inserter table info failed, error:%s", tstrerror(code2));
422✔
539
  }
540
  tDecoderClear(&coder);
2,645,306✔
541
  TAOS_UNUSED(tsem_post(&pInserter->ready));
2,645,517✔
542

543
  taosMemoryFree(pMsg->pData);
2,645,517✔
544

545
  return TSDB_CODE_SUCCESS;
2,645,517✔
546
}
547

548
void freeUseDbOutput_tmp(void* ppOutput) {
10,495✔
549
  SUseDbOutput* pOut = *(SUseDbOutput**)ppOutput;
10,495✔
550
  if (NULL == ppOutput) {
10,495✔
551
    return;
×
552
  }
553

554
  if (pOut->dbVgroup) {
10,495✔
555
    freeVgInfo(pOut->dbVgroup);
10,495✔
556
  }
557
  taosMemFree(pOut);
10,495✔
558
  *(SUseDbOutput**)ppOutput = NULL;
10,495✔
559
}
560

561
static int32_t processUseDbRspForInserter(void* param, SDataBuf* pMsg, int32_t code) {
39,839✔
562
  int32_t       lino = 0;
39,839✔
563
  SDBVgInfoReq* pVgInfoReq = (SDBVgInfoReq*)param;
39,839✔
564

565
  if (TSDB_CODE_SUCCESS != code) {
39,839✔
566
    // pInserter->pTaskInfo->code = rpcCvtErrCode(code);
567
    // if (pInserter->pTaskInfo->code != code) {
568
    //   qError("load db info rsp received, error:%s, cvted error:%s", tstrerror(code),
569
    //          tstrerror(pInserter->pTaskInfo->code));
570
    // } else {
571
    //   qError("load db info rsp received, error:%s", tstrerror(code));
572
    // }
573
    goto _return;
×
574
  }
575

576
  pVgInfoReq->pRsp = taosMemoryMalloc(sizeof(SUseDbRsp));
39,839✔
577
  QUERY_CHECK_NULL(pVgInfoReq->pRsp, code, lino, _return, terrno);
39,839✔
578

579
  code = tDeserializeSUseDbRsp(pMsg->pData, (int32_t)pMsg->len, pVgInfoReq->pRsp);
39,839✔
580
  QUERY_CHECK_CODE(code, lino, _return);
39,839✔
581

582
_return:
39,839✔
583
  taosMemoryFreeClear(pMsg->pData);
39,839✔
584
  taosMemoryFreeClear(pMsg->pEpSet);
39,839✔
585
  if (code != 0){
39,839✔
586
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
587
  }
588
  int ret = tsem_post(&pVgInfoReq->ready);
39,839✔
589
  if (ret != 0) {
39,839✔
590
    qError("%s failed code: %d", __func__, ret);
×
591
  }
592
  return code;
39,839✔
593
}
594

595

596
int inserterVgInfoComp(const void* lp, const void* rp) {
57,035✔
597
  SVgroupInfo* pLeft = (SVgroupInfo*)lp;
57,035✔
598
  SVgroupInfo* pRight = (SVgroupInfo*)rp;
57,035✔
599
  if (pLeft->hashBegin < pRight->hashBegin) {
57,035✔
600
    return -1;
48,372✔
601
  } else if (pLeft->hashBegin > pRight->hashBegin) {
8,663✔
602
    return 1;
8,663✔
603
  }
604

605
  return 0;
×
606
}
607

608
static int32_t buildDbVgInfoMap(void* clientRpc, const char* dbFName, SUseDbOutput* output) {
39,598✔
609
  int32_t      code = TSDB_CODE_SUCCESS;
39,598✔
610
  int32_t      lino = 0;
39,598✔
611
  char*        buf1 = NULL;
39,598✔
612
  SUseDbReq*   pReq = NULL;
39,598✔
613
  SDBVgInfoReq dbVgInfoReq = {0};
39,598✔
614
  code = tsem_init(&dbVgInfoReq.ready, 0, 0);
39,598✔
615
  if (code != TSDB_CODE_SUCCESS) {
39,839✔
616
    qError("tsem_init failed, error:%s", tstrerror(code));
×
617
    return code;
×
618
  }
619

620
  pReq = taosMemoryMalloc(sizeof(SUseDbReq));
39,839✔
621
  QUERY_CHECK_NULL(pReq, code, lino, _return, terrno);
39,839✔
622

623
  tstrncpy(pReq->db, dbFName, TSDB_DB_FNAME_LEN);
39,839✔
624
  QUERY_CHECK_CODE(code, lino, _return);
39,839✔
625

626
  int32_t contLen = tSerializeSUseDbReq(NULL, 0, pReq);
39,839✔
627
  buf1 = taosMemoryCalloc(1, contLen);
39,390✔
628
  QUERY_CHECK_NULL(buf1, code, lino, _return, terrno);
39,631✔
629

630
  int32_t tempRes = tSerializeSUseDbReq(buf1, contLen, pReq);
39,631✔
631
  if (tempRes < 0) {
39,839✔
632
    QUERY_CHECK_CODE(terrno, lino, _return);
×
633
  }
634

635
  SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
39,839✔
636
  QUERY_CHECK_NULL(pMsgSendInfo, code, lino, _return, terrno);
39,839✔
637

638
  SEpSet pEpSet = {0};
39,839✔
639
  QUERY_CHECK_CODE(getCurrentMnodeEpset(&pEpSet), lino, _return);
39,839✔
640

641
  pMsgSendInfo->param = &dbVgInfoReq;
39,839✔
642
  pMsgSendInfo->msgInfo.pData = buf1;
39,839✔
643
  buf1 = NULL;
39,839✔
644
  pMsgSendInfo->msgInfo.len = contLen;
39,839✔
645
  pMsgSendInfo->msgType = TDMT_MND_GET_DB_INFO;
39,839✔
646
  pMsgSendInfo->fp = processUseDbRspForInserter;
39,839✔
647
  // pMsgSendInfo->requestId = pTaskInfo->id.queryId;
648

649
  code = asyncSendMsgToServer(clientRpc, &pEpSet, NULL, pMsgSendInfo);
39,631✔
650
  QUERY_CHECK_CODE(code, lino, _return);
39,839✔
651

652
  code = tsem_wait(&dbVgInfoReq.ready);
39,839✔
653
  QUERY_CHECK_CODE(code, lino, _return);
39,839✔
654

655
  code = queryBuildUseDbOutput(output, dbVgInfoReq.pRsp);
39,839✔
656
  QUERY_CHECK_CODE(code, lino, _return);
39,839✔
657

658
  output->dbVgroup->vgArray = taosArrayInit(dbVgInfoReq.pRsp->vgNum, sizeof(SVgroupInfo));
39,839✔
659
  if (NULL == output->dbVgroup->vgArray) {
39,839✔
660
    code = terrno;
×
661
    QUERY_CHECK_CODE(code, lino, _return);
×
662
  }
663

664
  void* pIter = taosHashIterate(output->dbVgroup->vgHash, NULL);
39,839✔
665
  while (pIter) {
104,718✔
666
    if (NULL == taosArrayPush(output->dbVgroup->vgArray, pIter)) {
129,758✔
667
      taosHashCancelIterate(output->dbVgroup->vgHash, pIter);
×
668
      code = terrno;
×
669
      QUERY_CHECK_CODE(code, lino, _return);
×
670
    }
671

672
    pIter = taosHashIterate(output->dbVgroup->vgHash, pIter);
64,879✔
673
  }
674

675
  taosArraySort(output->dbVgroup->vgArray, inserterVgInfoComp);
39,839✔
676

677
_return:
39,839✔
678

679
  if (code) {
39,839✔
680
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
681
    taosMemoryFree(buf1);
×
682
  }
683
  taosMemoryFree(pReq);
39,839✔
684
  TAOS_UNUSED(tsem_destroy(&dbVgInfoReq.ready));
39,631✔
685
  if (dbVgInfoReq.pRsp) {
39,839✔
686
    tFreeSUsedbRsp(dbVgInfoReq.pRsp);
39,839✔
687
    taosMemoryFreeClear(dbVgInfoReq.pRsp);
39,839✔
688
  }
689
  return code;
39,631✔
690
}
691

692
int32_t inserterBuildCreateTbReq(SVCreateTbReq* pTbReq, const char* tname, STag* pTag, int64_t suid, const char* sname,
220,096✔
693
                                 SArray* tagName, uint8_t tagNum, int32_t ttl) {
694
  pTbReq->type = TD_CHILD_TABLE;
220,096✔
695
  pTbReq->ctb.pTag = (uint8_t*)pTag;
220,753✔
696
  pTbReq->name = taosStrdup(tname);
220,753✔
697
  if (!pTbReq->name) return terrno;
220,542✔
698
  pTbReq->ctb.suid = suid;
220,542✔
699
  pTbReq->ctb.tagNum = tagNum;
220,542✔
700
  if (sname) {
220,153✔
701
    pTbReq->ctb.stbName = taosStrdup(sname);
220,364✔
702
    if (!pTbReq->ctb.stbName) {
220,753✔
703
      taosMemoryFree(pTbReq->name);
×
704
      return terrno;
×
705
    }
706
  }
707
  pTbReq->ctb.tagName = tagName;
220,542✔
708
  pTbReq->ttl = ttl;
220,364✔
709
  pTbReq->commentLen = -1;
220,364✔
710

711
  return TSDB_CODE_SUCCESS;
220,753✔
712
}
713

714
int32_t inserterHashValueComp(void const* lp, void const* rp) {
3,676,174✔
715
  uint32_t*    key = (uint32_t*)lp;
3,676,174✔
716
  SVgroupInfo* pVg = (SVgroupInfo*)rp;
3,676,174✔
717

718
  if (*key < pVg->hashBegin) {
3,676,174✔
719
    return -1;
864,687✔
720
  } else if (*key > pVg->hashEnd) {
2,811,307✔
721
    return 1;
218,781✔
722
  }
723

724
  return 0;
2,593,370✔
725
}
726

727

728
int32_t inserterGetVgInfo(SDBVgInfo* dbInfo, char* tbName, SVgroupInfo* pVgInfo) {
2,593,159✔
729
  if (NULL == dbInfo) {
2,593,159✔
730
    return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
731
  }
732

733
  if (NULL == dbInfo->vgArray) {
2,593,159✔
734
    qError("empty db vgArray, hashSize:%d", taosHashGetSize(dbInfo->vgHash));
×
735
    return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
736
  }
737

738
  uint32_t hashValue =
2,592,526✔
739
      taosGetTbHashVal(tbName, (int32_t)strlen(tbName), dbInfo->hashMethod, dbInfo->hashPrefix, dbInfo->hashSuffix);
2,592,948✔
740
  SVgroupInfo* vgInfo = taosArraySearch(dbInfo->vgArray, &hashValue, inserterHashValueComp, TD_EQ);
2,593,159✔
741
  if (NULL == vgInfo) {
2,593,370✔
742
    qError("no hash range found for hash value [%u], table:%s, numOfVgId:%d", hashValue, tbName,
×
743
           (int32_t)taosArrayGetSize(dbInfo->vgArray));
744
    return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
745
  }
746
  
747
  *pVgInfo = *vgInfo;
2,593,370✔
748
  qDebug("insert get vgInfo, tbName:%s vgId:%d epset(%s:%d)", tbName, pVgInfo->vgId, pVgInfo->epSet.eps[0].fqdn,
2,593,128✔
749
        pVgInfo->epSet.eps[0].port);
750
        
751
  return TSDB_CODE_SUCCESS;
2,592,526✔
752
}
753

754
int32_t inserterGetVgId(SDBVgInfo* dbInfo, char* tbName, int32_t* vgId) {
×
755
  SVgroupInfo vgInfo = {0};
×
756
  int32_t     code = inserterGetVgInfo(dbInfo, tbName, &vgInfo);
×
757
  if (code != TSDB_CODE_SUCCESS) {
×
758
    qError("inserterGetVgId failed, code:%d", code);
×
759
    return code;
×
760
  }
761
  *vgId = vgInfo.vgId;
×
762

763
  return TSDB_CODE_SUCCESS;
×
764
}
765

766
int32_t inserterGetDbVgInfo(SDataInserterHandle* pInserter, const char* dbFName, SDBVgInfo** dbVgInfo) {
3,360✔
767
  int32_t       code = TSDB_CODE_SUCCESS;
3,360✔
768
  int32_t       line = 0;
3,360✔
769
  SUseDbOutput* output = NULL;
3,360✔
770

771
  // QRY_PARAM_CHECK(dbVgInfo);
772
  // QRY_PARAM_CHECK(pInserter);
773
  // QRY_PARAM_CHECK(name);
774

775
  if (pInserter->dbVgInfoMap == NULL) {
3,360✔
776
    pInserter->dbVgInfoMap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
3,360✔
777
    if (pInserter->dbVgInfoMap == NULL) {
3,360✔
778
      return TSDB_CODE_OUT_OF_MEMORY;
×
779
    }
780
  }
781

782
  SUseDbOutput** find = (SUseDbOutput**)taosHashGet(pInserter->dbVgInfoMap, dbFName, strlen(dbFName));
3,360✔
783

784
  if (find == NULL) {
3,360✔
785
    output = taosMemoryMalloc(sizeof(SUseDbOutput));
3,360✔
786
    if (output == NULL) {
3,360✔
787
      return TSDB_CODE_OUT_OF_MEMORY;
×
788
    }
789

790
    code = buildDbVgInfoMap(pInserter->pParam->readHandle->pMsgCb->clientRpc, dbFName, output);
3,360✔
791
    QUERY_CHECK_CODE(code, line, _return);
3,360✔
792

793
    code = taosHashPut(pInserter->dbVgInfoMap, dbFName, strlen(dbFName), &output, POINTER_BYTES);
3,360✔
794
    QUERY_CHECK_CODE(code, line, _return);
3,360✔
795
  } else {
796
    output = *find;
×
797
  }
798

799
  *dbVgInfo = output->dbVgroup;
3,360✔
800
  return code;
3,360✔
801

802
_return:
×
803
  qError("%s failed at line %d since %s", __func__, line, tstrerror(code));
×
804
  freeUseDbOutput_tmp(&output);
×
805
  return code;
×
806
}
807

808
int32_t getTableVgInfo(SDataInserterHandle* pInserter, const char* dbFName,
2,585,541✔
809
                       const char* tbName, SVgroupInfo* pVgInfo) {
810
  return getDbVgInfoForExec(pInserter->pParam->readHandle->pMsgCb->clientRpc, dbFName,
2,585,541✔
811
                              tbName, pVgInfo);
812
}
813

814
static int32_t sendSubmitRequest(SDataInserterHandle* pInserter, void* putParam, void* pMsg, int32_t msgLen,
2,645,309✔
815
                                 void* pTransporter, SEpSet* pEpset) {
816
  // send the fetch remote task result reques
817
  SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
2,645,309✔
818
  if (NULL == pMsgSendInfo) {
2,645,098✔
819
    taosMemoryFreeClear(pMsg);
×
820
    return terrno;
×
821
  }
822

823
  SSubmitRspParam* pParam = taosMemoryCalloc(1, sizeof(SSubmitRspParam));
2,645,098✔
824
  if (NULL == pParam) {
2,645,098✔
825
    taosMemoryFreeClear(pMsg);
×
826
    taosMemoryFreeClear(pMsgSendInfo);
×
827
    return terrno;
×
828
  }
829
  pParam->pInserter = pInserter;
2,645,098✔
830
  pParam->putParam = putParam;
2,645,098✔
831

832
  pMsgSendInfo->param = pParam;
2,644,887✔
833
  pMsgSendInfo->paramFreeFp = taosAutoMemoryFree;
2,645,098✔
834
  pMsgSendInfo->msgInfo.pData = pMsg;
2,644,887✔
835
  pMsgSendInfo->msgInfo.len = msgLen;
2,645,095✔
836
  pMsgSendInfo->msgType = TDMT_VND_SUBMIT;
2,644,676✔
837
  pMsgSendInfo->fp = inserterCallback;
2,645,306✔
838

839
  return asyncSendMsgToServer(pTransporter, pEpset, NULL, pMsgSendInfo);
2,644,673✔
840
}
841

842
static int32_t submitReqToMsg(int32_t vgId, SSubmitReq2* pReq, void** pData, int32_t* pLen) {
2,645,517✔
843
  int32_t code = TSDB_CODE_SUCCESS;
2,645,517✔
844
  int32_t len = 0;
2,645,517✔
845
  void*   pBuf = NULL;
2,645,517✔
846
  tEncodeSize(tEncodeSubmitReq, pReq, len, code);
2,645,517✔
847
  if (TSDB_CODE_SUCCESS == code) {
2,645,084✔
848
    SEncoder encoder;
2,643,454✔
849
    len += sizeof(SSubmitReq2Msg);
2,645,517✔
850
    pBuf = taosMemoryMalloc(len);
2,645,517✔
851
    if (NULL == pBuf) {
2,643,592✔
852
      return terrno;
×
853
    }
854
    ((SSubmitReq2Msg*)pBuf)->header.vgId = htonl(vgId);
2,643,592✔
855
    ((SSubmitReq2Msg*)pBuf)->header.contLen = htonl(len);
2,643,997✔
856
    ((SSubmitReq2Msg*)pBuf)->version = htobe64(1);
2,644,205✔
857
    tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SSubmitReq2Msg)), len - sizeof(SSubmitReq2Msg));
2,644,873✔
858
    code = tEncodeSubmitReq(&encoder, pReq);
2,644,854✔
859
    tEncoderClear(&encoder);
2,645,282✔
860
  }
861

862
  if (TSDB_CODE_SUCCESS == code) {
2,644,829✔
863
    *pData = pBuf;
2,644,829✔
864
    *pLen = len;
2,644,829✔
865
  } else {
866
    taosMemoryFree(pBuf);
×
867
  }
868

869
  return code;
2,644,627✔
870
}
871

872
int32_t buildSubmitReqFromStbBlock(SDataInserterHandle* pInserter, SHashObj* pHash, const SSDataBlock* pDataBlock,
3,360✔
873
                                   const STSchema* pTSchema, int64_t uid, int32_t vgId, tb_uid_t suid) {
874
  SArray* pVals = NULL;
3,360✔
875
  SArray* pTagVals = NULL;
3,360✔
876
  SSubmitReqSendInfo** ppSendInfo = NULL;
3,360✔
877
  int32_t numOfBlks = 0;
3,360✔
878

879
  terrno = TSDB_CODE_SUCCESS;
3,360✔
880

881
  int32_t colNum = taosArrayGetSize(pDataBlock->pDataBlock);
3,360✔
882
  int32_t rows = pDataBlock->info.rows;
3,360✔
883

884
  if (!pTagVals && !(pTagVals = taosArrayInit(colNum, sizeof(STagVal)))) {
3,360✔
885
    goto _end;
×
886
  }
887

888
  if (!pVals && !(pVals = taosArrayInit(colNum, sizeof(SColVal)))) {
3,360✔
889
    goto _end;
×
890
  }
891

892
  SDBVgInfo* dbInfo = NULL;
3,360✔
893
  int32_t    code = inserterGetDbVgInfo(pInserter, pInserter->dbFName, &dbInfo);
3,360✔
894
  if (code != TSDB_CODE_SUCCESS) {
3,360✔
895
    terrno = code;
×
896
    goto _end;
×
897
  }
898

899
  for (int32_t j = 0; j < rows; ++j) {
10,080✔
900
    SSubmitTbData tbData = {0};
6,720✔
901
    if (!(tbData.aRowP = taosArrayInit(rows, sizeof(SRow*)))) {
6,720✔
902
      goto _end;
×
903
    }
904
    tbData.suid = suid;
6,720✔
905
    tbData.uid = uid;
6,720✔
906
    tbData.sver = pTSchema->version;
6,720✔
907

908
    int64_t lastTs = TSKEY_MIN;
6,720✔
909

910
    taosArrayClear(pVals);
6,720✔
911

912
    int32_t offset = 0;
6,720✔
913
    taosArrayClear(pTagVals);
6,720✔
914
    tbData.uid = 0;
6,720✔
915
    tbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
6,720✔
916
    if (NULL == tbData.pCreateTbReq) {
6,720✔
917
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
918
      goto _end;
×
919
    }
920
    tbData.flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
6,720✔
921

922
    SColumnInfoData* tbname = taosArrayGet(pDataBlock->pDataBlock, 0);
6,720✔
923
    if (NULL == tbname) {
6,720✔
924
      terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
925
      qError("Insert into stable must have tbname column");
×
926
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
927
      goto _end;
×
928
    }
929
    if (tbname->info.type != TSDB_DATA_TYPE_BINARY) {
6,720✔
930
      terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
931
      qError("tbname column must be binary");
×
932
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
933
      goto _end;
×
934
    }
935

936
    if (colDataIsNull_s(tbname, j)) {
13,440✔
937
      qError("insert into stable tbname column is null");
×
938
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
939
      goto _end;
×
940
    }
941
    void*   data = colDataGetVarData(tbname, j);
6,720✔
942
    SValue  sv = (SValue){TSDB_DATA_TYPE_VARCHAR, .nData = varDataLen(data), .pData = varDataVal(data)};
6,720✔
943
    SColVal cv = COL_VAL_VALUE(0, sv);
6,720✔
944

945
    char tbFullName[TSDB_TABLE_FNAME_LEN];
6,720✔
946
    char tableName[TSDB_TABLE_FNAME_LEN];
6,720✔
947
    memcpy(tableName, sv.pData, sv.nData);
6,720✔
948
    tableName[sv.nData] = '\0';
6,720✔
949

950
    int32_t len = snprintf(tbFullName, TSDB_TABLE_FNAME_LEN, "%s.%s", pInserter->dbFName, tableName);
6,720✔
951
    if (len >= TSDB_TABLE_FNAME_LEN) {
6,720✔
952
      terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
953
      qError("table name too long after format, len:%d, maxLen:%d", len, TSDB_TABLE_FNAME_LEN);
×
954
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
955
      goto _end;
×
956
    }
957
    SVgroupInfo vgInfo = {0};
6,720✔
958
    code = inserterGetVgInfo(dbInfo, tbFullName, &vgInfo);
6,720✔
959
    if (code != TSDB_CODE_SUCCESS) {
6,720✔
960
      terrno = code;
×
961
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
962
      goto _end;
×
963
    }
964
    SSubmitReq2* pReq = NULL;
6,720✔
965
    ppSendInfo = taosHashGet(pHash, &vgInfo.vgId, sizeof(int32_t));
6,720✔
966
    if (ppSendInfo == NULL) {
6,720✔
967
      SSubmitReqSendInfo* pSendInfo = taosMemoryCalloc(1, sizeof(SSubmitReqSendInfo));
6,160✔
968
      if (NULL == pSendInfo) {
6,160✔
969
        tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
970
        goto _end;
×
971
      }
972
      
973
      pReq = taosMemoryCalloc(1, sizeof(SSubmitReq2));
6,160✔
974
      if (NULL == pReq) {
6,160✔
975
        taosMemoryFree(pSendInfo);
×
976
        tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
977
        goto _end;
×
978
      }
979

980
      if (!(pReq->aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData)))) {
6,160✔
981
        taosMemoryFree(pReq);
×
982
        taosMemoryFree(pSendInfo);
×
983
        tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
984
        goto _end;
×
985
      }
986
      
987
      pSendInfo->msg = pReq;
6,160✔
988
      pSendInfo->epSet = vgInfo.epSet;
6,160✔
989
      code = taosHashPut(pHash, &vgInfo.vgId, sizeof(int32_t), &pSendInfo, POINTER_BYTES);
6,160✔
990
      if (code != TSDB_CODE_SUCCESS) {
6,160✔
991
        tDestroySubmitReq(pReq, TSDB_MSG_FLG_ENCODE);
×
992
        taosMemoryFree(pReq);
×
993
        taosMemoryFree(pSendInfo);
×
994
        terrno = code;
×
995
        tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
996
        goto _end;
×
997
      }
998
    } else {
999
      pReq = (*ppSendInfo)->msg;
560✔
1000
    }
1001
    SArray* TagNames = taosArrayInit(8, TSDB_COL_NAME_LEN);
6,720✔
1002
    if (!TagNames) {
6,720✔
1003
      terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1004
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1005
      goto _end;
×
1006
    }
1007
    for (int32_t i = 0; i < pInserter->pTagSchema->nCols; ++i) {
17,920✔
1008
      SSchema* tSchema = &pInserter->pTagSchema->pSchema[i];
11,200✔
1009
      int16_t  colIdx = tSchema->colId;
11,200✔
1010
      int16_t* slotId = taosHashGet(pInserter->pCols, &colIdx, sizeof(colIdx));
11,200✔
1011
      if (NULL == slotId) {
11,200✔
1012
        continue;
6,720✔
1013
      }
1014
      if (NULL == taosArrayPush(TagNames, tSchema->name)) {
8,960✔
1015
        taosArrayDestroy(TagNames);
×
1016
        tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1017
        goto _end;
×
1018
      }
1019

1020
      colIdx = *slotId;
4,480✔
1021
      SColumnInfoData* pColInfoData = taosArrayGet(pDataBlock->pDataBlock, colIdx);
4,480✔
1022
      if (NULL == pColInfoData) {
4,480✔
1023
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1024
        taosArrayDestroy(TagNames);
×
1025
        tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1026
        goto _end;
×
1027
      }
1028
      // void* var = POINTER_SHIFT(pColInfoData->pData, j * pColInfoData->info.bytes);
1029
      switch (pColInfoData->info.type) {
4,480✔
1030
        case TSDB_DATA_TYPE_NCHAR:
3,360✔
1031
        case TSDB_DATA_TYPE_VARBINARY:
1032
        case TSDB_DATA_TYPE_VARCHAR: {
1033
          if (pColInfoData->info.type != tSchema->type) {
3,360✔
1034
            qError("tag:%d type:%d in block dismatch with schema tag:%d type:%d", colIdx, pColInfoData->info.type, i,
×
1035
                   tSchema->type);
1036
            terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1037
            taosArrayDestroy(TagNames);
×
1038
            tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1039
            goto _end;
×
1040
          }
1041
          if (colDataIsNull_s(pColInfoData, j)) {
6,720✔
1042
            continue;
×
1043
          } else {
1044
            void*   data = colDataGetVarData(pColInfoData, j);
3,360✔
1045
            STagVal tv = (STagVal){
3,360✔
1046
                .cid = tSchema->colId, .type = tSchema->type, .nData = varDataLen(data), .pData = varDataVal(data)};
3,360✔
1047
            if (NULL == taosArrayPush(pTagVals, &tv)) {
3,360✔
1048
              taosArrayDestroy(TagNames);
×
1049
              tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1050
              goto _end;
×
1051
            }
1052
          }
1053
          break;
3,360✔
1054
        }
1055
        case TSDB_DATA_TYPE_BLOB:
×
1056
        case TSDB_DATA_TYPE_JSON:
1057
        case TSDB_DATA_TYPE_MEDIUMBLOB:
1058
          qError("the tag type %" PRIi16 " is defined but not implemented yet", pColInfoData->info.type);
×
1059
          terrno = TSDB_CODE_APP_ERROR;
×
1060
          taosArrayDestroy(TagNames);
×
1061
          tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1062
          goto _end;
×
1063
          break;
1064
        default:
1,120✔
1065
          if (pColInfoData->info.type < TSDB_DATA_TYPE_MAX && pColInfoData->info.type > TSDB_DATA_TYPE_NULL) {
1,120✔
1066
            if (colDataIsNull_s(pColInfoData, j)) {
2,240✔
1067
              continue;
×
1068
            } else {
1069
              void*   data = colDataGetData(pColInfoData, j);
1,120✔
1070
              STagVal tv = {.cid = tSchema->colId, .type = tSchema->type};
1,120✔
1071
              memcpy(&tv.i64, data, tSchema->bytes);
1,120✔
1072
              if (NULL == taosArrayPush(pTagVals, &tv)) {
1,120✔
1073
                taosArrayDestroy(TagNames);
×
1074
                tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1075
                goto _end;
×
1076
              }
1077
            }
1078
          } else {
1079
            uError("the column type %" PRIi16 " is undefined\n", pColInfoData->info.type);
×
1080
            terrno = TSDB_CODE_APP_ERROR;
×
1081
            taosArrayDestroy(TagNames);
×
1082
            tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1083
            goto _end;
×
1084
          }
1085
          break;
1,120✔
1086
      }
1087
    }
1088
    STag* pTag = NULL;
6,720✔
1089
    code = tTagNew(pTagVals, 1, false, &pTag);
6,720✔
1090
    if (code != TSDB_CODE_SUCCESS) {
6,720✔
1091
      terrno = code;
×
1092
      qError("failed to create tag, error:%s", tstrerror(code));
×
1093
      taosArrayDestroy(TagNames);
×
1094
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1095
      goto _end;
×
1096
    }
1097

1098
    code = inserterBuildCreateTbReq(tbData.pCreateTbReq, tableName, pTag, suid, pInserter->pNode->tableName, TagNames,
6,720✔
1099
                                    pInserter->pTagSchema->nCols, TSDB_DEFAULT_TABLE_TTL);
6,720✔
1100
    if (code != TSDB_CODE_SUCCESS) {
6,720✔
1101
      terrno = code;
×
1102
      qError("failed to build create table request, error:%s", tstrerror(code));
×
1103
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1104
      goto _end;
×
1105
    }
1106

1107
    for (int32_t k = 0; k < pTSchema->numOfCols; ++k) {
38,080✔
1108
      int16_t         colIdx = k;
31,360✔
1109
      const STColumn* pCol = &pTSchema->columns[k];
31,360✔
1110
      int16_t*        slotId = taosHashGet(pInserter->pCols, &pCol->colId, sizeof(pCol->colId));
31,360✔
1111
      if (NULL == slotId) {
31,360✔
1112
        continue;
14,560✔
1113
      }
1114
      colIdx = *slotId;
16,800✔
1115

1116
      SColumnInfoData* pColInfoData = taosArrayGet(pDataBlock->pDataBlock, colIdx);
16,800✔
1117
      if (NULL == pColInfoData) {
16,800✔
1118
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1119
        tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1120
        goto _end;
×
1121
      }
1122
      void* var = POINTER_SHIFT(pColInfoData->pData, j * pColInfoData->info.bytes);
16,800✔
1123

1124
      switch (pColInfoData->info.type) {
16,800✔
1125
        case TSDB_DATA_TYPE_NCHAR:
×
1126
        case TSDB_DATA_TYPE_VARBINARY:
1127
        case TSDB_DATA_TYPE_VARCHAR: {
1128
          if (pColInfoData->info.type != pCol->type) {
×
1129
            qError("column:%d type:%d in block dismatch with schema col:%d type:%d", colIdx, pColInfoData->info.type, k,
×
1130
                   pCol->type);
1131
            terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1132
            tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1133
            goto _end;
×
1134
          }
1135
          if (colDataIsNull_s(pColInfoData, j)) {
×
1136
            SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);
×
1137
            if (NULL == taosArrayPush(pVals, &cv)) {
×
1138
              tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1139
              goto _end;
×
1140
            }
1141
          } else {
1142
            void*   data = colDataGetVarData(pColInfoData, j);
×
1143
            SValue  sv = (SValue){.type = pCol->type, .nData = varDataLen(data), .pData = varDataVal(data)};
×
1144
            SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
×
1145
            if (NULL == taosArrayPush(pVals, &cv)) {
×
1146
              tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1147
              goto _end;
×
1148
            }
1149
          }
1150
          break;
×
1151
        }
1152
        case TSDB_DATA_TYPE_BLOB:
×
1153
        case TSDB_DATA_TYPE_JSON:
1154
        case TSDB_DATA_TYPE_MEDIUMBLOB:
1155
          qError("the column type %" PRIi16 " is defined but not implemented yet", pColInfoData->info.type);
×
1156
          terrno = TSDB_CODE_APP_ERROR;
×
1157
          tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1158
          goto _end;
×
1159
          break;
1160
        default:
16,800✔
1161
          if (pColInfoData->info.type < TSDB_DATA_TYPE_MAX && pColInfoData->info.type > TSDB_DATA_TYPE_NULL) {
16,800✔
1162
            if (colDataIsNull_s(pColInfoData, j)) {
33,600✔
1163
              if (PRIMARYKEY_TIMESTAMP_COL_ID == pCol->colId) {
×
1164
                qError("Primary timestamp column should not be null");
×
1165
                terrno = TSDB_CODE_PAR_INCORRECT_TIMESTAMP_VAL;
×
1166
                tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1167
                goto _end;
×
1168
              }
1169

1170
              SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);
×
1171
              if (NULL == taosArrayPush(pVals, &cv)) {
×
1172
                tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1173
                goto _end;
×
1174
              }
1175
            } else {
1176
              // if (PRIMARYKEY_TIMESTAMP_COL_ID == pCol->colId && !needSortMerge) {
1177
              //   if (*(int64_t*)var <= lastTs) {
1178
              //     needSortMerge = true;
1179
              //   } else {
1180
              //     lastTs = *(int64_t*)var;
1181
              //   }
1182
              // }
1183

1184
              SValue sv = {.type = pCol->type};
16,800✔
1185
              valueSetDatum(&sv, sv.type, var, tDataTypes[pCol->type].bytes);
16,800✔
1186
              SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
16,800✔
1187
              if (NULL == taosArrayPush(pVals, &cv)) {
16,800✔
1188
                tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1189
                goto _end;
×
1190
              }
1191
            }
1192
          } else {
1193
            uError("the column type %" PRIi16 " is undefined\n", pColInfoData->info.type);
×
1194
            terrno = TSDB_CODE_APP_ERROR;
×
1195
            tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1196
            goto _end;
×
1197
          }
1198
          break;
16,800✔
1199
      }
1200
    }
1201

1202
    SRow* pRow = NULL;
6,720✔
1203
    SRowBuildScanInfo sinfo = {0};
6,720✔
1204
    if ((terrno = tRowBuild(pVals, pTSchema, &pRow, &sinfo)) < 0) {
6,720✔
1205
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1206
      goto _end;
×
1207
    }
1208
    if (NULL == taosArrayPush(tbData.aRowP, &pRow)) {
13,440✔
1209
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1210
      goto _end;
×
1211
    }
1212

1213
    if (NULL == taosArrayPush(pReq->aSubmitTbData, &tbData)) {
13,440✔
1214
      goto _end;
×
1215
    }
1216
  }
1217

1218
_end:
3,360✔
1219
  taosArrayDestroy(pTagVals);
3,360✔
1220
  taosArrayDestroy(pVals);
3,360✔
1221

1222
  return terrno;
3,360✔
1223
}
1224

1225
int32_t buildSubmitReqFromBlock(SDataInserterHandle* pInserter, SSubmitReq2** ppReq, const SSDataBlock* pDataBlock,
61,981✔
1226
                                const STSchema* pTSchema, int64_t* uid, int32_t* vgId, tb_uid_t* suid) {
1227
  SSubmitReq2* pReq = *ppReq;
61,981✔
1228
  SArray*      pVals = NULL;
61,981✔
1229
  SArray*      pTagVals = NULL;
61,981✔
1230
  int32_t      numOfBlks = 0;
61,981✔
1231
  char*        tableName = NULL;
61,981✔
1232
  int32_t      code = 0, lino = 0;
61,981✔
1233

1234
  terrno = TSDB_CODE_SUCCESS;
61,981✔
1235

1236
  if (NULL == pReq) {
61,981✔
1237
    if (!(pReq = taosMemoryCalloc(1, sizeof(SSubmitReq2)))) {
61,981✔
1238
      goto _end;
×
1239
    }
1240

1241
    if (!(pReq->aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData)))) {
61,981✔
1242
      goto _end;
×
1243
    }
1244
  }
1245

1246
  int32_t colNum = taosArrayGetSize(pDataBlock->pDataBlock);
61,981✔
1247
  int32_t rows = pDataBlock->info.rows;
61,981✔
1248

1249
  SSubmitTbData tbData = {0};
61,981✔
1250
  if (!(tbData.aRowP = taosArrayInit(rows, sizeof(SRow*)))) {
61,981✔
1251
    goto _end;
×
1252
  }
1253
  tbData.suid = *suid;
61,981✔
1254
  tbData.uid = *uid;
61,981✔
1255
  tbData.sver = pTSchema->version;
61,981✔
1256

1257
  if (!pVals && !(pVals = taosArrayInit(colNum, sizeof(SColVal)))) {
61,981✔
1258
    taosArrayDestroy(tbData.aRowP);
×
1259
    goto _end;
×
1260
  }
1261

1262
  if (pInserter->isStbInserter) {
61,981✔
1263
    if (!pTagVals && !(pTagVals = taosArrayInit(colNum, sizeof(STagVal)))) {
×
1264
      taosArrayDestroy(tbData.aRowP);
×
1265
      goto _end;
×
1266
    }
1267
  }
1268

1269
  int64_t lastTs = TSKEY_MIN;
61,981✔
1270
  bool    needSortMerge = false;
61,981✔
1271

1272
  for (int32_t j = 0; j < rows; ++j) {  // iterate by row
9,578,386✔
1273
    taosArrayClear(pVals);
9,523,677✔
1274

1275
    int32_t offset = 0;
9,523,677✔
1276
    // 处理超级表的tbname和tags
1277
    if (pInserter->isStbInserter) {
9,523,677✔
1278
      taosArrayClear(pTagVals);
×
1279
      tbData.uid = 0;
×
1280
      *uid = 0;
×
1281
      tbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1282
      tbData.flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
×
1283

1284
      SColumnInfoData* tbname = taosArrayGet(pDataBlock->pDataBlock, 0);
×
1285
      if (NULL == tbname) {
×
1286
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1287
        qError("Insert into stable must have tbname column");
×
1288
        goto _end;
×
1289
      }
1290
      if (tbname->info.type != TSDB_DATA_TYPE_BINARY) {
×
1291
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1292
        qError("tbname column must be binary");
×
1293
        goto _end;
×
1294
      }
1295

1296
      if (colDataIsNull_s(tbname, j)) {
×
1297
        qError("insert into stable tbname column is null");
×
1298
        goto _end;
×
1299
      }
1300
      void*   data = colDataGetVarData(tbname, j);
×
1301
      SValue  sv = (SValue){TSDB_DATA_TYPE_VARCHAR, .nData = varDataLen(data),
×
1302
                            .pData = varDataVal(data)};  // address copy, no value
×
1303
      SColVal cv = COL_VAL_VALUE(0, sv);
×
1304

1305
      // 获取子表vgId
1306
      SDBVgInfo* dbInfo = NULL;
×
1307
      code = inserterGetDbVgInfo(pInserter, pInserter->dbFName, &dbInfo);
×
1308
      if (code != TSDB_CODE_SUCCESS) {
×
1309
        goto _end;
×
1310
      }
1311

1312
      char tbFullName[TSDB_TABLE_FNAME_LEN];
×
1313
      taosMemoryFreeClear(tableName);
×
1314
      tableName = taosMemoryCalloc(1, sv.nData + 1);
×
1315
      TSDB_CHECK_NULL(tableName, code, lino, _end, terrno);
×
1316
      tstrncpy(tableName, sv.pData, sv.nData);
×
1317
      tableName[sv.nData] = '\0';
×
1318

1319
      int32_t len = snprintf(tbFullName, TSDB_TABLE_FNAME_LEN, "%s.%s", pInserter->dbFName, tableName);
×
1320
      if (len >= TSDB_TABLE_FNAME_LEN) {
×
1321
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1322
        qError("table name too long after format, len:%d, maxLen:%d", len, TSDB_TABLE_FNAME_LEN);
×
1323
        goto _end;
×
1324
      }
1325
      code = inserterGetVgId(dbInfo, tbFullName, vgId);
×
1326
      if (code != TSDB_CODE_SUCCESS) {
×
1327
        terrno = code;
×
1328
        goto _end;
×
1329
      }
1330
      // 解析tag
1331
      SArray* TagNames = taosArrayInit(8, TSDB_COL_NAME_LEN);
×
1332
      if (!TagNames) {
×
1333
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1334
        goto _end;
×
1335
      }
1336
      for (int32_t i = 0; i < pInserter->pTagSchema->nCols; ++i) {
×
1337
        SSchema* tSchema = &pInserter->pTagSchema->pSchema[i];
×
1338
        int16_t  colIdx = tSchema->colId;
×
1339
        if (NULL == taosArrayPush(TagNames, tSchema->name)) {
×
1340
          goto _end;
×
1341
        }
1342
        int16_t* slotId = taosHashGet(pInserter->pCols, &colIdx, sizeof(colIdx));
×
1343
        if (NULL == slotId) {
×
1344
          continue;
×
1345
        }
1346

1347
        colIdx = *slotId;
×
1348
        SColumnInfoData* pColInfoData = taosArrayGet(pDataBlock->pDataBlock, colIdx);
×
1349
        if (NULL == pColInfoData) {
×
1350
          terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1351
          goto _end;
×
1352
        }
1353
        // void* var = POINTER_SHIFT(pColInfoData->pData, j * pColInfoData->info.bytes);
1354
        switch (pColInfoData->info.type) {
×
1355
          case TSDB_DATA_TYPE_NCHAR:
×
1356
          case TSDB_DATA_TYPE_VARBINARY:
1357
          case TSDB_DATA_TYPE_VARCHAR: {  // TSDB_DATA_TYPE_BINARY
1358
            if (pColInfoData->info.type != tSchema->type) {
×
1359
              qError("tag:%d type:%d in block dismatch with schema tag:%d type:%d", colIdx, pColInfoData->info.type, i,
×
1360
                     tSchema->type);
1361
              terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1362
              goto _end;
×
1363
            }
1364
            if (colDataIsNull_s(pColInfoData, j)) {
×
1365
              continue;
×
1366
            } else {
1367
              void*   data = colDataGetVarData(pColInfoData, j);
×
1368
              STagVal tv = (STagVal){.cid = tSchema->colId,
×
1369
                                     .type = tSchema->type,
×
1370
                                     .nData = varDataLen(data),
×
1371
                                     .pData = varDataVal(data)};  // address copy, no value
×
1372
              if (NULL == taosArrayPush(pTagVals, &tv)) {
×
1373
                goto _end;
×
1374
              }
1375
            }
1376
            break;
×
1377
          }
1378
          case TSDB_DATA_TYPE_BLOB:
×
1379
          case TSDB_DATA_TYPE_JSON:
1380
          case TSDB_DATA_TYPE_MEDIUMBLOB:
1381
            qError("the tag type %" PRIi16 " is defined but not implemented yet", pColInfoData->info.type);
×
1382
            terrno = TSDB_CODE_APP_ERROR;
×
1383
            goto _end;
×
1384
            break;
1385
          default:
×
1386
            if (pColInfoData->info.type < TSDB_DATA_TYPE_MAX && pColInfoData->info.type > TSDB_DATA_TYPE_NULL) {
×
1387
              if (colDataIsNull_s(pColInfoData, j)) {
×
1388
                continue;
×
1389
              } else {
1390
                void*   data = colDataGetData(pColInfoData, j);
×
1391
                STagVal tv = {.cid = tSchema->colId, .type = tSchema->type};
×
1392
                memcpy(&tv.i64, data, tSchema->bytes);
×
1393
                if (NULL == taosArrayPush(pTagVals, &tv)) {
×
1394
                  goto _end;
×
1395
                }
1396
              }
1397
            } else {
1398
              uError("the column type %" PRIi16 " is undefined\n", pColInfoData->info.type);
×
1399
              terrno = TSDB_CODE_APP_ERROR;
×
1400
              goto _end;
×
1401
            }
1402
            break;
×
1403
        }
1404
      }
1405
      STag* pTag = NULL;
×
1406
      code = tTagNew(pTagVals, 1, false, &pTag);
×
1407
      if (code != TSDB_CODE_SUCCESS) {
×
1408
        terrno = code;
×
1409
        qError("failed to create tag, error:%s", tstrerror(code));
×
1410
        goto _end;
×
1411
      }
1412

1413
      code = inserterBuildCreateTbReq(tbData.pCreateTbReq, tableName, pTag, *suid, pInserter->pNode->tableName, TagNames,
×
1414
                               pInserter->pTagSchema->nCols, TSDB_DEFAULT_TABLE_TTL);
×
1415
    }
1416

1417
    for (int32_t k = 0; k < pTSchema->numOfCols; ++k) {  // iterate by column
47,655,064✔
1418
      int16_t         colIdx = k;
38,131,387✔
1419
      const STColumn* pCol = &pTSchema->columns[k];
38,131,387✔
1420
      if (!pInserter->fullOrderColList) {
38,131,387✔
1421
        int16_t* slotId = taosHashGet(pInserter->pCols, &pCol->colId, sizeof(pCol->colId));
129,136✔
1422
        if (NULL == slotId) {
129,136✔
1423
          continue;
31,444✔
1424
        }
1425

1426
        colIdx = *slotId;
97,692✔
1427
      }
1428

1429
      SColumnInfoData* pColInfoData = taosArrayGet(pDataBlock->pDataBlock, colIdx);
38,099,943✔
1430
      if (NULL == pColInfoData) {
38,099,943✔
1431
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1432
        goto _end;
×
1433
      }
1434
      void* var = POINTER_SHIFT(pColInfoData->pData, j * pColInfoData->info.bytes);
38,099,943✔
1435

1436
      switch (pColInfoData->info.type) {
38,099,943✔
1437
        case TSDB_DATA_TYPE_NCHAR:
244,087✔
1438
        case TSDB_DATA_TYPE_VARBINARY:
1439
        case TSDB_DATA_TYPE_VARCHAR: {  // TSDB_DATA_TYPE_BINARY
1440
          if (pColInfoData->info.type != pCol->type) {
244,087✔
1441
            qError("column:%d type:%d in block dismatch with schema col:%d type:%d", colIdx, pColInfoData->info.type, k,
×
1442
                   pCol->type);
1443
            terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1444
            goto _end;
×
1445
          }
1446
          if (colDataIsNull_s(pColInfoData, j)) {
488,174✔
1447
            SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);
2,424✔
1448
            if (NULL == taosArrayPush(pVals, &cv)) {
2,424✔
1449
              goto _end;
×
1450
            }
1451
          } else {
1452
            void*  data = colDataGetVarData(pColInfoData, j);
241,663✔
1453
            SValue sv = (SValue){
724,989✔
1454
                .type = pCol->type, .nData = varDataLen(data), .pData = varDataVal(data)};  // address copy, no value
241,663✔
1455
            SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
241,663✔
1456
            if (NULL == taosArrayPush(pVals, &cv)) {
241,663✔
1457
              goto _end;
×
1458
            }
1459
          }
1460
          break;
244,087✔
1461
        }
1462
        case TSDB_DATA_TYPE_BLOB:
×
1463
        case TSDB_DATA_TYPE_MEDIUMBLOB:
1464
        case TSDB_DATA_TYPE_JSON:
1465
          qError("the column type %" PRIi16 " is defined but not implemented yet", pColInfoData->info.type);
×
1466
          terrno = TSDB_CODE_APP_ERROR;
×
1467
          goto _end;
×
1468
          break;
1469
        default:
37,855,856✔
1470
          if (pColInfoData->info.type < TSDB_DATA_TYPE_MAX && pColInfoData->info.type > TSDB_DATA_TYPE_NULL) {
37,855,856✔
1471
            if (colDataIsNull_s(pColInfoData, j)) {
75,711,712✔
1472
              if (PRIMARYKEY_TIMESTAMP_COL_ID == pCol->colId) {
19,962✔
1473
                qError("Primary timestamp column should not be null");
×
1474
                terrno = TSDB_CODE_PAR_INCORRECT_TIMESTAMP_VAL;
×
1475
                goto _end;
×
1476
              }
1477

1478
              SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);  // should use pCol->type
19,962✔
1479
              if (NULL == taosArrayPush(pVals, &cv)) {
19,962✔
1480
                goto _end;
×
1481
              }
1482
            } else {
1483
              if (PRIMARYKEY_TIMESTAMP_COL_ID == pCol->colId && !needSortMerge) {
37,835,894✔
1484
                if (*(int64_t*)var <= lastTs) {
9,450,955✔
1485
                  needSortMerge = true;
11,027✔
1486
                } else {
1487
                  lastTs = *(int64_t*)var;
9,439,928✔
1488
                }
1489
              }
1490

1491
              SValue sv = {.type = pCol->type};
37,835,894✔
1492
              valueSetDatum(&sv, sv.type, var, tDataTypes[pCol->type].bytes);
37,835,894✔
1493
              SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
37,835,894✔
1494
              if (NULL == taosArrayPush(pVals, &cv)) {
37,835,894✔
1495
                goto _end;
×
1496
              }
1497
            }
1498
          } else {
1499
            uError("the column type %" PRIi16 " is undefined\n", pColInfoData->info.type);
×
1500
            terrno = TSDB_CODE_APP_ERROR;
×
1501
            goto _end;
×
1502
          }
1503
          break;
37,855,856✔
1504
      }
1505
    }
1506

1507
    SRow*             pRow = NULL;
9,523,677✔
1508
    SRowBuildScanInfo sinfo = {0};
9,523,677✔
1509
    if ((terrno = tRowBuild(pVals, pTSchema, &pRow, &sinfo)) < 0) {
9,523,677✔
1510
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
7,272✔
1511
      goto _end;
7,272✔
1512
    }
1513
    if (NULL == taosArrayPush(tbData.aRowP, &pRow)) {
19,032,810✔
1514
      goto _end;
×
1515
    }
1516
  }
1517

1518
  if (needSortMerge) {
54,709✔
1519
    if ((tRowSort(tbData.aRowP) != TSDB_CODE_SUCCESS) ||
11,027✔
1520
        (terrno = tRowMerge(tbData.aRowP, (STSchema*)pTSchema, KEEP_CONSISTENCY)) != 0) {
11,027✔
1521
      goto _end;
×
1522
    }
1523
  }
1524

1525
  if (NULL == taosArrayPush(pReq->aSubmitTbData, &tbData)) {
109,418✔
1526
    goto _end;
×
1527
  }
1528

1529
_end:
61,981✔
1530

1531
  taosMemoryFreeClear(tableName);
61,981✔
1532

1533
  taosArrayDestroy(pTagVals);
61,981✔
1534
  taosArrayDestroy(pVals);
61,981✔
1535

1536
  if (terrno != 0) {
61,981✔
1537
    *ppReq = NULL;
7,272✔
1538
    if (pReq) {
7,272✔
1539
      tDestroySubmitReq(pReq, TSDB_MSG_FLG_ENCODE);
7,272✔
1540
      taosMemoryFree(pReq);
7,272✔
1541
    }
1542

1543
    return terrno;
7,272✔
1544
  }
1545
  *ppReq = pReq;
54,709✔
1546

1547
  return TSDB_CODE_SUCCESS;
54,709✔
1548
}
1549

1550
static void destroySubmitReqWrapper(void* p) {
6,160✔
1551
  SSubmitReqSendInfo* pSendInfo = *(SSubmitReqSendInfo**)p;
6,160✔
1552
  if (pSendInfo != NULL) {
6,160✔
1553
    if (pSendInfo->msg != NULL) {
6,160✔
1554
      tDestroySubmitReq(pSendInfo->msg, TSDB_MSG_FLG_ENCODE);
6,160✔
1555
      taosMemoryFree(pSendInfo->msg);
6,160✔
1556
    }
1557
    taosMemoryFree(pSendInfo);
6,160✔
1558
  }
1559
}
6,160✔
1560

1561
int32_t dataBlocksToSubmitReqArray(SDataInserterHandle* pInserter, SArray* pMsgs) {
3,360✔
1562
  const SArray*   pBlocks = pInserter->pDataBlocks;
3,360✔
1563
  const STSchema* pTSchema = pInserter->pSchema;
3,360✔
1564
  int64_t         uid = pInserter->pNode->tableId;
3,360✔
1565
  int64_t         suid = pInserter->pNode->stableId;
3,360✔
1566
  int32_t         vgId = pInserter->pNode->vgId;
3,360✔
1567
  int32_t         sz = taosArrayGetSize(pBlocks);
3,360✔
1568
  int32_t         code = 0;
3,360✔
1569

1570
  SHashObj* pHash = NULL;
3,360✔
1571
  void*     iterator = NULL;
3,360✔
1572

1573
  for (int32_t i = 0; i < sz; i++) {
6,720✔
1574
    SSDataBlock* pDataBlock = taosArrayGetP(pBlocks, i);  // pDataBlock select查询到的结果
3,360✔
1575
    if (NULL == pDataBlock) {
3,360✔
1576
      return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1577
    }
1578
    if (pHash == NULL) {
3,360✔
1579
      pHash = taosHashInit(sz * pDataBlock->info.rows, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false,
3,360✔
1580
                           HASH_ENTRY_LOCK);
1581
      if (NULL == pHash) {
3,360✔
1582
        return terrno;
×
1583
      }
1584
      taosHashSetFreeFp(pHash, destroySubmitReqWrapper);
3,360✔
1585
    }
1586
    code = buildSubmitReqFromStbBlock(pInserter, pHash, pDataBlock, pTSchema, uid, vgId, suid);
3,360✔
1587
    if (code != TSDB_CODE_SUCCESS) {
3,360✔
1588
      goto _end;
×
1589
    }
1590
  }
1591

1592
  size_t keyLen = 0;
3,360✔
1593
  while ((iterator = taosHashIterate(pHash, iterator))) {
9,520✔
1594
    SSubmitReqSendInfo* pReqSendInfo = *(SSubmitReqSendInfo**)iterator;
6,160✔
1595
    int32_t*            ctbVgId = taosHashGetKey(iterator, &keyLen);
6,160✔
1596

1597
    SSubmitTbDataSendInfo* pTbSendInfo = taosMemoryCalloc(1, sizeof(SSubmitTbDataSendInfo));
6,160✔
1598
    if (NULL == pTbSendInfo) {
6,160✔
1599
      code = terrno;
×
1600
      goto _end;
×
1601
    }
1602
    code = submitReqToMsg(*ctbVgId, pReqSendInfo->msg, &pTbSendInfo->msg.pData, &pTbSendInfo->msg.len);
6,160✔
1603
    if (code != TSDB_CODE_SUCCESS) {
6,160✔
1604
      taosMemoryFree(pTbSendInfo);
×
1605
      goto _end;
×
1606
    }
1607
    pTbSendInfo->epSet = pReqSendInfo->epSet;
6,160✔
1608
    if (NULL == taosArrayPush(pMsgs, &pTbSendInfo)) {
6,160✔
1609
      taosMemoryFree(pTbSendInfo->msg.pData);
×
1610
      taosMemoryFree(pTbSendInfo);
×
1611
      code = terrno;
×
1612
      goto _end;
×
1613
    }
1614
  }
1615

1616
_end:
3,360✔
1617
  if (pHash != NULL) {
3,360✔
1618
    taosHashCleanup(pHash);
3,360✔
1619
  }
1620

1621
  return code;
3,360✔
1622
}
1623

1624
int32_t dataBlocksToSubmitReq(SDataInserterHandle* pInserter, void** pMsg, int32_t* msgLen) {
61,981✔
1625
  const SArray*   pBlocks = pInserter->pDataBlocks;
61,981✔
1626
  const STSchema* pTSchema = pInserter->pSchema;
61,981✔
1627
  int64_t         uid = pInserter->pNode->tableId;
61,981✔
1628
  int64_t         suid = pInserter->pNode->stableId;
61,981✔
1629
  int32_t         vgId = pInserter->pNode->vgId;
61,981✔
1630
  int32_t         sz = taosArrayGetSize(pBlocks);
61,981✔
1631
  int32_t         code = 0;
61,981✔
1632
  SSubmitReq2*    pReq = NULL;
61,981✔
1633

1634
  for (int32_t i = 0; i < sz; i++) {
116,690✔
1635
    SSDataBlock* pDataBlock = taosArrayGetP(pBlocks, i);  // pDataBlock select查询到的结果
61,981✔
1636
    if (NULL == pDataBlock) {
61,981✔
1637
      return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1638
    }
1639
    code = buildSubmitReqFromBlock(pInserter, &pReq, pDataBlock, pTSchema, &uid, &vgId, &suid);
61,981✔
1640
    if (code) {
61,981✔
1641
      if (pReq) {
7,272✔
1642
        tDestroySubmitReq(pReq, TSDB_MSG_FLG_ENCODE);
×
1643
        taosMemoryFree(pReq);
×
1644
      }
1645

1646
      return code;
7,272✔
1647
    }
1648
  }
1649

1650
  code = submitReqToMsg(vgId, pReq, pMsg, msgLen);
54,709✔
1651
  tDestroySubmitReq(pReq, TSDB_MSG_FLG_ENCODE);
54,709✔
1652
  taosMemoryFree(pReq);
54,709✔
1653

1654
  return code;
54,709✔
1655
}
1656

1657
int32_t getStreamInsertTableInfo(int64_t streamId, int64_t groupId, SInsertTableInfo*** ppTbInfo) {
2,607,549✔
1658
  int64_t            key[2] = {streamId, groupId};
2,607,549✔
1659
  SInsertTableInfo** pTmp = taosHashAcquire(gStreamGrpTableHash, key, sizeof(key));
2,608,203✔
1660
  if (NULL == pTmp || *pTmp == NULL) {
2,608,203✔
1661
    return TSDB_CODE_STREAM_INSERT_TBINFO_NOT_FOUND;
×
1662
  }
1663

1664
  *ppTbInfo = pTmp;
2,608,203✔
1665
  return TSDB_CODE_SUCCESS;
2,608,203✔
1666
}
1667

1668
static int32_t releaseStreamInsertTableInfo(SInsertTableInfo** ppTbInfo) {
2,607,992✔
1669
  taosHashRelease(gStreamGrpTableHash, ppTbInfo);
2,607,992✔
1670
  return TSDB_CODE_SUCCESS;
2,608,203✔
1671
}
1672

1673
int32_t buildNormalTableCreateReq(SDataInserterHandle* pInserter, SStreamInserterParam* pInsertParam,
93,015✔
1674
                                  SSubmitTbData* tbData) {
1675
  int32_t code = TSDB_CODE_SUCCESS;
93,015✔
1676

1677
  tbData->suid = 0;
93,015✔
1678

1679
  tbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
93,015✔
1680
  if (NULL == tbData->pCreateTbReq) {
93,015✔
1681
    goto _end;
×
1682
  }
1683
  tbData->flags |= (SUBMIT_REQ_AUTO_CREATE_TABLE | SUBMIT_REQ_SCHEMA_RES);
93,015✔
1684
  tbData->pCreateTbReq->type = TSDB_NORMAL_TABLE;
93,015✔
1685
  tbData->pCreateTbReq->flags |= (TD_CREATE_NORMAL_TB_IN_STREAM | TD_CREATE_IF_NOT_EXISTS);
93,015✔
1686
  tbData->pCreateTbReq->uid = 0;
93,015✔
1687
  tbData->sver = pInsertParam->sver;
93,015✔
1688

1689
  tbData->pCreateTbReq->name = taosStrdup(pInsertParam->tbname);
93,015✔
1690
  if (!tbData->pCreateTbReq->name) return terrno;
93,015✔
1691

1692
  int32_t numOfCols = pInsertParam->pFields->size;
93,015✔
1693
  tbData->pCreateTbReq->ntb.schemaRow.nCols = numOfCols;
93,015✔
1694
  tbData->pCreateTbReq->ntb.schemaRow.version = 1;
93,015✔
1695

1696
  tbData->pCreateTbReq->ntb.schemaRow.pSchema = taosMemoryCalloc(numOfCols, sizeof(SSchema));
93,015✔
1697
  if (NULL == tbData->pCreateTbReq->ntb.schemaRow.pSchema) {
93,015✔
1698
    goto _end;
×
1699
  }
1700
  for (int32_t i = 0; i < numOfCols; ++i) {
525,442✔
1701
    SFieldWithOptions* pField = taosArrayGet(pInsertParam->pFields, i);
432,427✔
1702
    if (NULL == pField) {
432,427✔
1703
      terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1704
      goto _end;
×
1705
    }
1706
    tbData->pCreateTbReq->ntb.schemaRow.pSchema[i].colId = i + 1;
432,427✔
1707
    tbData->pCreateTbReq->ntb.schemaRow.pSchema[i].type = pField->type;
432,427✔
1708
    tbData->pCreateTbReq->ntb.schemaRow.pSchema[i].bytes = pField->bytes;
432,427✔
1709
    tbData->pCreateTbReq->ntb.schemaRow.pSchema[i].flags = pField->flags;
432,427✔
1710
    if (i == 0 && pField->type != TSDB_DATA_TYPE_TIMESTAMP) {
432,427✔
1711
      terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1712
      qError("buildNormalTableCreateReq, the first column must be timestamp.");
×
1713
      goto _end;
×
1714
    }
1715
    snprintf(tbData->pCreateTbReq->ntb.schemaRow.pSchema[i].name, TSDB_COL_NAME_LEN, "%s", pField->name);
432,427✔
1716
    if (IS_DECIMAL_TYPE(pField->type)) {
432,427✔
1717
      if (!tbData->pCreateTbReq->pExtSchemas) {
×
1718
        tbData->pCreateTbReq->pExtSchemas = taosMemoryCalloc(numOfCols, sizeof(SExtSchema));
×
1719
        if (NULL == tbData->pCreateTbReq->pExtSchemas) {
×
1720
          tdDestroySVCreateTbReq(tbData->pCreateTbReq);
×
1721
          tbData->pCreateTbReq = NULL;
×
1722
          return terrno;
×
1723
        }
1724
      }
1725
      tbData->pCreateTbReq->pExtSchemas[i].typeMod = pField->typeMod;
×
1726
    }
1727
  }
1728
  return TSDB_CODE_SUCCESS;
93,015✔
1729
_end:
×
1730
  return code;
×
1731
}
1732

1733
// reference tBuildTSchema funciton
1734
static int32_t buildTSchmaFromInserter(SStreamInserterParam* pInsertParam, STSchema** ppTSchema) {
223,722✔
1735
  int32_t code = TSDB_CODE_SUCCESS;
223,722✔
1736

1737
  int32_t   numOfCols = pInsertParam->pFields->size;
223,722✔
1738
  STSchema* pTSchema = taosMemoryCalloc(1, sizeof(STSchema) + sizeof(STColumn) * numOfCols);
223,963✔
1739
  if (NULL == pTSchema) {
223,963✔
1740
    return terrno;
×
1741
  }
1742
  if (pInsertParam->tbType == TSDB_NORMAL_TABLE) {
223,963✔
1743
    pTSchema->version =
72,309✔
1744
        1;  // normal table version start from 1, if has exist table, it will be reset by resetInserterTbVersion
1745
  } else {
1746
    pTSchema->version = pInsertParam->sver;
151,654✔
1747
  }
1748
  pTSchema->numOfCols = numOfCols;
223,722✔
1749

1750
  SFieldWithOptions* pField = taosArrayGet(pInsertParam->pFields, 0);
223,722✔
1751
  if (NULL == pField) {
223,963✔
1752
    code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1753
    goto _end;
×
1754
  }
1755
  pTSchema->columns[0].colId = PRIMARYKEY_TIMESTAMP_COL_ID;
223,963✔
1756
  pTSchema->columns[0].type = pField->type;
223,963✔
1757
  pTSchema->columns[0].flags = pField->flags;
223,963✔
1758
  pTSchema->columns[0].bytes = TYPE_BYTES[pField->type];
223,722✔
1759
  pTSchema->columns[0].offset = -1;
223,722✔
1760

1761
  pTSchema->tlen = 0;
223,722✔
1762
  pTSchema->flen = 0;
223,963✔
1763
  for (int32_t i = 1; i < numOfCols; ++i) {
1,065,159✔
1764
    SFieldWithOptions* pField = taosArrayGet(pInsertParam->pFields, i);
841,196✔
1765
    if (NULL == pField) {
841,437✔
1766
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1767
      goto _end;
×
1768
    }
1769
    pTSchema->columns[i].colId = i + 1;
841,437✔
1770
    pTSchema->columns[i].type = pField->type;
841,437✔
1771
    pTSchema->columns[i].flags = pField->flags;
841,678✔
1772
    pTSchema->columns[i].bytes = pField->bytes;
841,678✔
1773
    pTSchema->columns[i].offset = pTSchema->flen;
841,678✔
1774

1775
    if (IS_VAR_DATA_TYPE(pField->type)) {
841,678✔
1776
      pTSchema->columns[i].bytes = pField->bytes;
12,602✔
1777
      pTSchema->tlen += (TYPE_BYTES[pField->type] + pField->bytes);
12,572✔
1778
    } else {
1779
      pTSchema->columns[i].bytes = TYPE_BYTES[pField->type];
829,106✔
1780
      pTSchema->tlen += TYPE_BYTES[pField->type];
829,106✔
1781
    }
1782

1783
    pTSchema->flen += TYPE_BYTES[pField->type];
841,226✔
1784
  }
1785

1786
#if 1
1787
  pTSchema->tlen += (int32_t)TD_BITMAP_BYTES(numOfCols);
223,963✔
1788
#endif
1789

1790
_end:
223,722✔
1791
  if (code != TSDB_CODE_SUCCESS) {
223,722✔
1792
    taosMemoryFree(pTSchema);
×
1793
    *ppTSchema = NULL;
×
1794
  } else {
1795
    *ppTSchema = pTSchema;
223,722✔
1796
  }
1797
  return code;
223,722✔
1798
}
1799

1800
static int32_t getTagValsFromStreamInserterInfo(SStreamDataInserterInfo* pInserterInfo, int32_t preCols,
213,587✔
1801
                                                SArray** ppTagVals, SArray* pTagCids) {
1802
  int32_t code = TSDB_CODE_SUCCESS;
213,587✔
1803
  int32_t nTags = pInserterInfo->pTagVals->size;
213,587✔
1804
  *ppTagVals = taosArrayInit(nTags, sizeof(STagVal));
213,828✔
1805
  if (!ppTagVals) {
214,033✔
1806
    return terrno;
×
1807
  }
1808
  for (int32_t i = 0; i < pInserterInfo->pTagVals->size; ++i) {
567,605✔
1809
    SStreamTagInfo* pTagInfo = taosArrayGet(pInserterInfo->pTagVals, i);
353,572✔
1810
    STagVal         tagVal = {
356,228✔
1811
                .cid = pTagCids ? *(col_id_t*)taosArrayGet(pTagCids, i) : preCols + i + 1,
353,367✔
1812
                .type = pTagInfo->val.data.type,
353,367✔
1813
    };
1814
    if (!pTagInfo->val.isNull) {
353,331✔
1815
      if (IS_VAR_DATA_TYPE(pTagInfo->val.data.type)) {
353,361✔
1816
        tagVal.nData = pTagInfo->val.data.nData;
257,176✔
1817
        tagVal.pData = pTagInfo->val.data.pData;
256,935✔
1818
      } else {
1819
        tagVal.i64 = pTagInfo->val.data.val;
96,607✔
1820
      }
1821

1822
      if (NULL == taosArrayPush(*ppTagVals, &tagVal)) {
706,457✔
1823
        code = terrno;
×
1824
        goto _end;
×
1825
      }
1826
    }
1827
  }
1828
_end:
214,033✔
1829
  if (code != TSDB_CODE_SUCCESS) {
213,581✔
1830
    taosArrayDestroy(*ppTagVals);
×
1831
    *ppTagVals = NULL;
×
1832
  }
1833
  return code;
214,033✔
1834
}
1835

1836
static int32_t buildStreamSubTableCreateReq(SStreamRunnerTask* pTask, SDataInserterHandle* pInserter,
213,587✔
1837
                                            SStreamInserterParam* pInsertParam, SStreamDataInserterInfo* pInserterInfo,
1838
                                            SSubmitTbData* tbData) {
1839
  int32_t code = TSDB_CODE_SUCCESS;
213,587✔
1840
  STag*   pTag = NULL;
213,587✔
1841
  SArray* pTagVals = NULL;
214,033✔
1842
  SArray* TagNames = NULL;
214,033✔
1843

1844
  if (pInsertParam->pTagFields == NULL) {
214,033✔
1845
    ST_TASK_ELOG("buildStreamSubTableCreateReq, pTagFields is NULL, suid:%" PRId64 ", sver:%d", pInsertParam->suid,
×
1846
                 pInsertParam->sver);
1847
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1848
  }
1849
  if (pInserterInfo->pTagVals == NULL || pInserterInfo->pTagVals->size == 0) {
214,033✔
UNCOV
1850
    ST_TASK_ELOG("buildStreamSubTableCreateReq, pTagVals is NULL, suid:%" PRId64 ", sver:%d", pInsertParam->suid,
×
1851
                 pInsertParam->sver);
1852
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1853
  }
1854
  if (pInsertParam->suid <= 0 || pInsertParam->sver <= 0) {
214,033✔
UNCOV
1855
    ST_TASK_ELOG("buildStreamSubTableCreateReq, suid:%" PRId64
×
1856
                 ", sver:%d"
1857
                 " must be greater than 0",
1858
                 pInsertParam->suid, pInsertParam->sver);
1859
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1860
  }
1861
  int32_t nTags = pInserterInfo->pTagVals->size;
214,033✔
1862

1863
  TagNames = taosArrayInit(nTags, TSDB_COL_NAME_LEN);
213,828✔
1864
  if (!TagNames) {
214,033✔
1865
    code = terrno;
×
1866
    goto _end;
×
1867
  }
1868
  for (int32_t i = 0; i < nTags; ++i) {
567,816✔
1869
    SFieldWithOptions* pField = taosArrayGet(pInsertParam->pTagFields, i);
353,783✔
1870
    if (NULL == taosArrayPush(TagNames, pField->name)) {
707,566✔
1871
      code = terrno;
×
1872
      goto _end;
×
1873
    }
1874
  }
1875

1876
  tbData->flags |= (SUBMIT_REQ_AUTO_CREATE_TABLE | SUBMIT_REQ_SCHEMA_RES);
214,033✔
1877
  tbData->uid = 0;
214,033✔
1878
  tbData->suid = pInsertParam->suid;
214,033✔
1879
  tbData->sver = pInsertParam->sver;
213,828✔
1880

1881
  tbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
214,033✔
1882
  if (NULL == tbData->pCreateTbReq) {
213,792✔
1883
    code = terrno;
×
1884
    goto _end;
×
1885
  }
1886
  tbData->pCreateTbReq->type = TSDB_CHILD_TABLE;
213,792✔
1887
  tbData->pCreateTbReq->flags |= (TD_CREATE_SUB_TB_IN_STREAM | TD_CREATE_IF_NOT_EXISTS);
213,792✔
1888

1889
  code = getTagValsFromStreamInserterInfo(pInserterInfo, pInsertParam->pFields->size, &pTagVals, pInsertParam->tagCids);
214,033✔
1890
  if (code != TSDB_CODE_SUCCESS) {
214,033✔
1891
    goto _end;
×
1892
  }
1893

1894
  code = tTagNew(pTagVals, pInsertParam->sver, false, &pTag);
214,033✔
1895
  if (code != TSDB_CODE_SUCCESS) {
213,617✔
1896
    ST_TASK_ELOG("failed to create tag, error:%s", tstrerror(code));
×
1897
    goto _end;
×
1898
  }
1899
  code = inserterBuildCreateTbReq(tbData->pCreateTbReq, pInserterInfo->tbName, pTag, tbData->suid,
213,828✔
1900
                                  pInsertParam->stbname, TagNames, nTags, TSDB_DEFAULT_TABLE_TTL);
213,617✔
1901
  if (code != TSDB_CODE_SUCCESS) {
214,033✔
1902
    ST_TASK_ELOG("failed to build create table request, error:%s", tstrerror(code));
×
1903
    goto _end;
×
1904
  }
1905

1906
_end:
214,033✔
1907
  if (code != TSDB_CODE_SUCCESS) {
214,033✔
1908
    ST_TASK_ELOG("buildStreamSubTableCreateReq failed, error:%s", tstrerror(code));
×
1909
    if (tbData->pCreateTbReq) {
×
1910
      taosMemoryFreeClear(tbData->pCreateTbReq->name);
×
1911
      taosMemoryFreeClear(tbData->pCreateTbReq);
×
1912
    }
1913
    if (TagNames) {
×
1914
      taosArrayDestroy(TagNames);
×
1915
    }
1916
  }
1917

1918
  if (pTagVals) {
214,033✔
1919
    taosArrayDestroy(pTagVals);
213,792✔
1920
  }
1921
  return code;
214,033✔
1922
}
1923

1924
static int32_t appendInsertData(SStreamInserterParam* pInsertParam, const SSDataBlock* pDataBlock,
2,586,198✔
1925
                                SSubmitTbData* tbData, STSchema* pTSchema, SBuildInsertDataInfo* dataInsertInfo) {
1926
  int32_t code = TSDB_CODE_SUCCESS;
2,586,198✔
1927
  int32_t lino = 0;
2,586,198✔
1928

1929
  int32_t rows = pDataBlock ? pDataBlock->info.rows : 0;
2,586,198✔
1930
  int32_t numOfCols = pInsertParam->pFields->size;
2,586,198✔
1931
  int32_t colNum = pDataBlock ? taosArrayGetSize(pDataBlock->pDataBlock) : 0;
2,586,409✔
1932

1933
  SArray* pVals = NULL;
2,585,776✔
1934
  if (!(pVals = taosArrayInit(colNum, sizeof(SColVal)))) {
2,585,776✔
1935
    code = terrno;
×
1936
    QUERY_CHECK_CODE(code, lino, _end);
211✔
1937
  }
1938

1939
  for (int32_t j = 0; j < rows; ++j) {  // iterate by row
33,568,452✔
1940
    taosArrayClear(pVals);
30,981,621✔
1941

1942
    bool tsOrPrimaryKeyIsNull = false;
30,981,410✔
1943
    for (int32_t k = 0; k < numOfCols; ++k) {  // iterate by column
125,158,714✔
1944
      int16_t colIdx = k + 1;
94,200,896✔
1945

1946
      SFieldWithOptions* pCol = taosArrayGet(pInsertParam->pFields, k);
94,200,896✔
1947
      if (PRIMARYKEY_TIMESTAMP_COL_ID != colIdx && TSDB_DATA_TYPE_NULL == pCol->type) {
94,197,203✔
1948
        SColVal cv = COL_VAL_NULL(colIdx, pCol->type);
×
1949
        if (NULL == taosArrayPush(pVals, &cv)) {
×
1950
          code = terrno;
×
1951
          QUERY_CHECK_CODE(code, lino, _end);
×
1952
        }
1953
        continue;
×
1954
      }
1955

1956
      SColumnInfoData* pColInfoData = taosArrayGet(pDataBlock->pDataBlock, k);
94,196,178✔
1957
      if (NULL == pColInfoData) {
94,193,091✔
1958
        code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1959
        QUERY_CHECK_CODE(code, lino, _end);
×
1960
      }
1961
      void* var = POINTER_SHIFT(pColInfoData->pData, j * pColInfoData->info.bytes);
94,193,091✔
1962

1963
      if (colDataIsNull_s(pColInfoData, j) && (pCol->flags & COL_IS_KEY)) {
188,408,548✔
1964
        tsOrPrimaryKeyIsNull = true;
24,174✔
1965
        qDebug("Primary key column should not be null, skip this row");
24,174✔
1966
        break;
24,174✔
1967
      }
1968
      switch (pColInfoData->info.type) {
94,190,671✔
1969
        case TSDB_DATA_TYPE_NCHAR:
2,667,358✔
1970
        case TSDB_DATA_TYPE_VARBINARY:
1971
        case TSDB_DATA_TYPE_VARCHAR: {  // TSDB_DATA_TYPE_BINARY
1972
          if (pColInfoData->info.type != pCol->type) {
2,667,358✔
1973
            qError("tb:%s column:%d type:%d in block dismatch with schema col:%d type:%d", pInsertParam->tbname, k,
×
1974
                   pColInfoData->info.type, k, pCol->type);
1975
            code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1976
            QUERY_CHECK_CODE(code, lino, _end);
×
1977
          }
1978
          if (colDataIsNull_s(pColInfoData, j)) {
5,334,716✔
1979
            SColVal cv = COL_VAL_NULL(colIdx, pCol->type);
2,924✔
1980
            if (NULL == taosArrayPush(pVals, &cv)) {
2,924✔
1981
              code = terrno;
×
1982
              QUERY_CHECK_CODE(code, lino, _end);
×
1983
            }
1984
          } else {
1985
            if (pColInfoData->pData == NULL) {
2,664,434✔
1986
              qError("build insert tb:%s, column:%d data is NULL in block", pInsertParam->tbname, k);
×
1987
              code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1988
              QUERY_CHECK_CODE(code, lino, _end);
×
1989
            }
1990
            void*  data = colDataGetVarData(pColInfoData, j);
2,664,434✔
1991
            SValue sv = (SValue){
7,993,302✔
1992
                .type = pCol->type, .nData = varDataLen(data), .pData = varDataVal(data)};  // address copy, no value
2,664,434✔
1993
            SColVal cv = COL_VAL_VALUE(colIdx, sv);
2,664,434✔
1994
            if (NULL == taosArrayPush(pVals, &cv)) {
2,664,434✔
1995
              code = terrno;
×
1996
              QUERY_CHECK_CODE(code, lino, _end);
×
1997
            }
1998
          }
1999
          break;
2,667,358✔
2000
        }
2001
        case TSDB_DATA_TYPE_BLOB:
×
2002
        case TSDB_DATA_TYPE_JSON:
2003
        case TSDB_DATA_TYPE_MEDIUMBLOB:
2004
          qError("the column type %" PRIi16 " is defined but not implemented yet", pColInfoData->info.type);
×
2005
          code = TSDB_CODE_APP_ERROR;
×
2006
          QUERY_CHECK_CODE(code, lino, _end);
×
2007
          break;
×
2008
        default:
91,504,242✔
2009
          if (pColInfoData->info.type < TSDB_DATA_TYPE_MAX && pColInfoData->info.type > TSDB_DATA_TYPE_NULL) {
91,504,242✔
2010
            if (colDataIsNull_s(pColInfoData, j)) {
183,027,986✔
2011
              if (PRIMARYKEY_TIMESTAMP_COL_ID == colIdx) {
1,050,161✔
2012
                tsOrPrimaryKeyIsNull = true;
2,095✔
2013
                qDebug("Primary timestamp column should not be null, skip this row");
2,095✔
2014
                break;
2,095✔
2015
              }
2016

2017
              SColVal cv = COL_VAL_NULL(colIdx, pCol->type);  // should use pCol->type
1,048,066✔
2018
              if (NULL == taosArrayPush(pVals, &cv)) {
1,048,488✔
2019
                code = terrno;
×
2020
                QUERY_CHECK_CODE(code, lino, _end);
×
2021
              }
2022
            } else {
2023
              if (PRIMARYKEY_TIMESTAMP_COL_ID == colIdx && !dataInsertInfo->needSortMerge) {
90,473,360✔
2024
                if (*(int64_t*)var <= dataInsertInfo->lastTs) {
9,013,850✔
2025
                  dataInsertInfo->needSortMerge = true;
21,041✔
2026
                } else {
2027
                  dataInsertInfo->lastTs = *(int64_t*)var;
8,992,598✔
2028
                }
2029
              }
2030

2031
              SValue sv = {.type = pCol->type};
90,473,571✔
2032
              valueSetDatum(&sv, sv.type, var, tDataTypes[pCol->type].bytes);
90,457,587✔
2033
              SColVal cv = COL_VAL_VALUE(colIdx, sv);
90,459,432✔
2034
              if (NULL == taosArrayPush(pVals, &cv)) {
90,462,507✔
2035
                code = terrno;
×
2036
                QUERY_CHECK_CODE(code, lino, _end);
615✔
2037
              }
2038
            }
2039
          } else {
2040
            uError("the column type %" PRIi16 " is undefined\n", pColInfoData->info.type);
×
2041
            code = TSDB_CODE_APP_ERROR;
205✔
2042
            QUERY_CHECK_CODE(code, lino, _end);
205✔
2043
          }
2044
          break;
91,510,983✔
2045
      }
2046
      if (tsOrPrimaryKeyIsNull) break;  // skip remaining columns because the primary key is null
94,179,399✔
2047
    }
2048
    if (tsOrPrimaryKeyIsNull) continue;  // skip this row if primary key is null
30,984,087✔
2049
    SRow*             pRow = NULL;
30,957,818✔
2050
    SRowBuildScanInfo sinfo = {0};
30,955,985✔
2051
    if ((code = tRowBuild(pVals, pTSchema, &pRow, &sinfo)) != TSDB_CODE_SUCCESS) {
30,955,985✔
2052
      QUERY_CHECK_CODE(code, lino, _end);
×
2053
    }
2054
    if (NULL == taosArrayPush(tbData->aRowP, &pRow)) {
61,911,759✔
2055
      taosMemFree(pRow);
×
2056
      code = terrno;
×
2057
      QUERY_CHECK_CODE(code, lino, _end);
×
2058
    }
2059
  }
2060
  if (dataInsertInfo->isLastBlock) {
2,586,831✔
2061
    int32_t nRows = taosArrayGetSize(tbData->aRowP);
2,584,648✔
2062
    if (taosArrayGetSize(tbData->aRowP) == 0) {
2,584,437✔
2063
      tbData->flags |= SUBMIT_REQ_ONLY_CREATE_TABLE;
1,955,725✔
2064
      stDebug("no valid data to insert, try to only create tabale:%s", pInsertParam->tbname);
1,955,725✔
2065
    }
2066
    stDebug("appendInsertData, isLastBlock:%d, needSortMerge:%d, totalRows:%d", dataInsertInfo->isLastBlock,
2,584,437✔
2067
            dataInsertInfo->needSortMerge, nRows);
2068
    if (dataInsertInfo->needSortMerge) {
2,584,437✔
2069
      if ((tRowSort(tbData->aRowP) != TSDB_CODE_SUCCESS) ||
42,082✔
2070
          (code = tRowMerge(tbData->aRowP, (STSchema*)pTSchema, KEEP_CONSISTENCY)) != 0) {
21,041✔
2071
        QUERY_CHECK_CODE(code, lino, _end);
×
2072
      }
2073
    }
2074
    nRows = taosArrayGetSize(tbData->aRowP);
2,584,437✔
2075
    stDebug("appendInsertData, after merge, totalRows:%d", nRows);
2,584,437✔
2076
  }
2077

2078
_end:
144,686✔
2079
  taosArrayDestroy(pVals);
2,586,198✔
2080
  return code;
2,586,198✔
2081
}
2082

2083
int32_t buildStreamSubmitReqFromBlock(SStreamRunnerTask* pTask, SDataInserterHandle* pInserter,
2,586,198✔
2084
                                      SStreamDataInserterInfo* pInserterInfo, SSubmitReq2** ppReq,
2085
                                      const SSDataBlock* pDataBlock, SVgroupInfo* vgInfo,
2086
                                      SBuildInsertDataInfo* tbDataInfo) {
2087
  SSubmitReq2* pReq = *ppReq;
2,586,198✔
2088
  int32_t      numOfBlks = 0;
2,586,198✔
2089

2090
  int32_t               code = TSDB_CODE_SUCCESS;
2,586,198✔
2091
  int32_t               lino = 0;
2,586,198✔
2092
  SStreamInserterParam* pInsertParam = pInserter->pParam->streamInserterParam;
2,586,198✔
2093
  SInsertTableInfo**    ppTbInfo = NULL;
2,586,198✔
2094
  SInsertTableInfo*     pTbInfo = NULL;
2,585,776✔
2095
  STSchema*             pTSchema = NULL;
2,585,776✔
2096
  SSubmitTbData*        tbData = &tbDataInfo->pTbData;
2,585,776✔
2097
  int32_t               colNum = 0;
2,586,409✔
2098
  int32_t               rows = 0;
2,586,409✔
2099

2100
  if (NULL == pReq) {
2,586,409✔
2101
    if (!(pReq = taosMemoryCalloc(1, sizeof(SSubmitReq2)))) {
2,584,437✔
2102
      code = terrno;
×
2103
      QUERY_CHECK_CODE(code, lino, _end);
×
2104
    }
2105
    *ppReq = pReq;
2,584,437✔
2106

2107
    if (!(pReq->aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData)))) {
2,584,648✔
2108
      code = terrno;
×
2109
      QUERY_CHECK_CODE(code, lino, _end);
×
2110
    }
2111
  }
2112

2113
  if (pDataBlock) {
2,586,409✔
2114
    colNum = taosArrayGetSize(pDataBlock->pDataBlock);
632,568✔
2115
    rows = pDataBlock->info.rows;
632,568✔
2116
  }
2117

2118
  tbData->flags |= SUBMIT_REQ_SCHEMA_RES;
2,586,409✔
2119

2120
  if (tbDataInfo->isFirstBlock) {
2,586,198✔
2121
    if (pInserterInfo->isAutoCreateTable) {
2,584,226✔
2122
      code = initTableInfo(pInserter, pInserterInfo);
307,048✔
2123
      QUERY_CHECK_CODE(code, lino, _end);
307,048✔
2124
      if (pInsertParam->tbType == TSDB_NORMAL_TABLE) {
307,048✔
2125
        code = buildNormalTableCreateReq(pInserter, pInsertParam, tbData);
93,015✔
2126
      } else if (pInsertParam->tbType == TSDB_SUPER_TABLE) {
214,033✔
2127
        code = buildStreamSubTableCreateReq(pTask, pInserter, pInsertParam, pInserterInfo, tbData);
214,033✔
2128
      } else {
2129
        code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
2130
        ST_TASK_ELOG("buildStreamSubmitReqFromBlock, unknown table type %d", pInsertParam->tbType);
×
2131
      }
2132
      QUERY_CHECK_CODE(code, lino, _end);
307,048✔
2133
    }
2134
  }
2135

2136
  code = getStreamInsertTableInfo(pInserterInfo->streamId, pInserterInfo->groupId, &ppTbInfo);
2,586,831✔
2137
  QUERY_CHECK_CODE(code, lino, _end);
2,586,409✔
2138

2139
  pTbInfo =  *ppTbInfo;
2,586,409✔
2140
  if (tbDataInfo->isFirstBlock) {
2,586,409✔
2141
    if (!pInserterInfo->isAutoCreateTable) {
2,584,437✔
2142
      tstrncpy(pInserterInfo->tbName, pTbInfo->tbname, TSDB_TABLE_NAME_LEN);
2,277,389✔
2143
    }
2144

2145
    tbData->uid = pTbInfo->uid;
2,584,648✔
2146
    tbData->sver = pTbInfo->version;
2,584,648✔
2147

2148
    if (pInsertParam->tbType == TSDB_SUPER_TABLE) {
2,584,196✔
2149
      tbData->suid = pInsertParam->suid;
2,203,868✔
2150
    }
2151

2152
    pTSchema = pTbInfo->pSchema;
2,584,202✔
2153
  } else {
2154
    pTSchema = pTbInfo->pSchema;
1,761✔
2155
  }
2156

2157
  code = getTableVgInfo(pInserter, pInsertParam->dbFName, pTbInfo->tbname, vgInfo);
2,585,963✔
2158
  QUERY_CHECK_CODE(code, lino, _end);
2,586,409✔
2159

2160
  ST_TASK_DLOG("[data inserter], Handle:%p, GROUP:%" PRId64 " tbname:%s autoCreate:%d uid:%" PRId64 " suid:%" PRId64
2,586,409✔
2161
               " sver:%d vgid:%d isLastBlock:%d",
2162
               pInserter, pInserterInfo->groupId, pInserterInfo->tbName, pInserterInfo->isAutoCreateTable, tbData->uid,
2163
               tbData->suid, tbData->sver, vgInfo->vgId, tbDataInfo->isFirstBlock);
2164

2165
  code = appendInsertData(pInsertParam, pDataBlock, tbData, pTSchema, tbDataInfo);
2,586,409✔
2166
  QUERY_CHECK_CODE(code, lino, _end);
2,585,987✔
2167

2168
_end:
2,585,987✔
2169
  releaseStreamInsertTableInfo(ppTbInfo);
2,585,987✔
2170
  if (code != TSDB_CODE_SUCCESS) {
2,586,409✔
2171
    ST_TASK_ELOG("buildStreamSubmitReqFromBlock, code:0x%0x, groupId:%" PRId64 " tbname:%s autoCreate:%d", code,
×
2172
                 pInserterInfo->groupId, pInserterInfo->tbName, pInserterInfo->isAutoCreateTable);
2173
  }
2174
  return code;
2,586,409✔
2175
}
2176

2177
int32_t streamDataBlocksToSubmitReq(SStreamRunnerTask* pTask, SDataInserterHandle* pInserter,
2,584,196✔
2178
                                    SStreamDataInserterInfo* pInserterInfo, void** pMsg, int32_t* msgLen,
2179
                                    SVgroupInfo* vgInfo) {
2180
  int32_t code = 0;
2,584,196✔
2181
  int32_t lino = 0;
2,584,196✔
2182

2183
  const SArray*        pBlocks = pInserter->pDataBlocks;
2,584,196✔
2184
  int32_t              sz = taosArrayGetSize(pBlocks);
2,584,648✔
2185
  SSubmitReq2*         pReq = NULL;
2,584,648✔
2186
  SBuildInsertDataInfo tbDataInfo = {0};
2,584,648✔
2187

2188
  int32_t rows = 0;
2,584,437✔
2189
  for (int32_t i = 0; i < sz; i++) {
5,170,846✔
2190
    SSDataBlock* pDataBlock = taosArrayGetP(pBlocks, i);
2,586,198✔
2191
    if (NULL == pDataBlock) {
2,586,409✔
2192
      stDebug("data block is NULL, just create empty table");
1,953,630✔
2193
      continue;
1,953,630✔
2194
    }
2195
    rows += pDataBlock->info.rows;
632,779✔
2196
  }
2197
  code = initInsertProcessInfo(&tbDataInfo, rows);
2,584,648✔
2198
  if (code != TSDB_CODE_SUCCESS) {
2,584,437✔
2199
    ST_TASK_ELOG("streamDataBlocksToSubmitReq, initInsertDataInfo failed, code:%d", code);
×
2200
    return code;
×
2201
  }
2202

2203
  for (int32_t i = 0; i < sz; i++) {
5,170,846✔
2204
    tbDataInfo.isFirstBlock = (i == 0);
2,586,198✔
2205
    tbDataInfo.isLastBlock = (i == sz - 1);
2,586,198✔
2206
    SSDataBlock* pDataBlock = taosArrayGetP(pBlocks, i);  // pDataBlock select查询到的结果
2,586,198✔
2207
    ST_TASK_DLOG("[data inserter], Handle:%p, GROUP:%" PRId64
2,586,198✔
2208
            " tbname:%s autoCreate:%d block: %d/%d rows:%" PRId64,
2209
            pInserter, pInserterInfo->groupId, pInserterInfo->tbName,
2210
            pInserterInfo->isAutoCreateTable, i + 1, sz, (pDataBlock != NULL ? pDataBlock->info.rows : 0));
2211
    code = buildStreamSubmitReqFromBlock(pTask, pInserter, pInserterInfo, &pReq, pDataBlock, vgInfo, &tbDataInfo);
2,586,198✔
2212
    QUERY_CHECK_CODE(code, lino, _end);
2,586,409✔
2213
  }
2214

2215
  if (NULL == taosArrayPush(pReq->aSubmitTbData, &tbDataInfo.pTbData)) {
5,169,296✔
2216
    code = terrno;
×
2217
    QUERY_CHECK_CODE(code, lino, _end);
×
2218
  }
2219

2220
  code = submitReqToMsg(vgInfo->vgId, pReq, pMsg, msgLen);
2,584,648✔
2221
  tDestroySubmitReq(pReq, TSDB_MSG_FLG_ENCODE);
2,583,097✔
2222
  taosMemoryFree(pReq);
2,583,745✔
2223
  ST_TASK_DLOG("[data inserter], submit req, vgid:%d, GROUP:%" PRId64 " tbname:%s autoCreate:%d code:%d ", vgInfo->vgId,
2,582,883✔
2224
               pInserterInfo->groupId, pInserterInfo->tbName, pInserterInfo->isAutoCreateTable, code);
2225

2226
_end:
2,581,958✔
2227
  if (code != 0) {
2,584,437✔
2228
    tDestroySubmitTbData(&tbDataInfo.pTbData, TSDB_MSG_FLG_ENCODE);
×
2229
    tDestroySubmitReq(pReq, TSDB_MSG_FLG_ENCODE);
×
2230
    taosMemoryFree(pReq);
×
2231
  }
2232

2233
  return code;
2,584,018✔
2234
}
2235

2236
static int32_t putDataBlock(SDataSinkHandle* pHandle, const SInputData* pInput, bool* pContinue) {
69,751✔
2237
  SDataInserterHandle* pInserter = (SDataInserterHandle*)pHandle;
69,751✔
2238
  if (!pInserter->explain) {
69,751✔
2239
    if (NULL == taosArrayPush(pInserter->pDataBlocks, &pInput->pData)) {
130,682✔
2240
      return terrno;
×
2241
    }
2242
    if (pInserter->isStbInserter) {
65,341✔
2243
      SArray* pMsgs = taosArrayInit(4, sizeof(POINTER_BYTES));
3,360✔
2244
      if (NULL == pMsgs) {
3,360✔
2245
        return terrno;
×
2246
      }
2247
      int32_t code = dataBlocksToSubmitReqArray(pInserter, pMsgs);
3,360✔
2248
      if (code) {
3,360✔
2249
        taosArrayDestroyP(pMsgs, destroySSubmitTbDataSendInfo);
×
2250
        return code;
×
2251
      }
2252
      taosArrayClear(pInserter->pDataBlocks);
3,360✔
2253
      for (int32_t i = 0; i < taosArrayGetSize(pMsgs); ++i) {
9,520✔
2254
        SSubmitTbDataSendInfo* pSendInfo = taosArrayGetP(pMsgs, i);
6,160✔
2255
        code = sendSubmitRequest(pInserter, NULL, pSendInfo->msg.pData, pSendInfo->msg.len,
12,320✔
2256
                                 pInserter->pParam->readHandle->pMsgCb->clientRpc, &pSendInfo->epSet);
6,160✔
2257
        taosMemoryFree(pSendInfo);
6,160✔
2258
        if (code) {
6,160✔
2259
          for (int j = i + 1; j < taosArrayGetSize(pMsgs); ++j) {
×
2260
            SSubmitTbDataSendInfo* pSendInfo2 = taosArrayGetP(pMsgs, j);
×
2261
            destroySSubmitTbDataSendInfo(pSendInfo2);
×
2262
          }
2263
          taosArrayDestroy(pMsgs);
×
2264
          return code;
×
2265
        }
2266
        QRY_ERR_RET(tsem_wait(&pInserter->ready));
6,160✔
2267

2268
        if (pInserter->submitRes.code) {
6,160✔
2269
          for (int j = i + 1; j < taosArrayGetSize(pMsgs); ++j) {
×
2270
            SSubmitTbDataSendInfo* pSendInfo2 = taosArrayGetP(pMsgs, j);
×
2271
            destroySSubmitTbDataSendInfo(pSendInfo2);
×
2272
          }
2273
          taosArrayDestroy(pMsgs);
×
2274
          return pInserter->submitRes.code;
×
2275
        }
2276
      }
2277

2278
      taosArrayDestroy(pMsgs);
3,360✔
2279

2280
    } else {
2281
      void*   pMsg = NULL;
61,981✔
2282
      int32_t msgLen = 0;
61,981✔
2283
      int32_t code = dataBlocksToSubmitReq(pInserter, &pMsg, &msgLen);
61,981✔
2284
      if (code) {
61,981✔
2285
        return code;
7,272✔
2286
      }
2287

2288
      taosArrayClear(pInserter->pDataBlocks);
54,709✔
2289

2290
      code = sendSubmitRequest(pInserter, NULL, pMsg, msgLen, pInserter->pParam->readHandle->pMsgCb->clientRpc,
54,709✔
2291
                               &pInserter->pNode->epSet);
54,709✔
2292
      if (code) {
54,709✔
2293
        return code;
×
2294
      }
2295

2296
      QRY_ERR_RET(tsem_wait(&pInserter->ready));
54,709✔
2297

2298
      if (pInserter->submitRes.code) {
54,709✔
2299
        return pInserter->submitRes.code;
×
2300
      }
2301
    }
2302
  }
2303

2304
  *pContinue = true;
62,479✔
2305

2306
  return TSDB_CODE_SUCCESS;
62,479✔
2307
}
2308

2309
static int32_t resetInserterTbVersion(SDataInserterHandle* pInserter, const SInputData* pInput) {
21,553✔
2310
  SInsertTableInfo** ppTbInfo = NULL;
21,553✔
2311
  int32_t           code = getStreamInsertTableInfo(pInput->pStreamDataInserterInfo->streamId, pInput->pStreamDataInserterInfo->groupId, &ppTbInfo);
21,553✔
2312
  if (code != TSDB_CODE_SUCCESS) {
21,553✔
2313
    return code;
×
2314
  }
2315

2316
  SInsertTableInfo*  pTbInfo  = *ppTbInfo;
21,553✔
2317
  stDebug("resetInserterTbVersion, streamId:0x%" PRIx64 " groupId:%" PRId64 " tbName:%s, uid:%" PRId64 ", version:%d",
21,553✔
2318
          pInput->pStreamDataInserterInfo->streamId, pInput->pStreamDataInserterInfo->groupId,
2319
          pInput->pStreamDataInserterInfo->tbName, pTbInfo->uid, pTbInfo->version);
2320
  if (pInserter->pParam->streamInserterParam->tbType != TSDB_NORMAL_TABLE) {
21,553✔
2321
    pInserter->pParam->streamInserterParam->sver = pTbInfo->version;
844✔
2322
  }
2323
  code = releaseStreamInsertTableInfo(ppTbInfo);
21,553✔
2324
  return code;
21,553✔
2325
}
2326

2327
static int32_t putStreamDataBlock(SDataSinkHandle* pHandle, const SInputData* pInput, bool* pContinue) {
2,562,643✔
2328
  int32_t              code = 0;
2,562,643✔
2329
  int32_t              lino = 0;
2,562,643✔
2330
  SDataInserterHandle* pInserter = (SDataInserterHandle*)pHandle;
2,562,643✔
2331
  SStreamRunnerTask*   pTask = pInput->pTask;
2,562,643✔
2332
  if (!pInserter || !pInserter->pParam || !pInserter->pParam->streamInserterParam) {
2,563,095✔
2333
    ST_TASK_ELOG("putStreamDataBlock invalid param, pInserter: %p, pParam:%p", pInserter,
422✔
2334
                 pInserter ? pInserter->pParam : NULL);
2335
    return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
2336
  }
2337
  if (!pInserter->explain) {
2,563,095✔
2338
    code = TSDB_CODE_SUCCESS;
2,563,095✔
2339
    if (NULL == taosArrayPush(pInserter->pDataBlocks, &pInput->pData)) {
5,126,190✔
2340
      return terrno;
×
2341
    }
2342
    void*       pMsg = NULL;
2,563,095✔
2343
    int32_t     msgLen = 0;
2,563,095✔
2344
    SVgroupInfo vgInfo = {0};
2,563,095✔
2345

2346
    code = streamDataBlocksToSubmitReq(pTask, pInserter, pInput->pStreamDataInserterInfo, &pMsg, &msgLen, &vgInfo);
2,563,095✔
2347
    QUERY_CHECK_CODE(code, lino, _return);
2,562,673✔
2348

2349
    code = sendSubmitRequest(pInserter, pInput->pStreamDataInserterInfo, pMsg, msgLen,
2,562,465✔
2350
                             pInserter->pParam->readHandle->pMsgCb->clientRpc, &vgInfo.epSet);
2,562,673✔
2351
    QUERY_CHECK_CODE(code, lino, _return);
2,563,095✔
2352

2353
    code = tsem_wait(&pInserter->ready);
2,563,095✔
2354
    QUERY_CHECK_CODE(code, lino, _return);
2,563,095✔
2355

2356
    if (pInserter->submitRes.code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
2,563,095✔
2357
      pInput->pStreamDataInserterInfo->isAutoCreateTable = false;
21,553✔
2358
      code = resetInserterTbVersion(pInserter, pInput);
21,553✔
2359
      QUERY_CHECK_CODE(code, lino, _return);
21,553✔
2360

2361
      code = streamDataBlocksToSubmitReq(pTask, pInserter, pInput->pStreamDataInserterInfo, &pMsg, &msgLen, &vgInfo);
21,553✔
2362
      QUERY_CHECK_CODE(code, lino, _return);
21,553✔
2363

2364
      code = sendSubmitRequest(pInserter, pInput->pStreamDataInserterInfo, pMsg, msgLen,
21,553✔
2365
                               pInserter->pParam->readHandle->pMsgCb->clientRpc, &vgInfo.epSet);
21,553✔
2366
      QUERY_CHECK_CODE(code, lino, _return);
21,553✔
2367

2368
      code = tsem_wait(&pInserter->ready);
21,553✔
2369
      QUERY_CHECK_CODE(code, lino, _return);
21,553✔
2370
    }
2371

2372
    if (pInput->pStreamDataInserterInfo->isAutoCreateTable &&
2,563,095✔
2373
        pInserter->submitRes.code == TSDB_CODE_VND_INVALID_VGROUP_ID) {
285,495✔
2374
      rmDbVgInfoFromCache(pInserter->pParam->streamInserterParam->dbFName);
1,761✔
2375
      ST_TASK_ILOG("putStreamDataBlock, stream inserter table info not found, groupId:%" PRId64
1,761✔
2376
                   ", tbName:%s. so reset dbVgInfo and try again",
2377
                   pInput->pStreamDataInserterInfo->groupId, pInput->pStreamDataInserterInfo->tbName);
2378
      return putStreamDataBlock(pHandle, pInput, pContinue);
1,761✔
2379
    }
2380

2381
    if ((pInserter->submitRes.code == TSDB_CODE_TDB_TABLE_NOT_EXIST &&
2,561,334✔
2382
         !pInput->pStreamDataInserterInfo->isAutoCreateTable) ||
606✔
2383
        pInserter->submitRes.code == TSDB_CODE_VND_INVALID_VGROUP_ID) {
2,560,728✔
2384
      rmDbVgInfoFromCache(pInserter->pParam->streamInserterParam->dbFName);
606✔
2385
      ST_TASK_ILOG("putStreamDataBlock, stream inserter table info not found, groupId:%" PRId64
606✔
2386
                   ", tbName:%s. so reset dbVgInfo",
2387
                   pInput->pStreamDataInserterInfo->groupId, pInput->pStreamDataInserterInfo->tbName);
2388
      code = TSDB_CODE_STREAM_INSERT_TBINFO_NOT_FOUND;
606✔
2389
      QUERY_CHECK_CODE(code, lino, _return);
606✔
2390
    }
2391

2392
    if (pInserter->submitRes.code) {
2,560,728✔
2393
      code = pInserter->submitRes.code;
927✔
2394
      ST_TASK_ELOG("submitRes err:%s, code:%0x", tstrerror(pInserter->submitRes.code), pInserter->submitRes.code);
927✔
2395
      QUERY_CHECK_CODE(code, lino, _return);
927✔
2396
    }
2397

2398
    *pContinue = true;
2,559,801✔
2399

2400
  _return:
2,561,334✔
2401
    taosArrayClear(pInserter->pDataBlocks);
2,561,334✔
2402
    if (code == TSDB_CODE_STREAM_NO_DATA) {
2,561,334✔
2403
      ST_TASK_DLOG("putStreamDataBlock, no valid data to insert, skip this block, groupID:%" PRId64,
×
2404
                   pInput->pStreamDataInserterInfo->groupId);
2405
      code = TSDB_CODE_SUCCESS;
×
2406
    } else if (code) {
2,561,334✔
2407
      ST_TASK_ELOG("submitRes err:%s, code:%0x lino:%d", tstrerror(code), code, lino);
1,533✔
2408
      return code;
1,533✔
2409
    }
2410
    return code;
2,559,801✔
2411
  }
2412
  return TSDB_CODE_SUCCESS;
×
2413
}
2414

2415
static void endPut(struct SDataSinkHandle* pHandle, uint64_t useconds) {
60,239✔
2416
  SDataInserterHandle* pInserter = (SDataInserterHandle*)pHandle;
60,239✔
2417
  (void)taosThreadMutexLock(&pInserter->mutex);
60,239✔
2418
  pInserter->queryEnd = true;
60,239✔
2419
  pInserter->useconds = useconds;
60,239✔
2420
  (void)taosThreadMutexUnlock(&pInserter->mutex);
60,239✔
2421
}
60,239✔
2422

2423
static void getDataLength(SDataSinkHandle* pHandle, int64_t* pLen, int64_t* pRawLen, bool* pQueryEnd) {
60,239✔
2424
  SDataInserterHandle* pDispatcher = (SDataInserterHandle*)pHandle;
60,239✔
2425
  *pLen = pDispatcher->submitRes.affectedRows;
60,239✔
2426
  qDebug("got total affectedRows %" PRId64, *pLen);
60,239✔
2427
}
60,239✔
2428

2429
static int32_t destroyDataSinker(SDataSinkHandle* pHandle) {
268,000✔
2430
  SDataInserterHandle* pInserter = (SDataInserterHandle*)pHandle;
268,000✔
2431
  (void)atomic_sub_fetch_64(&gDataSinkStat.cachedSize, pInserter->cachedSize);
268,000✔
2432
  taosArrayDestroy(pInserter->pDataBlocks);
268,000✔
2433
  taosMemoryFree(pInserter->pSchema);
268,000✔
2434
  if (pInserter->pParam->streamInserterParam) {
268,000✔
2435
    destroyStreamInserterParam(pInserter->pParam->streamInserterParam);
199,929✔
2436
    taosMemoryFree(pInserter->pParam->readHandle); // only for stream
199,929✔
2437
  }
2438
  taosMemoryFree(pInserter->pParam);
268,000✔
2439
  taosHashCleanup(pInserter->pCols);
268,000✔
2440
  nodesDestroyNode((SNode*)pInserter->pNode);
268,000✔
2441
  pInserter->pNode = NULL;
268,000✔
2442

2443
  (void)taosThreadMutexDestroy(&pInserter->mutex);
268,000✔
2444

2445
  taosMemoryFree(pInserter->pManager);
268,000✔
2446

2447
  if (pInserter->dbVgInfoMap) {
268,000✔
2448
    taosHashSetFreeFp(pInserter->dbVgInfoMap, freeUseDbOutput_tmp);
3,360✔
2449
    taosHashCleanup(pInserter->dbVgInfoMap);
3,360✔
2450
  }
2451

2452
  if (pInserter->pTagSchema) {
268,000✔
2453
    taosMemoryFreeClear(pInserter->pTagSchema->pSchema);
3,920✔
2454
    taosMemoryFree(pInserter->pTagSchema);
3,920✔
2455
  }
2456

2457
  return TSDB_CODE_SUCCESS;
268,000✔
2458
}
2459

2460
static int32_t getCacheSize(struct SDataSinkHandle* pHandle, uint64_t* size) {
×
2461
  SDataInserterHandle* pDispatcher = (SDataInserterHandle*)pHandle;
×
2462

2463
  *size = atomic_load_64(&pDispatcher->cachedSize);
×
2464
  return TSDB_CODE_SUCCESS;
×
2465
}
2466

2467
static int32_t getSinkFlags(struct SDataSinkHandle* pHandle, uint64_t* pFlags) {
67,511✔
2468
  SDataInserterHandle* pDispatcher = (SDataInserterHandle*)pHandle;
67,511✔
2469

2470
  *pFlags = atomic_load_64(&pDispatcher->flags);
67,511✔
2471
  return TSDB_CODE_SUCCESS;
67,511✔
2472
}
2473

2474
int32_t createDataInserter(SDataSinkManager* pManager, SDataSinkNode** ppDataSink, DataSinkHandle* pHandle,
68,071✔
2475
                           void* pParam) {
2476
  SDataSinkNode*       pDataSink = *ppDataSink;
68,071✔
2477
  SDataInserterHandle* inserter = taosMemoryCalloc(1, sizeof(SDataInserterHandle));
68,071✔
2478
  if (NULL == inserter) {
68,071✔
2479
    taosMemoryFree(pParam);
×
2480
    goto _return;
×
2481
  }
2482

2483
  SQueryInserterNode* pInserterNode = (SQueryInserterNode*)pDataSink;
68,071✔
2484
  inserter->sink.fPut = putDataBlock;
68,071✔
2485
  inserter->sink.fEndPut = endPut;
68,071✔
2486
  inserter->sink.fGetLen = getDataLength;
68,071✔
2487
  inserter->sink.fGetData = NULL;
68,071✔
2488
  inserter->sink.fDestroy = destroyDataSinker;
68,071✔
2489
  inserter->sink.fGetCacheSize = getCacheSize;
68,071✔
2490
  inserter->sink.fGetFlags = getSinkFlags;
68,071✔
2491
  inserter->pManager = pManager;
68,071✔
2492
  inserter->pNode = pInserterNode;
68,071✔
2493
  inserter->pParam = pParam;
68,071✔
2494
  inserter->status = DS_BUF_EMPTY;
68,071✔
2495
  inserter->queryEnd = false;
68,071✔
2496
  inserter->explain = pInserterNode->explain;
68,071✔
2497
  *ppDataSink = NULL;
68,071✔
2498

2499
  int64_t suid = 0;
68,071✔
2500
  int32_t code = pManager->pAPI->metaFn.getTableSchema(inserter->pParam->readHandle->vnode, pInserterNode->tableId,
68,071✔
2501
                                                       &inserter->pSchema, &suid, &inserter->pTagSchema);
2502
  if (code) {
68,071✔
2503
    terrno = code;
×
2504
    goto _return;
×
2505
  }
2506

2507
  pManager->pAPI->metaFn.getBasicInfo(inserter->pParam->readHandle->vnode, &inserter->dbFName, NULL, NULL, NULL);
68,071✔
2508

2509
  if (pInserterNode->tableType == TSDB_SUPER_TABLE) {
68,071✔
2510
    inserter->isStbInserter = true;
3,920✔
2511
  }
2512

2513
  if (pInserterNode->stableId != suid) {
68,071✔
2514
    terrno = TSDB_CODE_TDB_INVALID_TABLE_ID;
×
2515
    goto _return;
×
2516
  }
2517

2518
  inserter->pDataBlocks = taosArrayInit(1, POINTER_BYTES);
68,071✔
2519
  if (NULL == inserter->pDataBlocks) {
68,071✔
2520
    goto _return;
×
2521
  }
2522
  QRY_ERR_JRET(taosThreadMutexInit(&inserter->mutex, NULL));
68,071✔
2523

2524
  inserter->fullOrderColList = pInserterNode->pCols->length == inserter->pSchema->numOfCols;
68,071✔
2525

2526
  inserter->pCols = taosHashInit(pInserterNode->pCols->length, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT),
68,071✔
2527
                                 false, HASH_NO_LOCK);
2528
  if (NULL == inserter->pCols) {
68,071✔
2529
    goto _return;
×
2530
  }
2531

2532
  SNode*  pNode = NULL;
68,071✔
2533
  int32_t i = 0;
68,071✔
2534
  bool    foundTbname = false;
68,071✔
2535
  FOREACH(pNode, pInserterNode->pCols) {
282,437✔
2536
    if (pNode->type == QUERY_NODE_FUNCTION && ((SFunctionNode*)pNode)->funcType == FUNCTION_TYPE_TBNAME) {
214,366✔
2537
      int16_t colId = 0;
3,360✔
2538
      int16_t slotId = 0;
3,360✔
2539
      QRY_ERR_JRET(taosHashPut(inserter->pCols, &colId, sizeof(colId), &slotId, sizeof(slotId)));
3,360✔
2540
      foundTbname = true;
3,360✔
2541
      continue;
3,360✔
2542
    }
2543
    SColumnNode* pCol = (SColumnNode*)pNode;
211,006✔
2544
    QRY_ERR_JRET(taosHashPut(inserter->pCols, &pCol->colId, sizeof(pCol->colId), &pCol->slotId, sizeof(pCol->slotId)));
211,006✔
2545
    if (inserter->fullOrderColList && pCol->colId != inserter->pSchema->columns[i].colId) {
211,006✔
2546
      inserter->fullOrderColList = false;
1,120✔
2547
    }
2548
    ++i;
211,006✔
2549
  }
2550

2551
  if (inserter->isStbInserter && !foundTbname) {
68,071✔
2552
    QRY_ERR_JRET(TSDB_CODE_PAR_TBNAME_ERROR);
560✔
2553
  }
2554

2555
  QRY_ERR_JRET(tsem_init(&inserter->ready, 0, 0));
67,511✔
2556

2557
  inserter->dbVgInfoMap = NULL;
67,511✔
2558

2559
  *pHandle = inserter;
67,511✔
2560
  return TSDB_CODE_SUCCESS;
67,511✔
2561

2562
_return:
560✔
2563

2564
  if (inserter) {
560✔
2565
    (void)destroyDataSinker((SDataSinkHandle*)inserter);
560✔
2566
    taosMemoryFree(inserter);
560✔
2567
  } else {
2568
    taosMemoryFree(pManager);
×
2569
  }
2570

2571
  nodesDestroyNode((SNode*)*ppDataSink);
560✔
2572
  *ppDataSink = NULL;
560✔
2573

2574
  return terrno;
560✔
2575
}
2576

2577
                           
2578
static TdThreadOnce g_dbVgInfoMgrInit = PTHREAD_ONCE_INIT;
2579

2580
SDBVgInfoMgr g_dbVgInfoMgr = {0};
2581
                           
2582
void dbVgInfoMgrInitOnce() {
14,373✔
2583
  g_dbVgInfoMgr.dbVgInfoMap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
14,373✔
2584
  if (g_dbVgInfoMgr.dbVgInfoMap == NULL) {
14,373✔
2585
    stError("%s failed at line %d, error:%s", __FUNCTION__, __LINE__, tstrerror(terrno));
×
2586
    return;
×
2587
  }
2588

2589
  taosHashSetFreeFp(g_dbVgInfoMgr.dbVgInfoMap, freeUseDbOutput_tmp);
14,373✔
2590
}
2591

2592

2593

2594
int32_t createStreamDataInserter(SDataSinkManager* pManager, DataSinkHandle* pHandle, void* pParam) {
199,929✔
2595
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
199,929✔
2596

2597
  TAOS_UNUSED(taosThreadOnce(&g_dbVgInfoMgrInit, dbVgInfoMgrInitOnce));
199,929✔
2598
  TSDB_CHECK_NULL(g_dbVgInfoMgr.dbVgInfoMap, code, lino, _exit, terrno);
199,929✔
2599

2600
  SDataInserterHandle* inserter = taosMemoryCalloc(1, sizeof(SDataInserterHandle));
199,929✔
2601
  TSDB_CHECK_NULL(inserter, code, lino, _exit, terrno);
199,929✔
2602

2603
  inserter->sink.fPut = putStreamDataBlock;
199,929✔
2604
  inserter->sink.fEndPut = endPut;
199,929✔
2605
  inserter->sink.fGetLen = getDataLength;
199,929✔
2606
  inserter->sink.fGetData = NULL;
199,929✔
2607
  inserter->sink.fDestroy = destroyDataSinker;
199,929✔
2608
  inserter->sink.fGetCacheSize = getCacheSize;
199,929✔
2609
  inserter->sink.fGetFlags = getSinkFlags;
199,929✔
2610
  inserter->pManager = pManager;
199,929✔
2611
  inserter->pNode = NULL;
199,724✔
2612
  inserter->pParam = pParam;
199,724✔
2613
  inserter->status = DS_BUF_EMPTY;
199,929✔
2614
  inserter->queryEnd = false;
199,929✔
2615
  inserter->explain = false;
199,724✔
2616

2617
  inserter->pDataBlocks = taosArrayInit(1, POINTER_BYTES);
199,929✔
2618
  TSDB_CHECK_NULL(inserter->pDataBlocks, code, lino, _exit, terrno);
199,929✔
2619
  
2620
  TAOS_CHECK_EXIT(taosThreadMutexInit(&inserter->mutex, NULL));
199,929✔
2621
  TAOS_CHECK_EXIT(tsem_init(&inserter->ready, 0, 0));
199,929✔
2622

2623
  inserter->dbVgInfoMap = NULL;
199,929✔
2624

2625
  *pHandle = inserter;
199,929✔
2626
  return TSDB_CODE_SUCCESS;
199,929✔
2627

2628
_exit:
×
2629

2630
  if (inserter) {
×
2631
    (void)destroyDataSinker((SDataSinkHandle*)inserter);
×
2632
    taosMemoryFree(inserter);
×
2633
  } else {
2634
    taosMemoryFree(pManager);
×
2635
  }
2636

2637
  if (code) {
×
2638
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2639
  }
2640

2641
  return code;
×
2642
}
2643

2644
int32_t getDbVgInfoByTbName(void* clientRpc, const char* dbFName, SDBVgInfo** dbVgInfo) {
2,586,650✔
2645
  int32_t       code = TSDB_CODE_SUCCESS;
2,586,650✔
2646
  int32_t       line = 0;
2,586,650✔
2647
  SUseDbOutput* output = NULL;
2,586,650✔
2648

2649
  SUseDbOutput** find = (SUseDbOutput**)taosHashGet(g_dbVgInfoMgr.dbVgInfoMap, dbFName, strlen(dbFName));
2,586,650✔
2650

2651
  if (find == NULL) {
2,586,408✔
2652
    output = taosMemoryCalloc(1, sizeof(SUseDbOutput));
36,479✔
2653
    if (output == NULL) {
36,479✔
2654
      return TSDB_CODE_OUT_OF_MEMORY;
×
2655
    }
2656

2657
    code = buildDbVgInfoMap(clientRpc, dbFName, output);
36,479✔
2658
    QUERY_CHECK_CODE(code, line, _return);
36,479✔
2659

2660
    code = taosHashPut(g_dbVgInfoMgr.dbVgInfoMap, dbFName, strlen(dbFName), &output, POINTER_BYTES);
36,479✔
2661
    if (code == TSDB_CODE_DUP_KEY) {
36,479✔
2662
      code = TSDB_CODE_SUCCESS;
4,768✔
2663
      // another thread has put the same dbFName, so we need to free the output
2664
      freeUseDbOutput_tmp(&output);
4,768✔
2665
      find = (SUseDbOutput**)taosHashGet(g_dbVgInfoMgr.dbVgInfoMap, dbFName, strlen(dbFName));
4,768✔
2666
      if (find == NULL) {
4,768✔
2667
        QUERY_CHECK_CODE(code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR, line, _return);
×
2668
      }
2669
      output = *find;
4,768✔
2670
    }
2671
    QUERY_CHECK_CODE(code, line, _return);
36,479✔
2672
  } else {
2673
    output = *find;
2,549,929✔
2674
  }
2675

2676
  *dbVgInfo = output->dbVgroup;
2,586,197✔
2677
  return code;
2,585,986✔
2678

2679
_return:
×
2680
  qError("%s failed at line %d since %s", __func__, line, tstrerror(code));
×
2681
  freeUseDbOutput_tmp(&output);
×
2682
  return code;
×
2683
}
2684

2685
int32_t getDbVgInfoForExec(void* clientRpc, const char* dbFName, const char* tbName, SVgroupInfo* pVgInfo) {
2,586,650✔
2686
  SDBVgInfo* dbInfo = NULL;
2,586,650✔
2687
  int32_t code = 0, lino = 0;
2,586,650✔
2688
  char tbFullName[TSDB_TABLE_FNAME_LEN];
2,584,798✔
2689
  snprintf(tbFullName, TSDB_TABLE_FNAME_LEN, "%s.%s", dbFName, tbName);
2,586,650✔
2690
  
2691
  taosRLockLatch(&g_dbVgInfoMgr.lock);
2,586,650✔
2692
  
2693
  TAOS_CHECK_EXIT(getDbVgInfoByTbName(clientRpc, dbFName, &dbInfo));
2,586,650✔
2694

2695
  TAOS_CHECK_EXIT(inserterGetVgInfo(dbInfo, tbFullName, pVgInfo));
2,585,986✔
2696

2697
_exit:
2,586,228✔
2698

2699
  taosRUnLockLatch(&g_dbVgInfoMgr.lock);
2,586,439✔
2700

2701
  if (code) {
2,586,650✔
2702
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2703
  }
2704

2705
  return code;
2,586,650✔
2706
}
2707

2708
void rmDbVgInfoFromCache(const char* dbFName) {
2,367✔
2709
  taosWLockLatch(&g_dbVgInfoMgr.lock);
2,367✔
2710

2711
  TAOS_UNUSED(taosHashRemove(g_dbVgInfoMgr.dbVgInfoMap, dbFName, strlen(dbFName)));
2,367✔
2712

2713
  taosWUnLockLatch(&g_dbVgInfoMgr.lock);
2,367✔
2714
}
2,367✔
2715

2716
static int32_t dropTableReqToMsg(int32_t vgId, SVDropTbBatchReq* pReq, void** pData, int32_t* pLen) {
241✔
2717
  int32_t code = TSDB_CODE_SUCCESS;
241✔
2718
  int32_t len = 0;
241✔
2719
  void*   pBuf = NULL;
241✔
2720
  tEncodeSize(tEncodeSVDropTbBatchReq, pReq, len, code);
241✔
2721
  if (TSDB_CODE_SUCCESS == code) {
241✔
2722
    SEncoder encoder;
241✔
2723
    len += sizeof(SMsgHead);
241✔
2724
    pBuf = taosMemoryMalloc(len);
241✔
2725
    if (NULL == pBuf) {
241✔
2726
      return terrno;
×
2727
    }
2728
    ((SDropTbDataMsg*)pBuf)->header.vgId = htonl(vgId);
241✔
2729
    ((SDropTbDataMsg*)pBuf)->header.contLen = htonl(len);
241✔
2730
    //((SDropTbDataMsg*)pBuf)->pData = POINTER_SHIFT(pBuf, sizeof(SMsgHead));
2731
    tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SMsgHead)), len - sizeof(SMsgHead));
241✔
2732
    code = tEncodeSVDropTbBatchReq(&encoder, pReq);
241✔
2733
    tEncoderClear(&encoder);
241✔
2734
  }
2735

2736
  if (TSDB_CODE_SUCCESS == code) {
241✔
2737
    *pData = pBuf;
241✔
2738
    *pLen = len;
241✔
2739
  } else {
2740
    taosMemoryFree(pBuf);
×
2741
  }
2742

2743
  return code;
241✔
2744
}
2745

2746
int32_t dropTbCallback(void* param, SDataBuf* pMsg, int32_t code) {
241✔
2747
  SDropTbCtx* pCtx = (SDropTbCtx*)param;
241✔
2748
  if (code) {
241✔
2749
    stError("dropTbCallback, code:%d, stream:%" PRId64 " gid:%" PRId64, code, pCtx->req->streamId, pCtx->req->gid);
×
2750
  }
2751
  pCtx->code = code;
241✔
2752
  code = tsem_post(&pCtx->ready);
241✔
2753
  taosMemoryFree(pMsg->pData);
241✔
2754

2755
  return TSDB_CODE_SUCCESS;
241✔
2756
}
2757

2758
static int32_t sendDropTbRequest(SDropTbCtx* ctx, void* pMsg, int32_t msgLen, void* pTransporter, SEpSet* pEpset) {
241✔
2759
  // send the fetch remote task result reques
2760
  SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
241✔
2761
  if (NULL == pMsgSendInfo) {
241✔
2762
    return terrno;
×
2763
  }
2764

2765
  pMsgSendInfo->param = ctx;
241✔
2766
  pMsgSendInfo->paramFreeFp = NULL;
241✔
2767
  pMsgSendInfo->msgInfo.pData = pMsg;
241✔
2768
  pMsgSendInfo->msgInfo.len = msgLen;
241✔
2769
  pMsgSendInfo->msgType = TDMT_VND_SNODE_DROP_TABLE;
241✔
2770
  pMsgSendInfo->fp = dropTbCallback;
241✔
2771

2772
  return asyncSendMsgToServer(pTransporter, pEpset, NULL, pMsgSendInfo);
241✔
2773
}
2774

2775
int32_t doDropStreamTable(SMsgCb* pMsgCb, void* pTaskOutput, SSTriggerDropRequest* pReq) {
241✔
2776
  SStreamRunnerTaskOutput* pOutput = pTaskOutput;
241✔
2777
  int32_t                  code = 0;
241✔
2778
  int32_t                  lino = 0;
241✔
2779
  SVDropTbBatchReq         req = {.nReqs = 1};
241✔
2780
  SVDropTbReq*             pDropReq = NULL;
241✔
2781
  int32_t                  msgLen = 0;
241✔
2782
  tsem_t*                  pSem = NULL;
241✔
2783
  SDropTbDataMsg*          pMsg = NULL;
241✔
2784

2785
  SInsertTableInfo** ppTbInfo = NULL;
241✔
2786
  int32_t            vgId = 0;
241✔
2787

2788
  req.pArray = taosArrayInit_s(sizeof(SVDropTbReq), 1);
241✔
2789
  if (!req.pArray) return terrno;
241✔
2790

2791
  pDropReq = taosArrayGet(req.pArray, 0);
241✔
2792

2793
  code = getStreamInsertTableInfo(pReq->streamId, pReq->gid, &ppTbInfo);
241✔
2794
  if (TSDB_CODE_SUCCESS == code) {
241✔
2795
    pDropReq->name = taosStrdup((*ppTbInfo)->tbname);
241✔
2796
    pDropReq->suid = (*ppTbInfo)->uid;
241✔
2797
    pDropReq->uid = (*ppTbInfo)->uid;
241✔
2798
    pDropReq->igNotExists = true;
241✔
2799
    vgId = (*ppTbInfo)->vgid;
241✔
2800

2801
    int64_t key[2] = {pReq->streamId, pReq->gid};
241✔
2802
    TAOS_UNUSED(taosHashRemove(gStreamGrpTableHash, key, sizeof(key)));
241✔
2803
  } else {
2804
    code = TSDB_CODE_STREAM_INSERT_TBINFO_NOT_FOUND;
×
2805
  }
2806
  QUERY_CHECK_CODE(code, lino, _end);
241✔
2807

2808
  code = dropTableReqToMsg(vgId, &req, (void**)&pMsg, &msgLen);
241✔
2809
  QUERY_CHECK_CODE(code, lino, _end);
241✔
2810

2811
  SVgroupInfo vgInfo = {0};
241✔
2812
  code = getDbVgInfoForExec(pMsgCb->clientRpc, pOutput->outDbFName, pDropReq->name, &vgInfo);
241✔
2813
  QUERY_CHECK_CODE(code, lino, _end);
241✔
2814

2815
  SDropTbCtx ctx = {.req = pReq};
241✔
2816
  code = tsem_init(&ctx.ready, 0, 0);
241✔
2817
  QUERY_CHECK_CODE(code, lino, _end);
241✔
2818
  pSem = &ctx.ready;
241✔
2819

2820
  code = sendDropTbRequest(&ctx, pMsg, msgLen, pMsgCb->clientRpc, &vgInfo.epSet);
241✔
2821
  QUERY_CHECK_CODE(code, lino, _end);
241✔
2822
  pMsg = NULL;  // now owned by sendDropTbRequest
241✔
2823

2824
  code = tsem_wait(&ctx.ready);
241✔
2825
  code = ctx.code;
241✔
2826
  stDebug("doDropStreamTable,  code:0x%" PRIx32 " req:%p, streamId:0x%" PRIx64 " groupId:%" PRId64 " tbname:%s", code, pReq,
241✔
2827
          pReq->streamId, pReq->gid, pDropReq ? pDropReq->name : "unknown");
2828

2829
_end:
241✔
2830
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_STREAM_INSERT_TBINFO_NOT_FOUND) {
241✔
2831
    stError("doDropStreamTable, code:0x%" PRIx32 ", streamId:0x%" PRIx64 " groupId:%" PRId64 " tbname:%s", code, pReq->streamId,
×
2832
            pReq->gid, pDropReq ? pDropReq->name : "unknown");
2833
    if (pMsg) {
×
2834
      taosMemoryFreeClear(pMsg);
×
2835
    }
2836
  }
2837
  if (pSem) tsem_destroy(pSem);
241✔
2838
  if (pDropReq && pDropReq->name) taosMemoryFreeClear(pDropReq->name);
241✔
2839
  if (ppTbInfo) releaseStreamInsertTableInfo(ppTbInfo);
241✔
2840
  taosArrayDestroy(req.pArray);
241✔
2841

2842
  return code;
241✔
2843
}
2844

2845
int32_t doDropStreamTableByTbName(SMsgCb* pMsgCb, void* pTaskOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2846
  SStreamRunnerTaskOutput* pOutput = pTaskOutput;
×
2847
  int32_t                  code = 0;
×
2848
  int32_t                  lino = 0;
×
2849
  SVDropTbBatchReq         req = {.nReqs = 1};
×
2850
  SVDropTbReq*             pDropReq = NULL;
×
2851
  int32_t                  msgLen = 0;
×
2852
  tsem_t*                  pSem = NULL;
×
2853
  SDropTbDataMsg*          pMsg = NULL;
×
2854

2855
  TAOS_UNUSED(taosThreadOnce(&g_dbVgInfoMgrInit, dbVgInfoMgrInitOnce));
×
2856

2857
  req.pArray = taosArrayInit_s(sizeof(SVDropTbReq), 1);
×
2858
  if (!req.pArray) return terrno;
×
2859

2860
  pDropReq = taosArrayGet(req.pArray, 0);
×
2861

2862
  pDropReq->name = tbName;
×
2863
  pDropReq->igNotExists = true;
×
2864

2865
  int64_t key[2] = {pReq->streamId, pReq->gid};
×
2866
  TAOS_UNUSED(taosHashRemove(gStreamGrpTableHash, key, sizeof(key)));
×
2867

2868
  SVgroupInfo vgInfo = {0};
×
2869
  code = getDbVgInfoForExec(pMsgCb->clientRpc, pOutput->outDbFName, pDropReq->name, &vgInfo);
×
2870
  QUERY_CHECK_CODE(code, lino, _end);
×
2871

2872
  code = dropTableReqToMsg(vgInfo.vgId, &req, (void**)&pMsg, &msgLen);
×
2873
  QUERY_CHECK_CODE(code, lino, _end);
×
2874

2875
  SDropTbCtx ctx = {.req = pReq};
×
2876
  code = tsem_init(&ctx.ready, 0, 0);
×
2877
  QUERY_CHECK_CODE(code, lino, _end);
×
2878
  pSem = &ctx.ready;
×
2879

2880
  code = sendDropTbRequest(&ctx, pMsg, msgLen, pMsgCb->clientRpc, &vgInfo.epSet);
×
2881
  QUERY_CHECK_CODE(code, lino, _end);
×
2882
  pMsg = NULL;  // now owned by sendDropTbRequest
×
2883

2884
  code = tsem_wait(&ctx.ready);
×
2885
  code = ctx.code;
×
2886
  stDebug("doDropStreamTableByTbName,  code:%d req:%p, streamId:0x%" PRIx64 " groupId:%" PRId64 " tbname:%s", code, pReq,
×
2887
          pReq->streamId, pReq->gid, pDropReq ? pDropReq->name : "unknown");
2888

2889
_end:
×
2890
  if (code != TSDB_CODE_SUCCESS) {
×
2891
    stError("doDropStreamTableByTbName, code:%d, streamId:0x%" PRIx64 " groupId:%" PRId64 " tbname:%s", code, pReq->streamId,
×
2892
            pReq->gid, pDropReq ? pDropReq->name : "unknown");
2893
    if (pMsg) {
×
2894
      taosMemoryFreeClear(pMsg);
×
2895
    }
2896
  }
2897
  if (pSem) tsem_destroy(pSem);
×
2898
  taosArrayDestroy(req.pArray);
×
2899

2900
  return code;
×
2901
}
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