executor.c 5.3 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * 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/>.
14 15 16
 */

#include "executor.h"
17
#include <vnode.h>
H
Haojun Liao 已提交
18
#include "executorimpl.h"
19
#include "planner.h"
L
Liu Jicong 已提交
20
#include "tdatablock.h"
L
Liu Jicong 已提交
21
#include "vnode.h"
22

L
Liu Jicong 已提交
23
static int32_t doSetStreamBlock(SOperatorInfo* pOperator, void* input, size_t numOfBlocks, int32_t type, char* id) {
H
Haojun Liao 已提交
24
  ASSERT(pOperator != NULL);
X
Xiaoyu Wang 已提交
25
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
H
Haojun Liao 已提交
26
    if (pOperator->numOfDownstream == 0) {
27
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
H
Haojun Liao 已提交
28 29
      return TSDB_CODE_QRY_APP_ERROR;
    }
H
Haojun Liao 已提交
30

H
Haojun Liao 已提交
31
    if (pOperator->numOfDownstream > 1) {  // not handle this in join query
32
      qError("join not supported for stream block scan, %s" PRIx64, id);
H
Haojun Liao 已提交
33
      return TSDB_CODE_QRY_APP_ERROR;
H
Haojun Liao 已提交
34
    }
L
Liu Jicong 已提交
35
    pOperator->status = OP_NOT_OPENED;
H
Haojun Liao 已提交
36
    return doSetStreamBlock(pOperator->pDownstream[0], input, numOfBlocks, type, id);
H
Haojun Liao 已提交
37
  } else {
38 39
    pOperator->status = OP_NOT_OPENED;

H
Haojun Liao 已提交
40
    SStreamBlockScanInfo* pInfo = pOperator->info;
41 42 43 44 45

    // the block type can not be changed in the streamscan operators
    if (pInfo->blockType == 0) {
      pInfo->blockType = type;
    } else if (pInfo->blockType != type) {
L
Liu Jicong 已提交
46 47
      return TSDB_CODE_QRY_APP_ERROR;
    }
48 49

    if (type == STREAM_DATA_TYPE_SUBMIT_BLOCK) {
50
      if (tqReadHandleSetMsg(pInfo->streamBlockReader, input, 0) < 0) {
51 52 53 54
        qError("submit msg messed up when initing stream block, %s" PRIx64, id);
        return TSDB_CODE_QRY_APP_ERROR;
      }
    } else {
H
Haojun Liao 已提交
55
      for (int32_t i = 0; i < numOfBlocks; ++i) {
L
Liu Jicong 已提交
56
        SSDataBlock* pDataBlock = &((SSDataBlock*)input)[i];
57

58
        SSDataBlock* p = createOneDataBlock(pDataBlock, false);
H
Haojun Liao 已提交
59
        p->info = pDataBlock->info;
60

L
Liu Jicong 已提交
61
        taosArrayClear(p->pDataBlock);
H
Haojun Liao 已提交
62 63 64
        taosArrayAddAll(p->pDataBlock, pDataBlock->pDataBlock);
        taosArrayPush(pInfo->pBlockLists, &p);
      }
65 66
    }

H
Haojun Liao 已提交
67 68 69 70
    return TSDB_CODE_SUCCESS;
  }
}

71
int32_t qSetStreamInput(qTaskInfo_t tinfo, const void* input, int32_t type) {
L
Liu Jicong 已提交
72
  return qSetMultiStreamInput(tinfo, input, 1, type);
H
Haojun Liao 已提交
73 74
}

L
Liu Jicong 已提交
75
int32_t qSetMultiStreamInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type) {
H
Haojun Liao 已提交
76 77 78 79
  if (tinfo == NULL) {
    return TSDB_CODE_QRY_APP_ERROR;
  }

H
Haojun Liao 已提交
80
  if (pBlocks == NULL || numOfBlocks == 0) {
H
Haojun Liao 已提交
81 82 83
    return TSDB_CODE_SUCCESS;
  }

L
Liu Jicong 已提交
84
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
H
Haojun Liao 已提交
85

H
Haojun Liao 已提交
86
  int32_t code = doSetStreamBlock(pTaskInfo->pRoot, (void**)pBlocks, numOfBlocks, type, GET_TASKID(pTaskInfo));
H
Haojun Liao 已提交
87
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
88
    qError("%s failed to set the stream block data", GET_TASKID(pTaskInfo));
H
Haojun Liao 已提交
89
  } else {
H
Haojun Liao 已提交
90
    qDebug("%s set the stream block successfully", GET_TASKID(pTaskInfo));
H
Haojun Liao 已提交
91 92 93 94 95
  }

  return code;
}

L
Liu Jicong 已提交
96 97
qTaskInfo_t qCreateStreamExecTaskInfo(void* msg, void* streamReadHandle) {
  if (msg == NULL || streamReadHandle == NULL) {
98 99 100 101
    return NULL;
  }

  // print those info into log
L
Liu Jicong 已提交
102
#if 0
L
Liu Jicong 已提交
103 104 105 106
  pMsg->sId = pMsg->sId;
  pMsg->queryId = pMsg->queryId;
  pMsg->taskId = pMsg->taskId;
  pMsg->contentLen = pMsg->contentLen;
L
Liu Jicong 已提交
107
#endif
108

109
  /*qDebugL("stream task string %s", (const char*)msg);*/
X
bugfix  
Xiaoyu Wang 已提交
110

111
  struct SSubplan* plan = NULL;
L
Liu Jicong 已提交
112
  int32_t          code = qStringToSubplan(msg, &plan);
113 114 115 116 117 118
  if (code != TSDB_CODE_SUCCESS) {
    terrno = code;
    return NULL;
  }

  qTaskInfo_t pTaskInfo = NULL;
119
  code = qCreateExecTask(streamReadHandle, 0, 0, plan, &pTaskInfo, NULL, OPTR_EXEC_MODEL_STREAM);
120 121 122 123 124 125 126 127
  if (code != TSDB_CODE_SUCCESS) {
    // TODO: destroy SSubplan & pTaskInfo
    terrno = code;
    return NULL;
  }

  return pTaskInfo;
}
128

129
int32_t qUpdateQualifiedTableId(qTaskInfo_t tinfo, const SArray* tableIdList, bool isAdd) {
L
fix  
Liu Jicong 已提交
130
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
131

132
  // traverse to the stream scanner node to add this table id
133
  SOperatorInfo* pInfo = pTaskInfo->pRoot;
L
Liu Jicong 已提交
134
  while (pInfo->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
135 136 137 138 139
    pInfo = pInfo->pDownstream[0];
  }

  SStreamBlockScanInfo* pScanInfo = pInfo->info;
  if (isAdd) {
140 141 142 143
    SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));

    SMetaReader mr = {0};
    metaReaderInit(&mr, pScanInfo->readHandle.meta, 0);
144
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
145 146 147 148
      int64_t* id = (int64_t*)taosArrayGet(tableIdList, i);

      int32_t code = metaGetTableEntryByUid(&mr, *id);
      if (code != TSDB_CODE_SUCCESS) {
149
        qError("failed to get table meta, uid:%" PRIu64 " code:%s", *id, tstrerror(terrno));
150 151 152 153 154 155 156 157 158 159 160
        continue;
      }

      ASSERT(mr.me.type == TSDB_CHILD_TABLE);
      if (mr.me.ctbEntry.suid != pScanInfo->tableUid) {
        continue;
      }

      taosArrayPush(qa, id);
    }

161 162
    metaReaderClear(&mr);

163
    qDebug(" %d qualified child tables added into stream scanner", (int32_t)taosArrayGetSize(qa));
164
    int32_t code = tqReadHandleAddTbUidList(pScanInfo->streamBlockReader, qa);
165 166 167 168 169 170 171 172
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }
  } else {
    assert(0);
  }

  return TSDB_CODE_SUCCESS;
L
fix  
Liu Jicong 已提交
173
}