tsqlfunction.h 9.9 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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_TSQLFUNCTION_H
#define TDENGINE_TSQLFUNCTION_H

#ifdef __cplusplus
extern "C" {
#endif

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

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

#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 已提交
42 43 44 45 46 47
#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 已提交
48 49
#define TSDB_FUNC_TS           16
#define TSDB_FUNC_TS_DUMMY     17
S
slguan 已提交
50 51
#define TSDB_FUNC_TAG_DUMMY    18
#define TSDB_FUNC_TS_COMP      19
H
hzcheng 已提交
52

S
slguan 已提交
53 54
#define TSDB_FUNC_TAG          20
#define TSDB_FUNC_PRJ          21
H
hzcheng 已提交
55

S
slguan 已提交
56 57 58
#define TSDB_FUNC_TAGPRJ       22
#define TSDB_FUNC_ARITHM       23
#define TSDB_FUNC_DIFF         24
H
hzcheng 已提交
59

S
slguan 已提交
60 61 62
#define TSDB_FUNC_FIRST_DST    25
#define TSDB_FUNC_LAST_DST     26
#define TSDB_FUNC_INTERP       27
H
hzcheng 已提交
63

L
lihui 已提交
64 65 66 67 68 69 70
#define TSDB_FUNC_RATE         28
#define TSDB_FUNC_IRATE        29
#define TSDB_FUNC_SUM_RATE     30
#define TSDB_FUNC_SUM_IRATE    31
#define TSDB_FUNC_AVG_RATE     32
#define TSDB_FUNC_AVG_IRATE    33

71
#define TSDB_FUNC_TID_TAG      34
72 73 74 75 76 77 78 79 80
#define TSDB_FUNC_HISTOGRAM    35
#define TSDB_FUNC_HLL          36
#define TSDB_FUNC_MODE         37
#define TSDB_FUNC_SAMPLE       38
#define TSDB_FUNC_CEIL         39
#define TSDB_FUNC_FLOOR        40
#define TSDB_FUNC_ROUND        41
#define TSDB_FUNC_MAVG         42
#define TSDB_FUNC_CSUM         43
81 82 83 84 85 86 87 88 89 90 91

#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
#define TSDB_FUNCSTATE_STABLE       0x8u    // function avail for metric
#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 已提交
92 93


S
slguan 已提交
94
#define TSDB_FUNCTIONS_NAME_MAX_LENGTH 16
H
hzcheng 已提交
95 96 97
#define TSDB_AVG_FUNCTION_INTER_BUFFER_SIZE 50

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

#define QUERY_COND_REL_PREFIX_IN "IN|"
#define QUERY_COND_REL_PREFIX_LIKE "LIKE|"
H
hzcheng 已提交
102

103
#define QUERY_COND_REL_PREFIX_IN_LEN   3
S
slguan 已提交
104 105
#define QUERY_COND_REL_PREFIX_LIKE_LEN 5

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

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

111 112
#define MAX_INTERVAL_TIME_WINDOW 10000000
#define TOP_BOTTOM_QUERY_LIMIT   100
S
slguan 已提交
113

H
hzcheng 已提交
114
enum {
H
hjxilinx 已提交
115
  MASTER_SCAN           = 0x0u,
H
hjxilinx 已提交
116
  REVERSE_SCAN          = 0x1u,
H
hjxilinx 已提交
117 118 119
  REPEAT_SCAN           = 0x2u,  //repeat scan belongs to the master scan
  FIRST_STAGE_MERGE     = 0x10u,
  SECONDARY_STAGE_MERGE = 0x20u,
H
hzcheng 已提交
120 121
};

H
hjxilinx 已提交
122 123
#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 已提交
124
#define QUERY_IS_PROJECTION_QUERY(type)  (((type)&TSDB_QUERY_TYPE_PROJECTION_QUERY) != 0)
H
hjxilinx 已提交
125
#define QUERY_IS_FREE_RESOURCE(type)     (((type)&TSDB_QUERY_TYPE_FREE_RESOURCE) != 0)
S
slguan 已提交
126 127

typedef struct SArithmeticSupport {
H
hjxilinx 已提交
128
  SExprInfo   *pArithExpr;
H
hjxilinx 已提交
129 130
  int32_t      numOfCols;
  SColumnInfo *colList;
131
  SArray*      exprList;   // client side used
H
hjxilinx 已提交
132 133
  int32_t      offset;
  char**       data;
H
hzcheng 已提交
134 135 136
} SArithmeticSupport;

typedef struct SQLPreAggVal {
H
Haojun Liao 已提交
137 138
  bool        isSet;             // statistics info set or not
  bool        dataBlockLoaded;   // data block is loaded or not
139
  SDataStatis statis;
H
hzcheng 已提交
140 141
} SQLPreAggVal;

S
slguan 已提交
142 143 144 145 146 147 148
typedef struct SInterpInfoDetail {
  TSKEY  ts;  // interp specified timestamp
  int8_t type;
  int8_t primaryCol;
} SInterpInfoDetail;

typedef struct SResultInfo {
149
  int8_t   hasResult;       // result generated, not NULL value
H
Haojun Liao 已提交
150 151 152 153 154
  bool     initialized;   // output buffer has been initialized
  bool     complete;      // query has completed
  bool     superTableQ;   // is super table query
  uint32_t bufLen;       // buffer size
  uint64_t numOfRes;        // num of output result in current buffer
155
  void*    interResultBuf;  // output result buffer
S
slguan 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169
} SResultInfo;

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 已提交
170
typedef struct SQLFunctionCtx {
171 172 173 174 175 176 177 178 179
  int32_t      startOffset;
  int32_t      size;      // number of rows
  uint32_t     order;     // asc|desc
  int16_t      inputType;
  int16_t      inputBytes;
  
  int16_t      outputType;
  int16_t      outputBytes;  // size of results, determined by function and input column data type
  bool         hasNull;      // null value exist in current block
Y
TD-1230  
yihaoDeng 已提交
180
  bool         requireNull;    // require null in some function
181 182 183 184 185 186 187 188 189
  int16_t      functionId;   // function id
  void *       aInputElemBuf;
  char *       aOutputBuf;            // final result output buffer, point to sdata->data
  uint8_t      currentStage;          // record current running step, default: 0
  int64_t      nStartQueryTimestamp;  // timestamp range of current query when function is executed on a specific data block
  int32_t      numOfParams;
  tVariant     param[4];      // input parameter, e.g., top(k, 20), the number of results for top query is kept in param */
  int64_t *    ptsList;       // corresponding timestamp array list
  void *       ptsOutputBuf;  // corresponding output buffer for timestamp of each result, e.g., top/bottom*/
H
hzcheng 已提交
190
  SQLPreAggVal preAggVals;
S
slguan 已提交
191 192 193 194
  tVariant     tag;
  SResultInfo *resultInfo;

  SExtTagsInfo tagInfo;
H
hzcheng 已提交
195 196 197
} SQLFunctionCtx;

typedef struct SQLAggFuncElem {
H
Haojun Liao 已提交
198
  char     aName[TSDB_FUNCTIONS_NAME_MAX_LENGTH];
H
hzcheng 已提交
199

S
slguan 已提交
200 201
  uint8_t  nAggIdx;       // index of function in aAggs
  int8_t   stableFuncId;  // transfer function for super table query
H
hzcheng 已提交
202 203
  uint16_t nStatus;

S
slguan 已提交
204
  bool (*init)(SQLFunctionCtx *pCtx);  // setup the execute environment
H
hzcheng 已提交
205

S
slguan 已提交
206 207
  void (*xFunction)(SQLFunctionCtx *pCtx);                     // blocks version function
  void (*xFunctionF)(SQLFunctionCtx *pCtx, int32_t position);  // single-row function version
H
hzcheng 已提交
208

S
slguan 已提交
209 210
  // some sql function require scan data twice or more, e.g.,stddev
  void (*xNextStep)(SQLFunctionCtx *pCtx);
H
hzcheng 已提交
211 212 213

  /*
   * finalizer must be called after all xFunction has been executed to
S
slguan 已提交
214
   * generated final result. Otherwise, the value in aOutputBuf is a intern result.
H
hzcheng 已提交
215 216 217 218 219 220 221
   */
  void (*xFinalize)(SQLFunctionCtx *pCtx);

  void (*distMergeFunc)(SQLFunctionCtx *pCtx);

  void (*distSecondaryMergeFunc)(SQLFunctionCtx *pCtx);

222
  int32_t (*dataReqFunc)(SQLFunctionCtx *pCtx, TSKEY start, TSKEY end, int32_t colId);
H
hzcheng 已提交
223 224
} SQLAggFuncElem;

S
slguan 已提交
225 226 227
#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,
228
                          int16_t *len, int32_t *interBytes, int16_t extLength, bool isSuperTable);
H
hzcheng 已提交
229

S
slguan 已提交
230 231 232 233
#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 已提交
234 235 236

/* determine the real data need to calculated the result */
enum {
H
Haojun Liao 已提交
237
  BLK_DATA_NO_NEEDED     = 0x0,
H
Haojun Liao 已提交
238
  BLK_DATA_STATIS_NEEDED = 0x1,
H
Haojun Liao 已提交
239 240
  BLK_DATA_ALL_NEEDED    = 0x3,
  BLK_DATA_DISCARD       = 0x4,   // discard current data block since it is not qualified for filter
H
hzcheng 已提交
241 242
};

S
slguan 已提交
243 244 245 246 247 248
typedef struct STwaInfo {
  TSKEY   lastKey;
  int8_t  hasResult;  // flag to denote has value
  int16_t type;       // source data type
  TSKEY   SKey;
  TSKEY   EKey;
H
hzcheng 已提交
249 250 251 252 253 254 255 256 257 258

  union {
    double  dOutput;
    int64_t iOutput;
  };

  union {
    double  dLastValue;
    int64_t iLastValue;
  };
S
slguan 已提交
259
} STwaInfo;
H
hzcheng 已提交
260 261

/* global sql function array */
L
lihui 已提交
262
extern struct SQLAggFuncElem aAggs[];
H
hzcheng 已提交
263

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

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

268 269 270 271 272 273 274 275 276
/**
 * 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)

H
Haojun Liao 已提交
277
void setResultInfoBuf(SResultInfo *pResInfo, int32_t size, bool superTable, char* buf);
S
slguan 已提交
278

H
Haojun Liao 已提交
279 280 281 282 283 284 285 286 287 288
static FORCE_INLINE void initResultInfo(SResultInfo *pResInfo) {
  pResInfo->initialized = true;  // the this struct has been initialized flag
  
  pResInfo->complete = false;
  pResInfo->hasResult = false;
  pResInfo->numOfRes = 0;
  
  memset(pResInfo->interResultBuf, 0, (size_t)pResInfo->bufLen);
}

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

#endif  // TDENGINE_TSQLFUNCTION_H