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

taosdata / TDengine / #4884

14 Dec 2025 03:48AM UTC coverage: 60.617% (-4.1%) from 64.74%
#4884

push

travis-ci

web-flow
test: update coverage workflow time (#33918)

156854 of 258761 relevant lines covered (60.62%)

75258957.81 hits per line

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

59.54
/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) {
41,185✔
135
  pBuildInsertDataInfo->isLastBlock = false;
41,185✔
136
  pBuildInsertDataInfo->lastTs = TSKEY_MIN;
41,185✔
137
  pBuildInsertDataInfo->isFirstBlock = true;
41,185✔
138
  pBuildInsertDataInfo->needSortMerge = false;
41,185✔
139

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

144
  return TSDB_CODE_SUCCESS;
41,185✔
145
}
146

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

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

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

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

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

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

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

216
  return TSDB_CODE_SUCCESS;
11,216✔
217
}
218

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

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

238
  res->vgid = pCreateTbRsp->pMeta->vgId;
×
239

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

243
  res->version = pCreateTbRsp->pMeta->sversion;
×
244
  res->pSchema = tBuildTSchema(pCreateTbRsp->pMeta->pSchemas, pCreateTbRsp->pMeta->numOfColumns, res->version);
×
245
  if (res->pSchema == NULL) {
×
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;
×
251
  return TSDB_CODE_SUCCESS;
×
252
}
253

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

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

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

272
    TAOS_UNUSED(taosHashRemove(gStreamGrpTableHash, key, sizeof(key)));
×
273

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

276
    if (code == TSDB_CODE_DUP_KEY) {
×
277
      freeCacheTbInfo(&pNewInfo);
×
278
      code = TSDB_CODE_SUCCESS;
×
279
      goto _exit;
×
280
    } else if (code != TSDB_CODE_SUCCESS) {
×
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
×
288
           ", version:%d",
289
           pInserterInfo->streamId, pInserterInfo->groupId, pNewInfo->uid, pNewInfo->vgid, pNewInfo->version);
290
  }
291
  return TSDB_CODE_SUCCESS;
11,216✔
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) {
12,017✔
304
  int32_t           code = TSDB_CODE_SUCCESS;
12,017✔
305
  int32_t           lino = 0;
12,017✔
306
  SInsertTableInfo* res = taosMemoryCalloc(1, sizeof(SInsertTableInfo));
12,017✔
307
  if (res == NULL) {
12,017✔
308
    return terrno;
×
309
  }
310

311
  SStreamInserterParam* pInsertParam = pInserter->pParam->streamInserterParam;
12,017✔
312
  res->uid = 0;
12,017✔
313
  if (pInsertParam->tbType == TSDB_NORMAL_TABLE) {
12,017✔
314
    res->version = 1;
2,420✔
315
  } else {
316
    res->version = pInsertParam->sver;
9,597✔
317
  }
318

319
  res->tbname = taosStrdup(pInserterInfo->tbName);
12,017✔
320
  if (res->tbname == NULL) {
12,017✔
321
    taosMemoryFree(res);
×
322
    stError("failed to allocate memory for table name");
×
323
    return terrno;
×
324
  }
325

326
  code = buildTSchmaFromInserter(pInserter->pParam->streamInserterParam, &res->pSchema);
12,017✔
327
  QUERY_CHECK_CODE(code, lino, _return);
12,017✔
328

329
  int64_t key[2] = {pInserterInfo->streamId, pInserterInfo->groupId};
12,017✔
330
  code = taosHashPut(gStreamGrpTableHash, key, sizeof(key), &res, sizeof(SInsertTableInfo*));
12,017✔
331
  if (code == TSDB_CODE_DUP_KEY) {
12,017✔
332
    freeCacheTbInfo(&res);
4,401✔
333
    return TSDB_CODE_SUCCESS;
4,401✔
334
  }
335

336
_return:
7,616✔
337
  if (code != TSDB_CODE_SUCCESS) {
7,616✔
338
    stError("failed to build table info for streamId:%" PRIx64 ", groupId:%" PRIx64 ", code:%d",
×
339
            pInserterInfo->streamId, pInserterInfo->groupId, code);
340
    freeCacheTbInfo(&res);
×
341
  }
342
  return code;
7,616✔
343
}
344

345
static bool colsIsSupported(const STableMetaRsp* pTableMetaRsp, const SStreamInserterParam* pInserterParam) {
732✔
346
  SArray* pCreatingFields = pInserterParam->pFields;
732✔
347

348
  for (int32_t i = 0; i < pCreatingFields->size; ++i) {
5,856✔
349
    SFieldWithOptions* pField = taosArrayGet(pCreatingFields, i);
5,124✔
350
    if (NULL == pField) {
5,124✔
351
      stError("isSupportedSTableSchema: failed to get field from array");
×
352
      return false;
×
353
    }
354

355
    for (int j = 0; j < pTableMetaRsp->numOfColumns; ++j) {
20,496✔
356
      if (strncmp(pTableMetaRsp->pSchemas[j].name, pField->name, TSDB_COL_NAME_LEN) == 0) {
20,496✔
357
        if (pTableMetaRsp->pSchemas[j].type == pField->type && pTableMetaRsp->pSchemas[j].bytes == pField->bytes) {
5,124✔
358
          break;
359
        } else {
360
          return false;
×
361
        }
362
      }
363
    }
364
  }
365
  return true;
732✔
366
}
367

368
static bool TagsIsSupported(const STableMetaRsp* pTableMetaRsp, const SStreamInserterParam* pInserterParam) {
×
369
  SArray* pCreatingTags = pInserterParam->pTagFields;
×
370

371
  int32_t            tagIndexOffset = -1;
×
372
  SFieldWithOptions* pField = taosArrayGet(pCreatingTags, 0);
×
373
  if (NULL == pField) {
×
374
    stError("isSupportedSTableSchema: failed to get field from array");
×
375
    return false;
×
376
  }
377
  for (int32_t i = 0; i < pTableMetaRsp->numOfColumns + pTableMetaRsp->numOfTags; ++i) {
×
378
    if (strncmp(pTableMetaRsp->pSchemas[i].name, pField->name, TSDB_COL_NAME_LEN) != 0) {
×
379
      tagIndexOffset = i;
×
380
      break;
×
381
    }
382
  }
383
  if (tagIndexOffset == -1) {
×
384
    stError("isSupportedSTableSchema: failed to get tag index");
×
385
    return false;
×
386
  }
387

388
  for (int32_t i = 0; i < pTableMetaRsp->numOfTags; ++i) {
×
389
    int32_t            index = i + tagIndexOffset;
×
390
    SFieldWithOptions* pField = taosArrayGet(pCreatingTags, i);
×
391
    if (NULL == pField) {
×
392
      stError("isSupportedSTableSchema: failed to get field from array");
×
393
      return false;
×
394
    }
395

396
    for(int32_t j = 0; j < pTableMetaRsp->numOfTags; ++j) {
×
397
      if (strncmp(pTableMetaRsp->pSchemas[index].name, pField->name, TSDB_COL_NAME_LEN) == 0) {
×
398
        if (pTableMetaRsp->pSchemas[index].type == pField->type &&
×
399
            pTableMetaRsp->pSchemas[index].bytes == pField->bytes) {
×
400
          break;
401
        } else {
402
          return false;
×
403
        }
404
      }
405
    }
406
  }
407
  return true;
×
408
}
409

410
static bool isSupportedSTableSchema(const STableMetaRsp* pTableMetaRsp, const SStreamInserterParam* pInserterParam) {
×
411
  if (!colsIsSupported(pTableMetaRsp, pInserterParam)) {
×
412
    return false;
×
413
  }
414
  if (!TagsIsSupported(pTableMetaRsp, pInserterParam)) {
×
415
    return false;
×
416
  }
417
  return true;
×
418
}
419

420
static bool isSupportedNTableSchema(const STableMetaRsp* pTableMetaRsp, const SStreamInserterParam* pInserterParam) {
732✔
421
  return colsIsSupported(pTableMetaRsp, pInserterParam);
732✔
422
}
423

424
static int32_t checkAndSaveCreateGrpTableInfo(SDataInserterHandle*     pInserthandle,
732✔
425
                                              SStreamDataInserterInfo* pInserterInfo) {
426
  int32_t     code = TSDB_CODE_SUCCESS;
732✔
427
  SSubmitRes* pSubmitRes = &pInserthandle->submitRes;
732✔
428
  int8_t      tbType = pInserthandle->pParam->streamInserterParam->tbType;
732✔
429

430
  SVCreateTbRsp*        pCreateTbRsp = taosArrayGet(pSubmitRes->pRsp->aCreateTbRsp, 0);
732✔
431
  SSchema*              pExistRow = pCreateTbRsp->pMeta->pSchemas;
732✔
432
  SStreamInserterParam* pInserterParam = pInserthandle->pParam->streamInserterParam;
732✔
433

434
  if (tbType == TSDB_CHILD_TABLE || tbType == TSDB_SUPER_TABLE) {
732✔
435
    if (!isSupportedSTableSchema(pCreateTbRsp->pMeta, pInserterParam)) {
×
436
      stError("create table failed, schema is not supported");
×
437
      return TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
438
    }
439
  } else if (tbType == TSDB_NORMAL_TABLE) {
732✔
440
    if (!isSupportedNTableSchema(pCreateTbRsp->pMeta, pInserterParam)) {
732✔
441
      stError("create table failed, schema is not supported");
×
442
      return TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
443
    }
444
  } else {
445
    stError("checkAndSaveCreateGrpTableInfo failed, tbType:%d is not supported", tbType);
×
446
    return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
447
  }
448

449
  return updateInsertGrpTableInfo(pInserterInfo, pSubmitRes);
732✔
450
}
451

452
int32_t inserterCallback(void* param, SDataBuf* pMsg, int32_t code) {
77,561✔
453
  SSubmitRspParam*     pParam = (SSubmitRspParam*)param;
77,561✔
454
  SDataInserterHandle* pInserter = pParam->pInserter;
77,561✔
455
  int32_t              code2 = 0;
77,561✔
456

457
  if (code) {
77,561✔
458
    pInserter->submitRes.code = code;
1,777✔
459
  } else {
460
    pInserter->submitRes.code = TSDB_CODE_SUCCESS;
75,784✔
461
  }
462
  SDecoder coder = {0};
77,561✔
463

464
  if (code == TSDB_CODE_SUCCESS) {
77,561✔
465
    pInserter->submitRes.pRsp = taosMemoryCalloc(1, sizeof(SSubmitRsp2));
75,784✔
466
    if (NULL == pInserter->submitRes.pRsp) {
75,784✔
467
      pInserter->submitRes.code = terrno;
×
468
      goto _return;
×
469
    }
470

471
    tDecoderInit(&coder, pMsg->pData, pMsg->len);
75,784✔
472
    code = tDecodeSSubmitRsp2(&coder, pInserter->submitRes.pRsp);
75,784✔
473
    if (code) {
75,784✔
474
      tDestroySSubmitRsp2(pInserter->submitRes.pRsp, TSDB_MSG_FLG_DECODE);
×
475
      taosMemoryFree(pInserter->submitRes.pRsp);
×
476
      pInserter->submitRes.code = code;
×
477
      goto _return;
×
478
    }
479

480
    if (pInserter->submitRes.pRsp->affectedRows > 0) {
75,784✔
481
      SArray* pCreateTbList = pInserter->submitRes.pRsp->aCreateTbRsp;
52,116✔
482
      int32_t numOfTables = taosArrayGetSize(pCreateTbList);
52,116✔
483

484
      for (int32_t i = 0; i < numOfTables; ++i) {
65,912✔
485
        SVCreateTbRsp* pRsp = taosArrayGet(pCreateTbList, i);
13,796✔
486
        if (NULL == pRsp) {
13,796✔
487
          pInserter->submitRes.code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
488
          goto _return;
×
489
        }
490
        if (TSDB_CODE_SUCCESS != pRsp->code) {
13,796✔
491
          code = pRsp->code;
×
492
          tDestroySSubmitRsp2(pInserter->submitRes.pRsp, TSDB_MSG_FLG_DECODE);
×
493
          taosMemoryFree(pInserter->submitRes.pRsp);
×
494
          pInserter->submitRes.code = code;
×
495
          goto _return;
×
496
        }
497
      }
498
    }
499

500
    if (pParam->putParam != NULL && ((SStreamDataInserterInfo*)pParam->putParam)->isAutoCreateTable) {
75,784✔
501
      code2 = updateInsertGrpTableInfo((SStreamDataInserterInfo*)pParam->putParam, &pInserter->submitRes);
10,484✔
502
    }
503

504
    pInserter->submitRes.affectedRows += pInserter->submitRes.pRsp->affectedRows;
75,784✔
505
    qDebug("submit rsp received, affectedRows:%d, total:%" PRId64, pInserter->submitRes.pRsp->affectedRows,
75,784✔
506
           pInserter->submitRes.affectedRows);
507
    tDestroySSubmitRsp2(pInserter->submitRes.pRsp, TSDB_MSG_FLG_DECODE);
75,784✔
508
    taosMemoryFree(pInserter->submitRes.pRsp);
75,784✔
509
  } else if ((TSDB_CODE_TDB_TABLE_ALREADY_EXIST == code && pParam->putParam != NULL &&
1,777✔
510
              ((SStreamDataInserterInfo*)pParam->putParam)->isAutoCreateTable) ||
1,777✔
511
             TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER == code) {
512
    pInserter->submitRes.code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
732✔
513
    pInserter->submitRes.pRsp = taosMemoryCalloc(1, sizeof(SSubmitRsp2));
732✔
514
    if (NULL == pInserter->submitRes.pRsp) {
732✔
515
      code2 = terrno;
×
516
      goto _return;
×
517
    }
518

519
    tDecoderInit(&coder, pMsg->pData, pMsg->len);
732✔
520
    code2 = tDecodeSSubmitRsp2(&coder, pInserter->submitRes.pRsp);
732✔
521
    if (code2 == TSDB_CODE_SUCCESS) {
732✔
522
      code2 = checkAndSaveCreateGrpTableInfo(pInserter, (SStreamDataInserterInfo*)pParam->putParam);
732✔
523
    }
524
    tDestroySSubmitRsp2(pInserter->submitRes.pRsp, TSDB_MSG_FLG_DECODE);
732✔
525
    taosMemoryFree(pInserter->submitRes.pRsp);
732✔
526
  }
527

528
_return:
77,561✔
529

530
  if (code2) {
77,561✔
531
    qError("update inserter table info failed, error:%s", tstrerror(code2));
×
532
  }
533
  tDecoderClear(&coder);
77,561✔
534
  TAOS_UNUSED(tsem_post(&pInserter->ready));
77,561✔
535

536
  taosMemoryFree(pMsg->pData);
77,561✔
537

538
  return TSDB_CODE_SUCCESS;
77,561✔
539
}
540

541
void freeUseDbOutput_tmp(void* ppOutput) {
3,000✔
542
  SUseDbOutput* pOut = *(SUseDbOutput**)ppOutput;
3,000✔
543
  if (NULL == ppOutput) {
3,000✔
544
    return;
×
545
  }
546

547
  if (pOut->dbVgroup) {
3,000✔
548
    freeVgInfo(pOut->dbVgroup);
3,000✔
549
  }
550
  taosMemFree(pOut);
3,000✔
551
  *(SUseDbOutput**)ppOutput = NULL;
3,000✔
552
}
553

554
static int32_t processUseDbRspForInserter(void* param, SDataBuf* pMsg, int32_t code) {
3,543✔
555
  int32_t       lino = 0;
3,543✔
556
  SDBVgInfoReq* pVgInfoReq = (SDBVgInfoReq*)param;
3,543✔
557

558
  if (TSDB_CODE_SUCCESS != code) {
3,543✔
559
    // pInserter->pTaskInfo->code = rpcCvtErrCode(code);
560
    // if (pInserter->pTaskInfo->code != code) {
561
    //   qError("load db info rsp received, error:%s, cvted error:%s", tstrerror(code),
562
    //          tstrerror(pInserter->pTaskInfo->code));
563
    // } else {
564
    //   qError("load db info rsp received, error:%s", tstrerror(code));
565
    // }
566
    goto _return;
×
567
  }
568

569
  pVgInfoReq->pRsp = taosMemoryMalloc(sizeof(SUseDbRsp));
3,543✔
570
  QUERY_CHECK_NULL(pVgInfoReq->pRsp, code, lino, _return, terrno);
3,543✔
571

572
  code = tDeserializeSUseDbRsp(pMsg->pData, (int32_t)pMsg->len, pVgInfoReq->pRsp);
3,543✔
573
  QUERY_CHECK_CODE(code, lino, _return);
3,543✔
574

575
_return:
3,543✔
576
  taosMemoryFreeClear(pMsg->pData);
3,543✔
577
  taosMemoryFreeClear(pMsg->pEpSet);
3,543✔
578
  if (code != 0){
3,543✔
579
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
580
  }
581
  int ret = tsem_post(&pVgInfoReq->ready);
3,543✔
582
  if (ret != 0) {
3,543✔
583
    qError("%s failed code: %d", __func__, ret);
×
584
  }
585
  return code;
3,543✔
586
}
587

588

589
int inserterVgInfoComp(const void* lp, const void* rp) {
10,812✔
590
  SVgroupInfo* pLeft = (SVgroupInfo*)lp;
10,812✔
591
  SVgroupInfo* pRight = (SVgroupInfo*)rp;
10,812✔
592
  if (pLeft->hashBegin < pRight->hashBegin) {
10,812✔
593
    return -1;
6,552✔
594
  } else if (pLeft->hashBegin > pRight->hashBegin) {
4,260✔
595
    return 1;
4,260✔
596
  }
597

598
  return 0;
×
599
}
600

601
static int32_t buildDbVgInfoMap(void* clientRpc, const char* dbFName, SUseDbOutput* output) {
3,543✔
602
  int32_t      code = TSDB_CODE_SUCCESS;
3,543✔
603
  int32_t      lino = 0;
3,543✔
604
  char*        buf1 = NULL;
3,543✔
605
  SUseDbReq*   pReq = NULL;
3,543✔
606
  SDBVgInfoReq dbVgInfoReq = {0};
3,543✔
607
  code = tsem_init(&dbVgInfoReq.ready, 0, 0);
3,543✔
608
  if (code != TSDB_CODE_SUCCESS) {
3,543✔
609
    qError("tsem_init failed, error:%s", tstrerror(code));
×
610
    return code;
×
611
  }
612

613
  pReq = taosMemoryMalloc(sizeof(SUseDbReq));
3,543✔
614
  QUERY_CHECK_NULL(pReq, code, lino, _return, terrno);
3,543✔
615

616
  tstrncpy(pReq->db, dbFName, TSDB_DB_FNAME_LEN);
3,543✔
617
  QUERY_CHECK_CODE(code, lino, _return);
3,543✔
618

619
  int32_t contLen = tSerializeSUseDbReq(NULL, 0, pReq);
3,543✔
620
  buf1 = taosMemoryCalloc(1, contLen);
3,543✔
621
  QUERY_CHECK_NULL(buf1, code, lino, _return, terrno);
3,543✔
622

623
  int32_t tempRes = tSerializeSUseDbReq(buf1, contLen, pReq);
3,543✔
624
  if (tempRes < 0) {
3,543✔
625
    QUERY_CHECK_CODE(terrno, lino, _return);
×
626
  }
627

628
  SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
3,543✔
629
  QUERY_CHECK_NULL(pMsgSendInfo, code, lino, _return, terrno);
3,543✔
630

631
  SEpSet pEpSet = {0};
3,543✔
632
  QUERY_CHECK_CODE(getCurrentMnodeEpset(&pEpSet), lino, _return);
3,543✔
633

634
  pMsgSendInfo->param = &dbVgInfoReq;
3,543✔
635
  pMsgSendInfo->msgInfo.pData = buf1;
3,543✔
636
  buf1 = NULL;
3,543✔
637
  pMsgSendInfo->msgInfo.len = contLen;
3,543✔
638
  pMsgSendInfo->msgType = TDMT_MND_GET_DB_INFO;
3,543✔
639
  pMsgSendInfo->fp = processUseDbRspForInserter;
3,543✔
640
  // pMsgSendInfo->requestId = pTaskInfo->id.queryId;
641

642
  code = asyncSendMsgToServer(clientRpc, &pEpSet, NULL, pMsgSendInfo);
3,543✔
643
  QUERY_CHECK_CODE(code, lino, _return);
3,543✔
644

645
  code = tsem_wait(&dbVgInfoReq.ready);
3,543✔
646
  QUERY_CHECK_CODE(code, lino, _return);
3,543✔
647

648
  code = queryBuildUseDbOutput(output, dbVgInfoReq.pRsp);
3,543✔
649
  QUERY_CHECK_CODE(code, lino, _return);
3,543✔
650

651
  output->dbVgroup->vgArray = taosArrayInit(dbVgInfoReq.pRsp->vgNum, sizeof(SVgroupInfo));
3,543✔
652
  if (NULL == output->dbVgroup->vgArray) {
3,543✔
653
    code = terrno;
×
654
    QUERY_CHECK_CODE(code, lino, _return);
×
655
  }
656

657
  void* pIter = taosHashIterate(output->dbVgroup->vgHash, NULL);
3,543✔
658
  while (pIter) {
11,871✔
659
    if (NULL == taosArrayPush(output->dbVgroup->vgArray, pIter)) {
16,656✔
660
      taosHashCancelIterate(output->dbVgroup->vgHash, pIter);
×
661
      return terrno;
×
662
    }
663

664
    pIter = taosHashIterate(output->dbVgroup->vgHash, pIter);
8,328✔
665
  }
666

667
  taosArraySort(output->dbVgroup->vgArray, inserterVgInfoComp);
3,543✔
668

669
_return:
3,543✔
670

671
  if (code) {
3,543✔
672
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
673
    taosMemoryFree(buf1);
×
674
  }
675
  taosMemoryFree(pReq);
3,543✔
676
  TAOS_UNUSED(tsem_destroy(&dbVgInfoReq.ready));
3,543✔
677
  if (dbVgInfoReq.pRsp) {
3,543✔
678
    tFreeSUsedbRsp(dbVgInfoReq.pRsp);
3,543✔
679
    taosMemoryFreeClear(dbVgInfoReq.pRsp);
3,543✔
680
  }
681
  return code;
3,543✔
682
}
683

684
int32_t inserterBuildCreateTbReq(SVCreateTbReq* pTbReq, const char* tname, STag* pTag, int64_t suid, const char* sname,
12,909✔
685
                                 SArray* tagName, uint8_t tagNum, int32_t ttl) {
686
  pTbReq->type = TD_CHILD_TABLE;
12,909✔
687
  pTbReq->ctb.pTag = (uint8_t*)pTag;
12,909✔
688
  pTbReq->name = taosStrdup(tname);
12,909✔
689
  if (!pTbReq->name) return terrno;
12,909✔
690
  pTbReq->ctb.suid = suid;
12,909✔
691
  pTbReq->ctb.tagNum = tagNum;
12,909✔
692
  if (sname) {
12,909✔
693
    pTbReq->ctb.stbName = taosStrdup(sname);
12,909✔
694
    if (!pTbReq->ctb.stbName) {
12,909✔
695
      taosMemoryFree(pTbReq->name);
×
696
      return terrno;
×
697
    }
698
  }
699
  pTbReq->ctb.tagName = tagName;
12,909✔
700
  pTbReq->ttl = ttl;
12,909✔
701
  pTbReq->commentLen = -1;
12,909✔
702

703
  return TSDB_CODE_SUCCESS;
12,909✔
704
}
705

706
int32_t inserterHashValueComp(void const* lp, void const* rp) {
65,081✔
707
  uint32_t*    key = (uint32_t*)lp;
65,081✔
708
  SVgroupInfo* pVg = (SVgroupInfo*)rp;
65,081✔
709

710
  if (*key < pVg->hashBegin) {
65,081✔
711
    return -1;
1,242✔
712
  } else if (*key > pVg->hashEnd) {
63,839✔
713
    return 1;
18,541✔
714
  }
715

716
  return 0;
45,298✔
717
}
718

719

720
int32_t inserterGetVgInfo(SDBVgInfo* dbInfo, char* tbName, SVgroupInfo* pVgInfo) {
45,298✔
721
  if (NULL == dbInfo) {
45,298✔
722
    return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
723
  }
724

725
  if (NULL == dbInfo->vgArray) {
45,298✔
726
    qError("empty db vgArray, hashSize:%d", taosHashGetSize(dbInfo->vgHash));
×
727
    return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
728
  }
729

730
  uint32_t hashValue =
45,298✔
731
      taosGetTbHashVal(tbName, (int32_t)strlen(tbName), dbInfo->hashMethod, dbInfo->hashPrefix, dbInfo->hashSuffix);
45,298✔
732
  SVgroupInfo* vgInfo = taosArraySearch(dbInfo->vgArray, &hashValue, inserterHashValueComp, TD_EQ);
45,298✔
733
  if (NULL == vgInfo) {
45,298✔
734
    qError("no hash range found for hash value [%u], table:%s, numOfVgId:%d", hashValue, tbName,
×
735
           (int32_t)taosArrayGetSize(dbInfo->vgArray));
736
    return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
737
  }
738
  
739
  *pVgInfo = *vgInfo;
45,298✔
740
  qDebug("insert get vgInfo, tbName:%s vgId:%d epset(%s:%d)", tbName, pVgInfo->vgId, pVgInfo->epSet.eps[0].fqdn,
45,298✔
741
        pVgInfo->epSet.eps[0].port);
742
        
743
  return TSDB_CODE_SUCCESS;
45,298✔
744
}
745

746
int32_t inserterGetVgId(SDBVgInfo* dbInfo, char* tbName, int32_t* vgId) {
×
747
  SVgroupInfo vgInfo = {0};
×
748
  int32_t     code = inserterGetVgInfo(dbInfo, tbName, &vgInfo);
×
749
  if (code != TSDB_CODE_SUCCESS) {
×
750
    qError("inserterGetVgId failed, code:%d", code);
×
751
    return code;
×
752
  }
753
  *vgId = vgInfo.vgId;
×
754

755
  return TSDB_CODE_SUCCESS;
×
756
}
757

758
int32_t inserterGetDbVgInfo(SDataInserterHandle* pInserter, const char* dbFName, SDBVgInfo** dbVgInfo) {
1,656✔
759
  int32_t       code = TSDB_CODE_SUCCESS;
1,656✔
760
  int32_t       line = 0;
1,656✔
761
  SUseDbOutput* output = NULL;
1,656✔
762

763
  // QRY_PARAM_CHECK(dbVgInfo);
764
  // QRY_PARAM_CHECK(pInserter);
765
  // QRY_PARAM_CHECK(name);
766

767
  if (pInserter->dbVgInfoMap == NULL) {
1,656✔
768
    pInserter->dbVgInfoMap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
1,656✔
769
    if (pInserter->dbVgInfoMap == NULL) {
1,656✔
770
      return TSDB_CODE_OUT_OF_MEMORY;
×
771
    }
772
  }
773

774
  SUseDbOutput** find = (SUseDbOutput**)taosHashGet(pInserter->dbVgInfoMap, dbFName, strlen(dbFName));
1,656✔
775

776
  if (find == NULL) {
1,656✔
777
    output = taosMemoryMalloc(sizeof(SUseDbOutput));
1,656✔
778
    if (output == NULL) {
1,656✔
779
      return TSDB_CODE_OUT_OF_MEMORY;
×
780
    }
781

782
    code = buildDbVgInfoMap(pInserter->pParam->readHandle->pMsgCb->clientRpc, dbFName, output);
1,656✔
783
    QUERY_CHECK_CODE(code, line, _return);
1,656✔
784

785
    code = taosHashPut(pInserter->dbVgInfoMap, dbFName, strlen(dbFName), &output, POINTER_BYTES);
1,656✔
786
    QUERY_CHECK_CODE(code, line, _return);
1,656✔
787
  } else {
788
    output = *find;
×
789
  }
790

791
  *dbVgInfo = output->dbVgroup;
1,656✔
792
  return code;
1,656✔
793

794
_return:
×
795
  qError("%s failed at line %d since %s", __func__, line, tstrerror(code));
×
796
  freeUseDbOutput_tmp(&output);
×
797
  return code;
×
798
}
799

800
int32_t getTableVgInfo(SDataInserterHandle* pInserter, const char* dbFName,
41,986✔
801
                       const char* tbName, SVgroupInfo* pVgInfo) {
802
  return getDbVgInfoForExec(pInserter->pParam->readHandle->pMsgCb->clientRpc, dbFName,
41,986✔
803
                              tbName, pVgInfo);
804
}
805

806
static int32_t sendSubmitRequest(SDataInserterHandle* pInserter, void* putParam, void* pMsg, int32_t msgLen,
77,561✔
807
                                 void* pTransporter, SEpSet* pEpset) {
808
  // send the fetch remote task result reques
809
  SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
77,561✔
810
  if (NULL == pMsgSendInfo) {
77,561✔
811
    taosMemoryFreeClear(pMsg);
×
812
    return terrno;
×
813
  }
814

815
  SSubmitRspParam* pParam = taosMemoryCalloc(1, sizeof(SSubmitRspParam));
77,561✔
816
  if (NULL == pParam) {
77,561✔
817
    taosMemoryFreeClear(pMsg);
×
818
    taosMemoryFreeClear(pMsgSendInfo);
×
819
    return terrno;
×
820
  }
821
  pParam->pInserter = pInserter;
77,561✔
822
  pParam->putParam = putParam;
77,561✔
823

824
  pMsgSendInfo->param = pParam;
77,561✔
825
  pMsgSendInfo->paramFreeFp = taosAutoMemoryFree;
77,561✔
826
  pMsgSendInfo->msgInfo.pData = pMsg;
77,561✔
827
  pMsgSendInfo->msgInfo.len = msgLen;
77,561✔
828
  pMsgSendInfo->msgType = TDMT_VND_SUBMIT;
77,561✔
829
  pMsgSendInfo->fp = inserterCallback;
77,561✔
830

831
  return asyncSendMsgToServer(pTransporter, pEpset, NULL, pMsgSendInfo);
77,561✔
832
}
833

834
static int32_t submitReqToMsg(int32_t vgId, SSubmitReq2* pReq, void** pData, int32_t* pLen) {
77,561✔
835
  int32_t code = TSDB_CODE_SUCCESS;
77,561✔
836
  int32_t len = 0;
77,561✔
837
  void*   pBuf = NULL;
77,561✔
838
  tEncodeSize(tEncodeSubmitReq, pReq, len, code);
77,561✔
839
  if (TSDB_CODE_SUCCESS == code) {
77,561✔
840
    SEncoder encoder;
77,561✔
841
    len += sizeof(SSubmitReq2Msg);
77,561✔
842
    pBuf = taosMemoryMalloc(len);
77,561✔
843
    if (NULL == pBuf) {
77,561✔
844
      return terrno;
×
845
    }
846
    ((SSubmitReq2Msg*)pBuf)->header.vgId = htonl(vgId);
77,561✔
847
    ((SSubmitReq2Msg*)pBuf)->header.contLen = htonl(len);
77,561✔
848
    ((SSubmitReq2Msg*)pBuf)->version = htobe64(1);
77,561✔
849
    tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SSubmitReq2Msg)), len - sizeof(SSubmitReq2Msg));
77,561✔
850
    code = tEncodeSubmitReq(&encoder, pReq);
77,561✔
851
    tEncoderClear(&encoder);
77,561✔
852
  }
853

854
  if (TSDB_CODE_SUCCESS == code) {
77,561✔
855
    *pData = pBuf;
77,561✔
856
    *pLen = len;
77,561✔
857
  } else {
858
    taosMemoryFree(pBuf);
×
859
  }
860

861
  return code;
77,561✔
862
}
863

864
int32_t buildSubmitReqFromStbBlock(SDataInserterHandle* pInserter, SHashObj* pHash, const SSDataBlock* pDataBlock,
1,656✔
865
                                   const STSchema* pTSchema, int64_t uid, int32_t vgId, tb_uid_t suid) {
866
  SArray* pVals = NULL;
1,656✔
867
  SArray* pTagVals = NULL;
1,656✔
868
  SSubmitReqSendInfo** ppSendInfo = NULL;
1,656✔
869
  int32_t numOfBlks = 0;
1,656✔
870

871
  terrno = TSDB_CODE_SUCCESS;
1,656✔
872

873
  int32_t colNum = taosArrayGetSize(pDataBlock->pDataBlock);
1,656✔
874
  int32_t rows = pDataBlock->info.rows;
1,656✔
875

876
  if (!pTagVals && !(pTagVals = taosArrayInit(colNum, sizeof(STagVal)))) {
1,656✔
877
    goto _end;
×
878
  }
879

880
  if (!pVals && !(pVals = taosArrayInit(colNum, sizeof(SColVal)))) {
1,656✔
881
    goto _end;
×
882
  }
883

884
  SDBVgInfo* dbInfo = NULL;
1,656✔
885
  int32_t    code = inserterGetDbVgInfo(pInserter, pInserter->dbFName, &dbInfo);
1,656✔
886
  if (code != TSDB_CODE_SUCCESS) {
1,656✔
887
    terrno = code;
×
888
    goto _end;
×
889
  }
890

891
  for (int32_t j = 0; j < rows; ++j) {
4,968✔
892
    SSubmitTbData tbData = {0};
3,312✔
893
    if (!(tbData.aRowP = taosArrayInit(rows, sizeof(SRow*)))) {
3,312✔
894
      goto _end;
×
895
    }
896
    tbData.suid = suid;
3,312✔
897
    tbData.uid = uid;
3,312✔
898
    tbData.sver = pTSchema->version;
3,312✔
899

900
    int64_t lastTs = TSKEY_MIN;
3,312✔
901

902
    taosArrayClear(pVals);
3,312✔
903

904
    int32_t offset = 0;
3,312✔
905
    taosArrayClear(pTagVals);
3,312✔
906
    tbData.uid = 0;
3,312✔
907
    tbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
3,312✔
908
    if (NULL == tbData.pCreateTbReq) {
3,312✔
909
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
910
      goto _end;
×
911
    }
912
    tbData.flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
3,312✔
913

914
    SColumnInfoData* tbname = taosArrayGet(pDataBlock->pDataBlock, 0);
3,312✔
915
    if (NULL == tbname) {
3,312✔
916
      terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
917
      qError("Insert into stable must have tbname column");
×
918
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
919
      goto _end;
×
920
    }
921
    if (tbname->info.type != TSDB_DATA_TYPE_BINARY) {
3,312✔
922
      terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
923
      qError("tbname column must be binary");
×
924
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
925
      goto _end;
×
926
    }
927

928
    if (colDataIsNull_s(tbname, j)) {
6,624✔
929
      qError("insert into stable tbname column is null");
×
930
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
931
      goto _end;
×
932
    }
933
    void*   data = colDataGetVarData(tbname, j);
3,312✔
934
    SValue  sv = (SValue){TSDB_DATA_TYPE_VARCHAR, .nData = varDataLen(data), .pData = varDataVal(data)};
3,312✔
935
    SColVal cv = COL_VAL_VALUE(0, sv);
3,312✔
936

937
    char tbFullName[TSDB_TABLE_FNAME_LEN];
3,312✔
938
    char tableName[TSDB_TABLE_FNAME_LEN];
3,312✔
939
    memcpy(tableName, sv.pData, sv.nData);
3,312✔
940
    tableName[sv.nData] = '\0';
3,312✔
941

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

972
      if (!(pReq->aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData)))) {
3,312✔
973
        taosMemoryFree(pReq);
×
974
        taosMemoryFree(pSendInfo);
×
975
        tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
976
        goto _end;
×
977
      }
978
      
979
      pSendInfo->msg = pReq;
3,312✔
980
      pSendInfo->epSet = vgInfo.epSet;
3,312✔
981
      code = taosHashPut(pHash, &vgInfo.vgId, sizeof(int32_t), &pSendInfo, POINTER_BYTES);
3,312✔
982
      if (code != TSDB_CODE_SUCCESS) {
3,312✔
983
        tDestroySubmitReq(pReq, TSDB_MSG_FLG_ENCODE);
×
984
        taosMemoryFree(pReq);
×
985
        taosMemoryFree(pSendInfo);
×
986
        terrno = code;
×
987
        tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
988
        goto _end;
×
989
      }
990
    } else {
991
      pReq = (*ppSendInfo)->msg;
×
992
    }
993
    SArray* TagNames = taosArrayInit(8, TSDB_COL_NAME_LEN);
3,312✔
994
    if (!TagNames) {
3,312✔
995
      terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
996
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
997
      goto _end;
×
998
    }
999
    for (int32_t i = 0; i < pInserter->pTagSchema->nCols; ++i) {
9,936✔
1000
      SSchema* tSchema = &pInserter->pTagSchema->pSchema[i];
6,624✔
1001
      int16_t  colIdx = tSchema->colId;
6,624✔
1002
      int16_t* slotId = taosHashGet(pInserter->pCols, &colIdx, sizeof(colIdx));
6,624✔
1003
      if (NULL == slotId) {
6,624✔
1004
        continue;
3,312✔
1005
      }
1006
      if (NULL == taosArrayPush(TagNames, tSchema->name)) {
6,624✔
1007
        taosArrayDestroy(TagNames);
×
1008
        tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1009
        goto _end;
×
1010
      }
1011

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

1090
    code = inserterBuildCreateTbReq(tbData.pCreateTbReq, tableName, pTag, suid, pInserter->pNode->tableName, TagNames,
3,312✔
1091
                                    pInserter->pTagSchema->nCols, TSDB_DEFAULT_TABLE_TTL);
3,312✔
1092
    if (code != TSDB_CODE_SUCCESS) {
3,312✔
1093
      terrno = code;
×
1094
      qError("failed to build create table request, error:%s", tstrerror(code));
×
1095
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1096
      goto _end;
×
1097
    }
1098

1099
    for (int32_t k = 0; k < pTSchema->numOfCols; ++k) {
16,560✔
1100
      int16_t         colIdx = k;
13,248✔
1101
      const STColumn* pCol = &pTSchema->columns[k];
13,248✔
1102
      int16_t*        slotId = taosHashGet(pInserter->pCols, &pCol->colId, sizeof(pCol->colId));
13,248✔
1103
      if (NULL == slotId) {
13,248✔
1104
        continue;
2,484✔
1105
      }
1106
      colIdx = *slotId;
10,764✔
1107

1108
      SColumnInfoData* pColInfoData = taosArrayGet(pDataBlock->pDataBlock, colIdx);
10,764✔
1109
      if (NULL == pColInfoData) {
10,764✔
1110
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1111
        tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1112
        goto _end;
×
1113
      }
1114
      void* var = POINTER_SHIFT(pColInfoData->pData, j * pColInfoData->info.bytes);
10,764✔
1115

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

1162
              SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);
×
1163
              if (NULL == taosArrayPush(pVals, &cv)) {
×
1164
                tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1165
                goto _end;
×
1166
              }
1167
            } else {
1168
              // if (PRIMARYKEY_TIMESTAMP_COL_ID == pCol->colId && !needSortMerge) {
1169
              //   if (*(int64_t*)var <= lastTs) {
1170
              //     needSortMerge = true;
1171
              //   } else {
1172
              //     lastTs = *(int64_t*)var;
1173
              //   }
1174
              // }
1175

1176
              SValue sv = {.type = pCol->type};
10,764✔
1177
              valueSetDatum(&sv, sv.type, var, tDataTypes[pCol->type].bytes);
10,764✔
1178
              SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
10,764✔
1179
              if (NULL == taosArrayPush(pVals, &cv)) {
10,764✔
1180
                tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1181
                goto _end;
×
1182
              }
1183
            }
1184
          } else {
1185
            uError("the column type %" PRIi16 " is undefined\n", pColInfoData->info.type);
×
1186
            terrno = TSDB_CODE_APP_ERROR;
×
1187
            tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1188
            goto _end;
×
1189
          }
1190
          break;
10,764✔
1191
      }
1192
    }
1193

1194
    SRow* pRow = NULL;
3,312✔
1195
    SRowBuildScanInfo sinfo = {0};
3,312✔
1196
    if ((terrno = tRowBuild(pVals, pTSchema, &pRow, &sinfo)) < 0) {
3,312✔
1197
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1198
      goto _end;
×
1199
    }
1200
    if (NULL == taosArrayPush(tbData.aRowP, &pRow)) {
6,624✔
1201
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
1202
      goto _end;
×
1203
    }
1204

1205
    if (NULL == taosArrayPush(pReq->aSubmitTbData, &tbData)) {
6,624✔
1206
      goto _end;
×
1207
    }
1208
  }
1209

1210
_end:
1,656✔
1211
  taosArrayDestroy(pTagVals);
1,656✔
1212
  taosArrayDestroy(pVals);
1,656✔
1213

1214
  return terrno;
1,656✔
1215
}
1216

1217
int32_t buildSubmitReqFromBlock(SDataInserterHandle* pInserter, SSubmitReq2** ppReq, const SSDataBlock* pDataBlock,
38,440✔
1218
                                const STSchema* pTSchema, int64_t* uid, int32_t* vgId, tb_uid_t* suid) {
1219
  SSubmitReq2* pReq = *ppReq;
38,440✔
1220
  SArray*      pVals = NULL;
38,440✔
1221
  SArray*      pTagVals = NULL;
38,440✔
1222
  int32_t      numOfBlks = 0;
38,440✔
1223
  char*        tableName = NULL;
38,440✔
1224
  int32_t      code = 0, lino = 0;
38,440✔
1225

1226
  terrno = TSDB_CODE_SUCCESS;
38,440✔
1227

1228
  if (NULL == pReq) {
38,440✔
1229
    if (!(pReq = taosMemoryCalloc(1, sizeof(SSubmitReq2)))) {
38,440✔
1230
      goto _end;
×
1231
    }
1232

1233
    if (!(pReq->aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData)))) {
38,440✔
1234
      goto _end;
×
1235
    }
1236
  }
1237

1238
  int32_t colNum = taosArrayGetSize(pDataBlock->pDataBlock);
38,440✔
1239
  int32_t rows = pDataBlock->info.rows;
38,440✔
1240

1241
  SSubmitTbData tbData = {0};
38,440✔
1242
  if (!(tbData.aRowP = taosArrayInit(rows, sizeof(SRow*)))) {
38,440✔
1243
    goto _end;
×
1244
  }
1245
  tbData.suid = *suid;
38,440✔
1246
  tbData.uid = *uid;
38,440✔
1247
  tbData.sver = pTSchema->version;
38,440✔
1248

1249
  if (!pVals && !(pVals = taosArrayInit(colNum, sizeof(SColVal)))) {
38,440✔
1250
    taosArrayDestroy(tbData.aRowP);
×
1251
    goto _end;
×
1252
  }
1253

1254
  if (pInserter->isStbInserter) {
38,440✔
1255
    if (!pTagVals && !(pTagVals = taosArrayInit(colNum, sizeof(STagVal)))) {
×
1256
      taosArrayDestroy(tbData.aRowP);
×
1257
      goto _end;
×
1258
    }
1259
  }
1260

1261
  int64_t lastTs = TSKEY_MIN;
38,440✔
1262
  bool    needSortMerge = false;
38,440✔
1263

1264
  for (int32_t j = 0; j < rows; ++j) {  // iterate by row
734,135✔
1265
    taosArrayClear(pVals);
701,071✔
1266

1267
    int32_t offset = 0;
701,071✔
1268
    // 处理超级表的tbname和tags
1269
    if (pInserter->isStbInserter) {
701,071✔
1270
      taosArrayClear(pTagVals);
×
1271
      tbData.uid = 0;
×
1272
      *uid = 0;
×
1273
      tbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1274
      tbData.flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
×
1275

1276
      SColumnInfoData* tbname = taosArrayGet(pDataBlock->pDataBlock, 0);
×
1277
      if (NULL == tbname) {
×
1278
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1279
        qError("Insert into stable must have tbname column");
×
1280
        goto _end;
×
1281
      }
1282
      if (tbname->info.type != TSDB_DATA_TYPE_BINARY) {
×
1283
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1284
        qError("tbname column must be binary");
×
1285
        goto _end;
×
1286
      }
1287

1288
      if (colDataIsNull_s(tbname, j)) {
×
1289
        qError("insert into stable tbname column is null");
×
1290
        goto _end;
×
1291
      }
1292
      void*   data = colDataGetVarData(tbname, j);
×
1293
      SValue  sv = (SValue){TSDB_DATA_TYPE_VARCHAR, .nData = varDataLen(data),
×
1294
                            .pData = varDataVal(data)};  // address copy, no value
×
1295
      SColVal cv = COL_VAL_VALUE(0, sv);
×
1296

1297
      // 获取子表vgId
1298
      SDBVgInfo* dbInfo = NULL;
×
1299
      code = inserterGetDbVgInfo(pInserter, pInserter->dbFName, &dbInfo);
×
1300
      if (code != TSDB_CODE_SUCCESS) {
×
1301
        goto _end;
×
1302
      }
1303

1304
      char tbFullName[TSDB_TABLE_FNAME_LEN];
×
1305
      taosMemoryFreeClear(tableName);
×
1306
      tableName = taosMemoryCalloc(1, sv.nData + 1);
×
1307
      TSDB_CHECK_NULL(tableName, code, lino, _end, terrno);
×
1308
      tstrncpy(tableName, sv.pData, sv.nData);
×
1309
      tableName[sv.nData] = '\0';
×
1310

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

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

1405
      code = inserterBuildCreateTbReq(tbData.pCreateTbReq, tableName, pTag, *suid, pInserter->pNode->tableName, TagNames,
×
1406
                               pInserter->pTagSchema->nCols, TSDB_DEFAULT_TABLE_TTL);
×
1407
    }
1408

1409
    for (int32_t k = 0; k < pTSchema->numOfCols; ++k) {  // iterate by column
3,506,490✔
1410
      int16_t         colIdx = k;
2,805,419✔
1411
      const STColumn* pCol = &pTSchema->columns[k];
2,805,419✔
1412
      if (!pInserter->fullOrderColList) {
2,805,419✔
1413
        int16_t* slotId = taosHashGet(pInserter->pCols, &pCol->colId, sizeof(pCol->colId));
114,652✔
1414
        if (NULL == slotId) {
114,652✔
1415
          continue;
29,472✔
1416
        }
1417

1418
        colIdx = *slotId;
85,180✔
1419
      }
1420

1421
      SColumnInfoData* pColInfoData = taosArrayGet(pDataBlock->pDataBlock, colIdx);
2,775,947✔
1422
      if (NULL == pColInfoData) {
2,775,947✔
1423
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1424
        goto _end;
×
1425
      }
1426
      void* var = POINTER_SHIFT(pColInfoData->pData, j * pColInfoData->info.bytes);
2,775,947✔
1427

1428
      switch (pColInfoData->info.type) {
2,775,947✔
1429
        case TSDB_DATA_TYPE_NCHAR:
71,486✔
1430
        case TSDB_DATA_TYPE_VARBINARY:
1431
        case TSDB_DATA_TYPE_VARCHAR: {  // TSDB_DATA_TYPE_BINARY
1432
          if (pColInfoData->info.type != pCol->type) {
71,486✔
1433
            qError("column:%d type:%d in block dismatch with schema col:%d type:%d", colIdx, pColInfoData->info.type, k,
×
1434
                   pCol->type);
1435
            terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1436
            goto _end;
×
1437
          }
1438
          if (colDataIsNull_s(pColInfoData, j)) {
142,972✔
1439
            SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);
1,792✔
1440
            if (NULL == taosArrayPush(pVals, &cv)) {
1,792✔
1441
              goto _end;
×
1442
            }
1443
          } else {
1444
            void*  data = colDataGetVarData(pColInfoData, j);
69,694✔
1445
            SValue sv = (SValue){
209,082✔
1446
                .type = pCol->type, .nData = varDataLen(data), .pData = varDataVal(data)};  // address copy, no value
69,694✔
1447
            SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
69,694✔
1448
            if (NULL == taosArrayPush(pVals, &cv)) {
69,694✔
1449
              goto _end;
×
1450
            }
1451
          }
1452
          break;
71,486✔
1453
        }
1454
        case TSDB_DATA_TYPE_BLOB:
×
1455
        case TSDB_DATA_TYPE_MEDIUMBLOB:
1456
        case TSDB_DATA_TYPE_JSON:
1457
          qError("the column type %" PRIi16 " is defined but not implemented yet", pColInfoData->info.type);
×
1458
          terrno = TSDB_CODE_APP_ERROR;
×
1459
          goto _end;
×
1460
          break;
1461
        default:
2,704,461✔
1462
          if (pColInfoData->info.type < TSDB_DATA_TYPE_MAX && pColInfoData->info.type > TSDB_DATA_TYPE_NULL) {
2,704,461✔
1463
            if (colDataIsNull_s(pColInfoData, j)) {
5,408,922✔
1464
              if (PRIMARYKEY_TIMESTAMP_COL_ID == pCol->colId) {
4,472✔
1465
                qError("Primary timestamp column should not be null");
×
1466
                terrno = TSDB_CODE_PAR_INCORRECT_TIMESTAMP_VAL;
×
1467
                goto _end;
×
1468
              }
1469

1470
              SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);  // should use pCol->type
4,472✔
1471
              if (NULL == taosArrayPush(pVals, &cv)) {
4,472✔
1472
                goto _end;
×
1473
              }
1474
            } else {
1475
              if (PRIMARYKEY_TIMESTAMP_COL_ID == pCol->colId && !needSortMerge) {
2,699,989✔
1476
                if (*(int64_t*)var <= lastTs) {
678,375✔
1477
                  needSortMerge = true;
6,893✔
1478
                } else {
1479
                  lastTs = *(int64_t*)var;
671,482✔
1480
                }
1481
              }
1482

1483
              SValue sv = {.type = pCol->type};
2,699,989✔
1484
              valueSetDatum(&sv, sv.type, var, tDataTypes[pCol->type].bytes);
2,699,989✔
1485
              SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
2,699,989✔
1486
              if (NULL == taosArrayPush(pVals, &cv)) {
2,699,989✔
1487
                goto _end;
×
1488
              }
1489
            }
1490
          } else {
1491
            uError("the column type %" PRIi16 " is undefined\n", pColInfoData->info.type);
×
1492
            terrno = TSDB_CODE_APP_ERROR;
×
1493
            goto _end;
×
1494
          }
1495
          break;
2,704,461✔
1496
      }
1497
    }
1498

1499
    SRow*             pRow = NULL;
701,071✔
1500
    SRowBuildScanInfo sinfo = {0};
701,071✔
1501
    if ((terrno = tRowBuild(pVals, pTSchema, &pRow, &sinfo)) < 0) {
701,071✔
1502
      tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
5,376✔
1503
      goto _end;
5,376✔
1504
    }
1505
    if (NULL == taosArrayPush(tbData.aRowP, &pRow)) {
1,391,390✔
1506
      goto _end;
×
1507
    }
1508
  }
1509

1510
  if (needSortMerge) {
33,064✔
1511
    if ((tRowSort(tbData.aRowP) != TSDB_CODE_SUCCESS) ||
6,893✔
1512
        (terrno = tRowMerge(tbData.aRowP, (STSchema*)pTSchema, KEEP_CONSISTENCY)) != 0) {
6,893✔
1513
      goto _end;
×
1514
    }
1515
  }
1516

1517
  if (NULL == taosArrayPush(pReq->aSubmitTbData, &tbData)) {
66,128✔
1518
    goto _end;
×
1519
  }
1520

1521
_end:
38,440✔
1522

1523
  taosMemoryFreeClear(tableName);
38,440✔
1524

1525
  taosArrayDestroy(pTagVals);
38,440✔
1526
  taosArrayDestroy(pVals);
38,440✔
1527

1528
  if (terrno != 0) {
38,440✔
1529
    *ppReq = NULL;
5,376✔
1530
    if (pReq) {
5,376✔
1531
      tDestroySubmitReq(pReq, TSDB_MSG_FLG_ENCODE);
5,376✔
1532
      taosMemoryFree(pReq);
5,376✔
1533
    }
1534

1535
    return terrno;
5,376✔
1536
  }
1537
  *ppReq = pReq;
33,064✔
1538

1539
  return TSDB_CODE_SUCCESS;
33,064✔
1540
}
1541

1542
static void destroySubmitReqWrapper(void* p) {
3,312✔
1543
  SSubmitReqSendInfo* pSendInfo = *(SSubmitReqSendInfo**)p;
3,312✔
1544
  if (pSendInfo != NULL) {
3,312✔
1545
    if (pSendInfo->msg != NULL) {
3,312✔
1546
      tDestroySubmitReq(pSendInfo->msg, TSDB_MSG_FLG_ENCODE);
3,312✔
1547
      taosMemoryFree(pSendInfo->msg);
3,312✔
1548
    }
1549
    taosMemoryFree(pSendInfo);
3,312✔
1550
  }
1551
}
3,312✔
1552

1553
int32_t dataBlocksToSubmitReqArray(SDataInserterHandle* pInserter, SArray* pMsgs) {
1,656✔
1554
  const SArray*   pBlocks = pInserter->pDataBlocks;
1,656✔
1555
  const STSchema* pTSchema = pInserter->pSchema;
1,656✔
1556
  int64_t         uid = pInserter->pNode->tableId;
1,656✔
1557
  int64_t         suid = pInserter->pNode->stableId;
1,656✔
1558
  int32_t         vgId = pInserter->pNode->vgId;
1,656✔
1559
  int32_t         sz = taosArrayGetSize(pBlocks);
1,656✔
1560
  int32_t         code = 0;
1,656✔
1561

1562
  SHashObj* pHash = NULL;
1,656✔
1563
  void*     iterator = NULL;
1,656✔
1564

1565
  for (int32_t i = 0; i < sz; i++) {
3,312✔
1566
    SSDataBlock* pDataBlock = taosArrayGetP(pBlocks, i);  // pDataBlock select查询到的结果
1,656✔
1567
    if (NULL == pDataBlock) {
1,656✔
1568
      return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1569
    }
1570
    if (pHash == NULL) {
1,656✔
1571
      pHash = taosHashInit(sz * pDataBlock->info.rows, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false,
1,656✔
1572
                           HASH_ENTRY_LOCK);
1573
      if (NULL == pHash) {
1,656✔
1574
        return terrno;
×
1575
      }
1576
      taosHashSetFreeFp(pHash, destroySubmitReqWrapper);
1,656✔
1577
    }
1578
    code = buildSubmitReqFromStbBlock(pInserter, pHash, pDataBlock, pTSchema, uid, vgId, suid);
1,656✔
1579
    if (code != TSDB_CODE_SUCCESS) {
1,656✔
1580
      goto _end;
×
1581
    }
1582
  }
1583

1584
  size_t keyLen = 0;
1,656✔
1585
  while ((iterator = taosHashIterate(pHash, iterator))) {
4,968✔
1586
    SSubmitReqSendInfo* pReqSendInfo = *(SSubmitReqSendInfo**)iterator;
3,312✔
1587
    int32_t*            ctbVgId = taosHashGetKey(iterator, &keyLen);
3,312✔
1588

1589
    SSubmitTbDataSendInfo* pTbSendInfo = taosMemoryCalloc(1, sizeof(SSubmitTbDataSendInfo));
3,312✔
1590
    if (NULL == pTbSendInfo) {
3,312✔
1591
      code = terrno;
×
1592
      goto _end;
×
1593
    }
1594
    code = submitReqToMsg(*ctbVgId, pReqSendInfo->msg, &pTbSendInfo->msg.pData, &pTbSendInfo->msg.len);
3,312✔
1595
    if (code != TSDB_CODE_SUCCESS) {
3,312✔
1596
      taosMemoryFree(pTbSendInfo);
×
1597
      goto _end;
×
1598
    }
1599
    pTbSendInfo->epSet = pReqSendInfo->epSet;
3,312✔
1600
    if (NULL == taosArrayPush(pMsgs, &pTbSendInfo)) {
3,312✔
1601
      taosMemoryFree(pTbSendInfo->msg.pData);
×
1602
      taosMemoryFree(pTbSendInfo);
×
1603
      code = terrno;
×
1604
      goto _end;
×
1605
    }
1606
  }
1607

1608
_end:
1,656✔
1609
  if (pHash != NULL) {
1,656✔
1610
    taosHashCleanup(pHash);
1,656✔
1611
  }
1612

1613
  return code;
1,656✔
1614
}
1615

1616
int32_t dataBlocksToSubmitReq(SDataInserterHandle* pInserter, void** pMsg, int32_t* msgLen) {
38,440✔
1617
  const SArray*   pBlocks = pInserter->pDataBlocks;
38,440✔
1618
  const STSchema* pTSchema = pInserter->pSchema;
38,440✔
1619
  int64_t         uid = pInserter->pNode->tableId;
38,440✔
1620
  int64_t         suid = pInserter->pNode->stableId;
38,440✔
1621
  int32_t         vgId = pInserter->pNode->vgId;
38,440✔
1622
  int32_t         sz = taosArrayGetSize(pBlocks);
38,440✔
1623
  int32_t         code = 0;
38,440✔
1624
  SSubmitReq2*    pReq = NULL;
38,440✔
1625

1626
  for (int32_t i = 0; i < sz; i++) {
71,504✔
1627
    SSDataBlock* pDataBlock = taosArrayGetP(pBlocks, i);  // pDataBlock select查询到的结果
38,440✔
1628
    if (NULL == pDataBlock) {
38,440✔
1629
      return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1630
    }
1631
    code = buildSubmitReqFromBlock(pInserter, &pReq, pDataBlock, pTSchema, &uid, &vgId, &suid);
38,440✔
1632
    if (code) {
38,440✔
1633
      if (pReq) {
5,376✔
1634
        tDestroySubmitReq(pReq, TSDB_MSG_FLG_ENCODE);
×
1635
        taosMemoryFree(pReq);
×
1636
      }
1637

1638
      return code;
5,376✔
1639
    }
1640
  }
1641

1642
  code = submitReqToMsg(vgId, pReq, pMsg, msgLen);
33,064✔
1643
  tDestroySubmitReq(pReq, TSDB_MSG_FLG_ENCODE);
33,064✔
1644
  taosMemoryFree(pReq);
33,064✔
1645

1646
  return code;
33,064✔
1647
}
1648

1649
int32_t getStreamInsertTableInfo(int64_t streamId, int64_t groupId, SInsertTableInfo*** ppTbInfo) {
42,718✔
1650
  int64_t            key[2] = {streamId, groupId};
42,718✔
1651
  SInsertTableInfo** pTmp = taosHashAcquire(gStreamGrpTableHash, key, sizeof(key));
42,718✔
1652
  if (NULL == pTmp || *pTmp == NULL) {
42,718✔
1653
    return TSDB_CODE_STREAM_INSERT_TBINFO_NOT_FOUND;
×
1654
  }
1655

1656
  *ppTbInfo = pTmp;
42,718✔
1657
  return TSDB_CODE_SUCCESS;
42,718✔
1658
}
1659

1660
static int32_t releaseStreamInsertTableInfo(SInsertTableInfo** ppTbInfo) {
42,718✔
1661
  taosHashRelease(gStreamGrpTableHash, ppTbInfo);
42,718✔
1662
  return TSDB_CODE_SUCCESS;
42,718✔
1663
}
1664

1665
int32_t buildNormalTableCreateReq(SDataInserterHandle* pInserter, SStreamInserterParam* pInsertParam,
2,420✔
1666
                                  SSubmitTbData* tbData) {
1667
  int32_t code = TSDB_CODE_SUCCESS;
2,420✔
1668

1669
  tbData->suid = 0;
2,420✔
1670

1671
  tbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
2,420✔
1672
  if (NULL == tbData->pCreateTbReq) {
2,420✔
1673
    goto _end;
×
1674
  }
1675
  tbData->flags |= (SUBMIT_REQ_AUTO_CREATE_TABLE | SUBMIT_REQ_SCHEMA_RES);
2,420✔
1676
  tbData->pCreateTbReq->type = TSDB_NORMAL_TABLE;
2,420✔
1677
  tbData->pCreateTbReq->flags |= (TD_CREATE_NORMAL_TB_IN_STREAM | TD_CREATE_IF_NOT_EXISTS);
2,420✔
1678
  tbData->pCreateTbReq->uid = 0;
2,420✔
1679
  tbData->sver = pInsertParam->sver;
2,420✔
1680

1681
  tbData->pCreateTbReq->name = taosStrdup(pInsertParam->tbname);
2,420✔
1682
  if (!tbData->pCreateTbReq->name) return terrno;
2,420✔
1683

1684
  int32_t numOfCols = pInsertParam->pFields->size;
2,420✔
1685
  tbData->pCreateTbReq->ntb.schemaRow.nCols = numOfCols;
2,420✔
1686
  tbData->pCreateTbReq->ntb.schemaRow.version = 1;
2,420✔
1687

1688
  tbData->pCreateTbReq->ntb.schemaRow.pSchema = taosMemoryCalloc(numOfCols, sizeof(SSchema));
2,420✔
1689
  if (NULL == tbData->pCreateTbReq->ntb.schemaRow.pSchema) {
2,420✔
1690
    goto _end;
×
1691
  }
1692
  for (int32_t i = 0; i < numOfCols; ++i) {
21,140✔
1693
    SFieldWithOptions* pField = taosArrayGet(pInsertParam->pFields, i);
18,720✔
1694
    if (NULL == pField) {
18,720✔
1695
      terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1696
      goto _end;
×
1697
    }
1698
    tbData->pCreateTbReq->ntb.schemaRow.pSchema[i].colId = i + 1;
18,720✔
1699
    tbData->pCreateTbReq->ntb.schemaRow.pSchema[i].type = pField->type;
18,720✔
1700
    tbData->pCreateTbReq->ntb.schemaRow.pSchema[i].bytes = pField->bytes;
18,720✔
1701
    tbData->pCreateTbReq->ntb.schemaRow.pSchema[i].flags = pField->flags;
18,720✔
1702
    if (i == 0 && pField->type != TSDB_DATA_TYPE_TIMESTAMP) {
18,720✔
1703
      terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1704
      qError("buildNormalTableCreateReq, the first column must be timestamp.");
×
1705
      goto _end;
×
1706
    }
1707
    snprintf(tbData->pCreateTbReq->ntb.schemaRow.pSchema[i].name, TSDB_COL_NAME_LEN, "%s", pField->name);
18,720✔
1708
    if (IS_DECIMAL_TYPE(pField->type)) {
18,720✔
1709
      if (!tbData->pCreateTbReq->pExtSchemas) {
×
1710
        tbData->pCreateTbReq->pExtSchemas = taosMemoryCalloc(numOfCols, sizeof(SExtSchema));
×
1711
        if (NULL == tbData->pCreateTbReq->pExtSchemas) {
×
1712
          tdDestroySVCreateTbReq(tbData->pCreateTbReq);
×
1713
          tbData->pCreateTbReq = NULL;
×
1714
          return terrno;
×
1715
        }
1716
      }
1717
      tbData->pCreateTbReq->pExtSchemas[i].typeMod = pField->typeMod;
×
1718
    }
1719
  }
1720
  return TSDB_CODE_SUCCESS;
2,420✔
1721
_end:
×
1722
  return code;
×
1723
}
1724

1725
// reference tBuildTSchema funciton
1726
static int32_t buildTSchmaFromInserter(SStreamInserterParam* pInsertParam, STSchema** ppTSchema) {
12,017✔
1727
  int32_t code = TSDB_CODE_SUCCESS;
12,017✔
1728

1729
  int32_t   numOfCols = pInsertParam->pFields->size;
12,017✔
1730
  STSchema* pTSchema = taosMemoryCalloc(1, sizeof(STSchema) + sizeof(STColumn) * numOfCols);
12,017✔
1731
  if (NULL == pTSchema) {
12,017✔
1732
    return terrno;
×
1733
  }
1734
  if (pInsertParam->tbType == TSDB_NORMAL_TABLE) {
12,017✔
1735
    pTSchema->version =
2,420✔
1736
        1;  // normal table version start from 1, if has exist table, it will be reset by resetInserterTbVersion
1737
  } else {
1738
    pTSchema->version = pInsertParam->sver;
9,597✔
1739
  }
1740
  pTSchema->numOfCols = numOfCols;
12,017✔
1741

1742
  SFieldWithOptions* pField = taosArrayGet(pInsertParam->pFields, 0);
12,017✔
1743
  if (NULL == pField) {
12,017✔
1744
    code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1745
    goto _end;
×
1746
  }
1747
  pTSchema->columns[0].colId = PRIMARYKEY_TIMESTAMP_COL_ID;
12,017✔
1748
  pTSchema->columns[0].type = pField->type;
12,017✔
1749
  pTSchema->columns[0].flags = pField->flags;
12,017✔
1750
  pTSchema->columns[0].bytes = TYPE_BYTES[pField->type];
12,017✔
1751
  pTSchema->columns[0].offset = -1;
12,017✔
1752

1753
  pTSchema->tlen = 0;
12,017✔
1754
  pTSchema->flen = 0;
12,017✔
1755
  for (int32_t i = 1; i < numOfCols; ++i) {
69,854✔
1756
    SFieldWithOptions* pField = taosArrayGet(pInsertParam->pFields, i);
57,837✔
1757
    if (NULL == pField) {
57,837✔
1758
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1759
      goto _end;
×
1760
    }
1761
    pTSchema->columns[i].colId = i + 1;
57,837✔
1762
    pTSchema->columns[i].type = pField->type;
57,837✔
1763
    pTSchema->columns[i].flags = pField->flags;
57,837✔
1764
    pTSchema->columns[i].bytes = pField->bytes;
57,837✔
1765
    pTSchema->columns[i].offset = pTSchema->flen;
57,837✔
1766

1767
    if (IS_VAR_DATA_TYPE(pField->type)) {
57,837✔
1768
      pTSchema->columns[i].bytes = pField->bytes;
1,528✔
1769
      pTSchema->tlen += (TYPE_BYTES[pField->type] + pField->bytes);
1,528✔
1770
    } else {
1771
      pTSchema->columns[i].bytes = TYPE_BYTES[pField->type];
56,309✔
1772
      pTSchema->tlen += TYPE_BYTES[pField->type];
56,309✔
1773
    }
1774

1775
    pTSchema->flen += TYPE_BYTES[pField->type];
57,837✔
1776
  }
1777

1778
#if 1
1779
  pTSchema->tlen += (int32_t)TD_BITMAP_BYTES(numOfCols);
12,017✔
1780
#endif
1781

1782
_end:
12,017✔
1783
  if (code != TSDB_CODE_SUCCESS) {
12,017✔
1784
    taosMemoryFree(pTSchema);
×
1785
    *ppTSchema = NULL;
×
1786
  } else {
1787
    *ppTSchema = pTSchema;
12,017✔
1788
  }
1789
  return code;
12,017✔
1790
}
1791

1792
static int32_t getTagValsFromStreamInserterInfo(SStreamDataInserterInfo* pInserterInfo, int32_t preCols,
9,597✔
1793
                                                SArray** ppTagVals) {
1794
  int32_t code = TSDB_CODE_SUCCESS;
9,597✔
1795
  int32_t nTags = pInserterInfo->pTagVals->size;
9,597✔
1796
  *ppTagVals = taosArrayInit(nTags, sizeof(STagVal));
9,597✔
1797
  if (!ppTagVals) {
9,597✔
1798
    return terrno;
×
1799
  }
1800
  for (int32_t i = 0; i < pInserterInfo->pTagVals->size; ++i) {
19,194✔
1801
    SStreamTagInfo* pTagInfo = taosArrayGet(pInserterInfo->pTagVals, i);
9,597✔
1802
    STagVal         tagVal = {
9,597✔
1803
                .cid = preCols + i + 1,
9,597✔
1804
                .type = pTagInfo->val.data.type,
9,597✔
1805
    };
1806
    if (!pTagInfo->val.isNull) {
9,597✔
1807
      if (IS_VAR_DATA_TYPE(pTagInfo->val.data.type)) {
9,597✔
1808
        tagVal.nData = pTagInfo->val.data.nData;
9,597✔
1809
        tagVal.pData = pTagInfo->val.data.pData;
9,597✔
1810
      } else {
1811
        tagVal.i64 = pTagInfo->val.data.val;
×
1812
      }
1813

1814
      if (NULL == taosArrayPush(*ppTagVals, &tagVal)) {
19,194✔
1815
        code = terrno;
×
1816
        goto _end;
×
1817
      }
1818
    }
1819
  }
1820
_end:
9,597✔
1821
  if (code != TSDB_CODE_SUCCESS) {
9,597✔
1822
    taosArrayDestroy(*ppTagVals);
×
1823
    *ppTagVals = NULL;
×
1824
  }
1825
  return code;
9,597✔
1826
}
1827

1828
static int32_t buildStreamSubTableCreateReq(SStreamRunnerTask* pTask, SDataInserterHandle* pInserter,
9,597✔
1829
                                            SStreamInserterParam* pInsertParam, SStreamDataInserterInfo* pInserterInfo,
1830
                                            SSubmitTbData* tbData) {
1831
  int32_t code = TSDB_CODE_SUCCESS;
9,597✔
1832
  STag*   pTag = NULL;
9,597✔
1833
  SArray* pTagVals = NULL;
9,597✔
1834
  SArray* TagNames = NULL;
9,597✔
1835

1836
  if (pInsertParam->pTagFields == NULL) {
9,597✔
1837
    ST_TASK_ELOG("buildStreamSubTableCreateReq, pTagFields is NULL, suid:%" PRId64 ", sver:%d", pInsertParam->suid,
×
1838
                 pInsertParam->sver);
1839
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1840
  }
1841
  if (pInserterInfo->pTagVals == NULL || pInserterInfo->pTagVals->size == 0) {
9,597✔
1842
    ST_TASK_ELOG("buildStreamSubTableCreateReq, pTagVals is NULL, suid:%" PRId64 ", sver:%d", pInsertParam->suid,
×
1843
                 pInsertParam->sver);
1844
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1845
  }
1846
  if (pInsertParam->suid <= 0 || pInsertParam->sver <= 0) {
9,597✔
1847
    ST_TASK_ELOG("buildStreamSubTableCreateReq, suid:%" PRId64
×
1848
                 ", sver:%d"
1849
                 " must be greater than 0",
1850
                 pInsertParam->suid, pInsertParam->sver);
1851
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1852
  }
1853
  int32_t nTags = pInserterInfo->pTagVals->size;
9,597✔
1854

1855
  TagNames = taosArrayInit(nTags, TSDB_COL_NAME_LEN);
9,597✔
1856
  if (!TagNames) {
9,597✔
1857
    code = terrno;
×
1858
    goto _end;
×
1859
  }
1860
  for (int32_t i = 0; i < nTags; ++i) {
19,194✔
1861
    SFieldWithOptions* pField = taosArrayGet(pInsertParam->pTagFields, i);
9,597✔
1862
    if (NULL == taosArrayPush(TagNames, pField->name)) {
19,194✔
1863
      code = terrno;
×
1864
      goto _end;
×
1865
    }
1866
  }
1867

1868
  tbData->flags |= (SUBMIT_REQ_AUTO_CREATE_TABLE | SUBMIT_REQ_SCHEMA_RES);
9,597✔
1869
  tbData->uid = 0;
9,597✔
1870
  tbData->suid = pInsertParam->suid;
9,597✔
1871
  tbData->sver = pInsertParam->sver;
9,597✔
1872

1873
  tbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
9,597✔
1874
  if (NULL == tbData->pCreateTbReq) {
9,597✔
1875
    code = terrno;
×
1876
    goto _end;
×
1877
  }
1878
  tbData->pCreateTbReq->type = TSDB_CHILD_TABLE;
9,597✔
1879
  tbData->pCreateTbReq->flags |= (TD_CREATE_SUB_TB_IN_STREAM | TD_CREATE_IF_NOT_EXISTS);
9,597✔
1880

1881
  code = getTagValsFromStreamInserterInfo(pInserterInfo, pInsertParam->pFields->size, &pTagVals);
9,597✔
1882
  if (code != TSDB_CODE_SUCCESS) {
9,597✔
1883
    goto _end;
×
1884
  }
1885

1886
  code = tTagNew(pTagVals, pInsertParam->sver, false, &pTag);
9,597✔
1887
  if (code != TSDB_CODE_SUCCESS) {
9,597✔
1888
    ST_TASK_ELOG("failed to create tag, error:%s", tstrerror(code));
×
1889
    goto _end;
×
1890
  }
1891
  code = inserterBuildCreateTbReq(tbData->pCreateTbReq, pInserterInfo->tbName, pTag, tbData->suid,
9,597✔
1892
                                  pInsertParam->stbname, TagNames, nTags, TSDB_DEFAULT_TABLE_TTL);
9,597✔
1893
  if (code != TSDB_CODE_SUCCESS) {
9,597✔
1894
    ST_TASK_ELOG("failed to build create table request, error:%s", tstrerror(code));
×
1895
    goto _end;
×
1896
  }
1897

1898
_end:
9,597✔
1899
  if (code != TSDB_CODE_SUCCESS) {
9,597✔
1900
    ST_TASK_ELOG("buildStreamSubTableCreateReq failed, error:%s", tstrerror(code));
×
1901
    if (tbData->pCreateTbReq) {
×
1902
      taosMemoryFreeClear(tbData->pCreateTbReq->name);
×
1903
      taosMemoryFreeClear(tbData->pCreateTbReq);
×
1904
    }
1905
    if (TagNames) {
×
1906
      taosArrayDestroy(TagNames);
×
1907
    }
1908
  }
1909

1910
  if (pTagVals) {
9,597✔
1911
    taosArrayDestroy(pTagVals);
9,597✔
1912
  }
1913
  return code;
9,597✔
1914
}
1915

1916
static int32_t appendInsertData(SStreamInserterParam* pInsertParam, const SSDataBlock* pDataBlock,
41,986✔
1917
                                SSubmitTbData* tbData, STSchema* pTSchema, SBuildInsertDataInfo* dataInsertInfo) {
1918
  int32_t code = TSDB_CODE_SUCCESS;
41,986✔
1919
  int32_t lino = 0;
41,986✔
1920

1921
  int32_t rows = pDataBlock ? pDataBlock->info.rows : 0;
41,986✔
1922
  int32_t numOfCols = pInsertParam->pFields->size;
41,986✔
1923
  int32_t colNum = pDataBlock ? taosArrayGetSize(pDataBlock->pDataBlock) : 0;
41,986✔
1924

1925
  SArray* pVals = NULL;
41,986✔
1926
  if (!(pVals = taosArrayInit(colNum, sizeof(SColVal)))) {
41,986✔
1927
    code = terrno;
×
1928
    QUERY_CHECK_CODE(code, lino, _end);
×
1929
  }
1930

1931
  for (int32_t j = 0; j < rows; ++j) {  // iterate by row
114,908✔
1932
    taosArrayClear(pVals);
72,922✔
1933

1934
    bool tsOrPrimaryKeyIsNull = false;
72,922✔
1935
    for (int32_t k = 0; k < numOfCols; ++k) {  // iterate by column
553,802✔
1936
      int16_t colIdx = k + 1;
480,592✔
1937

1938
      SFieldWithOptions* pCol = taosArrayGet(pInsertParam->pFields, k);
480,592✔
1939
      if (PRIMARYKEY_TIMESTAMP_COL_ID != colIdx && TSDB_DATA_TYPE_NULL == pCol->type) {
480,656✔
1940
        SColVal cv = COL_VAL_NULL(colIdx, pCol->type);
×
1941
        if (NULL == taosArrayPush(pVals, &cv)) {
×
1942
          code = terrno;
×
1943
          QUERY_CHECK_CODE(code, lino, _end);
×
1944
        }
1945
        continue;
×
1946
      }
1947

1948
      SColumnInfoData* pColInfoData = taosArrayGet(pDataBlock->pDataBlock, k);
480,656✔
1949
      if (NULL == pColInfoData) {
480,720✔
1950
        code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1951
        QUERY_CHECK_CODE(code, lino, _end);
×
1952
      }
1953
      void* var = POINTER_SHIFT(pColInfoData->pData, j * pColInfoData->info.bytes);
480,720✔
1954

1955
      if (colDataIsNull_s(pColInfoData, j) && (pCol->flags & COL_IS_KEY)) {
961,376✔
1956
        tsOrPrimaryKeyIsNull = true;
×
1957
        qDebug("Primary key column should not be null, skip this row");
×
1958
        break;
×
1959
      }
1960
      switch (pColInfoData->info.type) {
480,688✔
1961
        case TSDB_DATA_TYPE_NCHAR:
20,480✔
1962
        case TSDB_DATA_TYPE_VARBINARY:
1963
        case TSDB_DATA_TYPE_VARCHAR: {  // TSDB_DATA_TYPE_BINARY
1964
          if (pColInfoData->info.type != pCol->type) {
20,480✔
1965
            qError("tb:%s column:%d type:%d in block dismatch with schema col:%d type:%d", pInsertParam->tbname, k,
×
1966
                   pColInfoData->info.type, k, pCol->type);
1967
            code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1968
            QUERY_CHECK_CODE(code, lino, _end);
×
1969
          }
1970
          if (colDataIsNull_s(pColInfoData, j)) {
40,960✔
1971
            SColVal cv = COL_VAL_NULL(colIdx, pCol->type);
1,708✔
1972
            if (NULL == taosArrayPush(pVals, &cv)) {
1,708✔
1973
              code = terrno;
×
1974
              QUERY_CHECK_CODE(code, lino, _end);
×
1975
            }
1976
          } else {
1977
            if (pColInfoData->pData == NULL) {
18,772✔
1978
              qError("build insert tb:%s, column:%d data is NULL in block", pInsertParam->tbname, k);
×
1979
              code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1980
              QUERY_CHECK_CODE(code, lino, _end);
×
1981
            }
1982
            void*  data = colDataGetVarData(pColInfoData, j);
18,772✔
1983
            SValue sv = (SValue){
56,316✔
1984
                .type = pCol->type, .nData = varDataLen(data), .pData = varDataVal(data)};  // address copy, no value
18,772✔
1985
            SColVal cv = COL_VAL_VALUE(colIdx, sv);
18,772✔
1986
            if (NULL == taosArrayPush(pVals, &cv)) {
18,772✔
1987
              code = terrno;
×
1988
              QUERY_CHECK_CODE(code, lino, _end);
×
1989
            }
1990
          }
1991
          break;
20,480✔
1992
        }
1993
        case TSDB_DATA_TYPE_BLOB:
×
1994
        case TSDB_DATA_TYPE_JSON:
1995
        case TSDB_DATA_TYPE_MEDIUMBLOB:
1996
          qError("the column type %" PRIi16 " is defined but not implemented yet", pColInfoData->info.type);
×
1997
          code = TSDB_CODE_APP_ERROR;
×
1998
          QUERY_CHECK_CODE(code, lino, _end);
×
1999
          break;
×
2000
        default:
460,240✔
2001
          if (pColInfoData->info.type < TSDB_DATA_TYPE_MAX && pColInfoData->info.type > TSDB_DATA_TYPE_NULL) {
460,240✔
2002
            if (colDataIsNull_s(pColInfoData, j)) {
920,448✔
2003
              if (PRIMARYKEY_TIMESTAMP_COL_ID == colIdx) {
1,068✔
2004
                tsOrPrimaryKeyIsNull = true;
×
2005
                qDebug("Primary timestamp column should not be null, skip this row");
×
2006
                break;
×
2007
              }
2008

2009
              SColVal cv = COL_VAL_NULL(colIdx, pCol->type);  // should use pCol->type
1,068✔
2010
              if (NULL == taosArrayPush(pVals, &cv)) {
1,068✔
2011
                code = terrno;
×
2012
                QUERY_CHECK_CODE(code, lino, _end);
×
2013
              }
2014
            } else {
2015
              if (PRIMARYKEY_TIMESTAMP_COL_ID == colIdx && !dataInsertInfo->needSortMerge) {
459,140✔
2016
                if (*(int64_t*)var <= dataInsertInfo->lastTs) {
54,842✔
2017
                  dataInsertInfo->needSortMerge = true;
993✔
2018
                } else {
2019
                  dataInsertInfo->lastTs = *(int64_t*)var;
53,849✔
2020
                }
2021
              }
2022

2023
              SValue sv = {.type = pCol->type};
459,140✔
2024
              valueSetDatum(&sv, sv.type, var, tDataTypes[pCol->type].bytes);
459,140✔
2025
              SColVal cv = COL_VAL_VALUE(colIdx, sv);
459,204✔
2026
              if (NULL == taosArrayPush(pVals, &cv)) {
459,332✔
2027
                code = terrno;
×
2028
                QUERY_CHECK_CODE(code, lino, _end);
×
2029
              }
2030
            }
2031
          } else {
2032
            uError("the column type %" PRIi16 " is undefined\n", pColInfoData->info.type);
×
2033
            code = TSDB_CODE_APP_ERROR;
×
2034
            QUERY_CHECK_CODE(code, lino, _end);
×
2035
          }
2036
          break;
460,400✔
2037
      }
2038
      if (tsOrPrimaryKeyIsNull) break;  // skip remaining columns because the primary key is null
480,880✔
2039
    }
2040
    if (tsOrPrimaryKeyIsNull) continue;  // skip this row if primary key is null
73,210✔
2041
    SRow*             pRow = NULL;
73,210✔
2042
    SRowBuildScanInfo sinfo = {0};
72,954✔
2043
    if ((code = tRowBuild(pVals, pTSchema, &pRow, &sinfo)) != TSDB_CODE_SUCCESS) {
72,954✔
2044
      QUERY_CHECK_CODE(code, lino, _end);
×
2045
    }
2046
    if (NULL == taosArrayPush(tbData->aRowP, &pRow)) {
145,844✔
2047
      taosMemFree(pRow);
×
2048
      code = terrno;
×
2049
      QUERY_CHECK_CODE(code, lino, _end);
×
2050
    }
2051
  }
2052
  if (dataInsertInfo->isLastBlock) {
41,986✔
2053
    int32_t nRows = taosArrayGetSize(tbData->aRowP);
41,185✔
2054
    if (taosArrayGetSize(tbData->aRowP) == 0) {
41,185✔
2055
      tbData->flags |= SUBMIT_REQ_ONLY_CREATE_TABLE;
23,912✔
2056
      stDebug("no valid data to insert, try to only create tabale:%s", pInsertParam->tbname);
23,912✔
2057
    }
2058
    stDebug("appendInsertData, isLastBlock:%d, needSortMerge:%d, totalRows:%d", dataInsertInfo->isLastBlock,
41,185✔
2059
            dataInsertInfo->needSortMerge, nRows);
2060
    if (dataInsertInfo->needSortMerge) {
41,185✔
2061
      if ((tRowSort(tbData->aRowP) != TSDB_CODE_SUCCESS) ||
1,986✔
2062
          (code = tRowMerge(tbData->aRowP, (STSchema*)pTSchema, KEEP_CONSISTENCY)) != 0) {
993✔
2063
        QUERY_CHECK_CODE(code, lino, _end);
×
2064
      }
2065
    }
2066
    nRows = taosArrayGetSize(tbData->aRowP);
41,185✔
2067
    stDebug("appendInsertData, after merge, totalRows:%d", nRows);
41,185✔
2068
  }
2069

2070
_end:
801✔
2071
  taosArrayDestroy(pVals);
41,986✔
2072
  return code;
41,986✔
2073
}
2074

2075
int32_t buildStreamSubmitReqFromBlock(SStreamRunnerTask* pTask, SDataInserterHandle* pInserter,
41,986✔
2076
                                      SStreamDataInserterInfo* pInserterInfo, SSubmitReq2** ppReq,
2077
                                      const SSDataBlock* pDataBlock, SVgroupInfo* vgInfo,
2078
                                      SBuildInsertDataInfo* tbDataInfo) {
2079
  SSubmitReq2* pReq = *ppReq;
41,986✔
2080
  int32_t      numOfBlks = 0;
41,986✔
2081

2082
  int32_t               code = TSDB_CODE_SUCCESS;
41,986✔
2083
  int32_t               lino = 0;
41,986✔
2084
  SStreamInserterParam* pInsertParam = pInserter->pParam->streamInserterParam;
41,986✔
2085
  SInsertTableInfo**    ppTbInfo = NULL;
41,986✔
2086
  SInsertTableInfo*     pTbInfo = NULL;
41,986✔
2087
  STSchema*             pTSchema = NULL;
41,986✔
2088
  SSubmitTbData*        tbData = &tbDataInfo->pTbData;
41,986✔
2089
  int32_t               colNum = 0;
41,986✔
2090
  int32_t               rows = 0;
41,986✔
2091

2092
  if (NULL == pReq) {
41,986✔
2093
    if (!(pReq = taosMemoryCalloc(1, sizeof(SSubmitReq2)))) {
41,185✔
2094
      code = terrno;
×
2095
      QUERY_CHECK_CODE(code, lino, _end);
×
2096
    }
2097
    *ppReq = pReq;
41,185✔
2098

2099
    if (!(pReq->aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData)))) {
41,185✔
2100
      code = terrno;
×
2101
      QUERY_CHECK_CODE(code, lino, _end);
×
2102
    }
2103
  }
2104

2105
  if (pDataBlock) {
41,986✔
2106
    colNum = taosArrayGetSize(pDataBlock->pDataBlock);
18,074✔
2107
    rows = pDataBlock->info.rows;
18,074✔
2108
  }
2109

2110
  tbData->flags |= SUBMIT_REQ_SCHEMA_RES;
41,986✔
2111

2112
  if (tbDataInfo->isFirstBlock) {
41,986✔
2113
    if (pInserterInfo->isAutoCreateTable) {
41,185✔
2114
      code = initTableInfo(pInserter, pInserterInfo);
12,017✔
2115
      QUERY_CHECK_CODE(code, lino, _end);
12,017✔
2116
      if (pInsertParam->tbType == TSDB_NORMAL_TABLE) {
12,017✔
2117
        code = buildNormalTableCreateReq(pInserter, pInsertParam, tbData);
2,420✔
2118
      } else if (pInsertParam->tbType == TSDB_SUPER_TABLE) {
9,597✔
2119
        code = buildStreamSubTableCreateReq(pTask, pInserter, pInsertParam, pInserterInfo, tbData);
9,597✔
2120
      } else {
2121
        code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
2122
        ST_TASK_ELOG("buildStreamSubmitReqFromBlock, unknown table type %d", pInsertParam->tbType);
×
2123
      }
2124
      QUERY_CHECK_CODE(code, lino, _end);
12,017✔
2125
    }
2126
  }
2127

2128
  code = getStreamInsertTableInfo(pInserterInfo->streamId, pInserterInfo->groupId, &ppTbInfo);
41,986✔
2129
  pTbInfo = *ppTbInfo;
41,986✔
2130
  if (tbDataInfo->isFirstBlock) {
41,986✔
2131
    if (!pInserterInfo->isAutoCreateTable) {
41,185✔
2132
      tstrncpy(pInserterInfo->tbName, pTbInfo->tbname, TSDB_TABLE_NAME_LEN);
29,168✔
2133
    }
2134

2135
    tbData->uid = pTbInfo->uid;
41,185✔
2136
    tbData->sver = pTbInfo->version;
41,185✔
2137

2138
    if (pInsertParam->tbType == TSDB_SUPER_TABLE) {
41,185✔
2139
      tbData->suid = pInsertParam->suid;
36,473✔
2140
    }
2141

2142
    pTSchema = pTbInfo->pSchema;
41,185✔
2143
  } else {
2144
    pTSchema = pTbInfo->pSchema;
801✔
2145
  }
2146

2147
  code = getTableVgInfo(pInserter, pInsertParam->dbFName, pTbInfo->tbname, vgInfo);
41,986✔
2148
  QUERY_CHECK_CODE(code, lino, _end);
41,986✔
2149

2150
  ST_TASK_DLOG("[data inserter], Handle:%p, GROUP:%" PRId64 " tbname:%s autoCreate:%d uid:%" PRId64 " suid:%" PRId64
41,986✔
2151
               " sver:%d vgid:%d isLastBlock:%d",
2152
               pInserter, pInserterInfo->groupId, pInserterInfo->tbName, pInserterInfo->isAutoCreateTable, tbData->uid,
2153
               tbData->suid, tbData->sver, vgInfo->vgId, tbDataInfo->isFirstBlock);
2154

2155
  code = appendInsertData(pInsertParam, pDataBlock, tbData, pTSchema, tbDataInfo);
41,986✔
2156
  QUERY_CHECK_CODE(code, lino, _end);
41,986✔
2157

2158
_end:
41,986✔
2159
  releaseStreamInsertTableInfo(ppTbInfo);
41,986✔
2160
  if (code != TSDB_CODE_SUCCESS) {
41,986✔
2161
    ST_TASK_ELOG("buildStreamSubmitReqFromBlock, code:0x%0x, groupId:%" PRId64 " tbname:%s autoCreate:%d", code,
×
2162
                 pInserterInfo->groupId, pInserterInfo->tbName, pInserterInfo->isAutoCreateTable);
2163
  }
2164
  return code;
41,986✔
2165
}
2166

2167
int32_t streamDataBlocksToSubmitReq(SStreamRunnerTask* pTask, SDataInserterHandle* pInserter,
41,185✔
2168
                                    SStreamDataInserterInfo* pInserterInfo, void** pMsg, int32_t* msgLen,
2169
                                    SVgroupInfo* vgInfo) {
2170
  int32_t code = 0;
41,185✔
2171
  int32_t lino = 0;
41,185✔
2172

2173
  const SArray*        pBlocks = pInserter->pDataBlocks;
41,185✔
2174
  int32_t              sz = taosArrayGetSize(pBlocks);
41,185✔
2175
  SSubmitReq2*         pReq = NULL;
41,185✔
2176
  SBuildInsertDataInfo tbDataInfo = {0};
41,185✔
2177

2178
  int32_t rows = 0;
41,185✔
2179
  for (int32_t i = 0; i < sz; i++) {
83,171✔
2180
    SSDataBlock* pDataBlock = taosArrayGetP(pBlocks, i);
41,986✔
2181
    if (NULL == pDataBlock) {
41,986✔
2182
      stDebug("data block is NULL, just create empty table");
23,912✔
2183
      continue;
23,912✔
2184
    }
2185
    rows += pDataBlock->info.rows;
18,074✔
2186
  }
2187
  code = initInsertProcessInfo(&tbDataInfo, rows);
41,185✔
2188
  if (code != TSDB_CODE_SUCCESS) {
41,185✔
2189
    ST_TASK_ELOG("streamDataBlocksToSubmitReq, initInsertDataInfo failed, code:%d", code);
×
2190
    return code;
×
2191
  }
2192

2193
  for (int32_t i = 0; i < sz; i++) {
83,171✔
2194
    tbDataInfo.isFirstBlock = (i == 0);
41,986✔
2195
    tbDataInfo.isLastBlock = (i == sz - 1);
41,986✔
2196
    SSDataBlock* pDataBlock = taosArrayGetP(pBlocks, i);  // pDataBlock select查询到的结果
41,986✔
2197
    ST_TASK_DLOG("[data inserter], Handle:%p, GROUP:%" PRId64
41,986✔
2198
            " tbname:%s autoCreate:%d block: %d/%d rows:%" PRId64,
2199
            pInserter, pInserterInfo->groupId, pInserterInfo->tbName,
2200
            pInserterInfo->isAutoCreateTable, i + 1, sz, (pDataBlock != NULL ? pDataBlock->info.rows : 0));
2201
    code = buildStreamSubmitReqFromBlock(pTask, pInserter, pInserterInfo, &pReq, pDataBlock, vgInfo, &tbDataInfo);
41,986✔
2202
    QUERY_CHECK_CODE(code, lino, _end);
41,986✔
2203
  }
2204

2205
  if (NULL == taosArrayPush(pReq->aSubmitTbData, &tbDataInfo.pTbData)) {
82,370✔
2206
    code = terrno;
×
2207
    QUERY_CHECK_CODE(code, lino, _end);
×
2208
  }
2209

2210
  code = submitReqToMsg(vgInfo->vgId, pReq, pMsg, msgLen);
41,185✔
2211
  tDestroySubmitReq(pReq, TSDB_MSG_FLG_ENCODE);
41,185✔
2212
  taosMemoryFree(pReq);
41,185✔
2213
  ST_TASK_DLOG("[data inserter], submit req, vgid:%d, GROUP:%" PRId64 " tbname:%s autoCreate:%d code:%d ", vgInfo->vgId,
41,185✔
2214
               pInserterInfo->groupId, pInserterInfo->tbName, pInserterInfo->isAutoCreateTable, code);
2215

2216
_end:
41,185✔
2217
  if (code != 0) {
41,185✔
2218
    tDestroySubmitTbData(&tbDataInfo.pTbData, TSDB_MSG_FLG_ENCODE);
×
2219
    tDestroySubmitReq(pReq, TSDB_MSG_FLG_ENCODE);
×
2220
    taosMemoryFree(pReq);
×
2221
  }
2222

2223
  return code;
41,185✔
2224
}
2225

2226
static int32_t putDataBlock(SDataSinkHandle* pHandle, const SInputData* pInput, bool* pContinue) {
43,300✔
2227
  SDataInserterHandle* pInserter = (SDataInserterHandle*)pHandle;
43,300✔
2228
  if (!pInserter->explain) {
43,300✔
2229
    if (NULL == taosArrayPush(pInserter->pDataBlocks, &pInput->pData)) {
80,192✔
2230
      return terrno;
×
2231
    }
2232
    if (pInserter->isStbInserter) {
40,096✔
2233
      SArray* pMsgs = taosArrayInit(4, sizeof(POINTER_BYTES));
1,656✔
2234
      if (NULL == pMsgs) {
1,656✔
2235
        return terrno;
×
2236
      }
2237
      int32_t code = dataBlocksToSubmitReqArray(pInserter, pMsgs);
1,656✔
2238
      if (code) {
1,656✔
2239
        taosArrayDestroyP(pMsgs, destroySSubmitTbDataSendInfo);
×
2240
        return code;
×
2241
      }
2242
      taosArrayClear(pInserter->pDataBlocks);
1,656✔
2243
      for (int32_t i = 0; i < taosArrayGetSize(pMsgs); ++i) {
4,968✔
2244
        SSubmitTbDataSendInfo* pSendInfo = taosArrayGetP(pMsgs, i);
3,312✔
2245
        code = sendSubmitRequest(pInserter, NULL, pSendInfo->msg.pData, pSendInfo->msg.len,
6,624✔
2246
                                 pInserter->pParam->readHandle->pMsgCb->clientRpc, &pSendInfo->epSet);
3,312✔
2247
        taosMemoryFree(pSendInfo);
3,312✔
2248
        if (code) {
3,312✔
2249
          for (int j = i + 1; j < taosArrayGetSize(pMsgs); ++j) {
×
2250
            SSubmitTbDataSendInfo* pSendInfo2 = taosArrayGetP(pMsgs, j);
×
2251
            destroySSubmitTbDataSendInfo(pSendInfo2);
×
2252
          }
2253
          taosArrayDestroy(pMsgs);
×
2254
          return code;
×
2255
        }
2256
        QRY_ERR_RET(tsem_wait(&pInserter->ready));
3,312✔
2257

2258
        if (pInserter->submitRes.code) {
3,312✔
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 pInserter->submitRes.code;
×
2265
        }
2266
      }
2267

2268
      taosArrayDestroy(pMsgs);
1,656✔
2269

2270
    } else {
2271
      void*   pMsg = NULL;
38,440✔
2272
      int32_t msgLen = 0;
38,440✔
2273
      int32_t code = dataBlocksToSubmitReq(pInserter, &pMsg, &msgLen);
38,440✔
2274
      if (code) {
38,440✔
2275
        return code;
5,376✔
2276
      }
2277

2278
      taosArrayClear(pInserter->pDataBlocks);
33,064✔
2279

2280
      code = sendSubmitRequest(pInserter, NULL, pMsg, msgLen, pInserter->pParam->readHandle->pMsgCb->clientRpc,
33,064✔
2281
                               &pInserter->pNode->epSet);
33,064✔
2282
      if (code) {
33,064✔
2283
        return code;
×
2284
      }
2285

2286
      QRY_ERR_RET(tsem_wait(&pInserter->ready));
33,064✔
2287

2288
      if (pInserter->submitRes.code) {
33,064✔
2289
        return pInserter->submitRes.code;
×
2290
      }
2291
    }
2292
  }
2293

2294
  *pContinue = true;
37,924✔
2295

2296
  return TSDB_CODE_SUCCESS;
37,924✔
2297
}
2298

2299
static int32_t resetInserterTbVersion(SDataInserterHandle* pInserter, const SInputData* pInput) {
732✔
2300
  SInsertTableInfo** ppTbInfo = NULL;
732✔
2301
  int32_t           code = getStreamInsertTableInfo(pInput->pStreamDataInserterInfo->streamId, pInput->pStreamDataInserterInfo->groupId, &ppTbInfo);
732✔
2302
  if (code != TSDB_CODE_SUCCESS) {
732✔
2303
    return code;
×
2304
  }
2305

2306
  SInsertTableInfo*  pTbInfo  = *ppTbInfo;
732✔
2307
  stDebug("resetInserterTbVersion, streamId:0x%" PRIx64 " groupId:%" PRId64 " tbName:%s, uid:%" PRId64 ", version:%d",
732✔
2308
          pInput->pStreamDataInserterInfo->streamId, pInput->pStreamDataInserterInfo->groupId,
2309
          pInput->pStreamDataInserterInfo->tbName, pTbInfo->uid, pTbInfo->version);
2310
  if (pInserter->pParam->streamInserterParam->tbType != TSDB_NORMAL_TABLE) {
732✔
2311
    pInserter->pParam->streamInserterParam->sver = pTbInfo->version;
×
2312
  }
2313
  code = releaseStreamInsertTableInfo(ppTbInfo);
732✔
2314
  return code;
732✔
2315
}
2316

2317
static int32_t putStreamDataBlock(SDataSinkHandle* pHandle, const SInputData* pInput, bool* pContinue) {
40,453✔
2318
  int32_t              code = 0;
40,453✔
2319
  int32_t              lino = 0;
40,453✔
2320
  SDataInserterHandle* pInserter = (SDataInserterHandle*)pHandle;
40,453✔
2321
  SStreamRunnerTask*   pTask = pInput->pTask;
40,453✔
2322
  if (!pInserter || !pInserter->pParam || !pInserter->pParam->streamInserterParam) {
40,453✔
2323
    ST_TASK_ELOG("putStreamDataBlock invalid param, pInserter: %p, pParam:%p", pInserter,
×
2324
                 pInserter ? pInserter->pParam : NULL);
2325
    return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
2326
  }
2327
  if (!pInserter->explain) {
40,453✔
2328
    code = TSDB_CODE_SUCCESS;
40,453✔
2329
    if (NULL == taosArrayPush(pInserter->pDataBlocks, &pInput->pData)) {
80,906✔
2330
      return terrno;
×
2331
    }
2332
    void*       pMsg = NULL;
40,453✔
2333
    int32_t     msgLen = 0;
40,453✔
2334
    SVgroupInfo vgInfo = {0};
40,453✔
2335

2336
    code = streamDataBlocksToSubmitReq(pTask, pInserter, pInput->pStreamDataInserterInfo, &pMsg, &msgLen, &vgInfo);
40,453✔
2337
    QUERY_CHECK_CODE(code, lino, _return);
40,453✔
2338

2339
    code = sendSubmitRequest(pInserter, pInput->pStreamDataInserterInfo, pMsg, msgLen,
40,453✔
2340
                             pInserter->pParam->readHandle->pMsgCb->clientRpc, &vgInfo.epSet);
40,453✔
2341
    QUERY_CHECK_CODE(code, lino, _return);
40,453✔
2342

2343
    code = tsem_wait(&pInserter->ready);
40,453✔
2344
    QUERY_CHECK_CODE(code, lino, _return);
40,453✔
2345

2346
    if (pInserter->submitRes.code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
40,453✔
2347
      pInput->pStreamDataInserterInfo->isAutoCreateTable = false;
732✔
2348
      code = resetInserterTbVersion(pInserter, pInput);
732✔
2349
      QUERY_CHECK_CODE(code, lino, _return);
732✔
2350

2351
      code = streamDataBlocksToSubmitReq(pTask, pInserter, pInput->pStreamDataInserterInfo, &pMsg, &msgLen, &vgInfo);
732✔
2352
      QUERY_CHECK_CODE(code, lino, _return);
732✔
2353

2354
      code = sendSubmitRequest(pInserter, pInput->pStreamDataInserterInfo, pMsg, msgLen,
732✔
2355
                               pInserter->pParam->readHandle->pMsgCb->clientRpc, &vgInfo.epSet);
732✔
2356
      QUERY_CHECK_CODE(code, lino, _return);
732✔
2357

2358
      code = tsem_wait(&pInserter->ready);
732✔
2359
      QUERY_CHECK_CODE(code, lino, _return);
732✔
2360
    }
2361

2362
    if (pInput->pStreamDataInserterInfo->isAutoCreateTable &&
40,453✔
2363
        pInserter->submitRes.code == TSDB_CODE_VND_INVALID_VGROUP_ID) {
11,285✔
2364
      rmDbVgInfoFromCache(pInserter->pParam->streamInserterParam->dbFName);
801✔
2365
      ST_TASK_ILOG("putStreamDataBlock, stream inserter table info not found, groupId:%" PRId64
801✔
2366
                   ", tbName:%s. so reset dbVgInfo and try again",
2367
                   pInput->pStreamDataInserterInfo->groupId, pInput->pStreamDataInserterInfo->tbName);
2368
      return putStreamDataBlock(pHandle, pInput, pContinue);
801✔
2369
    }
2370

2371
    if ((pInserter->submitRes.code == TSDB_CODE_TDB_TABLE_NOT_EXIST &&
39,652✔
2372
         !pInput->pStreamDataInserterInfo->isAutoCreateTable) ||
×
2373
        pInserter->submitRes.code == TSDB_CODE_VND_INVALID_VGROUP_ID) {
39,652✔
2374
      rmDbVgInfoFromCache(pInserter->pParam->streamInserterParam->dbFName);
×
2375
      ST_TASK_ILOG("putStreamDataBlock, stream inserter table info not found, groupId:%" PRId64
×
2376
                   ", tbName:%s. so reset dbVgInfo",
2377
                   pInput->pStreamDataInserterInfo->groupId, pInput->pStreamDataInserterInfo->tbName);
2378
      code = TSDB_CODE_STREAM_INSERT_TBINFO_NOT_FOUND;
×
2379
      QUERY_CHECK_CODE(code, lino, _return);
×
2380
    }
2381

2382
    if (pInserter->submitRes.code) {
39,652✔
2383
      code = pInserter->submitRes.code;
244✔
2384
      ST_TASK_ELOG("submitRes err:%s, code:%0x", tstrerror(pInserter->submitRes.code), pInserter->submitRes.code);
244✔
2385
      QUERY_CHECK_CODE(code, lino, _return);
244✔
2386
    }
2387

2388
    *pContinue = true;
39,408✔
2389

2390
  _return:
39,652✔
2391
    taosArrayClear(pInserter->pDataBlocks);
39,652✔
2392
    if (code == TSDB_CODE_STREAM_NO_DATA) {
39,652✔
2393
      ST_TASK_DLOG("putStreamDataBlock, no valid data to insert, skip this block, groupID:%" PRId64,
×
2394
                   pInput->pStreamDataInserterInfo->groupId);
2395
      code = TSDB_CODE_SUCCESS;
×
2396
    } else if (code) {
39,652✔
2397
      ST_TASK_ELOG("submitRes err:%s, code:%0x lino:%d", tstrerror(code), code, lino);
244✔
2398
      return code;
244✔
2399
    }
2400
    return code;
39,408✔
2401
  }
2402
  return TSDB_CODE_SUCCESS;
×
2403
}
2404

2405
static void endPut(struct SDataSinkHandle* pHandle, uint64_t useconds) {
37,789✔
2406
  SDataInserterHandle* pInserter = (SDataInserterHandle*)pHandle;
37,789✔
2407
  (void)taosThreadMutexLock(&pInserter->mutex);
37,789✔
2408
  pInserter->queryEnd = true;
37,789✔
2409
  pInserter->useconds = useconds;
37,789✔
2410
  (void)taosThreadMutexUnlock(&pInserter->mutex);
37,789✔
2411
}
37,789✔
2412

2413
static void getDataLength(SDataSinkHandle* pHandle, int64_t* pLen, int64_t* pRawLen, bool* pQueryEnd) {
37,789✔
2414
  SDataInserterHandle* pDispatcher = (SDataInserterHandle*)pHandle;
37,789✔
2415
  *pLen = pDispatcher->submitRes.affectedRows;
37,789✔
2416
  qDebug("got total affectedRows %" PRId64, *pLen);
37,789✔
2417
}
37,789✔
2418

2419
static int32_t destroyDataSinker(SDataSinkHandle* pHandle) {
51,195✔
2420
  SDataInserterHandle* pInserter = (SDataInserterHandle*)pHandle;
51,195✔
2421
  (void)atomic_sub_fetch_64(&gDataSinkStat.cachedSize, pInserter->cachedSize);
51,195✔
2422
  taosArrayDestroy(pInserter->pDataBlocks);
51,195✔
2423
  taosMemoryFree(pInserter->pSchema);
51,195✔
2424
  if (pInserter->pParam->streamInserterParam) {
51,195✔
2425
    destroyStreamInserterParam(pInserter->pParam->streamInserterParam);
7,616✔
2426
    taosMemoryFree(pInserter->pParam->readHandle); // only for stream
7,616✔
2427
  }
2428
  taosMemoryFree(pInserter->pParam);
51,195✔
2429
  taosHashCleanup(pInserter->pCols);
51,195✔
2430
  nodesDestroyNode((SNode*)pInserter->pNode);
51,195✔
2431
  pInserter->pNode = NULL;
51,195✔
2432

2433
  (void)taosThreadMutexDestroy(&pInserter->mutex);
51,195✔
2434

2435
  taosMemoryFree(pInserter->pManager);
51,195✔
2436

2437
  if (pInserter->dbVgInfoMap) {
51,195✔
2438
    taosHashSetFreeFp(pInserter->dbVgInfoMap, freeUseDbOutput_tmp);
1,656✔
2439
    taosHashCleanup(pInserter->dbVgInfoMap);
1,656✔
2440
  }
2441

2442
  if (pInserter->pTagSchema) {
51,195✔
2443
    taosMemoryFreeClear(pInserter->pTagSchema->pSchema);
2,070✔
2444
    taosMemoryFree(pInserter->pTagSchema);
2,070✔
2445
  }
2446

2447
  return TSDB_CODE_SUCCESS;
51,195✔
2448
}
2449

2450
static int32_t getCacheSize(struct SDataSinkHandle* pHandle, uint64_t* size) {
×
2451
  SDataInserterHandle* pDispatcher = (SDataInserterHandle*)pHandle;
×
2452

2453
  *size = atomic_load_64(&pDispatcher->cachedSize);
×
2454
  return TSDB_CODE_SUCCESS;
×
2455
}
2456

2457
static int32_t getSinkFlags(struct SDataSinkHandle* pHandle, uint64_t* pFlags) {
43,165✔
2458
  SDataInserterHandle* pDispatcher = (SDataInserterHandle*)pHandle;
43,165✔
2459

2460
  *pFlags = atomic_load_64(&pDispatcher->flags);
43,165✔
2461
  return TSDB_CODE_SUCCESS;
43,165✔
2462
}
2463

2464
int32_t createDataInserter(SDataSinkManager* pManager, SDataSinkNode** ppDataSink, DataSinkHandle* pHandle,
43,579✔
2465
                           void* pParam) {
2466
  SDataSinkNode*       pDataSink = *ppDataSink;
43,579✔
2467
  SDataInserterHandle* inserter = taosMemoryCalloc(1, sizeof(SDataInserterHandle));
43,579✔
2468
  if (NULL == inserter) {
43,579✔
2469
    taosMemoryFree(pParam);
×
2470
    goto _return;
×
2471
  }
2472

2473
  SQueryInserterNode* pInserterNode = (SQueryInserterNode*)pDataSink;
43,579✔
2474
  inserter->sink.fPut = putDataBlock;
43,579✔
2475
  inserter->sink.fEndPut = endPut;
43,579✔
2476
  inserter->sink.fGetLen = getDataLength;
43,579✔
2477
  inserter->sink.fGetData = NULL;
43,579✔
2478
  inserter->sink.fDestroy = destroyDataSinker;
43,579✔
2479
  inserter->sink.fGetCacheSize = getCacheSize;
43,579✔
2480
  inserter->sink.fGetFlags = getSinkFlags;
43,579✔
2481
  inserter->pManager = pManager;
43,579✔
2482
  inserter->pNode = pInserterNode;
43,579✔
2483
  inserter->pParam = pParam;
43,579✔
2484
  inserter->status = DS_BUF_EMPTY;
43,579✔
2485
  inserter->queryEnd = false;
43,579✔
2486
  inserter->explain = pInserterNode->explain;
43,579✔
2487
  *ppDataSink = NULL;
43,579✔
2488

2489
  int64_t suid = 0;
43,579✔
2490
  int32_t code = pManager->pAPI->metaFn.getTableSchema(inserter->pParam->readHandle->vnode, pInserterNode->tableId,
43,579✔
2491
                                                       &inserter->pSchema, &suid, &inserter->pTagSchema);
2492
  if (code) {
43,579✔
2493
    terrno = code;
×
2494
    goto _return;
×
2495
  }
2496

2497
  pManager->pAPI->metaFn.getBasicInfo(inserter->pParam->readHandle->vnode, &inserter->dbFName, NULL, NULL, NULL);
43,579✔
2498

2499
  if (pInserterNode->tableType == TSDB_SUPER_TABLE) {
43,579✔
2500
    inserter->isStbInserter = true;
2,070✔
2501
  }
2502

2503
  if (pInserterNode->stableId != suid) {
43,579✔
2504
    terrno = TSDB_CODE_TDB_INVALID_TABLE_ID;
×
2505
    goto _return;
×
2506
  }
2507

2508
  inserter->pDataBlocks = taosArrayInit(1, POINTER_BYTES);
43,579✔
2509
  if (NULL == inserter->pDataBlocks) {
43,579✔
2510
    goto _return;
×
2511
  }
2512
  QRY_ERR_JRET(taosThreadMutexInit(&inserter->mutex, NULL));
43,579✔
2513

2514
  inserter->fullOrderColList = pInserterNode->pCols->length == inserter->pSchema->numOfCols;
43,579✔
2515

2516
  inserter->pCols = taosHashInit(pInserterNode->pCols->length, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT),
43,579✔
2517
                                 false, HASH_NO_LOCK);
2518
  if (NULL == inserter->pCols) {
43,579✔
2519
    goto _return;
×
2520
  }
2521

2522
  SNode*  pNode = NULL;
43,579✔
2523
  int32_t i = 0;
43,579✔
2524
  bool    foundTbname = false;
43,579✔
2525
  FOREACH(pNode, pInserterNode->pCols) {
181,226✔
2526
    if (pNode->type == QUERY_NODE_FUNCTION && ((SFunctionNode*)pNode)->funcType == FUNCTION_TYPE_TBNAME) {
137,647✔
2527
      int16_t colId = 0;
1,656✔
2528
      int16_t slotId = 0;
1,656✔
2529
      QRY_ERR_JRET(taosHashPut(inserter->pCols, &colId, sizeof(colId), &slotId, sizeof(slotId)));
1,656✔
2530
      foundTbname = true;
1,656✔
2531
      continue;
1,656✔
2532
    }
2533
    SColumnNode* pCol = (SColumnNode*)pNode;
135,991✔
2534
    QRY_ERR_JRET(taosHashPut(inserter->pCols, &pCol->colId, sizeof(pCol->colId), &pCol->slotId, sizeof(pCol->slotId)));
135,991✔
2535
    if (inserter->fullOrderColList && pCol->colId != inserter->pSchema->columns[i].colId) {
135,991✔
2536
      inserter->fullOrderColList = false;
828✔
2537
    }
2538
    ++i;
135,991✔
2539
  }
2540

2541
  if (inserter->isStbInserter && !foundTbname) {
43,579✔
2542
    QRY_ERR_JRET(TSDB_CODE_PAR_TBNAME_ERROR);
414✔
2543
  }
2544

2545
  QRY_ERR_JRET(tsem_init(&inserter->ready, 0, 0));
43,165✔
2546

2547
  inserter->dbVgInfoMap = NULL;
43,165✔
2548

2549
  *pHandle = inserter;
43,165✔
2550
  return TSDB_CODE_SUCCESS;
43,165✔
2551

2552
_return:
414✔
2553

2554
  if (inserter) {
414✔
2555
    (void)destroyDataSinker((SDataSinkHandle*)inserter);
414✔
2556
    taosMemoryFree(inserter);
414✔
2557
  } else {
2558
    taosMemoryFree(pManager);
×
2559
  }
2560

2561
  nodesDestroyNode((SNode*)*ppDataSink);
414✔
2562
  *ppDataSink = NULL;
414✔
2563

2564
  return terrno;
414✔
2565
}
2566

2567
                           
2568
static TdThreadOnce g_dbVgInfoMgrInit = PTHREAD_ONCE_INIT;
2569

2570
SDBVgInfoMgr g_dbVgInfoMgr = {0};
2571
                           
2572
void dbVgInfoMgrInitOnce() {
543✔
2573
  g_dbVgInfoMgr.dbVgInfoMap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
543✔
2574
  if (g_dbVgInfoMgr.dbVgInfoMap == NULL) {
543✔
2575
    stError("%s failed at line %d, error:%s", __FUNCTION__, __LINE__, tstrerror(terrno));
×
2576
    return;
×
2577
  }
2578

2579
  taosHashSetFreeFp(g_dbVgInfoMgr.dbVgInfoMap, freeUseDbOutput_tmp);
543✔
2580
}
2581

2582

2583

2584
int32_t createStreamDataInserter(SDataSinkManager* pManager, DataSinkHandle* pHandle, void* pParam) {
7,616✔
2585
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
7,616✔
2586

2587
  TAOS_UNUSED(taosThreadOnce(&g_dbVgInfoMgrInit, dbVgInfoMgrInitOnce));
7,616✔
2588
  TSDB_CHECK_NULL(g_dbVgInfoMgr.dbVgInfoMap, code, lino, _exit, terrno);
7,616✔
2589

2590
  SDataInserterHandle* inserter = taosMemoryCalloc(1, sizeof(SDataInserterHandle));
7,616✔
2591
  TSDB_CHECK_NULL(inserter, code, lino, _exit, terrno);
7,616✔
2592

2593
  inserter->sink.fPut = putStreamDataBlock;
7,616✔
2594
  inserter->sink.fEndPut = endPut;
7,616✔
2595
  inserter->sink.fGetLen = getDataLength;
7,616✔
2596
  inserter->sink.fGetData = NULL;
7,616✔
2597
  inserter->sink.fDestroy = destroyDataSinker;
7,616✔
2598
  inserter->sink.fGetCacheSize = getCacheSize;
7,616✔
2599
  inserter->sink.fGetFlags = getSinkFlags;
7,616✔
2600
  inserter->pManager = pManager;
7,616✔
2601
  inserter->pNode = NULL;
7,616✔
2602
  inserter->pParam = pParam;
7,616✔
2603
  inserter->status = DS_BUF_EMPTY;
7,616✔
2604
  inserter->queryEnd = false;
7,616✔
2605
  inserter->explain = false;
7,616✔
2606

2607
  inserter->pDataBlocks = taosArrayInit(1, POINTER_BYTES);
7,616✔
2608
  TSDB_CHECK_NULL(inserter->pDataBlocks, code, lino, _exit, terrno);
7,616✔
2609
  
2610
  TAOS_CHECK_EXIT(taosThreadMutexInit(&inserter->mutex, NULL));
7,616✔
2611
  TAOS_CHECK_EXIT(tsem_init(&inserter->ready, 0, 0));
7,616✔
2612

2613
  inserter->dbVgInfoMap = NULL;
7,616✔
2614

2615
  *pHandle = inserter;
7,616✔
2616
  return TSDB_CODE_SUCCESS;
7,616✔
2617

2618
_exit:
×
2619

2620
  if (inserter) {
×
2621
    (void)destroyDataSinker((SDataSinkHandle*)inserter);
×
2622
    taosMemoryFree(inserter);
×
2623
  } else {
2624
    taosMemoryFree(pManager);
×
2625
  }
2626

2627
  if (code) {
×
2628
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2629
  }
2630

2631
  return code;
×
2632
}
2633

2634
int32_t getDbVgInfoByTbName(void* clientRpc, const char* dbFName, SDBVgInfo** dbVgInfo) {
41,986✔
2635
  int32_t       code = TSDB_CODE_SUCCESS;
41,986✔
2636
  int32_t       line = 0;
41,986✔
2637
  SUseDbOutput* output = NULL;
41,986✔
2638

2639
  SUseDbOutput** find = (SUseDbOutput**)taosHashGet(g_dbVgInfoMgr.dbVgInfoMap, dbFName, strlen(dbFName));
41,986✔
2640

2641
  if (find == NULL) {
41,986✔
2642
    output = taosMemoryCalloc(1, sizeof(SUseDbOutput));
1,887✔
2643
    if (output == NULL) {
1,887✔
2644
      return TSDB_CODE_OUT_OF_MEMORY;
×
2645
    }
2646

2647
    code = buildDbVgInfoMap(clientRpc, dbFName, output);
1,887✔
2648
    QUERY_CHECK_CODE(code, line, _return);
1,887✔
2649

2650
    code = taosHashPut(g_dbVgInfoMgr.dbVgInfoMap, dbFName, strlen(dbFName), &output, POINTER_BYTES);
1,887✔
2651
    if (code == TSDB_CODE_DUP_KEY) {
1,887✔
2652
      code = TSDB_CODE_SUCCESS;
543✔
2653
      // another thread has put the same dbFName, so we need to free the output
2654
      freeUseDbOutput_tmp(&output);
543✔
2655
      find = (SUseDbOutput**)taosHashGet(g_dbVgInfoMgr.dbVgInfoMap, dbFName, strlen(dbFName));
543✔
2656
      if (find == NULL) {
543✔
2657
        QUERY_CHECK_CODE(code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR, line, _return);
×
2658
      }
2659
      output = *find;
543✔
2660
    }
2661
    QUERY_CHECK_CODE(code, line, _return);
1,887✔
2662
  } else {
2663
    output = *find;
40,099✔
2664
  }
2665

2666
  *dbVgInfo = output->dbVgroup;
41,986✔
2667
  return code;
41,986✔
2668

2669
_return:
×
2670
  qError("%s failed at line %d since %s", __func__, line, tstrerror(code));
×
2671
  freeUseDbOutput_tmp(&output);
×
2672
  return code;
×
2673
}
2674

2675
int32_t getDbVgInfoForExec(void* clientRpc, const char* dbFName, const char* tbName, SVgroupInfo* pVgInfo) {
41,986✔
2676
  SDBVgInfo* dbInfo = NULL;
41,986✔
2677
  int32_t code = 0, lino = 0;
41,986✔
2678
  char tbFullName[TSDB_TABLE_FNAME_LEN];
41,986✔
2679
  snprintf(tbFullName, TSDB_TABLE_FNAME_LEN, "%s.%s", dbFName, tbName);
41,986✔
2680
  
2681
  taosRLockLatch(&g_dbVgInfoMgr.lock);
41,986✔
2682
  
2683
  TAOS_CHECK_EXIT(getDbVgInfoByTbName(clientRpc, dbFName, &dbInfo));
41,986✔
2684

2685
  TAOS_CHECK_EXIT(inserterGetVgInfo(dbInfo, tbFullName, pVgInfo));
41,986✔
2686

2687
_exit:
41,986✔
2688

2689
  taosRUnLockLatch(&g_dbVgInfoMgr.lock);
41,986✔
2690

2691
  if (code) {
41,986✔
2692
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2693
  }
2694

2695
  return code;
41,986✔
2696
}
2697

2698
void rmDbVgInfoFromCache(const char* dbFName) {
801✔
2699
  taosWLockLatch(&g_dbVgInfoMgr.lock);
801✔
2700

2701
  TAOS_UNUSED(taosHashRemove(g_dbVgInfoMgr.dbVgInfoMap, dbFName, strlen(dbFName)));
801✔
2702

2703
  taosWUnLockLatch(&g_dbVgInfoMgr.lock);
801✔
2704
}
801✔
2705

2706
static int32_t dropTableReqToMsg(int32_t vgId, SVDropTbBatchReq* pReq, void** pData, int32_t* pLen) {
×
2707
  int32_t code = TSDB_CODE_SUCCESS;
×
2708
  int32_t len = 0;
×
2709
  void*   pBuf = NULL;
×
2710
  tEncodeSize(tEncodeSVDropTbBatchReq, pReq, len, code);
×
2711
  if (TSDB_CODE_SUCCESS == code) {
×
2712
    SEncoder encoder;
×
2713
    len += sizeof(SMsgHead);
×
2714
    pBuf = taosMemoryMalloc(len);
×
2715
    if (NULL == pBuf) {
×
2716
      return terrno;
×
2717
    }
2718
    ((SDropTbDataMsg*)pBuf)->header.vgId = htonl(vgId);
×
2719
    ((SDropTbDataMsg*)pBuf)->header.contLen = htonl(len);
×
2720
    //((SDropTbDataMsg*)pBuf)->pData = POINTER_SHIFT(pBuf, sizeof(SMsgHead));
2721
    tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SMsgHead)), len - sizeof(SMsgHead));
×
2722
    code = tEncodeSVDropTbBatchReq(&encoder, pReq);
×
2723
    tEncoderClear(&encoder);
×
2724
  }
2725

2726
  if (TSDB_CODE_SUCCESS == code) {
×
2727
    *pData = pBuf;
×
2728
    *pLen = len;
×
2729
  } else {
2730
    taosMemoryFree(pBuf);
×
2731
  }
2732

2733
  return code;
×
2734
}
2735

2736
int32_t dropTbCallback(void* param, SDataBuf* pMsg, int32_t code) {
×
2737
  SDropTbCtx* pCtx = (SDropTbCtx*)param;
×
2738
  if (code) {
×
2739
    stError("dropTbCallback, code:%d, stream:%" PRId64 " gid:%" PRId64, code, pCtx->req->streamId, pCtx->req->gid);
×
2740
  }
2741
  pCtx->code = code;
×
2742
  code = tsem_post(&pCtx->ready);
×
2743
  taosMemoryFree(pMsg->pData);
×
2744

2745
  return TSDB_CODE_SUCCESS;
×
2746
}
2747

2748
static int32_t sendDropTbRequest(SDropTbCtx* ctx, void* pMsg, int32_t msgLen, void* pTransporter, SEpSet* pEpset) {
×
2749
  // send the fetch remote task result reques
2750
  SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
×
2751
  if (NULL == pMsgSendInfo) {
×
2752
    return terrno;
×
2753
  }
2754

2755
  pMsgSendInfo->param = ctx;
×
2756
  pMsgSendInfo->paramFreeFp = NULL;
×
2757
  pMsgSendInfo->msgInfo.pData = pMsg;
×
2758
  pMsgSendInfo->msgInfo.len = msgLen;
×
2759
  pMsgSendInfo->msgType = TDMT_VND_SNODE_DROP_TABLE;
×
2760
  pMsgSendInfo->fp = dropTbCallback;
×
2761

2762
  return asyncSendMsgToServer(pTransporter, pEpset, NULL, pMsgSendInfo);
×
2763
}
2764

2765
int32_t doDropStreamTable(SMsgCb* pMsgCb, void* pTaskOutput, SSTriggerDropRequest* pReq) {
×
2766
  SStreamRunnerTaskOutput* pOutput = pTaskOutput;
×
2767
  int32_t                  code = 0;
×
2768
  int32_t                  lino = 0;
×
2769
  SVDropTbBatchReq         req = {.nReqs = 1};
×
2770
  SVDropTbReq*             pDropReq = NULL;
×
2771
  int32_t                  msgLen = 0;
×
2772
  tsem_t*                  pSem = NULL;
×
2773
  SDropTbDataMsg*          pMsg = NULL;
×
2774

2775
  SInsertTableInfo** ppTbInfo = NULL;
×
2776
  int32_t            vgId = 0;
×
2777

2778
  req.pArray = taosArrayInit_s(sizeof(SVDropTbReq), 1);
×
2779
  if (!req.pArray) return terrno;
×
2780

2781
  pDropReq = taosArrayGet(req.pArray, 0);
×
2782

2783
  code = getStreamInsertTableInfo(pReq->streamId, pReq->gid, &ppTbInfo);
×
2784
  if (TSDB_CODE_SUCCESS == code) {
×
2785
    pDropReq->name = taosStrdup((*ppTbInfo)->tbname);
×
2786
    pDropReq->suid = (*ppTbInfo)->uid;
×
2787
    pDropReq->uid = (*ppTbInfo)->uid;
×
2788
    pDropReq->igNotExists = true;
×
2789
    vgId = (*ppTbInfo)->vgid;
×
2790

2791
    int64_t key[2] = {pReq->streamId, pReq->gid};
×
2792
    TAOS_UNUSED(taosHashRemove(gStreamGrpTableHash, key, sizeof(key)));
×
2793
  } else {
2794
    code = TSDB_CODE_STREAM_INSERT_TBINFO_NOT_FOUND;
×
2795
  }
2796
  QUERY_CHECK_CODE(code, lino, _end);
×
2797

2798
  code = dropTableReqToMsg(vgId, &req, (void**)&pMsg, &msgLen);
×
2799
  QUERY_CHECK_CODE(code, lino, _end);
×
2800

2801
  SVgroupInfo vgInfo = {0};
×
2802
  code = getDbVgInfoForExec(pMsgCb->clientRpc, pOutput->outDbFName, pDropReq->name, &vgInfo);
×
2803
  QUERY_CHECK_CODE(code, lino, _end);
×
2804

2805
  SDropTbCtx ctx = {.req = pReq};
×
2806
  code = tsem_init(&ctx.ready, 0, 0);
×
2807
  QUERY_CHECK_CODE(code, lino, _end);
×
2808
  pSem = &ctx.ready;
×
2809

2810
  code = sendDropTbRequest(&ctx, pMsg, msgLen, pMsgCb->clientRpc, &vgInfo.epSet);
×
2811
  QUERY_CHECK_CODE(code, lino, _end);
×
2812
  pMsg = NULL;  // now owned by sendDropTbRequest
×
2813

2814
  code = tsem_wait(&ctx.ready);
×
2815
  code = ctx.code;
×
2816
  stDebug("doDropStreamTable,  code:0x%" PRIx32 " req:%p, streamId:0x%" PRIx64 " groupId:%" PRId64 " tbname:%s", code, pReq,
×
2817
          pReq->streamId, pReq->gid, pDropReq ? pDropReq->name : "unknown");
2818

2819
_end:
×
2820
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_STREAM_INSERT_TBINFO_NOT_FOUND) {
×
2821
    stError("doDropStreamTable, code:0x%" PRIx32 ", streamId:0x%" PRIx64 " groupId:%" PRId64 " tbname:%s", code, pReq->streamId,
×
2822
            pReq->gid, pDropReq ? pDropReq->name : "unknown");
2823
    if (pMsg) {
×
2824
      taosMemoryFreeClear(pMsg);
×
2825
    }
2826
  }
2827
  if (pSem) tsem_destroy(pSem);
×
2828
  if (pDropReq && pDropReq->name) taosMemoryFreeClear(pDropReq->name);
×
2829
  if (ppTbInfo) releaseStreamInsertTableInfo(ppTbInfo);
×
2830
  taosArrayDestroy(req.pArray);
×
2831

2832
  return code;
×
2833
}
2834

2835
int32_t doDropStreamTableByTbName(SMsgCb* pMsgCb, void* pTaskOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2836
  SStreamRunnerTaskOutput* pOutput = pTaskOutput;
×
2837
  int32_t                  code = 0;
×
2838
  int32_t                  lino = 0;
×
2839
  SVDropTbBatchReq         req = {.nReqs = 1};
×
2840
  SVDropTbReq*             pDropReq = NULL;
×
2841
  int32_t                  msgLen = 0;
×
2842
  tsem_t*                  pSem = NULL;
×
2843
  SDropTbDataMsg*          pMsg = NULL;
×
2844

2845
  TAOS_UNUSED(taosThreadOnce(&g_dbVgInfoMgrInit, dbVgInfoMgrInitOnce));
×
2846

2847
  req.pArray = taosArrayInit_s(sizeof(SVDropTbReq), 1);
×
2848
  if (!req.pArray) return terrno;
×
2849

2850
  pDropReq = taosArrayGet(req.pArray, 0);
×
2851

2852
  pDropReq->name = tbName;
×
2853
  pDropReq->igNotExists = true;
×
2854

2855
  int64_t key[2] = {pReq->streamId, pReq->gid};
×
2856
  TAOS_UNUSED(taosHashRemove(gStreamGrpTableHash, key, sizeof(key)));
×
2857

2858
  SVgroupInfo vgInfo = {0};
×
2859
  code = getDbVgInfoForExec(pMsgCb->clientRpc, pOutput->outDbFName, pDropReq->name, &vgInfo);
×
2860
  QUERY_CHECK_CODE(code, lino, _end);
×
2861

2862
  code = dropTableReqToMsg(vgInfo.vgId, &req, (void**)&pMsg, &msgLen);
×
2863
  QUERY_CHECK_CODE(code, lino, _end);
×
2864

2865
  SDropTbCtx ctx = {.req = pReq};
×
2866
  code = tsem_init(&ctx.ready, 0, 0);
×
2867
  QUERY_CHECK_CODE(code, lino, _end);
×
2868
  pSem = &ctx.ready;
×
2869

2870
  code = sendDropTbRequest(&ctx, pMsg, msgLen, pMsgCb->clientRpc, &vgInfo.epSet);
×
2871
  QUERY_CHECK_CODE(code, lino, _end);
×
2872
  pMsg = NULL;  // now owned by sendDropTbRequest
×
2873

2874
  code = tsem_wait(&ctx.ready);
×
2875
  code = ctx.code;
×
2876
  stDebug("doDropStreamTableByTbName,  code:%d req:%p, streamId:0x%" PRIx64 " groupId:%" PRId64 " tbname:%s", code, pReq,
×
2877
          pReq->streamId, pReq->gid, pDropReq ? pDropReq->name : "unknown");
2878

2879
_end:
×
2880
  if (code != TSDB_CODE_SUCCESS) {
×
2881
    stError("doDropStreamTableByTbName, code:%d, streamId:0x%" PRIx64 " groupId:%" PRId64 " tbname:%s", code, pReq->streamId,
×
2882
            pReq->gid, pDropReq ? pDropReq->name : "unknown");
2883
    if (pMsg) {
×
2884
      taosMemoryFreeClear(pMsg);
×
2885
    }
2886
  }
2887
  if (pSem) tsem_destroy(pSem);
×
2888
  taosArrayDestroy(req.pArray);
×
2889

2890
  return code;
×
2891
}
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