executor.c 6.6 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"
H
Haojun Liao 已提交
17
#include "executorimpl.h"
18
#include "planner.h"
L
Liu Jicong 已提交
19
#include "tdatablock.h"
L
Liu Jicong 已提交
20
#include "vnode.h"
21

22 23
static int32_t doSetStreamBlock(SOperatorInfo* pOperator, void* input, size_t numOfBlocks, int32_t type, bool assignUid,
                                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;
36
    return doSetStreamBlock(pOperator->pDownstream[0], input, numOfBlocks, type, assignUid, id);
H
Haojun Liao 已提交
37
  } else {
38 39
    pOperator->status = OP_NOT_OPENED;

H
Haojun Liao 已提交
40
    SStreamBlockScanInfo* pInfo = pOperator->info;
41
    pInfo->assignBlockUid = assignUid;
42

43
    // no need to check
44
#if 0
45 46 47
    if (pInfo->blockType == 0) {
      pInfo->blockType = type;
    } else if (pInfo->blockType != type) {
48
      ASSERT(0);
L
Liu Jicong 已提交
49 50
      return TSDB_CODE_QRY_APP_ERROR;
    }
51
#endif
52
    pInfo->blockType = type;
53 54

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

63
        SSDataBlock* p = createOneDataBlock(pDataBlock, false);
H
Haojun Liao 已提交
64
        p->info = pDataBlock->info;
65

L
Liu Jicong 已提交
66
        taosArrayClear(p->pDataBlock);
H
Haojun Liao 已提交
67 68 69
        taosArrayAddAll(p->pDataBlock, pDataBlock->pDataBlock);
        taosArrayPush(pInfo->pBlockLists, &p);
      }
70 71
    } else {
      ASSERT(0);
72 73
    }

H
Haojun Liao 已提交
74 75 76 77
    return TSDB_CODE_SUCCESS;
  }
}

78 79
int32_t qSetStreamInput(qTaskInfo_t tinfo, const void* input, int32_t type, bool assignUid) {
  return qSetMultiStreamInput(tinfo, input, 1, type, assignUid);
H
Haojun Liao 已提交
80 81
}

82
int32_t qSetMultiStreamInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type, bool assignUid) {
H
Haojun Liao 已提交
83 84 85 86
  if (tinfo == NULL) {
    return TSDB_CODE_QRY_APP_ERROR;
  }

H
Haojun Liao 已提交
87
  if (pBlocks == NULL || numOfBlocks == 0) {
H
Haojun Liao 已提交
88 89 90
    return TSDB_CODE_SUCCESS;
  }

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

93 94
  int32_t code =
      doSetStreamBlock(pTaskInfo->pRoot, (void**)pBlocks, numOfBlocks, type, assignUid, GET_TASKID(pTaskInfo));
H
Haojun Liao 已提交
95
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
96
    qError("%s failed to set the stream block data", GET_TASKID(pTaskInfo));
H
Haojun Liao 已提交
97
  } else {
H
Haojun Liao 已提交
98
    qDebug("%s set the stream block successfully", GET_TASKID(pTaskInfo));
H
Haojun Liao 已提交
99 100 101 102 103
  }

  return code;
}

L
Liu Jicong 已提交
104
qTaskInfo_t qCreateStreamExecTaskInfo(void* msg, void* streamReadHandle) {
L
Liu Jicong 已提交
105
  if (msg == NULL) {
106 107 108 109
    return NULL;
  }

  // print those info into log
L
Liu Jicong 已提交
110
#if 0
L
Liu Jicong 已提交
111 112 113 114
  pMsg->sId = pMsg->sId;
  pMsg->queryId = pMsg->queryId;
  pMsg->taskId = pMsg->taskId;
  pMsg->contentLen = pMsg->contentLen;
L
Liu Jicong 已提交
115
#endif
116

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

119
  struct SSubplan* plan = NULL;
L
Liu Jicong 已提交
120
  int32_t          code = qStringToSubplan(msg, &plan);
121 122 123 124 125 126
  if (code != TSDB_CODE_SUCCESS) {
    terrno = code;
    return NULL;
  }

  qTaskInfo_t pTaskInfo = NULL;
127
  code = qCreateExecTask(streamReadHandle, 0, 0, plan, &pTaskInfo, NULL, NULL, OPTR_EXEC_MODEL_STREAM);
128 129 130 131 132 133 134 135
  if (code != TSDB_CODE_SUCCESS) {
    // TODO: destroy SSubplan & pTaskInfo
    terrno = code;
    return NULL;
  }

  return pTaskInfo;
}
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
static SArray* filterQualifiedChildTables(const SStreamBlockScanInfo* pScanInfo, const SArray* tableIdList) {
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));

  // let's discard the tables those are not created according to the queried super table.
  SMetaReader mr = {0};
  metaReaderInit(&mr, pScanInfo->readHandle.meta, 0);
  for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
    int64_t* id = (int64_t*)taosArrayGet(tableIdList, i);

    int32_t code = metaGetTableEntryByUid(&mr, *id);
    if (code != TSDB_CODE_SUCCESS) {
      qError("failed to get table meta, uid:%" PRIu64 " code:%s", *id, tstrerror(terrno));
      continue;
    }

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

    taosArrayPush(qa, id);
  }

  metaReaderClear(&mr);
  return qa;
}

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

167
  // traverse to the stream scanner node to add this table id
168
  SOperatorInfo* pInfo = pTaskInfo->pRoot;
L
Liu Jicong 已提交
169
  while (pInfo->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
170 171 172
    pInfo = pInfo->pDownstream[0];
  }

173
  int32_t               code = 0;
174
  SStreamBlockScanInfo* pScanInfo = pInfo->info;
175 176
  if (isAdd) {  // add new table id
    SArray* qa = filterQualifiedChildTables(pScanInfo, tableIdList);
177

178 179 180 181 182
    qDebug(" %d qualified child tables added into stream scanner", (int32_t)taosArrayGetSize(qa));
    code = tqReadHandleAddTbUidList(pScanInfo->streamBlockReader, qa);
    taosArrayDestroy(qa);
  } else {  // remove the table id in current list
    qDebug(" %d remove child tables from the stream scanner", (int32_t)taosArrayGetSize(tableIdList));
183
    code = tqReadHandleRemoveTbUidList(pScanInfo->streamBlockReader, tableIdList);
184 185
  }

186
  return code;
L
fix  
Liu Jicong 已提交
187
}
188

189 190
int32_t qGetQueriedTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, char* tableName, int32_t* sversion,
                                      int32_t* tversion) {
191
  ASSERT(tinfo != NULL && dbName != NULL && tableName != NULL);
192
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
193 194 195

  *sversion = pTaskInfo->schemaVer.sversion;
  *tversion = pTaskInfo->schemaVer.tversion;
196 197 198 199 200 201 202 203 204 205
  if (pTaskInfo->schemaVer.dbname) {
    strcpy(dbName, pTaskInfo->schemaVer.dbname);
  } else {
    dbName[0] = 0;
  }
  if (pTaskInfo->schemaVer.tablename) {
    strcpy(tableName, pTaskInfo->schemaVer.tablename);
  } else {
    tableName[0] = 0;
  }
206 207

  return 0;
208
}