executor.c 9.4 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
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* readers, int32_t* numOfCols,
L
Liu Jicong 已提交
124
                                     SSchemaWrapper** pSchemaWrapper, int64_t* ntbUid) {
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
  *ntbUid = ((SExecTaskInfo*)pTaskInfo)->streamInfo.ntbUid;
L
Liu Jicong 已提交
160 161 162
  return pTaskInfo;
}

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

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

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

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

  return pTaskInfo;
}
188

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

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

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

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

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

  metaReaderClear(&mr);
  return qa;
}

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

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

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

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

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

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

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

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

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

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

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

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

  return 0;
295
}