cachescanoperator.c 9.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#include "function.h"
H
Hongze Cheng 已提交
17
#include "os.h"
18 19 20 21 22 23 24 25 26 27
#include "tname.h"

#include "tdatablock.h"
#include "tmsg.h"

#include "executorimpl.h"
#include "tcompare.h"
#include "thash.h"
#include "ttypes.h"

28
static SSDataBlock* doScanCache(SOperatorInfo* pOperator);
H
Hongze Cheng 已提交
29 30
static void         destroyLastrowScanOperator(void* param);
static int32_t      extractTargetSlotId(const SArray* pColMatchInfo, SExecTaskInfo* pTaskInfo, int32_t** pSlotIds);
31

32 33 34
SOperatorInfo* createCacherowsScanOperator(SLastRowScanPhysiNode* pScanNode, SReadHandle* readHandle,
                                           SExecTaskInfo* pTaskInfo) {
  int32_t           code = TSDB_CODE_SUCCESS;
35 36 37
  SLastrowScanInfo* pInfo = taosMemoryCalloc(1, sizeof(SLastrowScanInfo));
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
38
    code = TSDB_CODE_OUT_OF_MEMORY;
39 40 41 42
    goto _error;
  }

  pInfo->readHandle = *readHandle;
H
Hongze Cheng 已提交
43
  pInfo->pRes = createResDataBlock(pScanNode->scan.node.pOutputDataBlockDesc);
44 45

  int32_t numOfCols = 0;
H
Hongze Cheng 已提交
46 47
  pInfo->pColMatchInfo = extractColMatchInfo(pScanNode->scan.pScanCols, pScanNode->scan.node.pOutputDataBlockDesc,
                                             &numOfCols, COL_MATCH_FROM_COL_ID);
48
  code = extractTargetSlotId(pInfo->pColMatchInfo, pTaskInfo, &pInfo->pSlotIds);
49 50 51 52
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

53 54
  STableListInfo* pTableList = &pTaskInfo->tableqinfoList;

55
  initResultSizeInfo(&pOperator->resultInfo, 4096);
56 57 58 59 60
  blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
  pInfo->pUidList = taosArrayInit(4, sizeof(int64_t));

  // partition by tbname
  if (taosArrayGetSize(pTableList->pGroupList) == taosArrayGetSize(pTableList->pTableList)) {
H
Hongze Cheng 已提交
61
    pInfo->retrieveType = CACHESCAN_RETRIEVE_TYPE_ALL | CACHESCAN_RETRIEVE_LAST_ROW;
62 63 64 65 66 67
    code = tsdbCacherowsReaderOpen(pInfo->readHandle.vnode, pInfo->retrieveType, pTableList->pTableList,
                                   taosArrayGetSize(pInfo->pColMatchInfo), &pInfo->pLastrowReader);
    if (code != TSDB_CODE_SUCCESS) {
      goto _error;
    }

68 69
    pInfo->pBufferredRes = createOneDataBlock(pInfo->pRes, false);
    blockDataEnsureCapacity(pInfo->pBufferredRes, pOperator->resultInfo.capacity);
H
Hongze Cheng 已提交
70 71
  } else {  // by tags
    pInfo->retrieveType = CACHESCAN_RETRIEVE_TYPE_SINGLE | CACHESCAN_RETRIEVE_LAST_ROW;
72
  }
73

X
Xiaoyu Wang 已提交
74
  if (pScanNode->scan.pScanPseudoCols != NULL) {
75 76
    SExprSupp* pPseudoExpr = &pInfo->pseudoExprSup;

X
Xiaoyu Wang 已提交
77
    pPseudoExpr->pExprInfo = createExprInfo(pScanNode->scan.pScanPseudoCols, NULL, &pPseudoExpr->numOfExprs);
H
Hongze Cheng 已提交
78 79
    pPseudoExpr->pCtx =
        createSqlFunctionCtx(pPseudoExpr->pExprInfo, pPseudoExpr->numOfExprs, &pPseudoExpr->rowEntryInfoOffset);
80
  }
81

H
Hongze Cheng 已提交
82
  pOperator->name = "LastrowScanOperator";
83
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN;
H
Hongze Cheng 已提交
84 85 86 87
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
  pOperator->pTaskInfo = pTaskInfo;
88 89 90
  pOperator->exprSupp.numOfExprs = taosArrayGetSize(pInfo->pRes->pDataBlock);

  pOperator->fpSet =
91
      createOperatorFpSet(operatorDummyOpenFn, doScanCache, NULL, NULL, destroyLastrowScanOperator, NULL, NULL, NULL);
92

93 94 95
  pOperator->cost.openCost = 0;
  return pOperator;

H
Hongze Cheng 已提交
96
_error:
97 98
  pTaskInfo->code = code;
  destroyLastrowScanOperator(pInfo);
99 100 101 102
  taosMemoryFree(pOperator);
  return NULL;
}

103
SSDataBlock* doScanCache(SOperatorInfo* pOperator) {
104 105 106 107 108 109
  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }

  SLastrowScanInfo* pInfo = pOperator->info;
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
110 111
  STableListInfo*   pTableList = &pTaskInfo->tableqinfoList;
  int32_t           size = taosArrayGetSize(pTableList->pTableList);
112
  if (size == 0) {
113
    doSetOperatorCompleted(pOperator);
114 115 116
    return NULL;
  }

117 118
  blockDataCleanup(pInfo->pRes);

119
  // check if it is a group by tbname
120
  if ((pInfo->retrieveType & CACHESCAN_RETRIEVE_TYPE_ALL) == CACHESCAN_RETRIEVE_TYPE_ALL) {
121 122 123 124
    if (pInfo->indexOfBufferedRes >= pInfo->pBufferredRes->info.rows) {
      blockDataCleanup(pInfo->pBufferredRes);
      taosArrayClear(pInfo->pUidList);

H
Hongze Cheng 已提交
125 126
      int32_t code =
          tsdbRetrieveCacheRows(pInfo->pLastrowReader, pInfo->pBufferredRes, pInfo->pSlotIds, pInfo->pUidList);
127
      if (code != TSDB_CODE_SUCCESS) {
128
        T_LONG_JMP(pTaskInfo->env, code);
129 130 131 132 133 134 135 136 137
      }

      // check for tag values
      int32_t resultRows = pInfo->pBufferredRes->info.rows;
      ASSERT(resultRows == taosArrayGetSize(pInfo->pUidList));
      pInfo->indexOfBufferedRes = 0;
    }

    if (pInfo->indexOfBufferedRes < pInfo->pBufferredRes->info.rows) {
H
Hongze Cheng 已提交
138
      for (int32_t i = 0; i < taosArrayGetSize(pInfo->pColMatchInfo); ++i) {
139
        SColMatchInfo* pMatchInfo = taosArrayGet(pInfo->pColMatchInfo, i);
H
Hongze Cheng 已提交
140
        int32_t        slotId = pMatchInfo->targetSlotId;
141 142 143 144 145

        SColumnInfoData* pSrc = taosArrayGet(pInfo->pBufferredRes->pDataBlock, slotId);
        SColumnInfoData* pDst = taosArrayGet(pInfo->pRes->pDataBlock, slotId);

        char* p = colDataGetData(pSrc, pInfo->indexOfBufferedRes);
H
Hongze Cheng 已提交
146
        bool  isNull = colDataIsNull_s(pSrc, pInfo->indexOfBufferedRes);
147 148 149
        colDataAppend(pDst, 0, p, isNull);
      }

150 151 152
      pInfo->pRes->info.uid = *(tb_uid_t*)taosArrayGet(pInfo->pUidList, pInfo->indexOfBufferedRes);
      pInfo->pRes->info.rows = 1;

153 154
      if (pInfo->pseudoExprSup.numOfExprs > 0) {
        SExprSupp* pSup = &pInfo->pseudoExprSup;
H
Hongze Cheng 已提交
155 156
        int32_t    code = addTagPseudoColumnData(&pInfo->readHandle, pSup->pExprInfo, pSup->numOfExprs, pInfo->pRes,
                                                 GET_TASKID(pTaskInfo));
157 158 159 160
        if (code != TSDB_CODE_SUCCESS) {
          pTaskInfo->code = code;
          return NULL;
        }
161 162
      }

163 164
      if (pTableList->map != NULL) {
        int64_t* groupId = taosHashGet(pTableList->map, &pInfo->pRes->info.uid, sizeof(int64_t));
H
Haojun Liao 已提交
165 166 167
        if (groupId != NULL) {
          pInfo->pRes->info.groupId = *groupId;
        }
168 169 170 171 172
      } else {
        ASSERT(taosArrayGetSize(pTableList->pTableList) == 1);
        STableKeyInfo* pKeyInfo = taosArrayGet(pTableList->pTableList, 0);
        pInfo->pRes->info.groupId = pKeyInfo->groupId;
      }
173 174 175 176 177 178

      pInfo->indexOfBufferedRes += 1;
      return pInfo->pRes;
    } else {
      doSetOperatorCompleted(pOperator);
      return NULL;
179
    }
180 181 182 183 184 185
  } else {
    size_t totalGroups = taosArrayGetSize(pTableList->pGroupList);

    while (pInfo->currentGroupIndex < totalGroups) {
      SArray* pGroupTableList = taosArrayGetP(pTableList->pGroupList, pInfo->currentGroupIndex);

186
      tsdbCacherowsReaderOpen(pInfo->readHandle.vnode, pInfo->retrieveType, pGroupTableList,
H
Hongze Cheng 已提交
187
                              taosArrayGetSize(pInfo->pColMatchInfo), &pInfo->pLastrowReader);
188 189
      taosArrayClear(pInfo->pUidList);

190
      int32_t code = tsdbRetrieveCacheRows(pInfo->pLastrowReader, pInfo->pRes, pInfo->pSlotIds, pInfo->pUidList);
191 192 193
      if (code != TSDB_CODE_SUCCESS) {
        longjmp(pTaskInfo->env, code);
      }
194

195 196 197 198 199 200 201 202 203 204 205
      pInfo->currentGroupIndex += 1;

      // check for tag values
      if (pInfo->pRes->info.rows > 0) {
        if (pInfo->pseudoExprSup.numOfExprs > 0) {
          SExprSupp* pSup = &pInfo->pseudoExprSup;
          pInfo->pRes->info.uid = *(tb_uid_t*)taosArrayGet(pInfo->pUidList, 0);

          STableKeyInfo* pKeyInfo = taosArrayGet(pGroupTableList, 0);
          pInfo->pRes->info.groupId = pKeyInfo->groupId;

206
          code = addTagPseudoColumnData(&pInfo->readHandle, pSup->pExprInfo, pSup->numOfExprs, pInfo->pRes,
H
Hongze Cheng 已提交
207 208
                                        GET_TASKID(pTaskInfo));
          if (code != TSDB_CODE_SUCCESS) {
209 210 211
            pTaskInfo->code = code;
            return NULL;
          }
212 213
        }

H
Haojun Liao 已提交
214
        pInfo->pLastrowReader = tsdbCacherowsReaderClose(pInfo->pLastrowReader);
215 216
        return pInfo->pRes;
      }
217 218 219
    }

    doSetOperatorCompleted(pOperator);
220
    return NULL;
221 222 223
  }
}

224
void destroyLastrowScanOperator(void* param) {
225 226
  SLastrowScanInfo* pInfo = (SLastrowScanInfo*)param;
  blockDataDestroy(pInfo->pRes);
H
Haojun Liao 已提交
227 228 229 230 231 232 233 234 235
  blockDataDestroy(pInfo->pBufferredRes);
  taosMemoryFree(pInfo->pSlotIds);
  taosArrayDestroy(pInfo->pUidList);
  taosArrayDestroy(pInfo->pColMatchInfo);

  if (pInfo->pLastrowReader != NULL) {
    pInfo->pLastrowReader = tsdbCacherowsReaderClose(pInfo->pLastrowReader);
  }

236 237 238 239
  taosMemoryFreeClear(param);
}

int32_t extractTargetSlotId(const SArray* pColMatchInfo, SExecTaskInfo* pTaskInfo, int32_t** pSlotIds) {
H
Hongze Cheng 已提交
240
  size_t numOfCols = taosArrayGetSize(pColMatchInfo);
241 242

  *pSlotIds = taosMemoryMalloc(numOfCols * sizeof(int32_t));
H
Hongze Cheng 已提交
243
  if (*pSlotIds == NULL) {
244 245 246 247 248
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  for (int32_t i = 0; i < numOfCols; ++i) {
    SColMatchInfo* pColMatch = taosArrayGet(pColMatchInfo, i);
249 250
    for (int32_t j = 0; j < pTaskInfo->schemaInfo.sw->nCols; ++j) {
      if (pColMatch->colId == pTaskInfo->schemaInfo.sw->pSchema[j].colId &&
251 252 253 254 255
          pColMatch->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
        (*pSlotIds)[pColMatch->targetSlotId] = -1;
        break;
      }

256
      if (pColMatch->colId == pTaskInfo->schemaInfo.sw->pSchema[j].colId) {
257 258 259 260 261 262 263 264
        (*pSlotIds)[pColMatch->targetSlotId] = j;
        break;
      }
    }
  }

  return TSDB_CODE_SUCCESS;
}