qAggMain.h 10.0 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

H
Haojun Liao 已提交
16 17
#ifndef TDENGINE_QAGGMAIN_H
#define TDENGINE_QAGGMAIN_H
H
hzcheng 已提交
18 19 20 21 22

#ifdef __cplusplus
extern "C" {
#endif

23
#include "os.h"
H
hzcheng 已提交
24

H
Haojun Liao 已提交
25
#include "tname.h"
26
#include "texpr.h"
H
hzcheng 已提交
27
#include "taosdef.h"
28
#include "trpc.h"
29
#include "tvariant.h"
H
Haojun Liao 已提交
30
#include "tsdb.h"
D
dapan1121 已提交
31
#include "qUdf.h"
H
hzcheng 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44

#define TSDB_FUNC_INVALID_ID  -1
#define TSDB_FUNC_COUNT        0
#define TSDB_FUNC_SUM          1
#define TSDB_FUNC_AVG          2
#define TSDB_FUNC_MIN          3
#define TSDB_FUNC_MAX          4
#define TSDB_FUNC_STDDEV       5
#define TSDB_FUNC_PERCT        6
#define TSDB_FUNC_APERCT       7
#define TSDB_FUNC_FIRST        8
#define TSDB_FUNC_LAST         9
#define TSDB_FUNC_LAST_ROW     10
S
slguan 已提交
45 46 47 48 49 50
#define TSDB_FUNC_TOP          11
#define TSDB_FUNC_BOTTOM       12
#define TSDB_FUNC_SPREAD       13
#define TSDB_FUNC_TWA          14
#define TSDB_FUNC_LEASTSQR     15

H
hzcheng 已提交
51 52
#define TSDB_FUNC_TS           16
#define TSDB_FUNC_TS_DUMMY     17
S
slguan 已提交
53 54
#define TSDB_FUNC_TAG_DUMMY    18
#define TSDB_FUNC_TS_COMP      19
H
hzcheng 已提交
55

S
slguan 已提交
56 57
#define TSDB_FUNC_TAG          20
#define TSDB_FUNC_PRJ          21
H
hzcheng 已提交
58

S
slguan 已提交
59
#define TSDB_FUNC_TAGPRJ       22
S
shenglian zhou 已提交
60
#define TSDB_FUNC_SCALAR_EXPR  23
S
slguan 已提交
61
#define TSDB_FUNC_DIFF         24
H
hzcheng 已提交
62

S
slguan 已提交
63 64
#define TSDB_FUNC_FIRST_DST    25
#define TSDB_FUNC_LAST_DST     26
65 66 67 68 69
#define TSDB_FUNC_STDDEV_DST   27
#define TSDB_FUNC_INTERP       28

#define TSDB_FUNC_RATE         29
#define TSDB_FUNC_IRATE        30
70
#define TSDB_FUNC_TID_TAG      31
71
#define TSDB_FUNC_DERIVATIVE   32
72

73 74 75
#define TSDB_FUNC_CSUM         33
#define TSDB_FUNC_MAVG         34
#define TSDB_FUNC_SAMPLE       35
76

77
#define TSDB_FUNC_BLKINFO      36
78

79
#define TSDB_FUNC_ELAPSED      37
80
#define TSDB_FUNC_HISTOGRAM    38
wmmhello's avatar
wmmhello 已提交
81
#define TSDB_FUNC_UNIQUE       39
wmmhello's avatar
wmmhello 已提交
82
#define TSDB_FUNC_MODE         40
wmmhello's avatar
wmmhello 已提交
83
#define TSDB_FUNC_TAIL         41
84 85 86
#define TSDB_FUNC_WSTART       42
#define TSDB_FUNC_WSTOP        43
#define TSDB_FUNC_WDURATION    44
87

88
#define TSDB_FUNC_MAX_NUM    45
89 90 91 92

#define TSDB_FUNCSTATE_SO           0x1u    // single output
#define TSDB_FUNCSTATE_MO           0x2u    // dynamic number of output, not multinumber of output e.g., TOP/BOTTOM
#define TSDB_FUNCSTATE_STREAM       0x4u    // function avail for stream
H
Haojun Liao 已提交
93
#define TSDB_FUNCSTATE_STABLE       0x8u    // function avail for super table
94 95 96 97 98 99
#define TSDB_FUNCSTATE_OF           0x10u   // outer forward
#define TSDB_FUNCSTATE_NEED_TS      0x20u   // timestamp is required during query processing
#define TSDB_FUNCSTATE_SELECTIVITY  0x40u   // selectivity functions, can exists along with tag columns

#define TSDB_BASE_FUNC_SO TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STREAM | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_OF
#define TSDB_BASE_FUNC_MO TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STREAM | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_OF
H
hzcheng 已提交
100

S
slguan 已提交
101
#define TSDB_FUNCTIONS_NAME_MAX_LENGTH 16
H
hzcheng 已提交
102 103 104
#define TSDB_AVG_FUNCTION_INTER_BUFFER_SIZE 50

#define DATA_SET_FLAG ','  // to denote the output area has data, not null value
S
slguan 已提交
105 106
#define DATA_SET_FLAG_SIZE sizeof(DATA_SET_FLAG)

107
#define QUERY_ASC_FORWARD_STEP   1
H
hzcheng 已提交
108
#define QUERY_DESC_FORWARD_STEP -1
S
slguan 已提交
109

110
#define GET_FORWARD_DIRECTION_FACTOR(ord) (((ord) == TSDB_ORDER_ASC) ? QUERY_ASC_FORWARD_STEP : QUERY_DESC_FORWARD_STEP)
H
hzcheng 已提交
111

H
Haojun Liao 已提交
112
#define MAX_INTERVAL_TIME_WINDOW 1000000  // maximum allowed time windows in final results
113
#define TOP_BOTTOM_QUERY_LIMIT   100
S
slguan 已提交
114

A
AlexDuan 已提交
115 116 117 118
// apercentile(arg1,agr2,arg3) param arg3 value is below:
#define ALGO_DEFAULT 0
#define ALGO_TDIGEST 1

H
hzcheng 已提交
119
enum {
H
Haojun Liao 已提交
120 121 122 123
  MASTER_SCAN   = 0x0u,
  REVERSE_SCAN  = 0x1u,
  REPEAT_SCAN   = 0x2u,  //repeat scan belongs to the master scan
  MERGE_STAGE   = 0x20u,
H
hzcheng 已提交
124 125
};

H
hjxilinx 已提交
126 127
#define QUERY_IS_STABLE_QUERY(type)      (((type)&TSDB_QUERY_TYPE_STABLE_QUERY) != 0)
#define QUERY_IS_JOIN_QUERY(type)        (TSDB_QUERY_HAS_TYPE(type, TSDB_QUERY_TYPE_JOIN_QUERY))
H
Haojun Liao 已提交
128
#define QUERY_IS_PROJECTION_QUERY(type)  (((type)&TSDB_QUERY_TYPE_PROJECTION_QUERY) != 0)
H
hjxilinx 已提交
129
#define QUERY_IS_FREE_RESOURCE(type)     (((type)&TSDB_QUERY_TYPE_FREE_RESOURCE) != 0)
S
slguan 已提交
130

S
shenglian zhou 已提交
131
typedef struct SScalarExprSupport {
H
Haojun Liao 已提交
132
  SExprInfo   *pExprInfo;
H
hjxilinx 已提交
133 134
  int32_t      numOfCols;
  SColumnInfo *colList;
H
Haojun Liao 已提交
135
  void        *exprList;   // client side used
H
hjxilinx 已提交
136 137
  int32_t      offset;
  char**       data;
S
shenglian zhou 已提交
138
} SScalarExprSupport;
H
hzcheng 已提交
139 140

typedef struct SQLPreAggVal {
H
Haojun Liao 已提交
141 142
  bool        isSet;             // statistics info set or not
  bool        dataBlockLoaded;   // data block is loaded or not
143
  SDataStatis statis;
H
hzcheng 已提交
144 145
} SQLPreAggVal;

S
slguan 已提交
146 147 148 149 150 151
typedef struct SInterpInfoDetail {
  TSKEY  ts;  // interp specified timestamp
  int8_t type;
  int8_t primaryCol;
} SInterpInfoDetail;

H
Haojun Liao 已提交
152
typedef struct SResultRowCellInfo {
153
  int8_t   hasResult;       // result generated, not NULL value
H
Haojun Liao 已提交
154 155
  bool     initialized;     // output buffer has been initialized
  bool     complete;        // query has completed
wmmhello's avatar
wmmhello 已提交
156
  int32_t  numOfRes;        // num of output result in current buffer
H
Haojun Liao 已提交
157 158
} SResultRowCellInfo;

159 160
typedef struct SPoint1 {
  int64_t key;
161
  union{double  val; char* ptr;};
162 163
} SPoint1;

H
Haojun Liao 已提交
164
#define GET_ROWCELL_INTERBUF(_c) ((void*) ((char*)(_c) + sizeof(SResultRowCellInfo)))
S
slguan 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177

struct SQLFunctionCtx;

/**
 * for selectivity query, the corresponding tag value is assigned if the data is qualified
 */
typedef struct SExtTagsInfo {
  int16_t                 tagsLen;      // keep the tags data for top/bottom query result
  int16_t                 numOfTagCols;
  struct SQLFunctionCtx **pTagCtxList;
} SExtTagsInfo;

// sql function runtime context
H
hzcheng 已提交
178
typedef struct SQLFunctionCtx {
179
  int32_t      size;      // number of rows
H
Haojun Liao 已提交
180
  void *       pInput;    // input data buffer
181 182
  uint32_t     order;     // asc|desc
  int16_t      inputType;
wmmhello's avatar
wmmhello 已提交
183
  int32_t      inputBytes;
184 185
  
  int16_t      outputType;
186
  int32_t      outputBytes;   // size of results, determined by function and input column data type
H
Haojun Liao 已提交
187 188
  int32_t      interBufBytes; // internal buffer size
  bool         hasNull;       // null value exist in current block
189
  bool         requireNull;   // require null in some function
H
Haojun Liao 已提交
190
  bool         stableQuery;
191 192 193 194
  int16_t      functionId;    // function id
  char *       pOutput;       // final result output buffer, point to sdata->data
  uint8_t      currentStage;  // record current running step, default: 0
  int64_t      startTs;       // timestamp range of current query when function is executed on a specific data block
D
dapan1121 已提交
195
  int64_t      endTs;
196
  int32_t      numOfParams;
H
Haojun Liao 已提交
197
  tVariant     param[4];      // input parameter, e.g., top(k, 20), the number of results for top query is kept in param
198 199
  int64_t     *ptsList;       // corresponding timestamp array list
  void        *ptsOutputBuf;  // corresponding output buffer for timestamp of each result, e.g., top/bottom*/
H
hzcheng 已提交
200
  SQLPreAggVal preAggVals;
S
slguan 已提交
201
  tVariant     tag;
H
Haojun Liao 已提交
202 203

  SResultRowCellInfo *resultInfo;
S
slguan 已提交
204

wmmhello's avatar
wmmhello 已提交
205
  int16_t      colId;         // used for user-specified constant value
S
slguan 已提交
206
  SExtTagsInfo tagInfo;
207 208
  SPoint1      start;
  SPoint1      end;
wmmhello's avatar
wmmhello 已提交
209

wmmhello's avatar
wmmhello 已提交
210
  SHashObj     **pUniqueSet;   // for unique function
wmmhello's avatar
wmmhello 已提交
211
  SHashObj     **pModeSet;     // for mode function
H
hzcheng 已提交
212 213
} SQLFunctionCtx;

214 215 216
typedef struct SAggFunctionInfo {
  char     name[TSDB_FUNCTIONS_NAME_MAX_LENGTH];
  uint8_t  index;       // index of function in aAggs
S
slguan 已提交
217
  int8_t   stableFuncId;  // transfer function for super table query
218
  uint16_t status;
H
hzcheng 已提交
219

H
Haojun Liao 已提交
220
  bool (*init)(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResultCellInfo);  // setup the execute environment
S
slguan 已提交
221
  void (*xFunction)(SQLFunctionCtx *pCtx);                     // blocks version function
H
hzcheng 已提交
222

H
Haojun Liao 已提交
223
  // finalizer must be called after all xFunction has been executed to generated final result.
H
hzcheng 已提交
224
  void (*xFinalize)(SQLFunctionCtx *pCtx);
H
Haojun Liao 已提交
225
  void (*mergeFunc)(SQLFunctionCtx *pCtx);
H
hzcheng 已提交
226

H
Haojun Liao 已提交
227
  int32_t (*dataReqFunc)(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId);
228
} SAggFunctionInfo;
H
hzcheng 已提交
229

S
slguan 已提交
230 231 232
#define GET_RES_INFO(ctx) ((ctx)->resultInfo)

int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionId, int32_t param, int16_t *type,
233
                          int32_t *len, int32_t *interBytes, int16_t extLength, bool isSuperTable, SUdfInfo* pUdfInfo);
H
Haojun Liao 已提交
234
int32_t isValidFunction(const char* name, int32_t len);
H
hzcheng 已提交
235

236 237
int16_t getTimeWindowFunctionID(int16_t colIndex);

S
slguan 已提交
238 239 240 241
#define IS_STREAM_QUERY_VALID(x)  (((x)&TSDB_FUNCSTATE_STREAM) != 0)
#define IS_MULTIOUTPUT(x)         (((x)&TSDB_FUNCSTATE_MO) != 0)
#define IS_SINGLEOUTPUT(x)        (((x)&TSDB_FUNCSTATE_SO) != 0)
#define IS_OUTER_FORWARD(x)       (((x)&TSDB_FUNCSTATE_OF) != 0)
H
hzcheng 已提交
242

H
Haojun Liao 已提交
243
// determine the real data need to calculated the result
H
hzcheng 已提交
244
enum {
H
Haojun Liao 已提交
245
  BLK_DATA_NO_NEEDED     = 0x0,
H
Haojun Liao 已提交
246
  BLK_DATA_STATIS_NEEDED = 0x1,
H
Haojun Liao 已提交
247 248
  BLK_DATA_ALL_NEEDED    = 0x3,
  BLK_DATA_DISCARD       = 0x4,   // discard current data block since it is not qualified for filter
H
hzcheng 已提交
249 250
};

S
slguan 已提交
251
typedef struct STwaInfo {
252 253
  int8_t      hasResult;  // flag to denote has value
  double      dOutput;
254
  SPoint1     p;
255
  STimeWindow win;
S
slguan 已提交
256
} STwaInfo;
H
hzcheng 已提交
257

H
Haojun Liao 已提交
258 259 260 261
struct SBufferWriter;
void blockDistInfoToBinary(STableBlockDist* pDist, struct SBufferWriter* bw);
void blockDistInfoFromBinary(const char* data, int32_t len, STableBlockDist* pDist);

H
hzcheng 已提交
262
/* global sql function array */
wmmhello's avatar
wmmhello 已提交
263
extern struct SAggFunctionInfo aAggs[TSDB_FUNC_MAX_NUM];
H
hzcheng 已提交
264

H
Haojun Liao 已提交
265
extern int32_t functionCompatList[]; // compatible check array list
H
hzcheng 已提交
266

H
Haojun Liao 已提交
267
bool topbot_datablock_filter(SQLFunctionCtx *pCtx, const char *minval, const char *maxval);
H
hjxilinx 已提交
268

269 270 271 272 273 274 275 276 277
/**
 * the numOfRes should be kept, since it may be used later
 * and allow the ResultInfo to be re initialized
 */
#define RESET_RESULT_INFO(_r)  \
  do {                         \
    (_r)->initialized = false; \
  } while (0)

278
static FORCE_INLINE void initResultInfo(SResultRowCellInfo *pResInfo, int32_t bufLen) {
H
Haojun Liao 已提交
279
  pResInfo->initialized = true;  // the this struct has been initialized flag
280

H
Haojun Liao 已提交
281
  pResInfo->complete  = false;
H
Haojun Liao 已提交
282
  pResInfo->hasResult = false;
H
Haojun Liao 已提交
283
  pResInfo->numOfRes  = 0;
284

285
  memset(GET_ROWCELL_INTERBUF(pResInfo), 0, bufLen);
H
Haojun Liao 已提交
286 287
}

H
hzcheng 已提交
288 289 290 291
#ifdef __cplusplus
}
#endif

H
Haojun Liao 已提交
292
#endif  // TDENGINE_QAGGMAIN_H