executor.c 8.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
    pInfo->blockType = type;
46

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

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

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

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

L
Liu Jicong 已提交
71
#if 0
L
Liu Jicong 已提交
72 73 74 75 76
int32_t qStreamScanSnapshot(qTaskInfo_t tinfo) {
  if (tinfo == NULL) {
    return TSDB_CODE_QRY_APP_ERROR;
  }
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
L
Liu Jicong 已提交
77
  return doSetStreamBlock(pTaskInfo->pRoot, NULL, 0, STREAM_INPUT__TABLE_SCAN, 0, NULL);
L
Liu Jicong 已提交
78
}
L
Liu Jicong 已提交
79
#endif
L
Liu Jicong 已提交
80

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

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

H
Haojun Liao 已提交
90
  if (pBlocks == NULL || numOfBlocks == 0) {
H
Haojun Liao 已提交
91 92 93
    return TSDB_CODE_SUCCESS;
  }

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

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

  return code;
}

107
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* readers, int32_t* numOfCols) {
L
Liu Jicong 已提交
108 109 110 111 112
  if (msg == NULL) {
    // TODO create raw scan
    return NULL;
  }

H
Haojun Liao 已提交
113 114
  struct SSubplan* pPlan = NULL;
  int32_t          code = qStringToSubplan(msg, &pPlan);
L
Liu Jicong 已提交
115 116 117 118 119 120
  if (code != TSDB_CODE_SUCCESS) {
    terrno = code;
    return NULL;
  }

  qTaskInfo_t pTaskInfo = NULL;
H
Haojun Liao 已提交
121
  code = qCreateExecTask(readers, 0, 0, pPlan, &pTaskInfo, NULL, NULL, OPTR_EXEC_MODEL_QUEUE);
L
Liu Jicong 已提交
122
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
123 124
    nodesDestroyNode((SNode*)pPlan);
    qDestroyTask(pTaskInfo);
L
Liu Jicong 已提交
125 126 127 128
    terrno = code;
    return NULL;
  }

129
  // extract the number of output columns
H
Haojun Liao 已提交
130
  SDataBlockDescNode* pDescNode = pPlan->pNode->pOutputDataBlockDesc;
131 132 133 134 135 136 137 138 139 140
  *numOfCols = 0;

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

L
Liu Jicong 已提交
141 142 143
  return pTaskInfo;
}

L
Liu Jicong 已提交
144
qTaskInfo_t qCreateStreamExecTaskInfo(void* msg, SReadHandle* readers) {
L
Liu Jicong 已提交
145
  if (msg == NULL) {
146 147 148
    return NULL;
  }

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

H
Haojun Liao 已提交
151 152
  struct SSubplan* pPlan = NULL;
  int32_t          code = qStringToSubplan(msg, &pPlan);
153 154 155 156 157 158
  if (code != TSDB_CODE_SUCCESS) {
    terrno = code;
    return NULL;
  }

  qTaskInfo_t pTaskInfo = NULL;
H
Haojun Liao 已提交
159
  code = qCreateExecTask(readers, 0, 0, pPlan, &pTaskInfo, NULL, NULL, OPTR_EXEC_MODEL_STREAM);
160
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
161 162
    nodesDestroyNode((SNode*)pPlan);
    qDestroyTask(pTaskInfo);
163 164 165 166 167 168
    terrno = code;
    return NULL;
  }

  return pTaskInfo;
}
169

170
static SArray* filterQualifiedChildTables(const SStreamScanInfo* pScanInfo, const SArray* tableIdList, const char* idstr) {
171 172 173 174 175 176
  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) {
177
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
178 179 180

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

185
    // TODO handle ntb case
L
Liu Jicong 已提交
186
    if (mr.me.type != TSDB_CHILD_TABLE || mr.me.ctbEntry.suid != pScanInfo->tableUid) {
187 188
      continue;
    }
189 190 191

    if (pScanInfo->pTagCond != NULL) {
      bool          qualified = false;
192
      STableKeyInfo info = {.groupId = 0, .uid = mr.me.uid};
193 194 195 196 197 198 199 200 201 202 203
      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;
      }
    }

204
    // handle multiple partition
205 206 207 208 209 210 211
    taosArrayPush(qa, id);
  }

  metaReaderClear(&mr);
  return qa;
}

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

215
  // traverse to the stream scanner node to add this table id
216
  SOperatorInfo* pInfo = pTaskInfo->pRoot;
L
Liu Jicong 已提交
217
  while (pInfo->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
218 219 220
    pInfo = pInfo->pDownstream[0];
  }

221 222
  int32_t          code = 0;
  SStreamScanInfo* pScanInfo = pInfo->info;
223
  if (isAdd) {  // add new table id
224
    SArray* qa = filterQualifiedChildTables(pScanInfo, tableIdList, GET_TASKID(pTaskInfo));
225

226
    qDebug(" %d qualified child tables added into stream scanner", (int32_t)taosArrayGetSize(qa));
L
Liu Jicong 已提交
227
    code = tqReaderAddTbUidList(pScanInfo->tqReader, qa);
228 229 230 231 232 233 234 235 236 237 238 239 240
    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);
    }

241 242 243
    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 已提交
244
    code = tqReaderRemoveTbUidList(pScanInfo->tqReader, tableIdList);
245 246
  }

247
  return code;
L
fix  
Liu Jicong 已提交
248
}
249

250
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, char* tableName, int32_t* sversion,
L
Liu Jicong 已提交
251
                                    int32_t* tversion) {
252
  ASSERT(tinfo != NULL && dbName != NULL && tableName != NULL);
253
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
254

255
  if (pTaskInfo->schemaInfo.sw == NULL) {
H
Haojun Liao 已提交
256 257 258
    return TSDB_CODE_SUCCESS;
  }

259 260 261 262
  *sversion = pTaskInfo->schemaInfo.sw->version;
  *tversion = pTaskInfo->schemaInfo.tversion;
  if (pTaskInfo->schemaInfo.dbname) {
    strcpy(dbName, pTaskInfo->schemaInfo.dbname);
263 264 265
  } else {
    dbName[0] = 0;
  }
266 267
  if (pTaskInfo->schemaInfo.tablename) {
    strcpy(tableName, pTaskInfo->schemaInfo.tablename);
268 269 270
  } else {
    tableName[0] = 0;
  }
271 272

  return 0;
273
}