cachescanoperator.c 11.8 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"

H
Haojun Liao 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41
typedef struct SCacheRowsScanInfo {
  SSDataBlock*    pRes;
  SReadHandle     readHandle;
  void*           pLastrowReader;
  SColMatchInfo   matchInfo;
  int32_t*        pSlotIds;
  SExprSupp       pseudoExprSup;
  int32_t         retrieveType;
  int32_t         currentGroupIndex;
  SSDataBlock*    pBufferredRes;
  SArray*         pUidList;
  int32_t         indexOfBufferedRes;
} SCacheRowsScanInfo;

42
static SSDataBlock* doScanCache(SOperatorInfo* pOperator);
H
Haojun Liao 已提交
43
static void         destroyCacheScanOperator(void* param);
44
static int32_t      extractCacheScanSlotId(const SArray* pColMatchInfo, SExecTaskInfo* pTaskInfo, int32_t** pSlotIds);
H
Haojun Liao 已提交
45
static int32_t      removeRedundantTsCol(SLastRowScanPhysiNode* pScanNode, SColMatchInfo* pColMatchInfo);
46

H
Haojun Liao 已提交
47 48
#define SCAN_ROW_TYPE(_t)  ((_t)? CACHESCAN_RETRIEVE_LAST : CACHESCAN_RETRIEVE_LAST_ROW)

49 50 51
SOperatorInfo* createCacherowsScanOperator(SLastRowScanPhysiNode* pScanNode, SReadHandle* readHandle,
                                           SExecTaskInfo* pTaskInfo) {
  int32_t           code = TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
52
  SCacheRowsScanInfo* pInfo = taosMemoryCalloc(1, sizeof(SCacheRowsScanInfo));
53 54
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
55
    code = TSDB_CODE_OUT_OF_MEMORY;
56 57 58 59
    goto _error;
  }

  pInfo->readHandle = *readHandle;
60 61

  SDataBlockDescNode* pDescNode = pScanNode->scan.node.pOutputDataBlockDesc;
H
Haojun Liao 已提交
62
  pInfo->pRes = createDataBlockFromDescNode(pDescNode);
63 64

  int32_t numOfCols = 0;
65 66
  code =
      extractColMatchInfo(pScanNode->scan.pScanCols, pDescNode, &numOfCols, COL_MATCH_FROM_COL_ID, &pInfo->matchInfo);
67 68 69 70
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

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

H
Haojun Liao 已提交
73
  code = extractCacheScanSlotId(pInfo->matchInfo.pList, pTaskInfo, &pInfo->pSlotIds);
74 75 76 77
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

H
Haojun Liao 已提交
78
  STableListInfo* pTableList = pTaskInfo->pTableInfoList;
79

H
Haojun Liao 已提交
80 81 82
  int32_t totalTables = tableListGetSize(pTableList);
  int32_t capacity = 0;

83 84
  pInfo->pUidList = taosArrayInit(4, sizeof(int64_t));

H
Haojun Liao 已提交
85 86 87
  // partition by tbname
  if (oneTableForEachGroup(pTableList) || (totalTables == 1)) {
    pInfo->retrieveType = CACHESCAN_RETRIEVE_TYPE_ALL | SCAN_ROW_TYPE(pScanNode->ignoreNull);
88

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

H
Haojun Liao 已提交
91
    uint64_t suid = tableListGetSuid(pTableList);
H
Haojun Liao 已提交
92
    code = tsdbCacherowsReaderOpen(pInfo->readHandle.vnode, pInfo->retrieveType, pList, totalTables,
H
Haojun Liao 已提交
93
                                   taosArrayGetSize(pInfo->matchInfo.pList), suid, &pInfo->pLastrowReader, pTaskInfo->id.str);
94 95 96 97
    if (code != TSDB_CODE_SUCCESS) {
      goto _error;
    }

H
Haojun Liao 已提交
98 99
    capacity = TMIN(totalTables, 4096);

100
    pInfo->pBufferredRes = createOneDataBlock(pInfo->pRes, false);
H
Haojun Liao 已提交
101
    blockDataEnsureCapacity(pInfo->pBufferredRes, capacity);
102
  } else {  // by tags
H
Haojun Liao 已提交
103 104
    pInfo->retrieveType = CACHESCAN_RETRIEVE_TYPE_SINGLE | SCAN_ROW_TYPE(pScanNode->ignoreNull);
    capacity = 1;  // only one row output
105
  }
106

H
Haojun Liao 已提交
107 108 109
  initResultSizeInfo(&pOperator->resultInfo, capacity);
  blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);

X
Xiaoyu Wang 已提交
110
  if (pScanNode->scan.pScanPseudoCols != NULL) {
111 112 113
    SExprSupp* p = &pInfo->pseudoExprSup;
    p->pExprInfo = createExprInfo(pScanNode->scan.pScanPseudoCols, NULL, &p->numOfExprs);
    p->pCtx = createSqlFunctionCtx(p->pExprInfo, p->numOfExprs, &p->rowEntryInfoOffset);
114
  }
115

H
Haojun Liao 已提交
116
  setOperatorInfo(pOperator, "CachedRowScanOperator", QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN, false, OP_NOT_OPENED, pInfo, pTaskInfo);
117 118 119
  pOperator->exprSupp.numOfExprs = taosArrayGetSize(pInfo->pRes->pDataBlock);

  pOperator->fpSet =
120
      createOperatorFpSet(optrDummyOpenFn, doScanCache, NULL, destroyCacheScanOperator, optrDefaultBufFn, NULL);
121

122 123 124
  pOperator->cost.openCost = 0;
  return pOperator;

125
  _error:
126
  pTaskInfo->code = code;
H
Haojun Liao 已提交
127
  destroyCacheScanOperator(pInfo);
128 129 130 131
  taosMemoryFree(pOperator);
  return NULL;
}

132
SSDataBlock* doScanCache(SOperatorInfo* pOperator) {
133 134 135 136
  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }

H
Haojun Liao 已提交
137
  SCacheRowsScanInfo* pInfo = pOperator->info;
138
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
H
Haojun Liao 已提交
139 140 141 142
  STableListInfo*   pTableList = pTaskInfo->pTableInfoList;

  uint64_t suid = tableListGetSuid(pTableList);
  int32_t  size = tableListGetSize(pTableList);
143
  if (size == 0) {
H
Haojun Liao 已提交
144
    setOperatorCompleted(pOperator);
145 146 147
    return NULL;
  }

148 149
  blockDataCleanup(pInfo->pRes);

150
  // check if it is a group by tbname
151
  if ((pInfo->retrieveType & CACHESCAN_RETRIEVE_TYPE_ALL) == CACHESCAN_RETRIEVE_TYPE_ALL) {
152 153 154 155
    if (isTaskKilled(pTaskInfo)) {
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
    }

156 157 158 159
    if (pInfo->indexOfBufferedRes >= pInfo->pBufferredRes->info.rows) {
      blockDataCleanup(pInfo->pBufferredRes);
      taosArrayClear(pInfo->pUidList);

H
Hongze Cheng 已提交
160 161
      int32_t code =
          tsdbRetrieveCacheRows(pInfo->pLastrowReader, pInfo->pBufferredRes, pInfo->pSlotIds, pInfo->pUidList);
162
      if (code != TSDB_CODE_SUCCESS) {
163
        T_LONG_JMP(pTaskInfo->env, code);
164 165 166 167
      }

      // check for tag values
      int32_t resultRows = pInfo->pBufferredRes->info.rows;
168 169 170

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

174 175
    SSDataBlock* pRes = pInfo->pRes;

176
    if (pInfo->indexOfBufferedRes < pInfo->pBufferredRes->info.rows) {
H
Haojun Liao 已提交
177 178 179
      for (int32_t i = 0; i < taosArrayGetSize(pInfo->matchInfo.pList); ++i) {
        SColMatchItem* pMatchInfo = taosArrayGet(pInfo->matchInfo.pList, i);
        int32_t        slotId = pMatchInfo->dstSlotId;
180 181

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

H
Haojun Liao 已提交
184
        if (colDataIsNull_s(pSrc, pInfo->indexOfBufferedRes)) {
185
          colDataSetNULL(pDst, 0);
H
Haojun Liao 已提交
186 187
        } else {
          char* p = colDataGetData(pSrc, pInfo->indexOfBufferedRes);
188
          colDataSetVal(pDst, 0, p, false);
H
Haojun Liao 已提交
189
        }
190 191
      }

H
Haojun Liao 已提交
192
      pRes->info.id.uid = *(tb_uid_t*)taosArrayGet(pInfo->pUidList, pInfo->indexOfBufferedRes);
193
      pRes->info.rows = 1;
194

H
Haojun Liao 已提交
195 196
      SExprSupp* pSup = &pInfo->pseudoExprSup;
      int32_t code = addTagPseudoColumnData(&pInfo->readHandle, pSup->pExprInfo, pSup->numOfExprs, pRes,
197
                                            pRes->info.rows, GET_TASKID(pTaskInfo), NULL);
H
Haojun Liao 已提交
198 199 200
      if (code != TSDB_CODE_SUCCESS) {
        pTaskInfo->code = code;
        return NULL;
201 202
      }

H
Haojun Liao 已提交
203
      pRes->info.id.groupId = getTableGroupId(pTableList, pRes->info.id.uid);
204
      pInfo->indexOfBufferedRes += 1;
205
      return pRes;
206
    } else {
H
Haojun Liao 已提交
207
      setOperatorCompleted(pOperator);
208
      return NULL;
209
    }
210
  } else {
H
Haojun Liao 已提交
211
    size_t totalGroups = tableListGetOutputGroups(pTableList);
212 213

    while (pInfo->currentGroupIndex < totalGroups) {
214 215 216 217
      if (isTaskKilled(pTaskInfo)) {
        T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
      }

218 219 220
      STableKeyInfo* pList = NULL;
      int32_t num = 0;

H
Haojun Liao 已提交
221
      int32_t code = tableListGetGroupList(pTableList, pInfo->currentGroupIndex, &pList, &num);
222 223 224 225
      if (code != TSDB_CODE_SUCCESS) {
        T_LONG_JMP(pTaskInfo->env, code);
      }

226 227 228 229 230 231 232 233 234
      code = tsdbCacherowsReaderOpen(pInfo->readHandle.vnode, pInfo->retrieveType, pList, num,
                                     taosArrayGetSize(pInfo->matchInfo.pList), suid, &pInfo->pLastrowReader,
                                     pTaskInfo->id.str);
      if (code != TSDB_CODE_SUCCESS) {
        pInfo->currentGroupIndex += 1;
        taosArrayClear(pInfo->pUidList);
        continue;
      }

235 236
      taosArrayClear(pInfo->pUidList);

237
      code = tsdbRetrieveCacheRows(pInfo->pLastrowReader, pInfo->pRes, pInfo->pSlotIds, pInfo->pUidList);
238
      if (code != TSDB_CODE_SUCCESS) {
239
        T_LONG_JMP(pTaskInfo->env, code);
240
      }
241

242 243 244 245 246 247 248
      pInfo->currentGroupIndex += 1;

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

249
          STableKeyInfo* pKeyInfo = &((STableKeyInfo*)pList)[0];
H
Haojun Liao 已提交
250
          pInfo->pRes->info.id.groupId = pKeyInfo->groupId;
251

H
Haojun Liao 已提交
252 253 254
          if (taosArrayGetSize(pInfo->pUidList) > 0) {
            ASSERT((pInfo->retrieveType & CACHESCAN_RETRIEVE_LAST_ROW) == CACHESCAN_RETRIEVE_LAST_ROW);

H
Haojun Liao 已提交
255
            pInfo->pRes->info.id.uid = *(tb_uid_t*)taosArrayGet(pInfo->pUidList, 0);
256
            code = addTagPseudoColumnData(&pInfo->readHandle, pSup->pExprInfo, pSup->numOfExprs, pInfo->pRes, pInfo->pRes->info.rows,
257
                                          GET_TASKID(pTaskInfo), NULL);
H
Haojun Liao 已提交
258 259 260 261
            if (code != TSDB_CODE_SUCCESS) {
              pTaskInfo->code = code;
              return NULL;
            }
262
          }
263 264
        }

H
Haojun Liao 已提交
265
        pInfo->pLastrowReader = tsdbCacherowsReaderClose(pInfo->pLastrowReader);
266
        return pInfo->pRes;
H
Haojun Liao 已提交
267 268
      } else {
        pInfo->pLastrowReader = tsdbCacherowsReaderClose(pInfo->pLastrowReader);
269
      }
270 271
    }

H
Haojun Liao 已提交
272
    setOperatorCompleted(pOperator);
273
    return NULL;
274 275 276
  }
}

H
Haojun Liao 已提交
277
void destroyCacheScanOperator(void* param) {
H
Haojun Liao 已提交
278
  SCacheRowsScanInfo* pInfo = (SCacheRowsScanInfo*)param;
279
  blockDataDestroy(pInfo->pRes);
H
Haojun Liao 已提交
280 281 282
  blockDataDestroy(pInfo->pBufferredRes);
  taosMemoryFree(pInfo->pSlotIds);
  taosArrayDestroy(pInfo->pUidList);
H
Haojun Liao 已提交
283
  taosArrayDestroy(pInfo->matchInfo.pList);
H
Haojun Liao 已提交
284 285 286 287 288

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

H
Haojun Liao 已提交
289
  cleanupExprSupp(&pInfo->pseudoExprSup);
290 291 292
  taosMemoryFreeClear(param);
}

293
int32_t extractCacheScanSlotId(const SArray* pColMatchInfo, SExecTaskInfo* pTaskInfo, int32_t** pSlotIds) {
H
Hongze Cheng 已提交
294
  size_t numOfCols = taosArrayGetSize(pColMatchInfo);
295 296

  *pSlotIds = taosMemoryMalloc(numOfCols * sizeof(int32_t));
H
Hongze Cheng 已提交
297
  if (*pSlotIds == NULL) {
298 299 300
    return TSDB_CODE_OUT_OF_MEMORY;
  }

301 302
  SSchemaWrapper* pWrapper = pTaskInfo->schemaInfo.sw;

303
  for (int32_t i = 0; i < numOfCols; ++i) {
H
Haojun Liao 已提交
304
    SColMatchItem* pColMatch = taosArrayGet(pColMatchInfo, i);
305
    for (int32_t j = 0; j < pWrapper->nCols; ++j) {
306
      if (pColMatch->colId == pWrapper->pSchema[j].colId && pColMatch->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
H
Haojun Liao 已提交
307
        (*pSlotIds)[pColMatch->dstSlotId] = -1;
308 309 310
        break;
      }

311
      if (pColMatch->colId == pWrapper->pSchema[j].colId) {
H
Haojun Liao 已提交
312
        (*pSlotIds)[pColMatch->dstSlotId] = j;
313 314 315 316 317 318
        break;
      }
    }
  }

  return TSDB_CODE_SUCCESS;
319
}
320

H
Haojun Liao 已提交
321
int32_t removeRedundantTsCol(SLastRowScanPhysiNode* pScanNode, SColMatchInfo* pColMatchInfo) {
322
  if (!pScanNode->ignoreNull) {  // retrieve cached last value
H
Haojun Liao 已提交
323
    return TSDB_CODE_SUCCESS;
324 325
  }

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

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

H
Haojun Liao 已提交
332
    int32_t    slotId = pColInfo->dstSlotId;
333 334 335 336 337 338 339 340
    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 已提交
341 342 343
  taosArrayDestroy(pColMatchInfo->pList);
  pColMatchInfo->pList = pMatchInfo;
  return TSDB_CODE_SUCCESS;
344
}