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

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);
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
Hongze Cheng 已提交
96
  pOperator->name = "LastrowScanOperator";
97
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN;
H
Hongze Cheng 已提交
98 99 100 101
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
  pOperator->pTaskInfo = pTaskInfo;
102 103 104
  pOperator->exprSupp.numOfExprs = taosArrayGetSize(pInfo->pRes->pDataBlock);

  pOperator->fpSet =
105
      createOperatorFpSet(operatorDummyOpenFn, doScanCache, NULL, NULL, destroyLastrowScanOperator, NULL);
106

107 108 109
  pOperator->cost.openCost = 0;
  return pOperator;

110
  _error:
111 112
  pTaskInfo->code = code;
  destroyLastrowScanOperator(pInfo);
113 114 115 116
  taosMemoryFree(pOperator);
  return NULL;
}

117
SSDataBlock* doScanCache(SOperatorInfo* pOperator) {
118 119 120 121 122 123
  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }

  SLastrowScanInfo* pInfo = pOperator->info;
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
H
Haojun Liao 已提交
124 125 126 127
  STableListInfo*   pTableList = pTaskInfo->pTableInfoList;

  uint64_t suid = tableListGetSuid(pTableList);
  int32_t  size = tableListGetSize(pTableList);
128
  if (size == 0) {
129
    doSetOperatorCompleted(pOperator);
130 131 132
    return NULL;
  }

133 134
  blockDataCleanup(pInfo->pRes);

135
  // check if it is a group by tbname
136
  if ((pInfo->retrieveType & CACHESCAN_RETRIEVE_TYPE_ALL) == CACHESCAN_RETRIEVE_TYPE_ALL) {
137 138 139 140
    if (pInfo->indexOfBufferedRes >= pInfo->pBufferredRes->info.rows) {
      blockDataCleanup(pInfo->pBufferredRes);
      taosArrayClear(pInfo->pUidList);

H
Hongze Cheng 已提交
141 142
      int32_t code =
          tsdbRetrieveCacheRows(pInfo->pLastrowReader, pInfo->pBufferredRes, pInfo->pSlotIds, pInfo->pUidList);
143
      if (code != TSDB_CODE_SUCCESS) {
144
        T_LONG_JMP(pTaskInfo->env, code);
145 146 147 148
      }

      // check for tag values
      int32_t resultRows = pInfo->pBufferredRes->info.rows;
149 150 151

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

155 156
    SSDataBlock* pRes = pInfo->pRes;

157
    if (pInfo->indexOfBufferedRes < pInfo->pBufferredRes->info.rows) {
H
Haojun Liao 已提交
158 159 160
      for (int32_t i = 0; i < taosArrayGetSize(pInfo->matchInfo.pList); ++i) {
        SColMatchItem* pMatchInfo = taosArrayGet(pInfo->matchInfo.pList, i);
        int32_t        slotId = pMatchInfo->dstSlotId;
161 162

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

        char* p = colDataGetData(pSrc, pInfo->indexOfBufferedRes);
H
Hongze Cheng 已提交
166
        bool  isNull = colDataIsNull_s(pSrc, pInfo->indexOfBufferedRes);
167 168 169
        colDataAppend(pDst, 0, p, isNull);
      }

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

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

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

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

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

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

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

209 210 211 212 213 214 215
      pInfo->currentGroupIndex += 1;

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

216
          STableKeyInfo* pKeyInfo = &((STableKeyInfo*)pTableList)[0];
217 218
          pInfo->pRes->info.groupId = pKeyInfo->groupId;

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

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

    doSetOperatorCompleted(pOperator);
238
    return NULL;
239 240 241
  }
}

242
void destroyLastrowScanOperator(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);
  }

254 255 256
  taosMemoryFreeClear(param);
}

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
283
}
284

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

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

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

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