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

16 17
#include "tcommon.h"
#include "tmsg.h"
L
Liu Jicong 已提交
18 19
#include "tq.h"

L
Liu Jicong 已提交
20
int32_t tqBuildDeleteReq(SVnode* pVnode, const char* stbFullName, const SSDataBlock* pDataBlock,
21 22
                         SBatchDeleteReq* deleteReq) {
  int32_t          totRow = pDataBlock->info.rows;
L
Liu Jicong 已提交
23 24
  SColumnInfoData* pStartTsCol = taosArrayGet(pDataBlock->pDataBlock, START_TS_COLUMN_INDEX);
  SColumnInfoData* pEndTsCol = taosArrayGet(pDataBlock->pDataBlock, END_TS_COLUMN_INDEX);
25
  SColumnInfoData* pGidCol = taosArrayGet(pDataBlock->pDataBlock, GROUPID_COLUMN_INDEX);
26 27
  SColumnInfoData* pTbNameCol = taosArrayGet(pDataBlock->pDataBlock, TABLE_NAME_COLUMN_INDEX);

28 29
  tqDebug("stream delete msg: row %d", totRow);

30
  for (int32_t row = 0; row < totRow; row++) {
L
Liu Jicong 已提交
31 32
    int64_t startTs = *(int64_t*)colDataGetData(pStartTsCol, row);
    int64_t endTs = *(int64_t*)colDataGetData(pEndTsCol, row);
33
    int64_t groupId = *(int64_t*)colDataGetData(pGidCol, row);
34
    char*   name;
35 36 37 38 39
    void*   varTbName = NULL;
    if (!colDataIsNull(pTbNameCol, totRow, row, NULL)) {
      varTbName = colDataGetVarData(pTbNameCol, row);
    }

40 41 42 43 44 45
    if (varTbName != NULL && varTbName != (void*)-1) {
      name = taosMemoryCalloc(1, TSDB_TABLE_NAME_LEN);
      memcpy(name, varDataVal(varTbName), varDataLen(varTbName));
    } else {
      name = buildCtbNameByGroupId(stbFullName, groupId);
    }
L
Liu Jicong 已提交
46 47
    tqDebug("stream delete msg: vgId:%d, groupId :%" PRId64 ", name: %s, start ts:%" PRId64 "end ts:%" PRId64,
            pVnode->config.vgId, groupId, name, startTs, endTs);
48
#if 0
49 50 51 52
    SMetaReader mr = {0};
    metaReaderInit(&mr, pVnode->pMeta, 0);
    if (metaGetTableEntryByName(&mr, name) < 0) {
      metaReaderClear(&mr);
53
      tqDebug("stream delete msg, skip vgId:%d since no table: %s", pVnode->config.vgId, name);
54
      taosMemoryFree(name);
5
54liuyao 已提交
55
      continue;
56 57 58 59 60
    }

    int64_t uid = mr.me.uid;
    metaReaderClear(&mr);
    taosMemoryFree(name);
61
#endif
62
    SSingleDeleteReq req = {
L
Liu Jicong 已提交
63 64
        .startTs = startTs,
        .endTs = endTs,
65
    };
L
Liu Jicong 已提交
66
    strncpy(req.tbname, name, TSDB_TABLE_NAME_LEN - 1);
67 68
    taosMemoryFree(name);
    /*tqDebug("stream delete msg, active: vgId:%d, ts:%" PRId64 " name:%s", pVnode->config.vgId, ts, name);*/
69 70 71 72 73
    taosArrayPush(deleteReq->deleteReqs, &req);
  }
  return 0;
}

L
Liu Jicong 已提交
74
void tqSinkToTablePipeline(SStreamTask* pTask, void* vnode, int64_t ver, void* data) {
L
Liu Jicong 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  const SArray*   pBlocks = (const SArray*)data;
  SVnode*         pVnode = (SVnode*)vnode;
  int64_t         suid = pTask->tbSink.stbUid;
  char*           stbFullName = pTask->tbSink.stbFullName;
  STSchema*       pTSchema = pTask->tbSink.pTSchema;
  SSchemaWrapper* pSchemaWrapper = pTask->tbSink.pSchemaWrapper;

  int32_t blockSz = taosArrayGetSize(pBlocks);

  SArray* tagArray = taosArrayInit(1, sizeof(STagVal));
  if (!tagArray) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return;
  }

  tqDebug("vgId:%d, task %d write into table, block num: %d", TD_VID(pVnode), pTask->taskId, blockSz);
  for (int32_t i = 0; i < blockSz; i++) {
92
    bool         createTb = true;
L
Liu Jicong 已提交
93 94 95 96 97 98
    SSDataBlock* pDataBlock = taosArrayGet(pBlocks, i);
    if (pDataBlock->info.type == STREAM_DELETE_RESULT) {
      SBatchDeleteReq deleteReq = {0};
      deleteReq.deleteReqs = taosArrayInit(0, sizeof(SSingleDeleteReq));
      deleteReq.suid = suid;
      tqBuildDeleteReq(pVnode, stbFullName, pDataBlock, &deleteReq);
99 100 101 102
      if (taosArrayGetSize(deleteReq.deleteReqs) == 0) {
        taosArrayDestroy(deleteReq.deleteReqs);
        continue;
      }
L
Liu Jicong 已提交
103 104 105 106 107

      int32_t len;
      int32_t code;
      tEncodeSize(tEncodeSBatchDeleteReq, &deleteReq, len, code);
      if (code < 0) {
L
Liu Jicong 已提交
108 109
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return;
L
Liu Jicong 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
      }
      SEncoder encoder;
      void*    serializedDeleteReq = rpcMallocCont(len + sizeof(SMsgHead));
      void*    abuf = POINTER_SHIFT(serializedDeleteReq, sizeof(SMsgHead));
      tEncoderInit(&encoder, abuf, len);
      tEncodeSBatchDeleteReq(&encoder, &deleteReq);
      tEncoderClear(&encoder);
      taosArrayDestroy(deleteReq.deleteReqs);

      ((SMsgHead*)serializedDeleteReq)->vgId = pVnode->config.vgId;

      SRpcMsg msg = {
          .msgType = TDMT_VND_BATCH_DEL,
          .pCont = serializedDeleteReq,
          .contLen = len + sizeof(SMsgHead),
      };
      if (tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &msg) != 0) {
        tqDebug("failed to put delete req into write-queue since %s", terrstr());
      }
    } else {
130 131
      char* ctbName = NULL;
      // set child table name
L
Liu Jicong 已提交
132
      if (pDataBlock->info.parTbName[0]) {
133
        ctbName = taosStrdup(pDataBlock->info.parTbName);
L
Liu Jicong 已提交
134
      } else {
H
Haojun Liao 已提交
135
        ctbName = buildCtbNameByGroupId(stbFullName, pDataBlock->info.id.groupId);
L
Liu Jicong 已提交
136 137
      }

138 139 140 141 142 143 144
      int32_t schemaLen = 0;
      void*   schemaStr = NULL;

      int64_t     uid = 0;
      SMetaReader mr = {0};
      metaReaderInit(&mr, pVnode->pMeta, 0);
      if (metaGetTableEntryByName(&mr, ctbName) < 0) {
L
Liu Jicong 已提交
145
        metaReaderClear(&mr);
146 147 148 149 150 151 152 153 154 155 156 157
        tqDebug("vgId:%d, stream write into %s, table auto created", TD_VID(pVnode), ctbName);

        SVCreateTbReq createTbReq = {0};

        // set const
        createTbReq.flags = 0;
        createTbReq.type = TSDB_CHILD_TABLE;
        createTbReq.ctb.suid = suid;

        // set super table name
        SName name = {0};
        tNameFromString(&name, stbFullName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
158
        createTbReq.ctb.stbName = taosStrdup((char*)tNameGetTableName(&name));  // taosStrdup(stbFullName);
159 160 161 162 163 164 165 166
        createTbReq.name = ctbName;
        ctbName = NULL;

        // set tag content
        taosArrayClear(tagArray);
        STagVal tagVal = {
            .cid = taosArrayGetSize(pDataBlock->pDataBlock) + 1,
            .type = TSDB_DATA_TYPE_UBIGINT,
H
Haojun Liao 已提交
167
            .i64 = (int64_t)pDataBlock->info.id.groupId,
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        };
        taosArrayPush(tagArray, &tagVal);
        createTbReq.ctb.tagNum = taosArrayGetSize(tagArray);

        STag* pTag = NULL;
        tTagNew(tagArray, 1, false, &pTag);
        if (pTag == NULL) {
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          taosArrayDestroy(tagArray);
          tdDestroySVCreateTbReq(&createTbReq);
          return;
        }
        createTbReq.ctb.pTag = (uint8_t*)pTag;

        // set tag name
        SArray* tagName = taosArrayInit(1, TSDB_COL_NAME_LEN);
        char    tagNameStr[TSDB_COL_NAME_LEN] = {0};
        strcpy(tagNameStr, "group_id");
        taosArrayPush(tagName, tagNameStr);
        createTbReq.ctb.tagName = tagName;

        int32_t code;
        tEncodeSize(tEncodeSVCreateTbReq, &createTbReq, schemaLen, code);
        if (code < 0) {
          tdDestroySVCreateTbReq(&createTbReq);
          taosArrayDestroy(tagArray);
          return;
        }
L
Liu Jicong 已提交
196

197 198 199 200 201 202 203 204
        // set schema str
        schemaStr = taosMemoryMalloc(schemaLen);
        if (schemaStr == NULL) {
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          tdDestroySVCreateTbReq(&createTbReq);
          taosArrayDestroy(tagArray);
          return;
        }
L
Liu Jicong 已提交
205

206 207 208 209 210 211 212 213 214 215 216
        SEncoder encoder = {0};
        tEncoderInit(&encoder, schemaStr, schemaLen);
        code = tEncodeSVCreateTbReq(&encoder, &createTbReq);
        if (code < 0) {
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          tdDestroySVCreateTbReq(&createTbReq);
          taosArrayDestroy(tagArray);
          tEncoderClear(&encoder);
          taosMemoryFree(schemaStr);
          return;
        }
L
Liu Jicong 已提交
217
        tEncoderClear(&encoder);
218 219 220 221 222 223 224 225 226 227
        tdDestroySVCreateTbReq(&createTbReq);
      } else {
        if (mr.me.type != TSDB_CHILD_TABLE) {
          tqError("vgId:%d, failed to write into %s, since table type incorrect, type %d", TD_VID(pVnode), ctbName,
                  mr.me.type);
          metaReaderClear(&mr);
          taosMemoryFree(ctbName);
          continue;
        }
        if (mr.me.ctbEntry.suid != suid) {
228 229
          tqError("vgId:%d, failed to write into %s, since suid mismatch, expect suid: %" PRId64
                  ", actual suid %" PRId64 "",
L
Liu Jicong 已提交
230
                  TD_VID(pVnode), ctbName, suid, mr.me.ctbEntry.suid);
231 232 233 234 235 236 237 238
          metaReaderClear(&mr);
          taosMemoryFree(ctbName);
          continue;
        }

        createTb = false;
        uid = mr.me.uid;
        metaReaderClear(&mr);
L
Liu Jicong 已提交
239

240 241
        tqDebug("vgId:%d, stream write, table %s, uid %" PRId64 " already exist, skip create", TD_VID(pVnode), ctbName,
                uid);
L
Liu Jicong 已提交
242

243
        taosMemoryFreeClear(ctbName);
L
Liu Jicong 已提交
244 245 246 247 248 249 250 251 252
      }

      int32_t cap = sizeof(SSubmitReq);

      int32_t rows = pDataBlock->info.rows;
      int32_t maxLen = TD_ROW_MAX_BYTES_FROM_SCHEMA(pTSchema);

      cap += sizeof(SSubmitBlk) + schemaLen + rows * maxLen;

253 254 255 256
      SSubmitReq* pSubmit = rpcMallocCont(cap);
      pSubmit->header.vgId = pVnode->config.vgId;
      pSubmit->length = sizeof(SSubmitReq);
      pSubmit->numOfBlocks = htonl(1);
L
Liu Jicong 已提交
257

258
      SSubmitBlk* blkHead = POINTER_SHIFT(pSubmit, sizeof(SSubmitReq));
L
Liu Jicong 已提交
259 260 261 262 263 264 265 266

      blkHead->numOfRows = htonl(pDataBlock->info.rows);
      blkHead->sversion = htonl(pTSchema->version);
      blkHead->suid = htobe64(suid);
      // uid is assigned by vnode
      blkHead->uid = 0;
      blkHead->schemaLen = 0;

L
Liu Jicong 已提交
267
      tqDebug("tq sink pipe1, convert block2 %d, rows: %d", i, rows);
L
Liu Jicong 已提交
268 269 270 271 272 273 274 275

      int32_t dataLen = 0;
      void*   blkSchema = POINTER_SHIFT(blkHead, sizeof(SSubmitBlk));
      STSRow* rowData = blkSchema;
      if (createTb) {
        memcpy(blkSchema, schemaStr, schemaLen);
        blkHead->schemaLen = htonl(schemaLen);
        rowData = POINTER_SHIFT(blkSchema, schemaLen);
276
      } else {
L
Liu Jicong 已提交
277
        blkHead->uid = htobe64(uid);
L
Liu Jicong 已提交
278 279
      }

280
      taosMemoryFreeClear(schemaStr);
L
Liu Jicong 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294

      for (int32_t j = 0; j < rows; j++) {
        SRowBuilder rb = {0};
        tdSRowInit(&rb, pTSchema->version);
        tdSRowSetTpInfo(&rb, pTSchema->numOfCols, pTSchema->flen);
        tdSRowResetBuf(&rb, rowData);

        for (int32_t k = 0; k < pTSchema->numOfCols; k++) {
          const STColumn*  pColumn = &pTSchema->columns[k];
          SColumnInfoData* pColData = taosArrayGet(pDataBlock->pDataBlock, k);
          if (colDataIsNull_s(pColData, j)) {
            tdAppendColValToRow(&rb, pColumn->colId, pColumn->type, TD_VTYPE_NULL, NULL, false, pColumn->offset, k);
          } else {
            void* colData = colDataGetData(pColData, j);
L
Liu Jicong 已提交
295
            if (k == 0) {
L
Liu Jicong 已提交
296
              tqDebug("tq sink pipe1, row %d ts %" PRId64, j, *(int64_t*)colData);
L
Liu Jicong 已提交
297
            }
L
Liu Jicong 已提交
298 299 300 301 302 303 304 305 306 307
            tdAppendColValToRow(&rb, pColumn->colId, pColumn->type, TD_VTYPE_NORM, colData, true, pColumn->offset, k);
          }
        }
        tdSRowEnd(&rb);
        int32_t rowLen = TD_ROW_LEN(rowData);
        rowData = POINTER_SHIFT(rowData, rowLen);
        dataLen += rowLen;
      }
      blkHead->dataLen = htonl(dataLen);

308 309
      pSubmit->length += sizeof(SSubmitBlk) + schemaLen + dataLen;
      pSubmit->length = htonl(pSubmit->length);
L
Liu Jicong 已提交
310 311 312

      SRpcMsg msg = {
          .msgType = TDMT_VND_SUBMIT,
313 314
          .pCont = pSubmit,
          .contLen = ntohl(pSubmit->length),
L
Liu Jicong 已提交
315 316 317 318 319 320 321 322 323 324
      };

      if (tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &msg) != 0) {
        tqDebug("failed to put into write-queue since %s", terrstr());
      }
    }
  }
  taosArrayDestroy(tagArray);
}

325
static int32_t encodeCreateChildTableForRPC(SVCreateTbBatchReq* pReqs, int32_t vgId, void** pBuf, int32_t* contLen) {
326
  int32_t ret = 0;
5
54liuyao 已提交
327

328
  tEncodeSize(tEncodeSVCreateTbBatchReq, pReqs, *contLen, ret);
5
54liuyao 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341
  if (ret < 0) {
    ret = -1;
    goto end;
  }
  *contLen += sizeof(SMsgHead);
  *pBuf = rpcMallocCont(*contLen);
  if (NULL == *pBuf) {
    ret = -1;
    goto end;
  }
  ((SMsgHead*)(*pBuf))->vgId = vgId;
  ((SMsgHead*)(*pBuf))->contLen = htonl(*contLen);
  SEncoder coder = {0};
342
  tEncoderInit(&coder, POINTER_SHIFT(*pBuf, sizeof(SMsgHead)), (*contLen) - sizeof(SMsgHead));
343
  if (tEncodeSVCreateTbBatchReq(&coder, pReqs) < 0) {
5
54liuyao 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356
    rpcFreeCont(*pBuf);
    *pBuf = NULL;
    *contLen = 0;
    tEncoderClear(&coder);
    ret = -1;
    goto end;
  }
  tEncoderClear(&coder);

end:
  return ret;
}

357
int32_t tqPutReqToQueue(SVnode* pVnode, SVCreateTbBatchReq* pReqs) {
5
54liuyao 已提交
358 359
  void*   buf = NULL;
  int32_t tlen = 0;
360
  encodeCreateChildTableForRPC(pReqs, TD_VID(pVnode), &buf, &tlen);
5
54liuyao 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378

  SRpcMsg msg = {
      .msgType = TDMT_VND_CREATE_TABLE,
      .pCont = buf,
      .contLen = tlen,
  };

  if (tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &msg) != 0) {
    tqError("failed to put into write-queue since %s", terrstr());
  }

  return TSDB_CODE_SUCCESS;

_error:
  terrno = TSDB_CODE_OUT_OF_MEMORY;
  tqError("failed to encode submit req since %s", terrstr());
  return TSDB_CODE_OUT_OF_MEMORY;
}
379

L
Liu Jicong 已提交
380
void tqSinkToTablePipeline2(SStreamTask* pTask, void* vnode, int64_t ver, void* data) {
L
Liu Jicong 已提交
381 382 383 384 385 386
  const SArray* pBlocks = (const SArray*)data;
  SVnode*       pVnode = (SVnode*)vnode;
  int64_t       suid = pTask->tbSink.stbUid;
  char*         stbFullName = pTask->tbSink.stbFullName;
  STSchema*     pTSchema = pTask->tbSink.pTSchema;
  /*SSchemaWrapper* pSchemaWrapper = pTask->tbSink.pSchemaWrapper;*/
L
Liu Jicong 已提交
387 388 389 390 391

  int32_t blockSz = taosArrayGetSize(pBlocks);

  tqDebug("vgId:%d, task %d write into table, block num: %d", TD_VID(pVnode), pTask->taskId, blockSz);

L
Liu Jicong 已提交
392 393 394
  void*   pBuf = NULL;
  SArray* tagArray = NULL;
  SArray* pVals = NULL;
395
  SArray* crTblArray = NULL;
L
Liu Jicong 已提交
396 397 398

  for (int32_t i = 0; i < blockSz; i++) {
    SSDataBlock* pDataBlock = taosArrayGet(pBlocks, i);
5
54liuyao 已提交
399
    int32_t      rows = pDataBlock->info.rows;
L
Liu Jicong 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    if (pDataBlock->info.type == STREAM_DELETE_RESULT) {
      SBatchDeleteReq deleteReq = {0};
      deleteReq.deleteReqs = taosArrayInit(0, sizeof(SSingleDeleteReq));
      deleteReq.suid = suid;
      tqBuildDeleteReq(pVnode, stbFullName, pDataBlock, &deleteReq);
      if (taosArrayGetSize(deleteReq.deleteReqs) == 0) {
        taosArrayDestroy(deleteReq.deleteReqs);
        continue;
      }

      int32_t len;
      int32_t code;
      tEncodeSize(tEncodeSBatchDeleteReq, &deleteReq, len, code);
      if (code < 0) {
        //
        ASSERT(0);
      }
      SEncoder encoder;
      void*    serializedDeleteReq = rpcMallocCont(len + sizeof(SMsgHead));
      void*    abuf = POINTER_SHIFT(serializedDeleteReq, sizeof(SMsgHead));
      tEncoderInit(&encoder, abuf, len);
      tEncodeSBatchDeleteReq(&encoder, &deleteReq);
      tEncoderClear(&encoder);
      taosArrayDestroy(deleteReq.deleteReqs);

      ((SMsgHead*)serializedDeleteReq)->vgId = pVnode->config.vgId;

      SRpcMsg msg = {
          .msgType = TDMT_VND_BATCH_DEL,
          .pCont = serializedDeleteReq,
          .contLen = len + sizeof(SMsgHead),
      };
      if (tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &msg) != 0) {
        tqDebug("failed to put delete req into write-queue since %s", terrstr());
      }
5
54liuyao 已提交
435
    } else if (pDataBlock->info.type == STREAM_CREATE_CHILD_TABLE) {
436 437 438 439 440
      SVCreateTbBatchReq reqs = {0};
      crTblArray = reqs.pArray = taosArrayInit(1, sizeof(struct SVCreateTbReq));
      if (NULL == reqs.pArray) {
        goto _end;
      }
5
54liuyao 已提交
441
      for (int32_t rowId = 0; rowId < rows; rowId++) {
442
        SVCreateTbReq  createTbReq = {0};
443
        SVCreateTbReq* pCreateTbReq = &createTbReq;
5
54liuyao 已提交
444 445 446 447 448 449 450 451 452 453 454 455
        if (!pCreateTbReq) {
          goto _end;
        }

        // set const
        pCreateTbReq->flags = 0;
        pCreateTbReq->type = TSDB_CHILD_TABLE;
        pCreateTbReq->ctb.suid = suid;

        // set super table name
        SName name = {0};
        tNameFromString(&name, stbFullName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
456
        pCreateTbReq->ctb.stbName = taosStrdup((char*)tNameGetTableName(&name));  // taosStrdup(stbFullName);
5
54liuyao 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

        // set tag content
        int32_t size = taosArrayGetSize(pDataBlock->pDataBlock);
        if (size == 2) {
          tagArray = taosArrayInit(1, sizeof(STagVal));
          if (!tagArray) {
            goto _end;
          }
          STagVal tagVal = {
              .cid = pTSchema->numOfCols + 1,
              .type = TSDB_DATA_TYPE_UBIGINT,
              .i64 = (int64_t)pDataBlock->info.id.groupId,
          };
          taosArrayPush(tagArray, &tagVal);

          // set tag name
          SArray* tagName = taosArrayInit(1, TSDB_COL_NAME_LEN);
          char    tagNameStr[TSDB_COL_NAME_LEN] = "group_id";
          taosArrayPush(tagName, tagNameStr);
          pCreateTbReq->ctb.tagName = tagName;
        } else {
          tagArray = taosArrayInit(size - 1, sizeof(STagVal));
          if (!tagArray) {
            goto _end;
          }
          for (int32_t tagId = UD_TAG_COLUMN_INDEX, step = 1; tagId < size; tagId++, step++) {
            SColumnInfoData* pTagData = taosArrayGet(pDataBlock->pDataBlock, tagId);
484 485 486
            STagVal          tagVal = {
                         .cid = pTSchema->numOfCols + step,
                         .type = pTagData->info.type,
5
54liuyao 已提交
487 488 489
            };
            void* pData = colDataGetData(pTagData, rowId);
            if (colDataIsNull_s(pTagData, rowId)) {
5
54liuyao 已提交
490
              continue;
5
54liuyao 已提交
491 492 493 494 495 496 497 498 499
            } else if (IS_VAR_DATA_TYPE(pTagData->info.type)) {
              tagVal.nData = varDataLen(pData);
              tagVal.pData = varDataVal(pData);
            } else {
              memcpy(&tagVal.i64, pData, pTagData->info.bytes);
            }
            taosArrayPush(tagArray, &tagVal);
          }
        }
5
54liuyao 已提交
500
        pCreateTbReq->ctb.tagNum = TMAX(size - UD_TAG_COLUMN_INDEX, 1);
5
54liuyao 已提交
501 502 503 504 505 506 507 508

        STag* pTag = NULL;
        tTagNew(tagArray, 1, false, &pTag);
        tagArray = taosArrayDestroy(tagArray);
        if (pTag == NULL) {
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          goto _end;
        }
509

510

5
54liuyao 已提交
511 512 513
        pCreateTbReq->ctb.pTag = (uint8_t*)pTag;

        // set table name
5
54liuyao 已提交
514
        if (!pDataBlock->info.parTbName[0]) {
5
54liuyao 已提交
515
          SColumnInfoData* pGpIdColInfo = taosArrayGet(pDataBlock->pDataBlock, UD_GROUPID_COLUMN_INDEX);
516
          void*            pGpIdData = colDataGetData(pGpIdColInfo, rowId);
5
54liuyao 已提交
517 518
          pCreateTbReq->name = buildCtbNameByGroupId(stbFullName, *(uint64_t*)pGpIdData);
        } else {
519
          pCreateTbReq->name = taosStrdup(pDataBlock->info.parTbName);
5
54liuyao 已提交
520
        }
521
        taosArrayPush(reqs.pArray, pCreateTbReq);
5
54liuyao 已提交
522
      }
523 524 525 526 527 528 529
      reqs.nReqs = taosArrayGetSize(reqs.pArray);
      if (tqPutReqToQueue(pVnode, &reqs) != TSDB_CODE_SUCCESS) {
        goto _end;
      }
      tagArray = taosArrayDestroy(tagArray);
      taosArrayDestroyEx(crTblArray, (FDelete)tdDestroySVCreateTbReq);
      crTblArray = NULL;
L
Liu Jicong 已提交
530
    } else {
L
Liu Jicong 已提交
531
      SSubmitTbData tbData = {0};
L
Liu Jicong 已提交
532
      tqDebug("tq sink pipe2, convert block1 %d, rows: %d", i, rows);
L
Liu Jicong 已提交
533 534

      if (!(tbData.aRowP = taosArrayInit(rows, sizeof(SRow*)))) {
L
Liu Jicong 已提交
535 536
        goto _end;
      }
L
Liu Jicong 已提交
537 538 539 540 541 542

      tbData.suid = suid;
      tbData.uid = 0;  // uid is assigned by vnode
      tbData.sver = pTSchema->version;

      char* ctbName = NULL;
5
54liuyao 已提交
543
      tqDebug("vgId:%d, stream write into %s, table auto created", TD_VID(pVnode), pDataBlock->info.parTbName);
L
Liu Jicong 已提交
544
      if (pDataBlock->info.parTbName[0]) {
545
        ctbName = taosStrdup(pDataBlock->info.parTbName);
L
Liu Jicong 已提交
546 547 548 549 550 551 552 553 554 555
      } else {
        ctbName = buildCtbNameByGroupId(stbFullName, pDataBlock->info.id.groupId);
      }

      SMetaReader mr = {0};
      metaReaderInit(&mr, pVnode->pMeta, 0);
      if (metaGetTableEntryByName(&mr, ctbName) < 0) {
        metaReaderClear(&mr);
        tqDebug("vgId:%d, stream write into %s, table auto created", TD_VID(pVnode), ctbName);

L
Liu Jicong 已提交
556
        SVCreateTbReq* pCreateTbReq = NULL;
L
Liu Jicong 已提交
557

L
Liu Jicong 已提交
558 559 560 561 562 563 564 565 566 567 568 569
        if (!(pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateStbReq)))) {
          goto _end;
        };

        // set const
        pCreateTbReq->flags = 0;
        pCreateTbReq->type = TSDB_CHILD_TABLE;
        pCreateTbReq->ctb.suid = suid;

        // set super table name
        SName name = {0};
        tNameFromString(&name, stbFullName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
570
        pCreateTbReq->ctb.stbName = taosStrdup((char*)tNameGetTableName(&name));  // taosStrdup(stbFullName);
L
Liu Jicong 已提交
571 572

        // set tag content
5
54liuyao 已提交
573 574 575 576
        tagArray = taosArrayInit(1, sizeof(STagVal));
        if (!tagArray) {
          goto _end;
        }
L
Liu Jicong 已提交
577
        STagVal tagVal = {
578
            .cid = pTSchema->numOfCols + 1,
L
Liu Jicong 已提交
579 580 581 582 583 584 585 586
            .type = TSDB_DATA_TYPE_UBIGINT,
            .i64 = (int64_t)pDataBlock->info.id.groupId,
        };
        taosArrayPush(tagArray, &tagVal);
        pCreateTbReq->ctb.tagNum = taosArrayGetSize(tagArray);

        STag* pTag = NULL;
        tTagNew(tagArray, 1, false, &pTag);
5
54liuyao 已提交
587
        tagArray = taosArrayDestroy(tagArray);
L
Liu Jicong 已提交
588 589 590
        if (pTag == NULL) {
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          goto _end;
L
Liu Jicong 已提交
591
        }
L
Liu Jicong 已提交
592
        pCreateTbReq->ctb.pTag = (uint8_t*)pTag;
L
Liu Jicong 已提交
593

L
Liu Jicong 已提交
594 595 596 597 598 599
        // set tag name
        SArray* tagName = taosArrayInit(1, TSDB_COL_NAME_LEN);
        char    tagNameStr[TSDB_COL_NAME_LEN] = {0};
        strcpy(tagNameStr, "group_id");
        taosArrayPush(tagName, tagNameStr);
        pCreateTbReq->ctb.tagName = tagName;
L
Liu Jicong 已提交
600

L
Liu Jicong 已提交
601
        // set table name
L
Liu Jicong 已提交
602 603
        pCreateTbReq->name = ctbName;
        ctbName = NULL;
L
Liu Jicong 已提交
604

L
Liu Jicong 已提交
605 606 607 608 609 610 611 612 613 614
        tbData.pCreateTbReq = pCreateTbReq;
        tbData.flags = SUBMIT_REQ_AUTO_CREATE_TABLE;
      } else {
        if (mr.me.type != TSDB_CHILD_TABLE) {
          tqError("vgId:%d, failed to write into %s, since table type incorrect, type %d", TD_VID(pVnode), ctbName,
                  mr.me.type);
          metaReaderClear(&mr);
          taosMemoryFree(ctbName);
          continue;
        }
L
Liu Jicong 已提交
615

L
Liu Jicong 已提交
616 617 618 619 620 621 622
        if (mr.me.ctbEntry.suid != suid) {
          tqError("vgId:%d, failed to write into %s, since suid mismatch, expect suid: %" PRId64
                  ", actual suid %" PRId64 "",
                  TD_VID(pVnode), ctbName, suid, mr.me.ctbEntry.suid);
          metaReaderClear(&mr);
          taosMemoryFree(ctbName);
        }
L
Liu Jicong 已提交
623

L
Liu Jicong 已提交
624 625 626
        tbData.uid = mr.me.uid;
        metaReaderClear(&mr);
        taosMemoryFreeClear(ctbName);
L
Liu Jicong 已提交
627 628
      }

L
Liu Jicong 已提交
629
      // rows
L
Liu Jicong 已提交
630
      if (!pVals && !(pVals = taosArrayInit(pTSchema->numOfCols, sizeof(SColVal)))) {
L
Liu Jicong 已提交
631
        taosArrayDestroy(tbData.aRowP);
L
Liu Jicong 已提交
632 633 634 635 636
        goto _end;
      }

      for (int32_t j = 0; j < rows; j++) {
        taosArrayClear(pVals);
637
        int32_t dataIndex = 0;
L
Liu Jicong 已提交
638
        for (int32_t k = 0; k < pTSchema->numOfCols; k++) {
639
          const STColumn* pCol = &pTSchema->columns[k];
L
Liu Jicong 已提交
640
          if (k == 0) {
641
            SColumnInfoData* pColData = taosArrayGet(pDataBlock->pDataBlock, dataIndex);
642
            void*            colData = colDataGetData(pColData, j);
L
Liu Jicong 已提交
643 644
            tqDebug("tq sink pipe2, row %d, col %d ts %" PRId64, j, k, *(int64_t*)colData);
          }
645
          if (IS_SET_NULL(pCol)) {
L
Liu Jicong 已提交
646 647
            SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);
            taosArrayPush(pVals, &cv);
648
          } else {
649 650 651
            SColumnInfoData* pColData = taosArrayGet(pDataBlock->pDataBlock, dataIndex);
            if (colDataIsNull_s(pColData, j)) {
              SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);
L
Liu Jicong 已提交
652
              taosArrayPush(pVals, &cv);
653
              dataIndex++;
L
Liu Jicong 已提交
654
            } else {
655 656 657 658 659 660 661 662 663 664 665 666 667
              void* colData = colDataGetData(pColData, j);
              if (IS_STR_DATA_TYPE(pCol->type)) {
                SValue sv =
                    (SValue){.nData = varDataLen(colData), .pData = varDataVal(colData)};  // address copy, no value
                SColVal cv = COL_VAL_VALUE(pCol->colId, pCol->type, sv);
                taosArrayPush(pVals, &cv);
              } else {
                SValue sv;
                memcpy(&sv.val, colData, tDataTypes[pCol->type].bytes);
                SColVal cv = COL_VAL_VALUE(pCol->colId, pCol->type, sv);
                taosArrayPush(pVals, &cv);
              }
              dataIndex++;
L
Liu Jicong 已提交
668 669 670 671 672
            }
          }
        }
        SRow* pRow = NULL;
        if ((terrno = tRowBuild(pVals, (STSchema*)pTSchema, &pRow)) < 0) {
L
Liu Jicong 已提交
673
          tDestroySSubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
L
Liu Jicong 已提交
674 675 676
          goto _end;
        }
        ASSERT(pRow);
L
Liu Jicong 已提交
677
        taosArrayPush(tbData.aRowP, &pRow);
L
Liu Jicong 已提交
678 679
      }

L
Liu Jicong 已提交
680 681 682 683 684 685
      SSubmitReq2 submitReq = {0};
      if (!(submitReq.aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData)))) {
        goto _end;
      }

      taosArrayPush(submitReq.aSubmitTbData, &tbData);
L
Liu Jicong 已提交
686 687

      // encode
L
Liu Jicong 已提交
688
      int32_t len;
L
Liu Jicong 已提交
689
      int32_t code;
L
Liu Jicong 已提交
690
      tEncodeSize(tEncodeSSubmitReq2, &submitReq, len, code);
L
Liu Jicong 已提交
691
      SEncoder encoder;
692
      len += sizeof(SSubmitReq2Msg);
L
Liu Jicong 已提交
693 694 695 696
      pBuf = rpcMallocCont(len);
      if (NULL == pBuf) {
        goto _end;
      }
697 698 699 700
      ((SSubmitReq2Msg*)pBuf)->header.vgId = TD_VID(pVnode);
      ((SSubmitReq2Msg*)pBuf)->header.contLen = htonl(len);
      ((SSubmitReq2Msg*)pBuf)->version = htobe64(1);
      tEncoderInit(&encoder, POINTER_SHIFT(pBuf, sizeof(SSubmitReq2Msg)), len - sizeof(SSubmitReq2Msg));
L
Liu Jicong 已提交
701
      if (tEncodeSSubmitReq2(&encoder, &submitReq) < 0) {
L
Liu Jicong 已提交
702 703
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        tqError("failed to encode submit req since %s", terrstr());
L
Liu Jicong 已提交
704 705 706
        tEncoderClear(&encoder);
        rpcFreeCont(pBuf);
        continue;
L
Liu Jicong 已提交
707 708
      }
      tEncoderClear(&encoder);
L
Liu Jicong 已提交
709
      tDestroySSubmitReq2(&submitReq, TSDB_MSG_FLG_ENCODE);
L
Liu Jicong 已提交
710 711 712 713

      SRpcMsg msg = {
          .msgType = TDMT_VND_SUBMIT,
          .pCont = pBuf,
L
Liu Jicong 已提交
714
          .contLen = len,
L
Liu Jicong 已提交
715 716 717 718 719 720 721 722 723 724
      };

      if (tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &msg) != 0) {
        tqDebug("failed to put into write-queue since %s", terrstr());
      }
    }
  }
_end:
  taosArrayDestroy(tagArray);
  taosArrayDestroy(pVals);
725
  taosArrayDestroyEx(crTblArray, (FDelete)tdDestroySVCreateTbReq);
L
Liu Jicong 已提交
726 727
  // TODO: change
}