executor.c 9.2 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;

40
    SStreamScanInfo* pInfo = pOperator->info;
41
    pInfo->assignBlockUid = assignUid;
42

43 44
    // TODO: if a block was set but not consumed,
    // prevent setting a different type of block
45 46
    pInfo->validBlockIndex = 0;
    taosArrayClear(pInfo->pBlockLists);
47

L
Liu Jicong 已提交
48 49 50 51 52
    if (type == STREAM_INPUT__MERGED_SUBMIT) {
      ASSERT(numOfBlocks > 1);
      for (int32_t i = 0; i < numOfBlocks; i++) {
        SSubmitReq* pReq = *(void**)POINTER_SHIFT(input, i * sizeof(void*));
        taosArrayPush(pInfo->pBlockLists, &pReq);
53
      }
L
Liu Jicong 已提交
54 55
      pInfo->blockType = STREAM_INPUT__DATA_SUBMIT;
    } else if (type == STREAM_INPUT__DATA_SUBMIT) {
56 57 58 59
      /*if (tqReaderSetDataMsg(pInfo->tqReader, input, 0) < 0) {*/
      /*qError("submit msg messed up when initing stream block, %s" PRIx64, id);*/
      /*return TSDB_CODE_QRY_APP_ERROR;*/
      /*}*/
L
Liu Jicong 已提交
60 61 62 63 64 65
      ASSERT(numOfBlocks == 1);
      /*if (numOfBlocks == 1) {*/
      taosArrayPush(pInfo->pBlockLists, &input);
      pInfo->blockType = STREAM_INPUT__DATA_SUBMIT;
      /*} else {*/
      /*}*/
L
Liu Jicong 已提交
66
    } else if (type == STREAM_INPUT__DATA_BLOCK) {
H
Haojun Liao 已提交
67
      for (int32_t i = 0; i < numOfBlocks; ++i) {
L
Liu Jicong 已提交
68
        SSDataBlock* pDataBlock = &((SSDataBlock*)input)[i];
69

70
        // TODO optimize
71
        SSDataBlock* p = createOneDataBlock(pDataBlock, false);
H
Haojun Liao 已提交
72
        p->info = pDataBlock->info;
73

L
Liu Jicong 已提交
74
        taosArrayClear(p->pDataBlock);
H
Haojun Liao 已提交
75 76 77
        taosArrayAddAll(p->pDataBlock, pDataBlock->pDataBlock);
        taosArrayPush(pInfo->pBlockLists, &p);
      }
L
Liu Jicong 已提交
78
      pInfo->blockType = STREAM_INPUT__DATA_BLOCK;
79 80
    } else {
      ASSERT(0);
81 82
    }

H
Haojun Liao 已提交
83 84 85 86
    return TSDB_CODE_SUCCESS;
  }
}

L
Liu Jicong 已提交
87
#if 0
L
Liu Jicong 已提交
88 89 90 91 92
int32_t qStreamScanSnapshot(qTaskInfo_t tinfo) {
  if (tinfo == NULL) {
    return TSDB_CODE_QRY_APP_ERROR;
  }
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
L
Liu Jicong 已提交
93
  return doSetStreamBlock(pTaskInfo->pRoot, NULL, 0, STREAM_INPUT__TABLE_SCAN, 0, NULL);
L
Liu Jicong 已提交
94
}
L
Liu Jicong 已提交
95
#endif
L
Liu Jicong 已提交
96

97 98
int32_t qSetStreamInput(qTaskInfo_t tinfo, const void* input, int32_t type, bool assignUid) {
  return qSetMultiStreamInput(tinfo, input, 1, type, assignUid);
H
Haojun Liao 已提交
99 100
}

101
int32_t qSetMultiStreamInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type, bool assignUid) {
H
Haojun Liao 已提交
102 103 104 105
  if (tinfo == NULL) {
    return TSDB_CODE_QRY_APP_ERROR;
  }

H
Haojun Liao 已提交
106
  if (pBlocks == NULL || numOfBlocks == 0) {
H
Haojun Liao 已提交
107 108 109
    return TSDB_CODE_SUCCESS;
  }

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

112 113
  int32_t code =
      doSetStreamBlock(pTaskInfo->pRoot, (void**)pBlocks, numOfBlocks, type, assignUid, GET_TASKID(pTaskInfo));
H
Haojun Liao 已提交
114
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
115
    qError("%s failed to set the stream block data", GET_TASKID(pTaskInfo));
H
Haojun Liao 已提交
116
  } else {
H
Haojun Liao 已提交
117
    qDebug("%s set the stream block successfully", GET_TASKID(pTaskInfo));
H
Haojun Liao 已提交
118 119 120 121 122
  }

  return code;
}

123
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* readers, int32_t* numOfCols, SSchemaWrapper** pSchemaWrapper) {
L
Liu Jicong 已提交
124 125 126 127 128
  if (msg == NULL) {
    // TODO create raw scan
    return NULL;
  }

H
Haojun Liao 已提交
129 130
  struct SSubplan* pPlan = NULL;
  int32_t          code = qStringToSubplan(msg, &pPlan);
L
Liu Jicong 已提交
131 132 133 134 135 136
  if (code != TSDB_CODE_SUCCESS) {
    terrno = code;
    return NULL;
  }

  qTaskInfo_t pTaskInfo = NULL;
H
Haojun Liao 已提交
137
  code = qCreateExecTask(readers, 0, 0, pPlan, &pTaskInfo, NULL, NULL, OPTR_EXEC_MODEL_QUEUE);
L
Liu Jicong 已提交
138
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
139 140
    nodesDestroyNode((SNode*)pPlan);
    qDestroyTask(pTaskInfo);
L
Liu Jicong 已提交
141 142 143 144
    terrno = code;
    return NULL;
  }

145
  // extract the number of output columns
H
Haojun Liao 已提交
146
  SDataBlockDescNode* pDescNode = pPlan->pNode->pOutputDataBlockDesc;
147 148 149 150 151 152 153 154 155 156
  *numOfCols = 0;

  SNode*  pNode;
  FOREACH(pNode, pDescNode->pSlots) {
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
    if (pSlotDesc->output) {
      ++(*numOfCols);
    }
  }

157
  *pSchemaWrapper = tCloneSSchemaWrapper(((SExecTaskInfo*)pTaskInfo)->schemaInfo.qsw);
L
Liu Jicong 已提交
158 159 160
  return pTaskInfo;
}

L
Liu Jicong 已提交
161
qTaskInfo_t qCreateStreamExecTaskInfo(void* msg, SReadHandle* readers) {
L
Liu Jicong 已提交
162
  if (msg == NULL) {
163 164 165
    return NULL;
  }

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

H
Haojun Liao 已提交
168 169
  struct SSubplan* pPlan = NULL;
  int32_t          code = qStringToSubplan(msg, &pPlan);
170 171 172 173 174 175
  if (code != TSDB_CODE_SUCCESS) {
    terrno = code;
    return NULL;
  }

  qTaskInfo_t pTaskInfo = NULL;
H
Haojun Liao 已提交
176
  code = qCreateExecTask(readers, 0, 0, pPlan, &pTaskInfo, NULL, NULL, OPTR_EXEC_MODEL_STREAM);
177
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
178 179
    nodesDestroyNode((SNode*)pPlan);
    qDestroyTask(pTaskInfo);
180 181 182 183 184 185
    terrno = code;
    return NULL;
  }

  return pTaskInfo;
}
186

187 188
static SArray* filterQualifiedChildTables(const SStreamScanInfo* pScanInfo, const SArray* tableIdList,
                                          const char* idstr) {
189 190 191 192 193 194
  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) {
195
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
196 197 198

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

203
    // TODO handle ntb case
L
Liu Jicong 已提交
204
    if (mr.me.type != TSDB_CHILD_TABLE || mr.me.ctbEntry.suid != pScanInfo->tableUid) {
205 206
      continue;
    }
207 208 209

    if (pScanInfo->pTagCond != NULL) {
      bool          qualified = false;
210
      STableKeyInfo info = {.groupId = 0, .uid = mr.me.uid};
211 212 213 214 215 216 217 218 219 220 221
      code = isTableOk(&info, pScanInfo->pTagCond, pScanInfo->readHandle.meta, &qualified);
      if (code != TSDB_CODE_SUCCESS) {
        qError("failed to filter new table, uid:0x%" PRIx64 ", %s", info.uid, idstr);
        continue;
      }

      if (!qualified) {
        continue;
      }
    }

222
    // handle multiple partition
223 224 225 226 227 228 229
    taosArrayPush(qa, id);
  }

  metaReaderClear(&mr);
  return qa;
}

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

233
  // traverse to the stream scanner node to add this table id
234
  SOperatorInfo* pInfo = pTaskInfo->pRoot;
L
Liu Jicong 已提交
235
  while (pInfo->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
236 237 238
    pInfo = pInfo->pDownstream[0];
  }

239 240
  int32_t          code = 0;
  SStreamScanInfo* pScanInfo = pInfo->info;
241
  if (isAdd) {  // add new table id
242
    SArray* qa = filterQualifiedChildTables(pScanInfo, tableIdList, GET_TASKID(pTaskInfo));
243

244
    qDebug(" %d qualified child tables added into stream scanner", (int32_t)taosArrayGetSize(qa));
L
Liu Jicong 已提交
245
    code = tqReaderAddTbUidList(pScanInfo->tqReader, qa);
246 247 248 249 250 251 252 253 254 255 256 257 258
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }

    // add to qTaskInfo
    // todo refactor STableList
    for(int32_t i = 0; i < taosArrayGetSize(qa); ++i) {
      uint64_t* uid = taosArrayGet(qa, i);

      STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
      taosArrayPush(pTaskInfo->tableqinfoList.pTableList, &keyInfo);
    }

259 260 261
    taosArrayDestroy(qa);
  } else {  // remove the table id in current list
    qDebug(" %d remove child tables from the stream scanner", (int32_t)taosArrayGetSize(tableIdList));
L
Liu Jicong 已提交
262
    code = tqReaderRemoveTbUidList(pScanInfo->tqReader, tableIdList);
263 264
  }

265
  return code;
L
fix  
Liu Jicong 已提交
266
}
267

268
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, char* tableName, int32_t* sversion,
L
Liu Jicong 已提交
269
                                    int32_t* tversion) {
270
  ASSERT(tinfo != NULL && dbName != NULL && tableName != NULL);
271
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
272

273
  if (pTaskInfo->schemaInfo.sw == NULL) {
H
Haojun Liao 已提交
274 275 276
    return TSDB_CODE_SUCCESS;
  }

277 278 279 280
  *sversion = pTaskInfo->schemaInfo.sw->version;
  *tversion = pTaskInfo->schemaInfo.tversion;
  if (pTaskInfo->schemaInfo.dbname) {
    strcpy(dbName, pTaskInfo->schemaInfo.dbname);
281 282 283
  } else {
    dbName[0] = 0;
  }
284 285
  if (pTaskInfo->schemaInfo.tablename) {
    strcpy(tableName, pTaskInfo->schemaInfo.tablename);
286 287 288
  } else {
    tableName[0] = 0;
  }
289 290

  return 0;
291
}