executor.c 9.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"
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;
}

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

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

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

146
  // extract the number of output columns
H
Haojun Liao 已提交
147
  SDataBlockDescNode* pDescNode = pPlan->pNode->pOutputDataBlockDesc;
148 149
  *numOfCols = 0;

L
Liu Jicong 已提交
150
  SNode* pNode;
151 152 153 154 155 156 157
  FOREACH(pNode, pDescNode->pSlots) {
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
    if (pSlotDesc->output) {
      ++(*numOfCols);
    }
  }

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

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

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

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

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

  return pTaskInfo;
}
187

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

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

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

    if (pScanInfo->pTagCond != NULL) {
      bool          qualified = false;
211
      STableKeyInfo info = {.groupId = 0, .uid = mr.me.uid};
212 213 214 215 216 217 218 219 220 221 222
      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;
      }
    }

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

  metaReaderClear(&mr);
  return qa;
}

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

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

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

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

    // add to qTaskInfo
    // todo refactor STableList
L
Liu Jicong 已提交
253
    for (int32_t i = 0; i < taosArrayGetSize(qa); ++i) {
254 255
      uint64_t* uid = taosArrayGet(qa, i);

L
Liu Jicong 已提交
256 257
      qDebug("table %ld added to task info", *uid);

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

262 263 264
    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 已提交
265
    code = tqReaderRemoveTbUidList(pScanInfo->tqReader, tableIdList);
266 267
  }

268
  return code;
L
fix  
Liu Jicong 已提交
269
}
270

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

276
  if (pTaskInfo->schemaInfo.sw == NULL) {
H
Haojun Liao 已提交
277 278 279
    return TSDB_CODE_SUCCESS;
  }

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

  return 0;
294
}