cachescanoperator.c 11.1 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, todo opt perf
  if (getNumOfGroups(pTableList) == getTotalTables(pTableList)) {
66 67
    pInfo->retrieveType =
        CACHESCAN_RETRIEVE_TYPE_ALL | (pScanNode->ignoreNull ? CACHESCAN_RETRIEVE_LAST : CACHESCAN_RETRIEVE_LAST_ROW);
68 69 70 71 72

    STableKeyInfo* pList = taosArrayGet(pTableList->pTableList, 0);
    size_t num = taosArrayGetSize(pTableList->pTableList);

    code = tsdbCacherowsReaderOpen(pInfo->readHandle.vnode, pInfo->retrieveType, pList, num,
73
                                   taosArrayGetSize(pInfo->matchInfo.pList), pTableList->suid, &pInfo->pLastrowReader);
74 75 76 77
    if (code != TSDB_CODE_SUCCESS) {
      goto _error;
    }

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

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

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

  pOperator->fpSet =
100
      createOperatorFpSet(operatorDummyOpenFn, doScanCache, NULL, NULL, destroyLastrowScanOperator, NULL);
101

102 103 104
  pOperator->cost.openCost = 0;
  return pOperator;

H
Hongze Cheng 已提交
105
_error:
106 107
  pTaskInfo->code = code;
  destroyLastrowScanOperator(pInfo);
108 109 110 111
  taosMemoryFree(pOperator);
  return NULL;
}

112
SSDataBlock* doScanCache(SOperatorInfo* pOperator) {
113 114 115 116 117 118
  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }

  SLastrowScanInfo* pInfo = pOperator->info;
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
119 120
  STableListInfo*   pTableList = &pTaskInfo->tableqinfoList;
  int32_t           size = taosArrayGetSize(pTableList->pTableList);
121
  if (size == 0) {
122
    doSetOperatorCompleted(pOperator);
123 124 125
    return NULL;
  }

126 127
  blockDataCleanup(pInfo->pRes);

128
  // check if it is a group by tbname
129
  if ((pInfo->retrieveType & CACHESCAN_RETRIEVE_TYPE_ALL) == CACHESCAN_RETRIEVE_TYPE_ALL) {
130 131 132 133
    if (pInfo->indexOfBufferedRes >= pInfo->pBufferredRes->info.rows) {
      blockDataCleanup(pInfo->pBufferredRes);
      taosArrayClear(pInfo->pUidList);

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

      // check for tag values
      int32_t resultRows = pInfo->pBufferredRes->info.rows;
142 143 144

      // the results may be null, if last values are all null
      ASSERT(resultRows == 0 || resultRows == taosArrayGetSize(pInfo->pUidList));
145 146 147 148
      pInfo->indexOfBufferedRes = 0;
    }

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

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

        char* p = colDataGetData(pSrc, pInfo->indexOfBufferedRes);
H
Hongze Cheng 已提交
157
        bool  isNull = colDataIsNull_s(pSrc, pInfo->indexOfBufferedRes);
158 159 160
        colDataAppend(pDst, 0, p, isNull);
      }

161 162 163
      pInfo->pRes->info.uid = *(tb_uid_t*)taosArrayGet(pInfo->pUidList, pInfo->indexOfBufferedRes);
      pInfo->pRes->info.rows = 1;

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

174 175
      if (pTableList->map != NULL) {
        int64_t* groupId = taosHashGet(pTableList->map, &pInfo->pRes->info.uid, sizeof(int64_t));
H
Haojun Liao 已提交
176 177 178
        if (groupId != NULL) {
          pInfo->pRes->info.groupId = *groupId;
        }
179 180 181 182 183
      } else {
        ASSERT(taosArrayGetSize(pTableList->pTableList) == 1);
        STableKeyInfo* pKeyInfo = taosArrayGet(pTableList->pTableList, 0);
        pInfo->pRes->info.groupId = pKeyInfo->groupId;
      }
184 185 186 187 188 189

      pInfo->indexOfBufferedRes += 1;
      return pInfo->pRes;
    } else {
      doSetOperatorCompleted(pOperator);
      return NULL;
190
    }
191
  } else {
192
    size_t totalGroups = getNumOfGroups(pTableList);
193 194 195

    while (pInfo->currentGroupIndex < totalGroups) {

196 197 198 199 200 201 202 203 204
      STableKeyInfo* pList = NULL;
      int32_t num = 0;

      int32_t code = getTablesOfGroup(pTableList, pInfo->currentGroupIndex, &pList, &num);
      if (code != TSDB_CODE_SUCCESS) {
        T_LONG_JMP(pTaskInfo->env, code);
      }

      tsdbCacherowsReaderOpen(pInfo->readHandle.vnode, pInfo->retrieveType, pList, num,
205
                              taosArrayGetSize(pInfo->matchInfo.pList), pTableList->suid, &pInfo->pLastrowReader);
206 207
      taosArrayClear(pInfo->pUidList);

208
      code = tsdbRetrieveCacheRows(pInfo->pLastrowReader, pInfo->pRes, pInfo->pSlotIds, pInfo->pUidList);
209
      if (code != TSDB_CODE_SUCCESS) {
210
        T_LONG_JMP(pTaskInfo->env, code);
211
      }
212

213 214 215 216 217 218 219
      pInfo->currentGroupIndex += 1;

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

220
          STableKeyInfo* pKeyInfo = &((STableKeyInfo*)pTableList)[0];
221 222
          pInfo->pRes->info.groupId = pKeyInfo->groupId;

H
Haojun Liao 已提交
223 224 225 226 227 228 229 230 231 232
          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;
            }
233
          }
234 235
        }

H
Haojun Liao 已提交
236
        pInfo->pLastrowReader = tsdbCacherowsReaderClose(pInfo->pLastrowReader);
237 238
        return pInfo->pRes;
      }
239 240 241
    }

    doSetOperatorCompleted(pOperator);
242
    return NULL;
243 244 245
  }
}

246
void destroyLastrowScanOperator(void* param) {
247 248
  SLastrowScanInfo* pInfo = (SLastrowScanInfo*)param;
  blockDataDestroy(pInfo->pRes);
H
Haojun Liao 已提交
249 250 251
  blockDataDestroy(pInfo->pBufferredRes);
  taosMemoryFree(pInfo->pSlotIds);
  taosArrayDestroy(pInfo->pUidList);
H
Haojun Liao 已提交
252
  taosArrayDestroy(pInfo->matchInfo.pList);
H
Haojun Liao 已提交
253 254 255 256 257

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

258 259 260
  taosMemoryFreeClear(param);
}

261
int32_t extractCacheScanSlotId(const SArray* pColMatchInfo, SExecTaskInfo* pTaskInfo, int32_t** pSlotIds) {
H
Hongze Cheng 已提交
262
  size_t numOfCols = taosArrayGetSize(pColMatchInfo);
263 264

  *pSlotIds = taosMemoryMalloc(numOfCols * sizeof(int32_t));
H
Hongze Cheng 已提交
265
  if (*pSlotIds == NULL) {
266 267 268
    return TSDB_CODE_OUT_OF_MEMORY;
  }

269 270
  SSchemaWrapper* pWrapper = pTaskInfo->schemaInfo.sw;

271
  for (int32_t i = 0; i < numOfCols; ++i) {
H
Haojun Liao 已提交
272
    SColMatchItem* pColMatch = taosArrayGet(pColMatchInfo, i);
273
    for (int32_t j = 0; j < pWrapper->nCols; ++j) {
274
      if (pColMatch->colId == pWrapper->pSchema[j].colId && pColMatch->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
H
Haojun Liao 已提交
275
        (*pSlotIds)[pColMatch->dstSlotId] = -1;
276 277 278
        break;
      }

279
      if (pColMatch->colId == pWrapper->pSchema[j].colId) {
H
Haojun Liao 已提交
280
        (*pSlotIds)[pColMatch->dstSlotId] = j;
281 282 283 284 285 286
        break;
      }
    }
  }

  return TSDB_CODE_SUCCESS;
287
}
288

H
Haojun Liao 已提交
289
int32_t removeRedundantTsCol(SLastRowScanPhysiNode* pScanNode, SColMatchInfo* pColMatchInfo) {
290
  if (!pScanNode->ignoreNull) {  // retrieve cached last value
H
Haojun Liao 已提交
291
    return TSDB_CODE_SUCCESS;
292 293
  }

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

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

H
Haojun Liao 已提交
300
    int32_t    slotId = pColInfo->dstSlotId;
301 302 303 304 305 306 307 308
    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 已提交
309 310 311
  taosArrayDestroy(pColMatchInfo->pList);
  pColMatchInfo->pList = pMatchInfo;
  return TSDB_CODE_SUCCESS;
312
}