function.h 11.0 KB
Newer Older
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/>.
 */

16 17
#ifndef TDENGINE_FUNCTION_H
#define TDENGINE_FUNCTION_H
18 19 20 21 22 23

#ifdef __cplusplus
extern "C" {
#endif

#include "common.h"
H
Haojun Liao 已提交
24
#include "tvariant.h"
25
#include "tbuffer.h"
26

27 28
#define MAX_INTERVAL_TIME_WINDOW 1000000  // maximum allowed time windows in final results

H
Haojun Liao 已提交
29 30
#define FUNCTION_TYPE_SCALAR       1
#define FUNCTION_TYPE_AGG          2
31

32
#define TOP_BOTTOM_QUERY_LIMIT    100
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#define FUNCTIONS_NAME_MAX_LENGTH 16

#define FUNCTION_INVALID_ID  -1
#define FUNCTION_COUNT        0
#define FUNCTION_SUM          1
#define FUNCTION_AVG          2
#define FUNCTION_MIN          3
#define FUNCTION_MAX          4
#define FUNCTION_STDDEV       5
#define FUNCTION_PERCT        6
#define FUNCTION_APERCT       7
#define FUNCTION_FIRST        8
#define FUNCTION_LAST         9
#define FUNCTION_LAST_ROW     10
#define FUNCTION_TOP          11
#define FUNCTION_BOTTOM       12
#define FUNCTION_SPREAD       13
#define FUNCTION_TWA          14
#define FUNCTION_LEASTSQR     15

#define FUNCTION_TS           16
#define FUNCTION_TS_DUMMY     17
#define FUNCTION_TAG_DUMMY    18
#define FUNCTION_TS_COMP      19

#define FUNCTION_TAG          20
#define FUNCTION_PRJ          21

#define FUNCTION_TAGPRJ       22
#define FUNCTION_ARITHM       23
#define FUNCTION_DIFF         24

#define FUNCTION_FIRST_DST    25
#define FUNCTION_LAST_DST     26
#define FUNCTION_STDDEV_DST   27
#define FUNCTION_INTERP       28

#define FUNCTION_RATE         29
#define FUNCTION_IRATE        30
#define FUNCTION_TID_TAG      31
#define FUNCTION_DERIVATIVE   32
#define FUNCTION_BLKINFO      33

#define FUNCTION_HISTOGRAM    34
#define FUNCTION_HLL          35
#define FUNCTION_MODE         36
#define FUNCTION_SAMPLE       37

81 82
#define FUNCTION_COV          38

H
Haojun Liao 已提交
83 84 85 86 87 88 89
typedef struct SResultRowEntryInfo {
  int8_t   hasResult;       // result generated, not NULL value
  bool     initialized;     // output buffer has been initialized
  bool     complete;        // query has completed
  uint32_t numOfRes;        // num of output result in current buffer
} SResultRowEntryInfo;

90 91 92 93 94 95 96 97 98
// determine the real data need to calculated the result
enum {
  BLK_DATA_NO_NEEDED     = 0x0,
  BLK_DATA_STATIS_NEEDED = 0x1,
  BLK_DATA_ALL_NEEDED    = 0x3,
  BLK_DATA_DISCARD       = 0x4,   // discard current data block since it is not qualified for filter
};

enum {
H
Haojun Liao 已提交
99
  MAIN_SCAN     = 0x0u,
100 101 102 103 104
  REVERSE_SCAN  = 0x1u,
  REPEAT_SCAN   = 0x2u,  //repeat scan belongs to the master scan
  MERGE_STAGE   = 0x20u,
};

105 106 107 108 109 110
typedef struct SPoint1 {
  int64_t   key;
  union{double  val; char* ptr;};
} SPoint1;

struct SQLFunctionCtx;
111
struct SResultRowEntryInfo;
112 113 114 115 116 117 118 119

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

H
Haojun Liao 已提交
120 121 122 123 124 125
typedef struct SResultDataInfo {
  int16_t type;
  int16_t bytes;
  int32_t intermediateBytes;
} SResultDataInfo;

126 127
#define GET_RES_INFO(ctx) ((ctx)->resultInfo)

H
Haojun Liao 已提交
128 129 130 131 132 133 134 135 136
typedef struct SFunctionFpSet {
  bool (*init)(struct SQLFunctionCtx *pCtx, struct SResultRowEntryInfo* pResultCellInfo);  // setup the execute environment
  void (*addInput)(struct SQLFunctionCtx *pCtx);

  // finalizer must be called after all exec has been executed to generated final result.
  void (*finalize)(struct SQLFunctionCtx *pCtx);
  void (*combine)(struct SQLFunctionCtx *pCtx);
} SFunctionFpSet;

H
Haojun Liao 已提交
137 138
extern SFunctionFpSet fpSet[1];

139 140 141 142 143 144 145 146
// sql function runtime context
typedef struct SQLFunctionCtx {
  int32_t      size;      // number of rows
  void *       pInput;    // input data buffer
  uint32_t     order;     // asc|desc
  int16_t      inputType;
  int16_t      inputBytes;

H
Haojun Liao 已提交
147
  SResultDataInfo resDataInfo;
148 149 150 151 152 153 154 155 156 157 158 159 160
  bool         hasNull;       // null value exist in current block
  bool         requireNull;   // require null in some function
  bool         stableQuery;
  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
  int32_t      numOfParams;
  SVariant     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*/
  SVariant     tag;

161
  bool        isAggSet;
H
Haojun Liao 已提交
162
  SColumnDataAgg agg;
163
  struct  SResultRowEntryInfo *resultInfo;
164 165 166
  SExtTagsInfo tagInfo;
  SPoint1      start;
  SPoint1      end;
H
Haojun Liao 已提交
167 168

  SFunctionFpSet* fpSet;
169 170
} SQLFunctionCtx;

171 172 173 174
enum {
  TEXPR_NODE_DUMMY     = 0x0,
  TEXPR_BINARYEXPR_NODE= 0x1,
  TEXPR_UNARYEXPR_NODE = 0x2,
175
  TEXPR_FUNCTION_NODE  = 0x3,
176 177 178 179 180 181 182 183
  TEXPR_COL_NODE       = 0x4,
  TEXPR_VALUE_NODE     = 0x8,
};

typedef struct tExprNode {
  uint8_t nodeType;
  union {
    struct {
184
      int32_t           optr;   // binary operator
185 186 187 188 189 190 191
      void             *info;   // support filter operation on this expression only available for leaf node
      struct tExprNode *pLeft;  // left child pointer
      struct tExprNode *pRight; // right child pointer
    } _node;

    SSchema            *pSchema;// column node
    struct SVariant    *pVal;   // value node
192 193

    struct {// function node
194
      char              functionName[FUNCTIONS_NAME_MAX_LENGTH];
195 196 197 198 199 200 201 202 203
      int32_t           num;

      // Note that the attribute of pChild is not the parameter of function, it is the columns that involved in the
      // calculation instead.
      // E.g., Cov(col1, col2), the column information, w.r.t. the col1 and col2, is kept in pChild nodes.
      //  The concat function, concat(col1, col2), is a binary scalar
      //  operator and is kept in the attribute of _node.
      struct tExprNode **pChild;
    } _function;
204 205 206
  };
} tExprNode;

207
//TODO create?
208 209 210
void exprTreeToBinary(SBufferWriter* bw, tExprNode* pExprTree);
void tExprTreeDestroy(tExprNode *pNode, void (*fp)(void *));

211
typedef struct SAggFunctionInfo {
212 213 214 215 216
  char      name[FUNCTIONS_NAME_MAX_LENGTH];
  int8_t    type;         // Scalar function or aggregation function
  uint32_t  functionId;   // Function Id
  int8_t    sFunctionId;  // Transfer function for super table query
  uint16_t  status;
217

218
  bool (*init)(SQLFunctionCtx *pCtx, struct SResultRowEntryInfo* pResultCellInfo);  // setup the execute environment
H
Haojun Liao 已提交
219
  void (*addInput)(SQLFunctionCtx *pCtx);
220 221

  // finalizer must be called after all exec has been executed to generated final result.
H
Haojun Liao 已提交
222 223
  void (*finalize)(SQLFunctionCtx *pCtx);
  void (*combine)(SQLFunctionCtx *pCtx);
224 225 226 227

  int32_t (*dataReqFunc)(SQLFunctionCtx *pCtx, STimeWindow* w, int32_t colId);
} SAggFunctionInfo;

228
struct SScalarFuncParam;
229

230 231 232 233
typedef struct SScalarFunctionInfo {
  char      name[FUNCTIONS_NAME_MAX_LENGTH];
  int8_t    type;              // scalar function or aggregation function
  uint32_t  functionId;        // index of scalar function
234
  void     (*process)(struct SScalarFuncParam* pOutput, size_t numOfInput, const struct SScalarFuncParam *pInput);
235 236
} SScalarFunctionInfo;

237 238 239
typedef struct SMultiFunctionsDesc {
  bool stableQuery;
  bool groupbyColumn;
240
  bool agg;
241 242 243 244 245 246 247 248
  bool arithmeticOnAgg;
  bool projectionQuery;
  bool hasFilter;
  bool onlyTagQuery;
  bool orderProjectQuery;
  bool globalMerge;
  bool multigroupResult;
  bool blockDistribution;
249
  bool stateWindow;
250
  bool timewindow;
251
  bool sessionWindow;
252 253
  bool topbotQuery;
  bool interpQuery;
H
Haojun Liao 已提交
254 255
  bool distinct;
  bool join;
256 257
} SMultiFunctionsDesc;

258 259
int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionId, int32_t param, SResultDataInfo* pInfo, int16_t extLength,
                          bool isSuperTable);
260 261 262 263 264 265 266

/**
 * If the given name is a valid built-in sql function, the value of true will be returned.
 * @param name
 * @param len
 * @return
 */
267
int32_t qIsBuiltinFunction(const char* name, int32_t len, bool* scalarFunction);
268

269 270
bool qIsValidUdf(SArray* pUdfInfo, const char* name, int32_t len, int32_t* functionId);

271
bool qIsAggregateFunction(const char* functionName);
272
bool qIsSelectivityFunction(const char* functionName);
273

274 275
tExprNode* exprTreeFromBinary(const void* data, size_t size);

276 277
void extractFunctionDesc(SArray* pFunctionIdList, SMultiFunctionsDesc* pDesc);

H
Haojun Liao 已提交
278 279
tExprNode* exprdup(tExprNode* pTree);

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
void resetResultRowEntryResult(SQLFunctionCtx* pCtx, int32_t num);
void cleanupResultRowEntry(struct SResultRowEntryInfo* pCell);
int32_t getNumOfResult(SQLFunctionCtx* pCtx, int32_t num);
bool isRowEntryCompleted(struct SResultRowEntryInfo* pEntry);
bool isRowEntryInitialized(struct SResultRowEntryInfo* pEntry);

struct SScalarFunctionSupport* createScalarFuncSupport(int32_t num);
void destroyScalarFuncSupport(struct SScalarFunctionSupport* pSupport, int32_t num);
struct SScalarFunctionSupport* getScalarFuncSupport(struct SScalarFunctionSupport* pSupport, int32_t index);

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// fill api
struct SFillInfo;
struct SFillColInfo;

typedef struct SPoint {
  int64_t key;
  void *  val;
} SPoint;

void taosFillSetStartInfo(struct SFillInfo* pFillInfo, int32_t numOfRows, TSKEY endKey);
void taosResetFillInfo(struct SFillInfo* pFillInfo, TSKEY startTimestamp);
void taosFillSetInputDataBlock(struct SFillInfo* pFillInfo, const struct SSDataBlock* pInput);
struct SFillColInfo* createFillColInfo(SExprInfo* pExpr, int32_t numOfOutput, const int64_t* fillVal);
bool taosFillHasMoreResults(struct SFillInfo* pFillInfo);

struct SFillInfo* taosCreateFillInfo(int32_t order, TSKEY skey, int32_t numOfTags, int32_t capacity, int32_t numOfCols,
                              int64_t slidingTime, int8_t slidingUnit, int8_t precision, int32_t fillType,
                              struct SFillColInfo* pFillCol, void* handle);

void* taosDestroyFillInfo(struct SFillInfo *pFillInfo);
int64_t taosFillResultDataBlock(struct SFillInfo* pFillInfo, void** output, int32_t capacity);
int64_t getFillInfoStart(struct SFillInfo *pFillInfo);

int32_t taosGetLinearInterpolationVal(SPoint* point, int32_t outputType, SPoint* point1, SPoint* point2, int32_t inputType);

H
Haojun Liao 已提交
316 317 318 319 320 321 322
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// udf api
struct SUdfInfo;

void qAddUdfInfo(uint64_t id, struct SUdfInfo* pUdfInfo);
void qRemoveUdfInfo(uint64_t id, struct SUdfInfo* pUdfInfo);

323 324 325 326
#ifdef __cplusplus
}
#endif

327
#endif  // TDENGINE_FUNCTION_H