executor.c 4.5 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

L
Liu Jicong 已提交
22
static int32_t doSetStreamBlock(SOperatorInfo* pOperator, void* input, size_t numOfBlocks, int32_t type, char* id) {
H
Haojun Liao 已提交
23
  ASSERT(pOperator != NULL);
X
Xiaoyu Wang 已提交
24
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
H
Haojun Liao 已提交
25
    if (pOperator->numOfDownstream == 0) {
26
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
H
Haojun Liao 已提交
27 28
      return TSDB_CODE_QRY_APP_ERROR;
    }
H
Haojun Liao 已提交
29

H
Haojun Liao 已提交
30
    if (pOperator->numOfDownstream > 1) {  // not handle this in join query
31
      qError("join not supported for stream block scan, %s" PRIx64, id);
H
Haojun Liao 已提交
32
      return TSDB_CODE_QRY_APP_ERROR;
H
Haojun Liao 已提交
33
    }
L
Liu Jicong 已提交
34
    pOperator->status = OP_NOT_OPENED;
H
Haojun Liao 已提交
35
    return doSetStreamBlock(pOperator->pDownstream[0], input, numOfBlocks, type, id);
H
Haojun Liao 已提交
36 37
  } else {
    SStreamBlockScanInfo* pInfo = pOperator->info;
38 39 40 41 42

    // the block type can not be changed in the streamscan operators
    if (pInfo->blockType == 0) {
      pInfo->blockType = type;
    } else if (pInfo->blockType != type) {
L
Liu Jicong 已提交
43 44
      return TSDB_CODE_QRY_APP_ERROR;
    }
45 46

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

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

L
Liu Jicong 已提交
58
        taosArrayClear(p->pDataBlock);
H
Haojun Liao 已提交
59 60 61
        taosArrayAddAll(p->pDataBlock, pDataBlock->pDataBlock);
        taosArrayPush(pInfo->pBlockLists, &p);
      }
62 63
    }

H
Haojun Liao 已提交
64 65 66 67
    return TSDB_CODE_SUCCESS;
  }
}

68
int32_t qSetStreamInput(qTaskInfo_t tinfo, const void* input, int32_t type) {
L
Liu Jicong 已提交
69
  return qSetMultiStreamInput(tinfo, input, 1, type);
H
Haojun Liao 已提交
70 71
}

L
Liu Jicong 已提交
72
int32_t qSetMultiStreamInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type) {
H
Haojun Liao 已提交
73 74 75 76
  if (tinfo == NULL) {
    return TSDB_CODE_QRY_APP_ERROR;
  }

H
Haojun Liao 已提交
77
  if (pBlocks == NULL || numOfBlocks == 0) {
H
Haojun Liao 已提交
78 79 80
    return TSDB_CODE_SUCCESS;
  }

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

H
Haojun Liao 已提交
83
  int32_t code = doSetStreamBlock(pTaskInfo->pRoot, (void**)pBlocks, numOfBlocks, type, GET_TASKID(pTaskInfo));
H
Haojun Liao 已提交
84
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
85
    qError("%s failed to set the stream block data", GET_TASKID(pTaskInfo));
H
Haojun Liao 已提交
86
  } else {
H
Haojun Liao 已提交
87
    qDebug("%s set the stream block successfully", GET_TASKID(pTaskInfo));
H
Haojun Liao 已提交
88 89 90 91 92
  }

  return code;
}

L
Liu Jicong 已提交
93 94
qTaskInfo_t qCreateStreamExecTaskInfo(void* msg, void* streamReadHandle) {
  if (msg == NULL || streamReadHandle == NULL) {
95 96 97 98
    return NULL;
  }

  // print those info into log
L
Liu Jicong 已提交
99
#if 0
L
Liu Jicong 已提交
100 101 102 103
  pMsg->sId = pMsg->sId;
  pMsg->queryId = pMsg->queryId;
  pMsg->taskId = pMsg->taskId;
  pMsg->contentLen = pMsg->contentLen;
L
Liu Jicong 已提交
104
#endif
105

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

108
  struct SSubplan* plan = NULL;
L
Liu Jicong 已提交
109
  int32_t          code = qStringToSubplan(msg, &plan);
110 111 112 113 114 115
  if (code != TSDB_CODE_SUCCESS) {
    terrno = code;
    return NULL;
  }

  qTaskInfo_t pTaskInfo = NULL;
116
  code = qCreateExecTask(streamReadHandle, 0, 0, plan, &pTaskInfo, NULL, OPTR_EXEC_MODEL_STREAM);
117 118 119 120 121 122 123 124
  if (code != TSDB_CODE_SUCCESS) {
    // TODO: destroy SSubplan & pTaskInfo
    terrno = code;
    return NULL;
  }

  return pTaskInfo;
}
125

H
Haojun Liao 已提交
126
int32_t qUpdateQualifiedTableId(qTaskInfo_t tinfo, SArray* tableIdList, bool isAdd) {
L
fix  
Liu Jicong 已提交
127
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
128 129 130

  // traverse to the streamscan node to add this table id
  SOperatorInfo* pInfo = pTaskInfo->pRoot;
L
Liu Jicong 已提交
131
  while (pInfo->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
132 133 134 135 136
    pInfo = pInfo->pDownstream[0];
  }

  SStreamBlockScanInfo* pScanInfo = pInfo->info;
  if (isAdd) {
L
fix  
Liu Jicong 已提交
137
    int32_t code = tqReadHandleAddTbUidList(pScanInfo->readerHandle, tableIdList);
138 139 140 141 142 143 144 145
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }
  } else {
    assert(0);
  }

  return TSDB_CODE_SUCCESS;
L
fix  
Liu Jicong 已提交
146
}