cachescanoperator.c 10.9 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
static void         destroyLastrowScanOperator(void* param);
30
static int32_t      extractCacheScanSlotId(const SArray* pColMatchInfo, SExecTaskInfo* pTaskInfo, int32_t** pSlotIds);
H
Haojun Liao 已提交
31
static int32_t      removeRedundantTsCol(SLastRowScanPhysiNode* pScanNode, SColMatchInfo* pColMatchInfo);
32

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

  pInfo->readHandle = *readHandle;
44 45 46

  SDataBlockDescNode* pDescNode = pScanNode->scan.node.pOutputDataBlockDesc;
  pInfo->pRes = createResDataBlock(pDescNode);
47 48

  int32_t numOfCols = 0;
49 50
  code =
      extractColMatchInfo(pScanNode->scan.pScanCols, pDescNode, &numOfCols, COL_MATCH_FROM_COL_ID, &pInfo->matchInfo);
H
Haojun Liao 已提交
51
  removeRedundantTsCol(pScanNode, &pInfo->matchInfo);
52

H
Haojun Liao 已提交
53
  code = extractCacheScanSlotId(pInfo->matchInfo.pList, pTaskInfo, &pInfo->pSlotIds);
54 55 56 57
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

58 59
  STableListInfo* pTableList = &pTaskInfo->tableqinfoList;

60
  initResultSizeInfo(&pOperator->resultInfo, 4096);
61 62 63
  blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
  pInfo->pUidList = taosArrayInit(4, sizeof(int64_t));

64 65
  // partition by tbname
  if (taosArrayGetSize(pTableList->pGroupList) == taosArrayGetSize(pTableList->pTableList)) {
66 67
    pInfo->retrieveType =
        CACHESCAN_RETRIEVE_TYPE_ALL | (pScanNode->ignoreNull ? CACHESCAN_RETRIEVE_LAST : CACHESCAN_RETRIEVE_LAST_ROW);
68
    code = tsdbCacherowsReaderOpen(pInfo->readHandle.vnode, pInfo->retrieveType, pTableList->pTableList,
69
                                   taosArrayGetSize(pInfo->matchInfo.pList), pTableList->suid, &pInfo->pLastrowReader);
70 71 72 73
    if (code != TSDB_CODE_SUCCESS) {
      goto _error;
    }

74 75
    pInfo->pBufferredRes = createOneDataBlock(pInfo->pRes, false);
    blockDataEnsureCapacity(pInfo->pBufferredRes, pOperator->resultInfo.capacity);
76 77 78
  } else {  // by tags
    pInfo->retrieveType = CACHESCAN_RETRIEVE_TYPE_SINGLE |
                          (pScanNode->ignoreNull ? CACHESCAN_RETRIEVE_LAST : CACHESCAN_RETRIEVE_LAST_ROW);
79
  }
80

X
Xiaoyu Wang 已提交
81
  if (pScanNode->scan.pScanPseudoCols != NULL) {
82 83 84
    SExprSupp* p = &pInfo->pseudoExprSup;
    p->pExprInfo = createExprInfo(pScanNode->scan.pScanPseudoCols, NULL, &p->numOfExprs);
    p->pCtx = createSqlFunctionCtx(p->pExprInfo, p->numOfExprs, &p->rowEntryInfoOffset);
85
  }
86

H
Hongze Cheng 已提交
87
  pOperator->name = "LastrowScanOperator";
88
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN;
H
Hongze Cheng 已提交
89 90 91 92
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
  pOperator->pTaskInfo = pTaskInfo;
93 94 95
  pOperator->exprSupp.numOfExprs = taosArrayGetSize(pInfo->pRes->pDataBlock);

  pOperator->fpSet =
96
      createOperatorFpSet(operatorDummyOpenFn, doScanCache, NULL, NULL, destroyLastrowScanOperator, NULL);
97

98 99 100
  pOperator->cost.openCost = 0;
  return pOperator;

H
Hongze Cheng 已提交
101
_error:
102 103
  pTaskInfo->code = code;
  destroyLastrowScanOperator(pInfo);
104 105 106 107
  taosMemoryFree(pOperator);
  return NULL;
}

108
SSDataBlock* doScanCache(SOperatorInfo* pOperator) {
109 110 111 112 113 114
  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }

  SLastrowScanInfo* pInfo = pOperator->info;
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
115 116
  STableListInfo*   pTableList = &pTaskInfo->tableqinfoList;
  int32_t           size = taosArrayGetSize(pTableList->pTableList);
117
  if (size == 0) {
118
    doSetOperatorCompleted(pOperator);
119 120 121
    return NULL;
  }

122 123
  blockDataCleanup(pInfo->pRes);

124
  // check if it is a group by tbname
125
  if ((pInfo->retrieveType & CACHESCAN_RETRIEVE_TYPE_ALL) == CACHESCAN_RETRIEVE_TYPE_ALL) {
126 127 128 129
    if (pInfo->indexOfBufferedRes >= pInfo->pBufferredRes->info.rows) {
      blockDataCleanup(pInfo->pBufferredRes);
      taosArrayClear(pInfo->pUidList);

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

      // check for tag values
      int32_t resultRows = pInfo->pBufferredRes->info.rows;
138 139 140

      // the results may be null, if last values are all null
      ASSERT(resultRows == 0 || resultRows == taosArrayGetSize(pInfo->pUidList));
141 142 143 144
      pInfo->indexOfBufferedRes = 0;
    }

    if (pInfo->indexOfBufferedRes < pInfo->pBufferredRes->info.rows) {
H
Haojun Liao 已提交
145 146 147
      for (int32_t i = 0; i < taosArrayGetSize(pInfo->matchInfo.pList); ++i) {
        SColMatchItem* pMatchInfo = taosArrayGet(pInfo->matchInfo.pList, i);
        int32_t        slotId = pMatchInfo->dstSlotId;
148 149 150 151 152

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

        char* p = colDataGetData(pSrc, pInfo->indexOfBufferedRes);
H
Hongze Cheng 已提交
153
        bool  isNull = colDataIsNull_s(pSrc, pInfo->indexOfBufferedRes);
154 155 156
        colDataAppend(pDst, 0, p, isNull);
      }

157 158 159
      pInfo->pRes->info.uid = *(tb_uid_t*)taosArrayGet(pInfo->pUidList, pInfo->indexOfBufferedRes);
      pInfo->pRes->info.rows = 1;

160 161
      if (pInfo->pseudoExprSup.numOfExprs > 0) {
        SExprSupp* pSup = &pInfo->pseudoExprSup;
H
Hongze Cheng 已提交
162 163
        int32_t    code = addTagPseudoColumnData(&pInfo->readHandle, pSup->pExprInfo, pSup->numOfExprs, pInfo->pRes,
                                                 GET_TASKID(pTaskInfo));
164 165 166 167
        if (code != TSDB_CODE_SUCCESS) {
          pTaskInfo->code = code;
          return NULL;
        }
168 169
      }

170 171 172 173 174 175 176 177 178 179
      if (pTableList->map != NULL) {
        int64_t* groupId = taosHashGet(pTableList->map, &pInfo->pRes->info.uid, sizeof(int64_t));
        if (groupId != NULL) {
          pInfo->pRes->info.groupId = *groupId;
        }
      } else {
        ASSERT(taosArrayGetSize(pTableList->pTableList) == 1);
        STableKeyInfo* pKeyInfo = taosArrayGet(pTableList->pTableList, 0);
        pInfo->pRes->info.groupId = pKeyInfo->groupId;
      }
180 181 182 183 184 185

      pInfo->indexOfBufferedRes += 1;
      return pInfo->pRes;
    } else {
      doSetOperatorCompleted(pOperator);
      return NULL;
186
    }
187
  } else {
188
    size_t totalGroups = taosArrayGetSize(pTableList->pGroupList);
189 190

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

193
      tsdbCacherowsReaderOpen(pInfo->readHandle.vnode, pInfo->retrieveType, pGroupTableList,
194
                              taosArrayGetSize(pInfo->matchInfo.pList), pTableList->suid, &pInfo->pLastrowReader);
195 196
      taosArrayClear(pInfo->pUidList);

197
      int32_t code = tsdbRetrieveCacheRows(pInfo->pLastrowReader, pInfo->pRes, pInfo->pSlotIds, pInfo->pUidList);
198
      if (code != TSDB_CODE_SUCCESS) {
199
        longjmp(pTaskInfo->env, code);
200
      }
201

202 203 204 205 206 207 208
      pInfo->currentGroupIndex += 1;

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

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

H
Haojun Liao 已提交
212 213 214 215 216 217 218 219 220 221
          if (taosArrayGetSize(pInfo->pUidList) > 0) {
            ASSERT((pInfo->retrieveType & CACHESCAN_RETRIEVE_LAST_ROW) == CACHESCAN_RETRIEVE_LAST_ROW);

            pInfo->pRes->info.uid = *(tb_uid_t*)taosArrayGet(pInfo->pUidList, 0);
            code = addTagPseudoColumnData(&pInfo->readHandle, pSup->pExprInfo, pSup->numOfExprs, pInfo->pRes,
                                          GET_TASKID(pTaskInfo));
            if (code != TSDB_CODE_SUCCESS) {
              pTaskInfo->code = code;
              return NULL;
            }
222
          }
223 224
        }

H
Haojun Liao 已提交
225
        pInfo->pLastrowReader = tsdbCacherowsReaderClose(pInfo->pLastrowReader);
226 227
        return pInfo->pRes;
      }
228 229 230
    }

    doSetOperatorCompleted(pOperator);
231
    return NULL;
232 233 234
  }
}

235
void destroyLastrowScanOperator(void* param) {
236 237
  SLastrowScanInfo* pInfo = (SLastrowScanInfo*)param;
  blockDataDestroy(pInfo->pRes);
H
Haojun Liao 已提交
238 239 240
  blockDataDestroy(pInfo->pBufferredRes);
  taosMemoryFree(pInfo->pSlotIds);
  taosArrayDestroy(pInfo->pUidList);
H
Haojun Liao 已提交
241
  taosArrayDestroy(pInfo->matchInfo.pList);
H
Haojun Liao 已提交
242 243 244 245 246

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

247 248 249
  taosMemoryFreeClear(param);
}

250
int32_t extractCacheScanSlotId(const SArray* pColMatchInfo, SExecTaskInfo* pTaskInfo, int32_t** pSlotIds) {
H
Hongze Cheng 已提交
251
  size_t numOfCols = taosArrayGetSize(pColMatchInfo);
252 253

  *pSlotIds = taosMemoryMalloc(numOfCols * sizeof(int32_t));
H
Hongze Cheng 已提交
254
  if (*pSlotIds == NULL) {
255 256 257
    return TSDB_CODE_OUT_OF_MEMORY;
  }

258 259
  SSchemaWrapper* pWrapper = pTaskInfo->schemaInfo.sw;

260
  for (int32_t i = 0; i < numOfCols; ++i) {
H
Haojun Liao 已提交
261
    SColMatchItem* pColMatch = taosArrayGet(pColMatchInfo, i);
262
    for (int32_t j = 0; j < pWrapper->nCols; ++j) {
263
      if (pColMatch->colId == pWrapper->pSchema[j].colId && pColMatch->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
H
Haojun Liao 已提交
264
        (*pSlotIds)[pColMatch->dstSlotId] = -1;
265 266 267
        break;
      }

268
      if (pColMatch->colId == pWrapper->pSchema[j].colId) {
H
Haojun Liao 已提交
269
        (*pSlotIds)[pColMatch->dstSlotId] = j;
270 271 272 273 274 275
        break;
      }
    }
  }

  return TSDB_CODE_SUCCESS;
276
}
277

H
Haojun Liao 已提交
278
int32_t removeRedundantTsCol(SLastRowScanPhysiNode* pScanNode, SColMatchInfo* pColMatchInfo) {
279
  if (!pScanNode->ignoreNull) {  // retrieve cached last value
H
Haojun Liao 已提交
280
    return TSDB_CODE_SUCCESS;
281 282
  }

H
Haojun Liao 已提交
283
  size_t size = taosArrayGetSize(pColMatchInfo->pList);
284
  SArray* pMatchInfo = taosArrayInit(size, sizeof(SColMatchItem));
285

H
Haojun Liao 已提交
286 287
  for (int32_t i = 0; i < size; ++i) {
    SColMatchItem* pColInfo = taosArrayGet(pColMatchInfo->pList, i);
288

H
Haojun Liao 已提交
289
    int32_t    slotId = pColInfo->dstSlotId;
290 291 292 293 294 295 296 297
    SNodeList* pList = pScanNode->scan.node.pOutputDataBlockDesc->pSlots;

    SSlotDescNode* pDesc = (SSlotDescNode*)nodesListGetNode(pList, slotId);
    if (pDesc->dataType.type != TSDB_DATA_TYPE_TIMESTAMP) {
      taosArrayPush(pMatchInfo, pColInfo);
    }
  }

H
Haojun Liao 已提交
298 299 300
  taosArrayDestroy(pColMatchInfo->pList);
  pColMatchInfo->pList = pMatchInfo;
  return TSDB_CODE_SUCCESS;
301
}