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
Haojun Liao 已提交
29
static void         destroyCacheScanOperator(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);
51 52 53 54
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

H
Haojun Liao 已提交
55
  removeRedundantTsCol(pScanNode, &pInfo->matchInfo);
56

H
Haojun Liao 已提交
57
  code = extractCacheScanSlotId(pInfo->matchInfo.pList, pTaskInfo, &pInfo->pSlotIds);
58 59 60 61
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

H
Haojun Liao 已提交
62
  STableListInfo* pTableList = pTaskInfo->pTableInfoList;
63

64
  initResultSizeInfo(&pOperator->resultInfo, 4096);
65 66 67
  blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
  pInfo->pUidList = taosArrayInit(4, sizeof(int64_t));

68
  // partition by tbname, todo opt perf
H
Haojun Liao 已提交
69
  if (oneTableForEachGroup(pTableList) || (tableListGetSize(pTableList) == 1)) {
70 71
    pInfo->retrieveType =
        CACHESCAN_RETRIEVE_TYPE_ALL | (pScanNode->ignoreNull ? CACHESCAN_RETRIEVE_LAST : CACHESCAN_RETRIEVE_LAST_ROW);
72

H
Haojun Liao 已提交
73
    STableKeyInfo* pList = tableListGetInfo(pTableList, 0);
74

H
Haojun Liao 已提交
75 76
    size_t   num = tableListGetSize(pTableList);
    uint64_t suid = tableListGetSuid(pTableList);
77
    code = tsdbCacherowsReaderOpen(pInfo->readHandle.vnode, pInfo->retrieveType, pList, num,
H
Haojun Liao 已提交
78
                                   taosArrayGetSize(pInfo->matchInfo.pList), suid, &pInfo->pLastrowReader);
79 80 81 82
    if (code != TSDB_CODE_SUCCESS) {
      goto _error;
    }

83 84
    pInfo->pBufferredRes = createOneDataBlock(pInfo->pRes, false);
    blockDataEnsureCapacity(pInfo->pBufferredRes, pOperator->resultInfo.capacity);
85 86 87
  } else {  // by tags
    pInfo->retrieveType = CACHESCAN_RETRIEVE_TYPE_SINGLE |
                          (pScanNode->ignoreNull ? CACHESCAN_RETRIEVE_LAST : CACHESCAN_RETRIEVE_LAST_ROW);
88
  }
89

X
Xiaoyu Wang 已提交
90
  if (pScanNode->scan.pScanPseudoCols != NULL) {
91 92 93
    SExprSupp* p = &pInfo->pseudoExprSup;
    p->pExprInfo = createExprInfo(pScanNode->scan.pScanPseudoCols, NULL, &p->numOfExprs);
    p->pCtx = createSqlFunctionCtx(p->pExprInfo, p->numOfExprs, &p->rowEntryInfoOffset);
94
  }
95

H
Haojun Liao 已提交
96
  setOperatorInfo(pOperator, "CachedRowScanOperator", QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN, false, OP_NOT_OPENED, pInfo, pTaskInfo);
97 98 99
  pOperator->exprSupp.numOfExprs = taosArrayGetSize(pInfo->pRes->pDataBlock);

  pOperator->fpSet =
H
Haojun Liao 已提交
100
      createOperatorFpSet(operatorDummyOpenFn, doScanCache, NULL, destroyCacheScanOperator, NULL);
101

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

105
  _error:
106
  pTaskInfo->code = code;
H
Haojun Liao 已提交
107
  destroyCacheScanOperator(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;
H
Haojun Liao 已提交
119 120 121 122
  STableListInfo*   pTableList = pTaskInfo->pTableInfoList;

  uint64_t suid = tableListGetSuid(pTableList);
  int32_t  size = tableListGetSize(pTableList);
123
  if (size == 0) {
H
Haojun Liao 已提交
124
    setOperatorCompleted(pOperator);
125 126 127
    return NULL;
  }

128 129
  blockDataCleanup(pInfo->pRes);

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

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

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

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

150 151
    SSDataBlock* pRes = pInfo->pRes;

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

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

H
Haojun Liao 已提交
160 161 162 163 164 165
        if (colDataIsNull_s(pSrc, pInfo->indexOfBufferedRes)) {
          colDataAppendNULL(pDst, 0);
        } else {
          char* p = colDataGetData(pSrc, pInfo->indexOfBufferedRes);
          colDataAppend(pDst, 0, p, false);
        }
166 167
      }

168 169
      pRes->info.uid = *(tb_uid_t*)taosArrayGet(pInfo->pUidList, pInfo->indexOfBufferedRes);
      pRes->info.rows = 1;
170

H
Haojun Liao 已提交
171 172
      SExprSupp* pSup = &pInfo->pseudoExprSup;
      int32_t code = addTagPseudoColumnData(&pInfo->readHandle, pSup->pExprInfo, pSup->numOfExprs, pRes,
173
                                            pRes->info.rows, GET_TASKID(pTaskInfo), NULL);
H
Haojun Liao 已提交
174 175 176
      if (code != TSDB_CODE_SUCCESS) {
        pTaskInfo->code = code;
        return NULL;
177 178
      }

179
      pRes->info.groupId = getTableGroupId(pTableList, pRes->info.uid);
180
      pInfo->indexOfBufferedRes += 1;
181
      return pRes;
182
    } else {
H
Haojun Liao 已提交
183
      setOperatorCompleted(pOperator);
184
      return NULL;
185
    }
186
  } else {
H
Haojun Liao 已提交
187
    size_t totalGroups = tableListGetOutputGroups(pTableList);
188 189

    while (pInfo->currentGroupIndex < totalGroups) {
190 191 192
      STableKeyInfo* pList = NULL;
      int32_t num = 0;

H
Haojun Liao 已提交
193
      int32_t code = tableListGetGroupList(pTableList, pInfo->currentGroupIndex, &pList, &num);
194 195 196 197 198
      if (code != TSDB_CODE_SUCCESS) {
        T_LONG_JMP(pTaskInfo->env, code);
      }

      tsdbCacherowsReaderOpen(pInfo->readHandle.vnode, pInfo->retrieveType, pList, num,
H
Haojun Liao 已提交
199
                              taosArrayGetSize(pInfo->matchInfo.pList), suid, &pInfo->pLastrowReader);
200 201
      taosArrayClear(pInfo->pUidList);

202
      code = tsdbRetrieveCacheRows(pInfo->pLastrowReader, pInfo->pRes, pInfo->pSlotIds, pInfo->pUidList);
203
      if (code != TSDB_CODE_SUCCESS) {
204
        T_LONG_JMP(pTaskInfo->env, code);
205
      }
206

207 208 209 210 211 212 213
      pInfo->currentGroupIndex += 1;

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

214
          STableKeyInfo* pKeyInfo = &((STableKeyInfo*)pList)[0];
215 216
          pInfo->pRes->info.groupId = pKeyInfo->groupId;

H
Haojun Liao 已提交
217 218 219 220
          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);
221
            code = addTagPseudoColumnData(&pInfo->readHandle, pSup->pExprInfo, pSup->numOfExprs, pInfo->pRes, pInfo->pRes->info.rows,
222
                                          GET_TASKID(pTaskInfo), NULL);
H
Haojun Liao 已提交
223 224 225 226
            if (code != TSDB_CODE_SUCCESS) {
              pTaskInfo->code = code;
              return NULL;
            }
227
          }
228 229
        }

H
Haojun Liao 已提交
230
        pInfo->pLastrowReader = tsdbCacherowsReaderClose(pInfo->pLastrowReader);
231
        return pInfo->pRes;
H
Haojun Liao 已提交
232 233
      } else {
        pInfo->pLastrowReader = tsdbCacherowsReaderClose(pInfo->pLastrowReader);
234
      }
235 236
    }

H
Haojun Liao 已提交
237
    setOperatorCompleted(pOperator);
238
    return NULL;
239 240 241
  }
}

H
Haojun Liao 已提交
242
void destroyCacheScanOperator(void* param) {
243 244
  SLastrowScanInfo* pInfo = (SLastrowScanInfo*)param;
  blockDataDestroy(pInfo->pRes);
H
Haojun Liao 已提交
245 246 247
  blockDataDestroy(pInfo->pBufferredRes);
  taosMemoryFree(pInfo->pSlotIds);
  taosArrayDestroy(pInfo->pUidList);
H
Haojun Liao 已提交
248
  taosArrayDestroy(pInfo->matchInfo.pList);
H
Haojun Liao 已提交
249 250 251 252 253

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

H
Haojun Liao 已提交
254
  cleanupExprSupp(&pInfo->pseudoExprSup);
255 256 257
  taosMemoryFreeClear(param);
}

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

  *pSlotIds = taosMemoryMalloc(numOfCols * sizeof(int32_t));
H
Hongze Cheng 已提交
262
  if (*pSlotIds == NULL) {
263 264 265
    return TSDB_CODE_OUT_OF_MEMORY;
  }

266 267
  SSchemaWrapper* pWrapper = pTaskInfo->schemaInfo.sw;

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

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

  return TSDB_CODE_SUCCESS;
284
}
285

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

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

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

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