tqSink.c 25.7 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
  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;
  }

90
  tqDebug("vgId:%d, s-task:%s write into table, block num: %d", TD_VID(pVnode), pTask->id.idStr, blockSz);
L
Liu Jicong 已提交
91
  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

  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;
}
374

L
Liu Jicong 已提交
375
void tqSinkToTablePipeline2(SStreamTask* pTask, void* vnode, int64_t ver, void* data) {
L
Liu Jicong 已提交
376 377 378 379 380 381
  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 已提交
382 383 384

  int32_t blockSz = taosArrayGetSize(pBlocks);

385
  tqDebug("vgId:%d, s-task:%s write results blocks:%d into table", TD_VID(pVnode), pTask->id.idStr, blockSz);
L
Liu Jicong 已提交
386

L
Liu Jicong 已提交
387 388 389
  void*   pBuf = NULL;
  SArray* tagArray = NULL;
  SArray* pVals = NULL;
390
  SArray* crTblArray = NULL;
L
Liu Jicong 已提交
391 392 393

  for (int32_t i = 0; i < blockSz; i++) {
    SSDataBlock* pDataBlock = taosArrayGet(pBlocks, i);
5
54liuyao 已提交
394
    int32_t      rows = pDataBlock->info.rows;
L
Liu Jicong 已提交
395 396 397 398 399 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
    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 已提交
430
    } else if (pDataBlock->info.type == STREAM_CREATE_CHILD_TABLE) {
431 432 433 434 435
      SVCreateTbBatchReq reqs = {0};
      crTblArray = reqs.pArray = taosArrayInit(1, sizeof(struct SVCreateTbReq));
      if (NULL == reqs.pArray) {
        goto _end;
      }
5
54liuyao 已提交
436
      for (int32_t rowId = 0; rowId < rows; rowId++) {
437
        SVCreateTbReq  createTbReq = {0};
438
        SVCreateTbReq* pCreateTbReq = &createTbReq;
5
54liuyao 已提交
439 440 441 442 443 444 445 446 447

        // 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);
448
        pCreateTbReq->ctb.stbName = taosStrdup((char*)tNameGetTableName(&name));  // taosStrdup(stbFullName);
5
54liuyao 已提交
449 450 451 452 453 454

        // set tag content
        int32_t size = taosArrayGetSize(pDataBlock->pDataBlock);
        if (size == 2) {
          tagArray = taosArrayInit(1, sizeof(STagVal));
          if (!tagArray) {
wmmhello's avatar
wmmhello 已提交
455
            tdDestroySVCreateTbReq(pCreateTbReq);
5
54liuyao 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
            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) {
wmmhello's avatar
wmmhello 已提交
473
            tdDestroySVCreateTbReq(pCreateTbReq);
5
54liuyao 已提交
474 475 476 477
            goto _end;
          }
          for (int32_t tagId = UD_TAG_COLUMN_INDEX, step = 1; tagId < size; tagId++, step++) {
            SColumnInfoData* pTagData = taosArrayGet(pDataBlock->pDataBlock, tagId);
478 479 480

            STagVal tagVal = {.cid = pTSchema->numOfCols + step, .type = pTagData->info.type};
            void*   pData = colDataGetData(pTagData, rowId);
5
54liuyao 已提交
481
            if (colDataIsNull_s(pTagData, rowId)) {
5
54liuyao 已提交
482
              continue;
5
54liuyao 已提交
483 484 485 486 487 488 489 490 491
            } 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 已提交
492
        pCreateTbReq->ctb.tagNum = TMAX(size - UD_TAG_COLUMN_INDEX, 1);
5
54liuyao 已提交
493 494 495 496 497

        STag* pTag = NULL;
        tTagNew(tagArray, 1, false, &pTag);
        tagArray = taosArrayDestroy(tagArray);
        if (pTag == NULL) {
wmmhello's avatar
wmmhello 已提交
498
          tdDestroySVCreateTbReq(pCreateTbReq);
5
54liuyao 已提交
499 500 501
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          goto _end;
        }
502

503

5
54liuyao 已提交
504 505 506
        pCreateTbReq->ctb.pTag = (uint8_t*)pTag;

        // set table name
5
54liuyao 已提交
507
        if (!pDataBlock->info.parTbName[0]) {
5
54liuyao 已提交
508
          SColumnInfoData* pGpIdColInfo = taosArrayGet(pDataBlock->pDataBlock, UD_GROUPID_COLUMN_INDEX);
509
          void*            pGpIdData = colDataGetData(pGpIdColInfo, rowId);
5
54liuyao 已提交
510 511
          pCreateTbReq->name = buildCtbNameByGroupId(stbFullName, *(uint64_t*)pGpIdData);
        } else {
512
          pCreateTbReq->name = taosStrdup(pDataBlock->info.parTbName);
5
54liuyao 已提交
513
        }
514
        taosArrayPush(reqs.pArray, pCreateTbReq);
5
54liuyao 已提交
515
      }
516 517 518 519 520 521 522
      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 已提交
523
    } else {
L
Liu Jicong 已提交
524
      SSubmitTbData tbData = {0};
L
Liu Jicong 已提交
525
      tqDebug("tq sink pipe2, convert block1 %d, rows: %d", i, rows);
L
Liu Jicong 已提交
526 527

      if (!(tbData.aRowP = taosArrayInit(rows, sizeof(SRow*)))) {
L
Liu Jicong 已提交
528 529
        goto _end;
      }
L
Liu Jicong 已提交
530 531 532 533 534 535

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

      char* ctbName = NULL;
5
54liuyao 已提交
536
      tqDebug("vgId:%d, stream write into %s, table auto created", TD_VID(pVnode), pDataBlock->info.parTbName);
L
Liu Jicong 已提交
537
      if (pDataBlock->info.parTbName[0]) {
538
        ctbName = taosStrdup(pDataBlock->info.parTbName);
L
Liu Jicong 已提交
539 540 541 542 543 544 545 546 547 548
      } 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 已提交
549
        SVCreateTbReq* pCreateTbReq = NULL;
L
Liu Jicong 已提交
550

L
Liu Jicong 已提交
551
        if (!(pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateStbReq)))) {
wmmhello's avatar
wmmhello 已提交
552
          taosMemoryFree(ctbName);
L
Liu Jicong 已提交
553 554 555 556 557 558 559 560 561 562 563
          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);
564
        pCreateTbReq->ctb.stbName = taosStrdup((char*)tNameGetTableName(&name));  // taosStrdup(stbFullName);
L
Liu Jicong 已提交
565 566

        // set tag content
5
54liuyao 已提交
567 568
        tagArray = taosArrayInit(1, sizeof(STagVal));
        if (!tagArray) {
wmmhello's avatar
wmmhello 已提交
569 570
          taosMemoryFree(ctbName);
          tdDestroySVCreateTbReq(pCreateTbReq);
5
54liuyao 已提交
571 572
          goto _end;
        }
L
Liu Jicong 已提交
573
        STagVal tagVal = {
574
            .cid = pTSchema->numOfCols + 1,
L
Liu Jicong 已提交
575 576 577 578 579 580 581 582
            .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 已提交
583
        tagArray = taosArrayDestroy(tagArray);
L
Liu Jicong 已提交
584
        if (pTag == NULL) {
wmmhello's avatar
wmmhello 已提交
585 586
          taosMemoryFree(ctbName);
          tdDestroySVCreateTbReq(pCreateTbReq);
L
Liu Jicong 已提交
587
          terrno = TSDB_CODE_OUT_OF_MEMORY;
wmmhello's avatar
wmmhello 已提交
588 589
          taosMemoryFree(ctbName);
          tdDestroySVCreateTbReq(pCreateTbReq);
L
Liu Jicong 已提交
590
          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
        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);
5
54liuyao 已提交
622
          continue;
L
Liu Jicong 已提交
623
        }
L
Liu Jicong 已提交
624

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

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

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

L
Liu Jicong 已提交
682 683
      SSubmitReq2 submitReq = {0};
      if (!(submitReq.aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData)))) {
wmmhello's avatar
wmmhello 已提交
684
        tDestroySSubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
L
Liu Jicong 已提交
685 686 687 688
        goto _end;
      }

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

      // encode
L
Liu Jicong 已提交
691
      int32_t len;
L
Liu Jicong 已提交
692
      int32_t code;
L
Liu Jicong 已提交
693
      tEncodeSize(tEncodeSSubmitReq2, &submitReq, len, code);
L
Liu Jicong 已提交
694
      SEncoder encoder;
695
      len += sizeof(SSubmitReq2Msg);
L
Liu Jicong 已提交
696 697
      pBuf = rpcMallocCont(len);
      if (NULL == pBuf) {
wmmhello's avatar
wmmhello 已提交
698
        tDestroySSubmitReq2(&submitReq, TSDB_MSG_FLG_ENCODE);
L
Liu Jicong 已提交
699 700
        goto _end;
      }
701 702 703 704
      ((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 已提交
705
      if (tEncodeSSubmitReq2(&encoder, &submitReq) < 0) {
L
Liu Jicong 已提交
706 707
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        tqError("failed to encode submit req since %s", terrstr());
L
Liu Jicong 已提交
708 709
        tEncoderClear(&encoder);
        rpcFreeCont(pBuf);
wmmhello's avatar
wmmhello 已提交
710
        tDestroySSubmitReq2(&submitReq, TSDB_MSG_FLG_ENCODE);
L
Liu Jicong 已提交
711
        continue;
L
Liu Jicong 已提交
712 713
      }
      tEncoderClear(&encoder);
L
Liu Jicong 已提交
714
      tDestroySSubmitReq2(&submitReq, TSDB_MSG_FLG_ENCODE);
L
Liu Jicong 已提交
715 716 717 718

      SRpcMsg msg = {
          .msgType = TDMT_VND_SUBMIT,
          .pCont = pBuf,
L
Liu Jicong 已提交
719
          .contLen = len,
L
Liu Jicong 已提交
720 721 722 723 724 725 726 727 728 729
      };

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