cachescanoperator.c 11.4 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);
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 (pInfo->indexOfBufferedRes >= pInfo->pBufferredRes->info.rows) {
      blockDataCleanup(pInfo->pBufferredRes);
      taosArrayClear(pInfo->pUidList);

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

      // check for tag values
      int32_t resultRows = pInfo->pBufferredRes->info.rows;
164 165

      // the results may be null, if last values are all null
166
      ASSERT(resultRows == 0 || resultRows == taosArrayGetSize(pInfo->pUidList));
167 168 169
      pInfo->indexOfBufferedRes = 0;
    }

170 171
    SSDataBlock* pRes = pInfo->pRes;

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

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

H
Haojun Liao 已提交
180 181 182 183 184 185
        if (colDataIsNull_s(pSrc, pInfo->indexOfBufferedRes)) {
          colDataAppendNULL(pDst, 0);
        } else {
          char* p = colDataGetData(pSrc, pInfo->indexOfBufferedRes);
          colDataAppend(pDst, 0, p, false);
        }
186 187
      }

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

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

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

    while (pInfo->currentGroupIndex < totalGroups) {
210 211 212
      STableKeyInfo* pList = NULL;
      int32_t num = 0;

H
Haojun Liao 已提交
213
      int32_t code = tableListGetGroupList(pTableList, pInfo->currentGroupIndex, &pList, &num);
214 215 216 217 218
      if (code != TSDB_CODE_SUCCESS) {
        T_LONG_JMP(pTaskInfo->env, code);
      }

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

222
      code = tsdbRetrieveCacheRows(pInfo->pLastrowReader, pInfo->pRes, pInfo->pSlotIds, pInfo->pUidList);
223
      if (code != TSDB_CODE_SUCCESS) {
224
        T_LONG_JMP(pTaskInfo->env, code);
225
      }
226

227 228 229 230 231 232 233
      pInfo->currentGroupIndex += 1;

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

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

H
Haojun Liao 已提交
237
          if (taosArrayGetSize(pInfo->pUidList) > 0) {
238
            ASSERT((pInfo->retrieveType & CACHESCAN_RETRIEVE_LAST_ROW) == CACHESCAN_RETRIEVE_LAST_ROW);
H
Haojun Liao 已提交
239

H
Haojun Liao 已提交
240
            pInfo->pRes->info.id.uid = *(tb_uid_t*)taosArrayGet(pInfo->pUidList, 0);
241
            code = addTagPseudoColumnData(&pInfo->readHandle, pSup->pExprInfo, pSup->numOfExprs, pInfo->pRes, pInfo->pRes->info.rows,
242
                                          GET_TASKID(pTaskInfo), NULL);
H
Haojun Liao 已提交
243 244 245 246
            if (code != TSDB_CODE_SUCCESS) {
              pTaskInfo->code = code;
              return NULL;
            }
247
          }
248 249
        }

H
Haojun Liao 已提交
250
        pInfo->pLastrowReader = tsdbCacherowsReaderClose(pInfo->pLastrowReader);
251
        return pInfo->pRes;
H
Haojun Liao 已提交
252 253
      } else {
        pInfo->pLastrowReader = tsdbCacherowsReaderClose(pInfo->pLastrowReader);
254
      }
255 256
    }

H
Haojun Liao 已提交
257
    setOperatorCompleted(pOperator);
258
    return NULL;
259 260 261
  }
}

H
Haojun Liao 已提交
262
void destroyCacheScanOperator(void* param) {
H
Haojun Liao 已提交
263
  SCacheRowsScanInfo* pInfo = (SCacheRowsScanInfo*)param;
264
  blockDataDestroy(pInfo->pRes);
H
Haojun Liao 已提交
265 266 267
  blockDataDestroy(pInfo->pBufferredRes);
  taosMemoryFree(pInfo->pSlotIds);
  taosArrayDestroy(pInfo->pUidList);
H
Haojun Liao 已提交
268
  taosArrayDestroy(pInfo->matchInfo.pList);
H
Haojun Liao 已提交
269 270 271 272 273

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

H
Haojun Liao 已提交
274
  cleanupExprSupp(&pInfo->pseudoExprSup);
275 276 277
  taosMemoryFreeClear(param);
}

278
int32_t extractCacheScanSlotId(const SArray* pColMatchInfo, SExecTaskInfo* pTaskInfo, int32_t** pSlotIds) {
H
Hongze Cheng 已提交
279
  size_t numOfCols = taosArrayGetSize(pColMatchInfo);
280 281

  *pSlotIds = taosMemoryMalloc(numOfCols * sizeof(int32_t));
H
Hongze Cheng 已提交
282
  if (*pSlotIds == NULL) {
283 284 285
    return TSDB_CODE_OUT_OF_MEMORY;
  }

286 287
  SSchemaWrapper* pWrapper = pTaskInfo->schemaInfo.sw;

288
  for (int32_t i = 0; i < numOfCols; ++i) {
H
Haojun Liao 已提交
289
    SColMatchItem* pColMatch = taosArrayGet(pColMatchInfo, i);
290
    for (int32_t j = 0; j < pWrapper->nCols; ++j) {
291
      if (pColMatch->colId == pWrapper->pSchema[j].colId && pColMatch->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
H
Haojun Liao 已提交
292
        (*pSlotIds)[pColMatch->dstSlotId] = -1;
293 294 295
        break;
      }

296
      if (pColMatch->colId == pWrapper->pSchema[j].colId) {
H
Haojun Liao 已提交
297
        (*pSlotIds)[pColMatch->dstSlotId] = j;
298 299 300 301 302 303
        break;
      }
    }
  }

  return TSDB_CODE_SUCCESS;
304
}
305

H
Haojun Liao 已提交
306
int32_t removeRedundantTsCol(SLastRowScanPhysiNode* pScanNode, SColMatchInfo* pColMatchInfo) {
307
  if (!pScanNode->ignoreNull) {  // retrieve cached last value
H
Haojun Liao 已提交
308
    return TSDB_CODE_SUCCESS;
309 310
  }

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

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

H
Haojun Liao 已提交
317
    int32_t    slotId = pColInfo->dstSlotId;
318 319 320 321 322 323 324 325
    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 已提交
326 327 328
  taosArrayDestroy(pColMatchInfo->pList);
  pColMatchInfo->pList = pMatchInfo;
  return TSDB_CODE_SUCCESS;
329
}