cachescanoperator.c 10.7 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
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, destroyLastrowScanOperator, NULL);
101

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

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;
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 160

        char* p = colDataGetData(pSrc, pInfo->indexOfBufferedRes);
H
Hongze Cheng 已提交
161
        bool  isNull = colDataIsNull_s(pSrc, pInfo->indexOfBufferedRes);
162 163 164
        colDataAppend(pDst, 0, p, isNull);
      }

165 166
      pRes->info.uid = *(tb_uid_t*)taosArrayGet(pInfo->pUidList, pInfo->indexOfBufferedRes);
      pRes->info.rows = 1;
167

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

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

    while (pInfo->currentGroupIndex < totalGroups) {
187 188 189
      STableKeyInfo* pList = NULL;
      int32_t num = 0;

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

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

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

204 205 206 207 208 209 210
      pInfo->currentGroupIndex += 1;

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

211
          STableKeyInfo* pKeyInfo = &((STableKeyInfo*)pTableList)[0];
212 213
          pInfo->pRes->info.groupId = pKeyInfo->groupId;

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

H
Haojun Liao 已提交
227
        pInfo->pLastrowReader = tsdbCacherowsReaderClose(pInfo->pLastrowReader);
228 229
        return pInfo->pRes;
      }
230 231
    }

H
Haojun Liao 已提交
232
    setOperatorCompleted(pOperator);
233
    return NULL;
234 235 236
  }
}

237
void destroyLastrowScanOperator(void* param) {
238 239
  SLastrowScanInfo* pInfo = (SLastrowScanInfo*)param;
  blockDataDestroy(pInfo->pRes);
H
Haojun Liao 已提交
240 241 242
  blockDataDestroy(pInfo->pBufferredRes);
  taosMemoryFree(pInfo->pSlotIds);
  taosArrayDestroy(pInfo->pUidList);
H
Haojun Liao 已提交
243
  taosArrayDestroy(pInfo->matchInfo.pList);
H
Haojun Liao 已提交
244 245 246 247 248

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

249 250 251
  taosMemoryFreeClear(param);
}

252
int32_t extractCacheScanSlotId(const SArray* pColMatchInfo, SExecTaskInfo* pTaskInfo, int32_t** pSlotIds) {
H
Hongze Cheng 已提交
253
  size_t numOfCols = taosArrayGetSize(pColMatchInfo);
254 255

  *pSlotIds = taosMemoryMalloc(numOfCols * sizeof(int32_t));
H
Hongze Cheng 已提交
256
  if (*pSlotIds == NULL) {
257 258 259
    return TSDB_CODE_OUT_OF_MEMORY;
  }

260 261
  SSchemaWrapper* pWrapper = pTaskInfo->schemaInfo.sw;

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

270
      if (pColMatch->colId == pWrapper->pSchema[j].colId) {
H
Haojun Liao 已提交
271
        (*pSlotIds)[pColMatch->dstSlotId] = j;
272 273 274 275 276 277
        break;
      }
    }
  }

  return TSDB_CODE_SUCCESS;
278
}
279

H
Haojun Liao 已提交
280
int32_t removeRedundantTsCol(SLastRowScanPhysiNode* pScanNode, SColMatchInfo* pColMatchInfo) {
281
  if (!pScanNode->ignoreNull) {  // retrieve cached last value
H
Haojun Liao 已提交
282
    return TSDB_CODE_SUCCESS;
283 284
  }

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

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

H
Haojun Liao 已提交
291
    int32_t    slotId = pColInfo->dstSlotId;
292 293 294 295 296 297 298 299
    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 已提交
300 301 302
  taosArrayDestroy(pColMatchInfo->pList);
  pColMatchInfo->pList = pMatchInfo;
  return TSDB_CODE_SUCCESS;
303
}