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

18 19 20
#include "function.h"
#include "nodes.h"
#include "plannodes.h"
H
Haojun Liao 已提交
21
#include "tcommon.h"
H
Haojun Liao 已提交
22
#include "tpagedbuf.h"
23
#include "tsimplehash.h"
24
#include "vnode.h"
H
Haojun Liao 已提交
25
#include "executor.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)

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

42
#define GET_TASKID(_t) (((SExecTaskInfo*)(_t))->id.str)
H
Haojun Liao 已提交
43

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

typedef struct SResultRow {
52 53 54 55 56 57 58
  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 已提交
59
  struct SResultRowEntryInfo pEntryInfo[];  // For each result column, there is a resultInfo
60 61
} SResultRow;

62 63 64 65 66
typedef struct SResultRowPosition {
  int32_t pageId;
  int32_t offset;
} SResultRowPosition;

67 68
typedef struct SResKeyPos {
  SResultRowPosition pos;
69
  uint64_t           groupId;
70
  char key[];
71 72
} SResKeyPos;

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

H
Haojun Liao 已提交
79 80 81 82 83 84 85 86 87 88 89 90
typedef struct SColMatchItem {
  int32_t colId;
  int32_t srcSlotId;
  int32_t dstSlotId;
  bool    needOutput;
} 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 已提交
91
typedef struct SExecTaskInfo SExecTaskInfo;
H
Haojun Liao 已提交
92
typedef struct STableListInfo STableListInfo;
93
struct SqlFunctionCtx;
94

H
Haojun Liao 已提交
95
int32_t createScanTableListInfo(SScanPhysiNode* pScanNode, SNodeList* pGroupTags, bool groupSort, SReadHandle* pHandle,
H
Haojun Liao 已提交
96
                                STableListInfo* pTableListInfo, SNode* pTagCond, SNode* pTagIndexCond, SExecTaskInfo* pTaskInfo);
H
Haojun Liao 已提交
97 98

STableListInfo* tableListCreate();
H
Haojun Liao 已提交
99 100 101 102 103 104 105 106 107 108 109
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);
H
Haojun Liao 已提交
110

111 112
size_t getResultRowSize(struct SqlFunctionCtx* pCtx, int32_t numOfOutput);
void   initResultRowInfo(SResultRowInfo* pResultRowInfo);
113
void   closeResultRow(SResultRow* pResultRow);
114
void   resetResultRow(SResultRow* pResultRow, size_t entrySize);
115

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

118
static FORCE_INLINE SResultRow* getResultRowByPos(SDiskbasedBuf* pBuf, SResultRowPosition* pos, bool forUpdate) {
119
  SFilePage* bufPage = (SFilePage*)getBufPage(pBuf, pos->pageId);
120 121 122
  if (forUpdate) {
    setBufPageDirty(bufPage, true);
  }
H
Haojun Liao 已提交
123

H
Haojun Liao 已提交
124 125
  SResultRow* pRow = (SResultRow*)((char*)bufPage + pos->offset);
  return pRow;
H
Haojun Liao 已提交
126
}
H
Haojun Liao 已提交
127

128
void initGroupedResultInfo(SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap, int32_t order);
129
void cleanupGroupResInfo(SGroupResInfo* pGroupResInfo);
H
Haojun Liao 已提交
130

H
Haojun Liao 已提交
131
void initMultiResInfoFromArrayList(SGroupResInfo* pGroupResInfo, SArray* pArrayList);
132
bool hasRemainResults(SGroupResInfo* pGroupResInfo);
H
Haojun Liao 已提交
133

H
Haojun Liao 已提交
134 135
int32_t getNumOfTotalRes(SGroupResInfo* pGroupResInfo);

H
Haojun Liao 已提交
136
SSDataBlock* createDataBlockFromDescNode(SDataBlockDescNode* pNode);
137

wmmhello's avatar
wmmhello 已提交
138
EDealRes doTranslateTagExpr(SNode** pNode, void* pContext);
139 140 141 142 143
int32_t  getGroupIdFromTagsVal(void* pMeta, uint64_t uid, SNodeList* pGroupNode, char* keyBuf, uint64_t* pGroupId);
size_t   getTableTagsBufLen(const SNodeList* pGroups);

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

147
void       createExprFromOneNode(SExprInfo* pExp, SNode* pNode, int16_t slotId);
148
void       createExprFromTargetNode(SExprInfo* pExp, STargetNode* pTargetNode);
149 150
SExprInfo* createExprInfo(SNodeList* pNodeList, SNodeList* pGroupKeys, int32_t* numOfExprs);

151
SqlFunctionCtx* createSqlFunctionCtx(SExprInfo* pExprInfo, int32_t numOfOutput, int32_t** rowEntryInfoOffset);
152 153
void relocateColumnData(SSDataBlock* pBlock, const SArray* pColMatchInfo, SArray* pCols, bool outputEveryColumn);
void initExecTimeWindowInfo(SColumnInfoData* pColData, STimeWindow* pQueryWindow);
154 155 156 157 158

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

int32_t initQueryTableDataCond(SQueryTableDataCond* pCond, const STableScanPhysiNode* pTableScanNode);
159
void    cleanupQueryTableDataCond(SQueryTableDataCond* pCond);
160

161
int32_t convertFillType(int32_t mode);
5
54liuyao 已提交
162
int32_t resultrowComparAsc(const void* p1, const void* p2);
H
Haojun Liao 已提交
163
int32_t isQualifiedTable(STableKeyInfo* info, SNode* pTagCond, void* metaHandle, bool* pQualified);
164

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

167
#endif  // TDENGINE_QUERYUTIL_H