executil.h 8.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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/>.
 */
15 16
#ifndef TDENGINE_EXECUTIL_H
#define TDENGINE_EXECUTIL_H
17

dengyihao's avatar
dengyihao 已提交
18
#include "executor.h"
19 20 21
#include "function.h"
#include "nodes.h"
#include "plannodes.h"
22
#include "storageapi.h"
H
Haojun Liao 已提交
23
#include "tcommon.h"
H
Haojun Liao 已提交
24
#include "tpagedbuf.h"
25
#include "tsimplehash.h"
26

27 28 29 30
#define T_LONG_JMP(_obj, _c) \
  do {                       \
    ASSERT((_c) != -1);      \
    longjmp((_obj), (_c));   \
31
  } while (0)
32

H
Haojun Liao 已提交
33 34 35
#define SET_RES_WINDOW_KEY(_k, _ori, _len, _uid)     \
  do {                                               \
    assert(sizeof(_uid) == sizeof(uint64_t));        \
36
    *(uint64_t*)(_k) = (_uid);                       \
H
Haojun Liao 已提交
37 38 39
    memcpy((_k) + sizeof(uint64_t), (_ori), (_len)); \
  } while (0)

dengyihao's avatar
dengyihao 已提交
40
#define GET_RES_WINDOW_KEY_LEN(_l) ((_l) + sizeof(uint64_t))
W
fix bug  
wpan 已提交
41

42 43
typedef struct SGroupResInfo {
  int32_t index;
44
  SArray* pRows;  // SArray<SResKeyPos>
H
Haojun Liao 已提交
45
  char*   pBuf;
H
Haojun Liao 已提交
46
  bool    freeItem;
47 48 49
} SGroupResInfo;

typedef struct SResultRow {
50 51 52 53 54 55 56
  int32_t                    pageId;  // pageId & rowId is the position of current result in disk-based output buffer
  int32_t                    offset : 29;  // row index in buffer page
  bool                       startInterp;  // the time window start timestamp has done the interpolation already.
  bool                       endInterp;    // the time window end timestamp has done the interpolation already.
  bool                       closed;       // this result status: closed or opened
  uint32_t                   numOfRows;    // number of rows of current time window
  STimeWindow                win;
H
Haojun Liao 已提交
57
  struct SResultRowEntryInfo pEntryInfo[];  // For each result column, there is a resultInfo
58 59
} SResultRow;

60 61 62 63 64
typedef struct SResultRowPosition {
  int32_t pageId;
  int32_t offset;
} SResultRowPosition;

65 66
typedef struct SResKeyPos {
  SResultRowPosition pos;
67
  uint64_t           groupId;
dengyihao's avatar
dengyihao 已提交
68
  char               key[];
69 70
} SResKeyPos;

71
typedef struct SResultRowInfo {
72
  int32_t            size;  // number of result set
73
  SResultRowPosition cur;
74
  SList*             openWindow;
75 76
} SResultRowInfo;

H
Haojun Liao 已提交
77
typedef struct SColMatchItem {
78 79 80 81 82
  int32_t   colId;
  int32_t   srcSlotId;
  int32_t   dstSlotId;
  bool      needOutput;
  SDataType dataType;
H
Haojun Liao 已提交
83 84 85 86 87 88 89
} SColMatchItem;

typedef struct SColMatchInfo {
  SArray* pList;      // SArray<SColMatchItem>
  int32_t matchType;  // determinate the source according to col id or slot id
} SColMatchInfo;

H
Haojun Liao 已提交
90
typedef struct SExecTaskInfo SExecTaskInfo;
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

typedef struct STableListIdInfo {
  uint64_t suid;
  uint64_t uid;
  int32_t  tableType;
} STableListIdInfo;

// If the numOfOutputGroups is 1, the data blocks that belongs to different groups will be provided randomly
// The numOfOutputGroups is specified by physical plan. and will not be affect by numOfGroups
typedef struct STableListInfo {
  bool             oneTableForEachGroup;
  int32_t          numOfOuputGroups;  // the data block will be generated one by one
  int32_t*         groupOffset;       // keep the offset value for each group in the tableList
  SArray*          pTableList;
  SHashObj*        map;     // speedup acquire the tableQueryInfo by table uid
  STableListIdInfo idInfo;  // this maybe the super table or ordinary table
} STableListInfo;

109
struct SqlFunctionCtx;
110

H
Haojun Liao 已提交
111
int32_t createScanTableListInfo(SScanPhysiNode* pScanNode, SNodeList* pGroupTags, bool groupSort, SReadHandle* pHandle,
dengyihao's avatar
dengyihao 已提交
112 113
                                STableListInfo* pTableListInfo, SNode* pTagCond, SNode* pTagIndexCond,
                                SExecTaskInfo* pTaskInfo);
H
Haojun Liao 已提交
114 115

STableListInfo* tableListCreate();
H
Haojun Liao 已提交
116 117 118 119 120 121 122 123 124 125 126
void*           tableListDestroy(STableListInfo* pTableListInfo);
void            tableListClear(STableListInfo* pTableListInfo);
int32_t         tableListGetOutputGroups(const STableListInfo* pTableList);
bool            oneTableForEachGroup(const STableListInfo* pTableList);
uint64_t        getTableGroupId(const STableListInfo* pTableList, uint64_t tableUid);
int32_t         tableListAddTableInfo(STableListInfo* pTableList, uint64_t uid, uint64_t gid);
int32_t         tableListGetGroupList(const STableListInfo* pTableList, int32_t ordinalIndex, STableKeyInfo** pKeyInfo,
                                      int32_t* num);
uint64_t        tableListGetSize(const STableListInfo* pTableList);
uint64_t        tableListGetSuid(const STableListInfo* pTableList);
STableKeyInfo*  tableListGetInfo(const STableListInfo* pTableList, int32_t index);
127
int32_t         tableListFind(const STableListInfo* pTableList, uint64_t uid, int32_t startIndex);
128
void            tableListGetSourceTableInfo(const STableListInfo* pTableList, uint64_t* psuid, uint64_t* uid, int32_t* type);
H
Haojun Liao 已提交
129

130 131
size_t getResultRowSize(struct SqlFunctionCtx* pCtx, int32_t numOfOutput);
void   initResultRowInfo(SResultRowInfo* pResultRowInfo);
132
void   closeResultRow(SResultRow* pResultRow);
133
void   resetResultRow(SResultRow* pResultRow, size_t entrySize);
134

135
struct SResultRowEntryInfo* getResultEntryInfo(const SResultRow* pRow, int32_t index, const int32_t* offset);
H
Haojun Liao 已提交
136

137
static FORCE_INLINE SResultRow* getResultRowByPos(SDiskbasedBuf* pBuf, SResultRowPosition* pos, bool forUpdate) {
138
  SFilePage* bufPage = (SFilePage*)getBufPage(pBuf, pos->pageId);
K
kailixu 已提交
139
  if (!bufPage) {
K
kailixu 已提交
140
    uFatal("failed to get the buffer page:%d since %s", pos->pageId, terrstr());
K
kailixu 已提交
141 142
    return NULL;
  }
143 144 145
  if (forUpdate) {
    setBufPageDirty(bufPage, true);
  }
H
Haojun Liao 已提交
146

H
Haojun Liao 已提交
147 148
  SResultRow* pRow = (SResultRow*)((char*)bufPage + pos->offset);
  return pRow;
H
Haojun Liao 已提交
149
}
H
Haojun Liao 已提交
150

151
void initGroupedResultInfo(SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap, int32_t order);
152
void cleanupGroupResInfo(SGroupResInfo* pGroupResInfo);
H
Haojun Liao 已提交
153

H
Haojun Liao 已提交
154
void initMultiResInfoFromArrayList(SGroupResInfo* pGroupResInfo, SArray* pArrayList);
155
bool hasRemainResults(SGroupResInfo* pGroupResInfo);
H
Haojun Liao 已提交
156

H
Haojun Liao 已提交
157 158
int32_t getNumOfTotalRes(SGroupResInfo* pGroupResInfo);

H
Haojun Liao 已提交
159
SSDataBlock* createDataBlockFromDescNode(SDataBlockDescNode* pNode);
160

wmmhello's avatar
wmmhello 已提交
161
EDealRes doTranslateTagExpr(SNode** pNode, void* pContext);
162
int32_t  getGroupIdFromTagsVal(void* pVnode, uint64_t uid, SNodeList* pGroupNode, char* keyBuf, uint64_t* pGroupId, SStorageAPI* pAPI);
163 164 165 166
size_t   getTableTagsBufLen(const SNodeList* pGroups);

SArray* createSortInfo(SNodeList* pNodeList);
SArray* extractPartitionColInfo(SNodeList* pNodeList);
H
Haojun Liao 已提交
167 168
int32_t extractColMatchInfo(SNodeList* pNodeList, SDataBlockDescNode* pOutputNodeList, int32_t* numOfOutputCols,
                            int32_t type, SColMatchInfo* pMatchInfo);
169

170
void       createExprFromOneNode(SExprInfo* pExp, SNode* pNode, int16_t slotId);
171
void       createExprFromTargetNode(SExprInfo* pExp, STargetNode* pTargetNode);
172 173
SExprInfo* createExprInfo(SNodeList* pNodeList, SNodeList* pGroupKeys, int32_t* numOfExprs);

174
SqlFunctionCtx* createSqlFunctionCtx(SExprInfo* pExprInfo, int32_t numOfOutput, int32_t** rowEntryInfoOffset, SFunctionStateStore* pStore);
175 176
void relocateColumnData(SSDataBlock* pBlock, const SArray* pColMatchInfo, SArray* pCols, bool outputEveryColumn);
void initExecTimeWindowInfo(SColumnInfoData* pColData, STimeWindow* pQueryWindow);
177 178 179 180 181

SInterval extractIntervalInfo(const STableScanPhysiNode* pTableScanNode);
SColumn   extractColumnFromColumnNode(SColumnNode* pColNode);

int32_t initQueryTableDataCond(SQueryTableDataCond* pCond, const STableScanPhysiNode* pTableScanNode);
182
void    cleanupQueryTableDataCond(SQueryTableDataCond* pCond);
183

184
int32_t convertFillType(int32_t mode);
5
54liuyao 已提交
185
int32_t resultrowComparAsc(const void* p1, const void* p2);
186
int32_t isQualifiedTable(STableKeyInfo* info, SNode* pTagCond, void* metaHandle, bool* pQualified, SStorageAPI *pAPI);
187

H
Haojun Liao 已提交
188 189
void printDataBlock(SSDataBlock* pBlock, const char* flag);

190 191 192
void getNextTimeWindow(const SInterval* pInterval, STimeWindow* tw, int32_t order);
void getInitialStartTimeWindow(SInterval* pInterval, TSKEY ts, STimeWindow* w, bool ascQuery);

193
#endif  // TDENGINE_EXECUTIL_H